Skip to content

Commit

Permalink
Extend sails-run to also look for installable commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Dec 7, 2017
1 parent 3ae3331 commit 23d68da
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions bin/sails-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,12 @@ module.exports = function(scriptName) {
}


// Now check the `scripts/` directory to see if the file exists.
var relativePathToScript = 'scripts/'+scriptName+'.'+fileExtension;
var doesScriptFileExist = fs.existsSync(path.resolve(relativePathToScript));
// Now check both the `scripts/` directory and node_modules to see if a matching script exists.
var relativePathToAppScript = 'scripts/'+scriptName+'.'+fileExtension;
var relativePathToInstalledScript = 'node_modules/sails-cmd-'+scriptName;
var installedScriptExists = fs.existsSync(path.resolve(relativePathToInstalledScript));
var appScriptExists = fs.existsSync(path.resolve(relativePathToAppScript));
var doesScriptFileExist = appScriptExists || installedScriptExists;

// Ensure that this script is not defined in BOTH places.
if (pjCommandToRun && doesScriptFileExist) {
Expand All @@ -146,7 +149,7 @@ module.exports = function(scriptName) {
// Ensure that this script exists one place or the other.
if (!pjCommandToRun && !doesScriptFileExist) {
console.error('Unknown script: `'+scriptName+'`');
console.error('No matching script is defined at `'+relativePathToScript+'`.');
console.error('No matching script is defined at `'+relativePathToAppScript+'`.');
console.error('(And there is no matching NPM script in the package.json file.)');
return process.exit(1);
}
Expand All @@ -156,7 +159,7 @@ module.exports = function(scriptName) {
// to get the module definition, then run it using MaS.
if (!pjCommandToRun) {
try {
var pathToScriptDef = path.resolve(process.cwd(), 'scripts/'+scriptName);
var pathToScriptDef = path.resolve(process.cwd(), appScriptExists ? relativePathToAppScript : relativePathToInstalledScript);
var scriptDef;
try {
scriptDef = require(pathToScriptDef);
Expand Down

0 comments on commit 23d68da

Please sign in to comment.