@@ -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 && / ^ ( n p m | y a r n ) / . test ( buildArtifact ) ) {
138- console . warn (
158+ return handleError ( 'A build artifact must be supplied.' )
159+ } else if ( / ^ ( n p m | y a r n ) / . 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
0 commit comments