Skip to content
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

Allow built-in install task #110

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
/test-workspace/tasks/lib
/test-workspace/test.txt
/test.js
npm-debug.log
/*.log
15 changes: 14 additions & 1 deletion lib/match-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Minimatch = require("minimatch").Minimatch

const COLON_OR_SLASH = /[:/]/g
const CONVERT_MAP = { ":": "/", "/": ":" }
const BUILT_IN_TASKS = ["restart", "env", "install"]

/**
* Swaps ":" and "/", in order to use ":" as the separator in minimatch.
Expand Down Expand Up @@ -78,6 +79,18 @@ class TaskSet {
}
}

/**
* Checks if an array includes an item.
*
* @param {*[]} array - an array which may or may not include the item.
* @param {*} item - an item to check if the array includes it.
*
* @returns {boolean} whether array includes item
*/
function arrayIncludes(array, item) {
return array.indexOf(item) > -1
}

//------------------------------------------------------------------------------
// Public Interface
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -111,7 +124,7 @@ module.exports = function matchTasks(taskList, patterns) {
}

// Built-in tasks should be allowed.
if (!found && (filter.task === "restart" || filter.task === "env")) {
if (!found && arrayIncludes(BUILT_IN_TASKS, filter.task)) {
taskSet.add(filter.task + filter.args, filter.task)
found = true
}
Expand Down
34 changes: 24 additions & 10 deletions lib/run-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,28 @@ function detectStreamKind(stream, std) {
)
}

/**
* Create spawnArgs for running a task.
*
* @param {string|undefined} npmPath any available npmPath
* @param {string} task the run-script to execute, or "install"
* @param {object} prefixOptions options for spawn
* @returns {Array.<*>} args for spawn
*/
function createSpawnArgs(npmPath, task, prefixOptions) {
if (task === "install") {
return [].concat(
npmPath ? [npmPath, "install"] : ["install"],
prefixOptions
)
}

return [].concat(
npmPath ? [npmPath, "run"] : ["run"],
prefixOptions,
parseArgs(task)
)
}
//------------------------------------------------------------------------------
// Interface
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -138,22 +160,14 @@ module.exports = function runTask(task, options) {
if (path.extname(options.npmPath || "a.js") === ".js") {
const npmPath = options.npmPath || process.env.npm_execpath //eslint-disable-line no-process-env
const execPath = npmPath ? process.execPath : "npm"
const spawnArgs = [].concat(
npmPath ? [npmPath, "run"] : ["run"],
options.prefixOptions,
parseArgs(task)
)
const spawnArgs = createSpawnArgs(npmPath, task, options.prefixOptions)

// Execute.
cp = spawn(execPath, spawnArgs, spawnOptions)
}
else {
const execPath = options.npmPath
const spawnArgs = [].concat(
["run"],
options.prefixOptions,
parseArgs(task)
)
const spawnArgs = createSpawnArgs(undefined, task, options.prefixOptions)

// Execute.
cp = spawn(execPath, spawnArgs, spawnOptions)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"node": ">= 4"
},
"scripts": {
"_mocha": "mocha \"test/*.js\" --compilers js:babel-register --timeout 60000",
"_mocha": "mocha \"test/*.js\" --require babel-register --timeout 60000",
"clean": "rimraf .nyc_output coverage jsdoc \"test-workspace/{build,test.txt}\"",
"docs": "jsdoc -c jsdoc.json",
"lint": "eslint bin lib scripts test \"test-workspace/tasks/*.js\"",
Expand Down Expand Up @@ -48,7 +48,7 @@
"eslint": "^4.5.0",
"eslint-config-mysticatea": "^12.0.0",
"jsdoc": "^3.5.4",
"mocha": "^3.5.0",
"mocha": "^4.0.1",
"nyc": "^11.1.0",
"power-assert": "^1.4.4",
"rimraf": "^2.6.1"
Expand Down
7 changes: 7 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ describe("[common]", () => {
it("run-p command", () => runPar(["env"]))
})

describe("should be able to use `install` built-in task:", () => {
it("Node API", () => nodeApi("install"))
it("npm-run-all command", () => runAll(["install"]))
it("run-s command", () => runSeq(["install"]))
it("run-p command", () => runPar(["install"]))
})

if (process.platform === "win32") {
describe("issue14", () => {
it("Node API", () => nodeApi("test-task:issue14:win32"))
Expand Down