Created
October 24, 2017 05:00
-
-
Save chengjianhua/4e377d300d90f7a24a63395c1f85562b to your computer and use it in GitHub Desktop.
The `cnpm login` in Continuous Integration. Because the private registry hosted by cnpm incapable of generating a `auth_token` after `npm login <registry>`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cp = require('child_process'); | |
// eslint-disable-next-line no-unused-vars | |
function inspect(...args) { | |
return console.dir(...args, { | |
showHidden: true, | |
depth: Infinity, | |
colors: true, | |
}); | |
} | |
const { NPM_USER, NPM_PWD, NPM_EMAIL } = process.env; | |
console.log(NPM_USER, NPM_PWD, NPM_EMAIL); | |
const login = cp.spawn('npm login', [ | |
'--registry=http://reg-npmjs.llsstaging.com', | |
'--scope=@lls', | |
], { | |
shell: true, | |
}); | |
const promises = []; | |
const finished = []; | |
function buildInput(input) { | |
const last = promises[promises.length - 1]; | |
const handleInput = () => { | |
const currentHandler = () => { | |
const currentPromise = new Promise((resolve) => { | |
login.stdin.write(`${input}\n`); | |
resolve(); | |
}); | |
Object.assign(handleInput, { | |
promise: currentPromise, | |
input, | |
}); | |
return currentPromise; | |
}; | |
if (last) { | |
return last.promise.then(currentHandler); | |
} | |
return currentHandler(); | |
}; | |
promises.push(handleInput); | |
return handleInput; | |
} | |
[NPM_USER, NPM_PWD, NPM_EMAIL].forEach((input) => { | |
buildInput(input); | |
}); | |
function handleData(data) { | |
const result = data.toString(); | |
const reg = /Logged in/; | |
process.stdout.write(`${result}\n`); | |
if (reg.test(result)) { | |
login.stdin.end(); | |
} else { | |
const handler = promises.shift(); | |
handler(); | |
finished.push(handler); | |
} | |
} | |
login.on('error', (err) => { | |
process.stderr.write(err); | |
}); | |
login.stdout.on('data', handleData); | |
login.stderr.on('data', (data) => { | |
process.stderr.write(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment