forked from charlesguse/run-script-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·40 lines (36 loc) · 1.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#! /usr/bin/env node
"use strict";
const path = require("path");
if (!process.env["npm_config_argv"]) {
console.log("This is meant to be run from within npm script. See https://github.com/charlesguse/run-script-os");
return;
}
const spawn = require("child_process").spawn;
let scripts;
if (process.platform === "win32") {
scripts = require("./package.json").scripts;
} else {
scripts = require(path.join(process.env.PWD, "package.json")).scripts;
}
let npmArgs = JSON.parse(process.env["npm_config_argv"]);
let options = npmArgs.original;
if (!(options[0] === "run" || options[0] === "run-script")) {
options.unshift("run");
}
let osCommand = `${options[1]}:${process.platform}`
if (!(osCommand in scripts)) {
let regex = new RegExp(`^(${options[1]}):([a-zA-Z0-9-]*:)*(${process.platform})(:[a-zA-Z0-9-]*)*$`, "g")
for (let command in scripts) {
if (command.match(regex)) {
osCommand = command;
break;
}
}
}
options[1] = osCommand;
let platformSpecific;
if (process.platform === "win32") {
platformSpecific = spawn("npm.cmd", options, { shell: true, stdio: "inherit"});
} else {
platformSpecific = spawn("npm", options, { shell: true, stdio: "inherit" });
}