This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lifecycle): Run lifecycle events, implement prefix option, add u…
…nit tests (#1) * feat(lifecycle): run lifecycle events * tmp: move things around
- Loading branch information
1 parent
0d10d0d
commit d6629be
Showing
11 changed files
with
542 additions
and
84 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/usr/bin/env node | ||
|
||
'use strict' | ||
|
||
const yargs = require('yargs') | ||
const main = require('../index.js') | ||
|
||
main(parseArgs()).then((details) => { | ||
console.log(`added ${details.count} packages in ${ | ||
details.time / 1000 | ||
}s.`) | ||
}) | ||
|
||
function parseArgs () { | ||
return yargs | ||
.option('loglevel', { | ||
type: 'string', | ||
describe: 'log level for npmlog', | ||
default: 'notice' | ||
}) | ||
.option('offline', { | ||
type: 'boolean', | ||
describe: 'force cipm to run offline, or error' | ||
}) | ||
.option('ignore-scripts', { | ||
type: 'boolean', | ||
describe: 'skip running lifecycle scripts' | ||
}) | ||
.option('prefix', { | ||
type: 'string', | ||
describe: 'path to package.json' | ||
}) | ||
.option('userconfig', { | ||
type: 'string', | ||
describe: 'path to npmrc' | ||
}) | ||
.argv | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict' | ||
|
||
const BB = require('bluebird') | ||
const log = require('npmlog') | ||
const spawn = require('child_process').spawn | ||
|
||
module.exports = getConfig | ||
module.exports._resetConfig = _resetConfig | ||
|
||
let _config | ||
|
||
function readConfig () { | ||
return new BB((resolve, reject) => { | ||
const child = spawn('npm', ['config', 'ls', '--json'], { | ||
env: process.env, | ||
cwd: process.cwd(), | ||
stdio: 'inherit' | ||
}) | ||
|
||
let stdout = '' | ||
if (child.stdout) { | ||
child.stdout.on('data', (chunk) => { | ||
stdout += chunk | ||
}) | ||
} | ||
|
||
child.on('error', reject) | ||
child.on('close', (code) => { | ||
if (code === 127) { | ||
reject(new Error('`npm` command not found. Please ensure you have [email protected] or later installed.')) | ||
} else { | ||
try { | ||
resolve(JSON.parse(stdout)) | ||
} catch (e) { | ||
reject(new Error('`npm config ls --json` failed to output json. Please ensure you have [email protected] or later installed.')) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* used solely for testing | ||
*/ | ||
function _resetConfig () { | ||
_config = undefined | ||
} | ||
|
||
function getConfig () { | ||
if (_config) return BB.resolve(_config) | ||
return readConfig().then(config => { | ||
_config = config | ||
_config.log = log | ||
|
||
return _config | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
const BB = require('bluebird') | ||
const npa = require('npm-package-arg') | ||
const path = require('path') | ||
const workerFarm = require('worker-farm') | ||
|
||
const extractionWorker = require('./worker.js') | ||
const WORKER_PATH = require.resolve('./worker.js') | ||
|
||
module.exports = { | ||
startWorkers () { | ||
this._workers = workerFarm({ | ||
maxConcurrentCallsPerWorker: 30, | ||
maxRetries: 1 | ||
}, WORKER_PATH) | ||
}, | ||
|
||
stopWorkers () { | ||
workerFarm.end(this._workers) | ||
}, | ||
|
||
child (name, child, childPath) { | ||
if (child.bundled) return BB.resolve() | ||
|
||
const spec = npa.resolve(name, child.resolved || child.version) | ||
const opts = { | ||
cache: path.resolve(process.env.HOME, '.npm/_cacache'), | ||
integrity: child.integrity | ||
} | ||
const args = [spec, childPath, opts] | ||
return BB.fromNode((cb) => { | ||
let launcher = extractionWorker | ||
let msg = args | ||
const spec = typeof args[0] === 'string' ? npa(args[0]) : args[0] | ||
if (spec.registry || spec.type === 'remote') { | ||
// workers will run things in parallel! | ||
launcher = this._workers | ||
try { | ||
msg = JSON.stringify(msg) | ||
} catch (e) { | ||
return cb(e) | ||
} | ||
} | ||
launcher(msg, cb) | ||
}) | ||
} | ||
} |
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.