Skip to content

Use Installer when packaging git dependencies #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 15 additions & 74 deletions lib/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const BB = require('bluebird')
const byteSize = require('byte-size')
const cacache = require('cacache')
const columnify = require('columnify')
const cp = require('child_process')
const deprCheck = require('./utils/depr-check')
const fpm = require('./fetch-package-metadata')
const fs = require('graceful-fs')
const install = require('./install')
const lifecycle = BB.promisify(require('./utils/lifecycle'))
const noProgressTillDone = require('./utils/no-progress-while-running').tillDone
const log = require('npmlog')
const move = require('move-concurrently')
const npm = require('./npm')
Expand Down Expand Up @@ -265,89 +265,30 @@ function getContents (pkg, target, filename, silent) {
})
}

const PASSTHROUGH_OPTS = [
'always-auth',
'auth-type',
'ca',
'cafile',
'cert',
'git',
'local-address',
'maxsockets',
'offline',
'prefer-offline',
'prefer-online',
'proxy',
'https-proxy',
'registry',
'send-metrics',
'sso-poll-frequency',
'sso-type',
'strict-ssl'
]

module.exports.packGitDep = packGitDep
function packGitDep (manifest, dir) {
const stream = new PassThrough()
readJson(path.join(dir, 'package.json')).then((pkg) => {
if (pkg.scripts && pkg.scripts.prepare) {
log.verbose('prepareGitDep', `${manifest._spec}: installing devDeps and running prepare script.`)
const cliArgs = PASSTHROUGH_OPTS.reduce((acc, opt) => {
if (npm.config.get(opt, 'cli') != null) {
acc.push(`--${opt}=${npm.config.get(opt)}`)
}
return acc
}, [])
const child = cp.spawn(process.env.NODE || process.execPath, [
require.resolve('../bin/npm-cli.js'),
'install',
'--dev',
'--prod',
'--ignore-prepublish',
'--no-progress',
'--no-save'
].concat(cliArgs), {
cwd: dir,
env: process.env
})
let errData = []
let errDataLen = 0
let outData = []
let outDataLen = 0
child.stdout.on('data', (data) => {
outData.push(data)
outDataLen += data.length
log.gauge.pulse('preparing git package')
})
child.stderr.on('data', (data) => {
errData.push(data)
errDataLen += data.length
log.gauge.pulse('preparing git package')
})
return BB.fromNode((cb) => {
child.on('error', cb)
child.on('exit', (code, signal) => {
if (code > 0) {
const err = new Error(`${signal}: npm exited with code ${code} while attempting to build ${manifest._requested}. Clone the repository manually and run 'npm install' in it for more information.`)
err.code = code
err.signal = signal
cb(err)
} else {
cb()
}
})
}).then(() => {
if (outDataLen > 0) log.silly('prepareGitDep', '1>', Buffer.concat(outData, outDataLen).toString())
if (errDataLen > 0) log.silly('prepareGitDep', '2>', Buffer.concat(errData, errDataLen).toString())
}, (err) => {
if (outDataLen > 0) log.error('prepareGitDep', '1>', Buffer.concat(outData, outDataLen).toString())
if (errDataLen > 0) log.error('prepareGitDep', '2>', Buffer.concat(errData, errDataLen).toString())
throw err

const dryrun = !!npm.config.get('dry-run')
const args = []
const opts = {
dev: true,
prod: true
}
const installer = new install.Installer(dir, dryrun, args, opts)
// No-op changes to manifest/lockfile (--no-save)
installer.saveToDependencies = cb => cb()

return BB.fromNode(cb => {
installer.run(noProgressTillDone(cb))
})
}
}).then(() => {
return readJson(path.join(dir, 'package.json'))
}).then((pkg) => {
}).then(() => {
return cacache.tmp.withTmp(npm.tmp, {
tmpPrefix: 'pacote-packing'
}, (tmp) => {
Expand Down
2 changes: 1 addition & 1 deletion test/tap/git-prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const fixture = new Tacks(Dir({
version: '1.0.3',
main: 'dobuild.js',
scripts: {
'prepublish': 'exit 123',
'prepublishOnly': 'exit 123',
'prepare': 'writer build-artifact'
},
devDependencies: {
Expand Down