Skip to content
Merged
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
44 changes: 25 additions & 19 deletions functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
* @param {array} scripts - list of available scripts defined in package.json
*/
exports.matchScript = function matchScript(script, platform, scripts) {
/**
* Save the result so we can determine if there was a match
* First check for a basic match before we have to go through each script with a regex
*/
let result = (`${script}:${platform}` in scripts) ? `${script}:${platform}` : false;
if(result){
return result;
}

/**
* Regular expresion match
* it helps when the "in" operator can't determine if there's a real match or not,
Expand All @@ -18,47 +27,44 @@
if (command.match(regex)) return command;
}

/**
* Save the result so we can determine if there was a match
*/
let result;

/**
* Alias match, allows for a more verbose description of the platform
* it also helps to group similar platforms on a single execution
*/
switch (platform) {
case 'win32': result = (`${script}:windows` in scripts) ? `${script}:windows` : false;

case 'win32':
result = (`${script}:windows` in scripts) ? `${script}:windows` : false;
break;
case 'aix':
case 'linux':
case 'sunos':
case 'openbsd':
case 'freebsd':
case 'android': result = (`${script}:nix` in scripts) ? `${script}:nix` : false;

case 'android':
result = (`${script}:nix` in scripts) ? `${script}:nix` : false;
break;
case 'darwin':
/**
* macOS specific scripts (e.g. brew)
*/
if (script === 'darwin')
result = (`${script}:darwin` in scripts) ? `${script}:darwin` : false;
result = (`${script}:darwin` in scripts) ? `${script}:darwin` : false;

/**
* nix compatible scripts (cp, rm...)
*/
else if (script === 'nix')
result = (`${script}:nix` in scripts) ? `${script}:nix` : false;

if(!result) {
result = (`${script}:nix` in scripts) ? `${script}:nix` : false;
}
break;

default: return false;
default: result = false;
}

/**
* Test if no script could be matched and try to return the default
*/
if (!result) {
return (`${script}:default` in scripts) ? `${script}:default` : false;
if(result){
return result;
}
}

return (`${script}:default` in scripts) ? `${script}:default` : false;
};
31 changes: 13 additions & 18 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,17 @@ if (process.env.npm_config_user_agent.includes('yarn') && !argument) {
}

/**
* Prepare command structure to test the available scripts defined in "scripts: { ... }"
*/
let osCommand = `${options[1]}:${platform}`;

/**
* Basic match upon the property name
* determine wether the name of the script exists in the list of defined scripts
*/
let foundMatch = osCommand in scripts;

if (!foundMatch) {
/**
* More in-depth match
* Execute the regular expression to help identify missing scripts
* It also tests for different aliases
*/
osCommand = matchScript(argument, platform, scripts) || matchScript(event, platform, scripts);
foundMatch = !!osCommand;
}
*/
let osCommand = matchScript(event || argument, platform, scripts);

/**
* Test again, this time to end the process gracefully
*/
if (!foundMatch) {
console.log(`run-script-os was unable to execute the script '${osCommand}'`);
if (!osCommand) {
console.log(`run-script-os was unable to execute the script '${event || argument}'`);
process.exit(0);
}

Expand All @@ -94,6 +80,15 @@ if (!(options[0] === "run" || options[0] === "run-script")) {
*/
options[1] = osCommand;

/**
* Check if we should be passing the original arguments to the new script
* Fix for #23
*/
const args = process.argv.slice(2).map((a) => a.toLowerCase());
if(args.includes('--no-arguments')){
options = options.slice(0,2);
}

/**
* Spawn new process to run the required script
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "run-script-os",
"version": "1.1.2",
"version": "1.1.3",
"description": "run-script-os is a tool that will let you use generic npm script commands that will pass through to os specific commands.",
"main": "index.js",
"scripts": {
Expand Down