Skip to content

Commit 4f2e259

Browse files
authored
Add verbose log messages (#21)
1 parent bf1c50f commit 4f2e259

File tree

4 files changed

+66
-27
lines changed

4 files changed

+66
-27
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ postinstall-build [options] <artifact> [command]
6060
* `--only-as-dependency`: Run only if the package is being installed as a
6161
dependency, not if `npm install` (no args) is being run in the package’s own
6262
directory (usually while you are developing the package itself).
63-
* `--silent`: Silence the build command’s stdout and stderr. This was the
64-
default behavior pre-1.0. Note that this may make debugging much more
65-
difficult if something goes wrong.
63+
* `--silent`: Silence the build command’s stdout and stderr, as well as any
64+
warnings from `postinstall-build` itself. Fatal errors will still be printed.
65+
Note that this may make debugging much more difficult if something goes wrong.
66+
* `--verbose`: Print information about what `postinstall-build` is doing and
67+
why (as well as the usual warnings and errors).
6668

6769
If neither `command` nor `--script` is supplied, the build command defaults to
6870
`npm run build`.

index.js

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ function postinstallBuild () {
8888

8989
var buildArtifact
9090
var buildCommand
91-
var flags = { quote: false }
91+
var flags = {
92+
quote: false,
93+
verbosity: 1
94+
}
9295
for (var i = 2; i < process.argv.length; i++) {
9396
var arg = process.argv[i]
9497
if (arg === '--silent') {
95-
flags.silent = true
98+
flags.verbosity = 0
99+
} else if (arg === '--verbose') {
100+
flags.verbosity = 2
96101
} else if (arg === '--only-as-dependency') {
97102
flags.onlyAsDependency = true
98103
} else if (arg === '--script') {
@@ -109,7 +114,7 @@ function postinstallBuild () {
109114

110115
// Some packages (e.g. `ember-cli`) install their own version of npm, which
111116
// can shadow the expected version and break the package trying to use
112-
// `postinstall-build`. If we're running
117+
// `postinstall-build`.
113118
var npm = 'npm'
114119
var execPath = process.env.npm_execpath
115120
var userAgent = process.env.npm_config_user_agent || ''
@@ -132,10 +137,27 @@ function postinstallBuild () {
132137
buildCommand = npm + ' run build'
133138
}
134139

140+
var _write = function (verbosity, message) {
141+
if (flags.verbosity >= verbosity) {
142+
process.stderr.write(message + '\n')
143+
}
144+
}
145+
146+
var log = {
147+
info: _write.bind(this, 2),
148+
warn: _write.bind(this, 1),
149+
error: _write.bind(this, 0)
150+
}
151+
152+
var handleError = function (err) {
153+
log.error('postinstall-build:\n ' + err + '\n')
154+
safeExit(1)
155+
}
156+
135157
if (buildArtifact == null) {
136-
throw new Error('A build artifact must be supplied to postinstall-build.')
137-
} else if (!flags.silent && /^(npm|yarn) /.test(buildArtifact)) {
138-
console.warn(
158+
return handleError('A build artifact must be supplied.')
159+
} else if (/^(npm|yarn) /.test(buildArtifact)) {
160+
log.warn(
139161
"postinstall-build:\n '" + buildArtifact + "' is being passed as the " +
140162
'build artifact, not the build command.\n If your build artifact is ' +
141163
"a file or folder named '" + buildArtifact + "', you may ignore\n " +
@@ -156,6 +178,9 @@ function postinstallBuild () {
156178
var isDependency = path.basename(path.dirname(CWD)) === 'node_modules'
157179

158180
if (flags.onlyAsDependency && !isDependency) {
181+
log.info(
182+
'postinstall-build:\n Not installed as a dependency, skipping build.\n'
183+
)
159184
return
160185
}
161186

@@ -169,11 +194,6 @@ function postinstallBuild () {
169194
process.env.npm_config_only === 'prod')
170195
var shouldPrune = isDependency || isProduction || isOnlyProduction
171196

172-
var handleError = function (err) {
173-
console.error(err)
174-
safeExit(1)
175-
}
176-
177197
var getInstallArgs = function () {
178198
var packageFile = path.join(CWD, 'package.json')
179199
var packageInfo = require(packageFile)
@@ -190,14 +210,12 @@ function postinstallBuild () {
190210
// `buildDependencies` and expects it to be installed by
191211
// `postinstall-build`, then it must also be in `devDependencies`.
192212
if (typeof spec === 'undefined') {
193-
if (!flags.silent) {
194-
console.warn(
195-
"postinstall-build:\n The dependency '" + name + "' appears in " +
196-
'buildDependencies but not devDependencies.\n Instead of ' +
197-
'installing it, postinstall-build will assume it is already ' +
198-
'available.\n'
199-
)
200-
}
213+
log.warn(
214+
"postinstall-build:\n The dependency '" + name + "' appears in " +
215+
'buildDependencies but not devDependencies.\n Instead of ' +
216+
'installing it, postinstall-build will assume it is already ' +
217+
'available.\n'
218+
)
201219
return ''
202220
} else if (spec) {
203221
// This previously used `npm-package-arg` to determine which specs are
@@ -220,8 +238,15 @@ function postinstallBuild () {
220238
var checkBuildArtifact = function (callback) {
221239
fs.stat(buildArtifact, function (err, stats) {
222240
if (err || !(stats.isFile() || stats.isDirectory())) {
241+
log.info(
242+
'postinstall-build:\n Build artifact not found, proceeding to ' +
243+
'build.\n'
244+
)
223245
callback(null, true)
224246
} else {
247+
log.info(
248+
'postinstall-build:\n Build artifact found, skipping build.\n'
249+
)
225250
callback(null, false)
226251
}
227252
})
@@ -238,24 +263,36 @@ function postinstallBuild () {
238263
// extra dependencies.
239264
var installArgs = getInstallArgs()
240265
if (installArgs) {
241-
return exec(npm + ' install' + installArgs, execOpts, callback)
266+
var command = npm + ' install' + installArgs
267+
log.info('postinstall-build:\n ' + command + '\n')
268+
return exec(command, execOpts, callback)
242269
}
270+
log.info(
271+
'postinstall-build:\n No install arguments, skipping install.\n'
272+
)
243273
}
274+
log.info(
275+
'postinstall-build:\n Already have devDependencies, skipping install.\n'
276+
)
244277
callback(null)
245278
}
246279

247280
var runBuildCommand = function (execOpts, callback) {
248281
// Only quote the build command if necessary, otherwise run it exactly
249282
// as npm would.
250283
execOpts.quote = flags.quote
284+
log.info('postinstall-build:\n ' + buildCommand + '\n')
251285
exec(buildCommand, execOpts, callback)
252286
}
253287

254288
var cleanUp = function (execOpts, callback) {
255289
if (shouldPrune) {
256290
execOpts.quote = true
257-
return exec(npm + ' prune --production', execOpts, callback)
291+
var command = npm + ' prune --production'
292+
log.info('postinstall-build:\n ' + command + '\n')
293+
return exec(command, execOpts, callback)
258294
}
295+
log.info('postinstall-build:\n Skipping prune.\n')
259296
callback(null)
260297
}
261298

@@ -275,7 +312,7 @@ function postinstallBuild () {
275312
env: process.env,
276313
quote: true
277314
}
278-
if (flags.silent) {
315+
if (flags.verbosity === 0) {
279316
execOpts.stdio = 'ignore'
280317
}
281318

test/package-a/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "lib/index.js",
77
"scripts": {
88
"build-lib": "babel --presets es2015 -d lib src",
9-
"postinstall": "postinstall-build --only-as-dependency lib \"npm run build-lib\""
9+
"postinstall": "postinstall-build --verbose --only-as-dependency lib \"npm run build-lib\""
1010
},
1111
"author": "",
1212
"license": "ISC",

test/package-d/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "lib/index.js",
77
"scripts": {
88
"build-lib": "babel --presets es2015 -d lib src",
9-
"postinstall": "postinstall-build lib --script=build-lib"
9+
"postinstall": "postinstall-build --verbose lib --script=build-lib"
1010
},
1111
"author": "",
1212
"license": "ISC",

0 commit comments

Comments
 (0)