Created
March 23, 2016 11:42
-
-
Save benhoIIand/26b6b293060320a184f0 to your computer and use it in GitHub Desktop.
Publishing to NPM via a script
This file contains 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
// http://source.entelect.co.za/automatically-publish-to-npm-using-gulp-teamcity | |
'use strict'; | |
const execSync = require('child_process').execSync; | |
const npm = require('npm'); | |
const fs = require('fs'); | |
const args = require('yargs').argv; | |
const uri = 'http://package.brighter.io:4873'; | |
console.log('args', args); | |
const username = args.username; | |
const password = args.password; | |
const email = args.email; | |
// const username = 'bholland'; | |
// const password = 'Holland11'; | |
// const email = '[email protected]'; | |
if (!username) { | |
throw new Error('Username is required as an argument --username'); | |
} | |
if (!password) { | |
throw new Error('Password is required as an argument --password'); | |
} | |
if (!email) { | |
throw new Error('Email is required as an argument --email'); | |
} | |
try { | |
execSync('npm version 1.0.2 --no-git-tag-version'); | |
npm.load(null, (loadError) => { | |
if (loadError) { | |
throw new Error(loadError); | |
} | |
const auth = { | |
username, | |
password, | |
email, | |
alwaysAuth: true | |
}; | |
const addUserParams = { | |
auth | |
}; | |
npm.registry.adduser(uri, addUserParams, (addUserError) => { | |
if (addUserError) { | |
throw new Error(addUserError); | |
} | |
let metadata = require('./package.json'); | |
metadata = JSON.parse(JSON.stringify(metadata)); | |
npm.commands.pack([], (packError) => { | |
if (packError) { | |
throw new Error(packError); | |
} | |
const fileName = `${metadata.name}-${metadata.version}.tgz`; | |
const bodyPath = require.resolve(`./${fileName}`); | |
const body = fs.createReadStream(bodyPath); | |
const publishParams = { | |
metadata, | |
access: 'public', | |
body, | |
auth | |
}; | |
// npm.registry.publish(uri, publishParams, (publishError) => { | |
// if (publishError) { | |
// throw new Error(publishError); | |
// } | |
console.log(`Successfully published ${metadata.name}@${metadata.version}`); | |
// }); | |
}); | |
}); | |
}); | |
} catch (error) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment