-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
55 lines (48 loc) · 1.66 KB
/
publish.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import child_process from 'child_process'
import path from 'path'
import fs from 'fs'
const args = process.argv.splice(2)
const folder = args[0]
console.log('preparing to publish folder `' + folder + '`')
const pkgJsonPath = path.join(folder, 'package.json')
const pkgJson = JSON.parse(fs.readFileSync(pkgJsonPath).toString())
const pkgJsonVersion = pkgJson.version
const pkg = pkgJson.name
console.log('checking version for ' + pkg + ' (' + pkgJsonVersion + ')')
exec('npm view ' + pkg + ' version', [], (version, err) => {
if(err.startsWith('npm ERR! code E404') || err.startsWith('npm error code E404') || err.startsWith('npm error 404')) {
console.log(' > not existing yet')
} else if(err.length) {
console.error('npm view error:', err)
return
}
if(pkgJsonVersion === version) {
console.log(' > skipping, same version online')
} else {
console.log(' > current version: ' + (version || '-'))
console.log(' publishing ...')
exec('cd ' + folder + '&& npm publish', [], (result) => {
console.log(' ', result)
})
}
})
function exec(command, args, callback) {
const child = child_process.exec(command)
let scriptOutput = ''
let scriptOutputErr = ''
child.stdout.setEncoding('utf8')
child.stdout.on('data', function(data) {
scriptOutput += data.toString()
})
child.stderr.setEncoding('utf8')
child.stderr.on('data', function(data) {
scriptOutputErr += data.toString()
})
child.on('close', function(code) {
callback(
scriptOutput.trim(),
scriptOutputErr.trim(),
code,
)
})
}