cryptocom-react-cli
Version:
It's a quick tool to help you build a react project with one command
90 lines (73 loc) • 2.17 kB
JavaScript
import fs from 'fs'
import child_process from 'child_process'
import readline from 'readline'
import { npmLogin, getNPMToken } from 'npm-shell-login'
const rd = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
let packageJson = fs.readFileSync('package.json', 'utf8')
const json = JSON.parse(packageJson)
const currentVersion = json['version']
let newVersion
const setVersion = async () => {
return new Promise((resolve, reject) => {
const flow = () => {
rd.question(`Please enter a new version: `, version => {
if (!/^\d{1,3}\.\d{1,2}\.\d{1,2}$/.test(version)) {
console.error('Invalid input.')
flow()
return
}
if (currentVersion === version) {
console.error(
'The new version should be different with current version.'
)
flow()
return
}
newVersion = version
rd.close()
resolve()
})
}
flow()
})
}
setVersion()
.then(() => {
json.version = newVersion
fs.writeFileSync('./package.json', JSON.stringify(json, null, 2))
console.log(`updated version to ${newVersion}`)
})
.then(async () => {
await login()
})
.then(() => {
console.log('publishing...')
child_process.exec('npm publish', function (error, stdout, stderr) {
if (error) {
console.error(error)
}
console.log(`stdout: ${stdout}`)
console.error(`stderr: ${stderr}`)
console.log(`Version ${newVersion} published.`)
})
})
// npmLogin returns a promise which resolves with the response code from
// the login process
// if it's 0, all went well, if not, the login failed
// it will only reject if the child process times out (20 seconds)
const login = async () => {
let pwd = fs.readFileSync('./.npmpwd', 'utf8')
if (!pwd) return
const code = await npmLogin('cryptocom', pwd, 'jing@crypto.com')
if (code === 0) {
console.log('Login success')
} else {
console.log('Login failed')
}
// getNPMToken will return a promise with the token from your ~/.npmrc file
const token = await getNPMToken()
}