| [81bc7da] | 1 | const path = require('path');
|
|---|
| 2 | const fs = require('fs');
|
|---|
| 3 | const existsSync = fs.existsSync;
|
|---|
| 4 | const utils = require('../utils');
|
|---|
| 5 |
|
|---|
| 6 | module.exports = exec;
|
|---|
| 7 | module.exports.expandScript = expandScript;
|
|---|
| 8 |
|
|---|
| 9 | /**
|
|---|
| 10 | * Reads the cwd/package.json file and looks to see if it can load a script
|
|---|
| 11 | * and possibly an exec first from package.main, then package.start.
|
|---|
| 12 | *
|
|---|
| 13 | * @return {Object} exec & script if found
|
|---|
| 14 | */
|
|---|
| 15 | function execFromPackage() {
|
|---|
| 16 | // doing a try/catch because we can't use the path.exist callback pattern
|
|---|
| 17 | // or we could, but the code would get messy, so this will do exactly
|
|---|
| 18 | // what we're after - if the file doesn't exist, it'll throw.
|
|---|
| 19 | try {
|
|---|
| 20 | // note: this isn't nodemon's package, it's the user's cwd package
|
|---|
| 21 | var pkg = require(path.join(process.cwd(), 'package.json'));
|
|---|
| 22 | if (pkg.main !== undefined) {
|
|---|
| 23 | // no app found to run - so give them a tip and get the feck out
|
|---|
| 24 | return { exec: null, script: pkg.main };
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | if (pkg.scripts && pkg.scripts.start) {
|
|---|
| 28 | const parts = pkg.scripts.start.split(' ');
|
|---|
| 29 |
|
|---|
| 30 | // fixes a loop issue where the script repeats over and over #2258
|
|---|
| 31 | if (parts.includes('nodemon')) {
|
|---|
| 32 | return null;
|
|---|
| 33 | }
|
|---|
| 34 | return { exec: pkg.scripts.start };
|
|---|
| 35 | }
|
|---|
| 36 | } catch (e) {}
|
|---|
| 37 |
|
|---|
| 38 | return null;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | function replace(map, str) {
|
|---|
| 42 | var re = new RegExp('{{(' + Object.keys(map).join('|') + ')}}', 'g');
|
|---|
| 43 | return str.replace(re, function (all, m) {
|
|---|
| 44 | return map[m] || all || '';
|
|---|
| 45 | });
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | function expandScript(script, ext) {
|
|---|
| 49 | if (!ext) {
|
|---|
| 50 | ext = '.js';
|
|---|
| 51 | }
|
|---|
| 52 | if (script.indexOf(ext) !== -1) {
|
|---|
| 53 | return script;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | if (existsSync(path.resolve(script))) {
|
|---|
| 57 | return script;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | if (existsSync(path.resolve(script + ext))) {
|
|---|
| 61 | return script + ext;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | return script;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Discovers all the options required to run the script
|
|---|
| 69 | * and if a custom exec has been passed in, then it will
|
|---|
| 70 | * also try to work out what extensions to monitor and
|
|---|
| 71 | * whether there's a special way of running that script.
|
|---|
| 72 | *
|
|---|
| 73 | * @param {Object} nodemonOptions
|
|---|
| 74 | * @param {Object} execMap
|
|---|
| 75 | * @return {Object} new and updated version of nodemonOptions
|
|---|
| 76 | */
|
|---|
| 77 | function exec(nodemonOptions, execMap) {
|
|---|
| 78 | if (!execMap) {
|
|---|
| 79 | execMap = {};
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | var options = utils.clone(nodemonOptions || {});
|
|---|
| 83 | var script;
|
|---|
| 84 |
|
|---|
| 85 | // if there's no script passed, try to get it from the first argument
|
|---|
| 86 | if (!options.script && (options.args || []).length) {
|
|---|
| 87 | script = expandScript(
|
|---|
| 88 | options.args[0],
|
|---|
| 89 | options.ext && '.' + (options.ext || 'js').split(',')[0]
|
|---|
| 90 | );
|
|---|
| 91 |
|
|---|
| 92 | // if the script was found, shift it off our args
|
|---|
| 93 | if (script !== options.args[0]) {
|
|---|
| 94 | options.script = script;
|
|---|
| 95 | options.args.shift();
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | // if there's no exec found yet, then try to read it from the local
|
|---|
| 100 | // package.json this logic used to sit in the cli/parse, but actually the cli
|
|---|
| 101 | // should be parsed first, then the user options (via nodemon.json) then
|
|---|
| 102 | // finally default down to pot shots at the directory via package.json
|
|---|
| 103 | if (!options.exec && !options.script) {
|
|---|
| 104 | var found = execFromPackage();
|
|---|
| 105 | if (found !== null) {
|
|---|
| 106 | if (found.exec) {
|
|---|
| 107 | options.exec = found.exec;
|
|---|
| 108 | }
|
|---|
| 109 | if (!options.script) {
|
|---|
| 110 | options.script = found.script;
|
|---|
| 111 | }
|
|---|
| 112 | if (Array.isArray(options.args) && options.scriptPosition === null) {
|
|---|
| 113 | options.scriptPosition = options.args.length;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // var options = utils.clone(nodemonOptions || {});
|
|---|
| 119 | script = path.basename(options.script || '');
|
|---|
| 120 |
|
|---|
| 121 | var scriptExt = path.extname(script).slice(1);
|
|---|
| 122 |
|
|---|
| 123 | var extension = options.ext;
|
|---|
| 124 | if (extension === undefined) {
|
|---|
| 125 | var isJS = scriptExt === 'js' || scriptExt === 'mjs' || scriptExt === 'cjs';
|
|---|
| 126 | extension = isJS || !scriptExt ? 'js,mjs,cjs' : scriptExt;
|
|---|
| 127 | extension += ',json'; // Always watch JSON files
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | var execDefined = !!options.exec;
|
|---|
| 131 |
|
|---|
| 132 | // allows the user to simplify cli usage:
|
|---|
| 133 | // https://github.com/remy/nodemon/issues/195
|
|---|
| 134 | // but always give preference to the user defined argument
|
|---|
| 135 | if (!options.exec && execMap[scriptExt] !== undefined) {
|
|---|
| 136 | options.exec = execMap[scriptExt];
|
|---|
| 137 | execDefined = true;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | options.execArgs = nodemonOptions.execArgs || [];
|
|---|
| 141 |
|
|---|
| 142 | if (Array.isArray(options.exec)) {
|
|---|
| 143 | options.execArgs = options.exec;
|
|---|
| 144 | options.exec = options.execArgs.shift();
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | if (options.exec === undefined) {
|
|---|
| 148 | options.exec = 'node';
|
|---|
| 149 | } else {
|
|---|
| 150 | // allow variable substitution for {{filename}} and {{pwd}}
|
|---|
| 151 | var substitution = replace.bind(null, {
|
|---|
| 152 | filename: options.script,
|
|---|
| 153 | pwd: process.cwd(),
|
|---|
| 154 | });
|
|---|
| 155 |
|
|---|
| 156 | var newExec = substitution(options.exec);
|
|---|
| 157 | if (
|
|---|
| 158 | newExec !== options.exec &&
|
|---|
| 159 | options.exec.indexOf('{{filename}}') !== -1
|
|---|
| 160 | ) {
|
|---|
| 161 | options.script = null;
|
|---|
| 162 | }
|
|---|
| 163 | options.exec = newExec;
|
|---|
| 164 |
|
|---|
| 165 | var newExecArgs = options.execArgs.map(substitution);
|
|---|
| 166 | if (newExecArgs.join('') !== options.execArgs.join('')) {
|
|---|
| 167 | options.execArgs = newExecArgs;
|
|---|
| 168 | delete options.script;
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | if (options.exec === 'node' && options.nodeArgs && options.nodeArgs.length) {
|
|---|
| 173 | options.execArgs = options.execArgs.concat(options.nodeArgs);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | // note: indexOf('coffee') handles both .coffee and .litcoffee
|
|---|
| 177 | if (
|
|---|
| 178 | !execDefined &&
|
|---|
| 179 | options.exec === 'node' &&
|
|---|
| 180 | scriptExt.indexOf('coffee') !== -1
|
|---|
| 181 | ) {
|
|---|
| 182 | options.exec = 'coffee';
|
|---|
| 183 |
|
|---|
| 184 | // we need to get execArgs set before the script
|
|---|
| 185 | // for example, in `nodemon --debug my-script.coffee --my-flag`, debug is an
|
|---|
| 186 | // execArg, while my-flag is a script arg
|
|---|
| 187 | var leadingArgs = (options.args || []).splice(0, options.scriptPosition);
|
|---|
| 188 | options.execArgs = options.execArgs.concat(leadingArgs);
|
|---|
| 189 | options.scriptPosition = 0;
|
|---|
| 190 |
|
|---|
| 191 | if (options.execArgs.length > 0) {
|
|---|
| 192 | // because this is the coffee executable, we need to combine the exec args
|
|---|
| 193 | // into a single argument after the nodejs flag
|
|---|
| 194 | options.execArgs = ['--nodejs', options.execArgs.join(' ')];
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | if (options.exec === 'coffee') {
|
|---|
| 199 | // don't override user specified extension tracking
|
|---|
| 200 | if (options.ext === undefined) {
|
|---|
| 201 | if (extension) {
|
|---|
| 202 | extension += ',';
|
|---|
| 203 | }
|
|---|
| 204 | extension += 'coffee,litcoffee';
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | // because windows can't find 'coffee', it needs the real file 'coffee.cmd'
|
|---|
| 208 | if (utils.isWindows) {
|
|---|
| 209 | options.exec += '.cmd';
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | // allow users to make a mistake on the extension to monitor
|
|---|
| 214 | // converts .js, pug => js,pug
|
|---|
| 215 | // BIG NOTE: user can't do this: nodemon -e *.js
|
|---|
| 216 | // because the terminal will automatically expand the glob against
|
|---|
| 217 | // the file system :(
|
|---|
| 218 | extension = (extension.match(/[^,*\s]+/g) || [])
|
|---|
| 219 | .map((ext) => ext.replace(/^\./, ''))
|
|---|
| 220 | .join(',');
|
|---|
| 221 |
|
|---|
| 222 | options.ext = extension;
|
|---|
| 223 |
|
|---|
| 224 | if (options.script) {
|
|---|
| 225 | options.script = expandScript(
|
|---|
| 226 | options.script,
|
|---|
| 227 | extension && '.' + extension.split(',')[0]
|
|---|
| 228 | );
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | options.env = {};
|
|---|
| 232 | // make sure it's an object (and since we don't have )
|
|---|
| 233 | if ({}.toString.apply(nodemonOptions.env) === '[object Object]') {
|
|---|
| 234 | options.env = utils.clone(nodemonOptions.env);
|
|---|
| 235 | } else if (nodemonOptions.env !== undefined) {
|
|---|
| 236 | throw new Error('nodemon env values must be an object: { PORT: 8000 }');
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | return options;
|
|---|
| 240 | }
|
|---|