Skip to content
This repository has been archived by the owner on Aug 11, 2022. It is now read-only.

Add INIT_CWD environment variable when npm run scripts #12356

Closed
wants to merge 4 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions doc/cli/npm-run-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ you should write:

instead of `"scripts": {"test": "node_modules/.bin/tap test/\*.js"}` to run your tests.

Also, `npm run` adds current working directory to `INIT_CWD`. You can run scripts in subdirectory of project. For example, you want to run babel in specific subdirectory. You should write:

"scripts": {
"build": "babel $INIT_CWD/src -d $INIT_CWD/lib"
}

or you can write:

"scripts": {
"build": "cd $INIT_CWD && babel src -d lib"
}

Now, you can run script, `npm run build` in any subdirectory you want to compiler JavaScript.

`npm run` sets the `NODE` environment variable to the `node` executable with
which `npm` is executed. Also, if the `--scripts-prepend-node-path` is passed,
the directory within which `node` resides is added to the
Expand Down
1 change: 1 addition & 0 deletions lib/utils/lifecycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ function lifecycle (pkg, stage, wd, unsafe, failOk, cb) {
env.npm_lifecycle_event = stage
env.npm_node_execpath = env.NODE = env.NODE || process.execPath
env.npm_execpath = require.main.filename
env.INIT_CWD = process.cwd()

// 'nobody' typically doesn't have permission to write to /tmp
// even if it's never used, sh freaks out.
Expand Down
58 changes: 58 additions & 0 deletions test/tap/lifecycle-INIT_CWD.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var fs = require('fs')
var path = require('path')

var mkdirp = require('mkdirp')
var osenv = require('osenv')
var rimraf = require('rimraf')
var test = require('tap').test

var common = require('../common-tap.js')

var pkg = path.resolve(__dirname, 'lifecycle-initcwd')
var subdir = path.resolve(pkg, 'subdir')

var json = {
name: 'init-cwd',
version: '1.0.0',
scripts: {
initcwd: 'echo "$INIT_CWD"'
}
}

test('setup', function (t) {
cleanup()
mkdirp.sync(pkg)
mkdirp.sync(subdir)
fs.writeFileSync(
path.join(pkg, 'package.json'),
JSON.stringify(json, null, 2)
)

process.chdir(subdir)
t.end()
})

test('make sure the env.INIT_CWD is correct', function (t) {
common.npm(['run-script', 'initcwd'], {
cwd: subdir
}, function (er, code, stdout) {
if (er) throw er
t.equal(code, 0, 'exit code')
stdout = stdout.trim().split(/\r|\n/).pop()
var actual = stdout

t.equal(actual, subdir)
t.end()
})
})

test('cleanup', function (t) {
cleanup()
t.end()
})

function cleanup () {
process.chdir(osenv.tmpdir())
rimraf.sync(subdir)
rimraf.sync(pkg)
}