| 1 | var debug = require('debug')('nodemon');
|
|---|
| 2 | var fs = require('fs');
|
|---|
| 3 | var path = require('path');
|
|---|
| 4 | var exists = fs.exists || path.exists;
|
|---|
| 5 | var utils = require('../utils');
|
|---|
| 6 | var rules = require('../rules');
|
|---|
| 7 | var parse = require('../rules/parse');
|
|---|
| 8 | var exec = require('./exec');
|
|---|
| 9 | var defaults = require('./defaults');
|
|---|
| 10 |
|
|---|
| 11 | module.exports = load;
|
|---|
| 12 | module.exports.mutateExecOptions = mutateExecOptions;
|
|---|
| 13 |
|
|---|
| 14 | var existsSync = fs.existsSync || path.existsSync;
|
|---|
| 15 |
|
|---|
| 16 | function findAppScript() {
|
|---|
| 17 | // nodemon has been run alone, so try to read the package file
|
|---|
| 18 | // or try to read the index.js file
|
|---|
| 19 |
|
|---|
| 20 | var pkg =
|
|---|
| 21 | existsSync(path.join(process.cwd(), 'package.json')) &&
|
|---|
| 22 | require(path.join(process.cwd(), 'package.json'));
|
|---|
| 23 | if ((!pkg || pkg.main == undefined) && existsSync('./index.js')) {
|
|---|
| 24 | return 'index.js';
|
|---|
| 25 | }
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * Load the nodemon config, first reading the global root/nodemon.json, then
|
|---|
| 30 | * the local nodemon.json to the exec and then overwriting using any user
|
|---|
| 31 | * specified settings (i.e. from the cli)
|
|---|
| 32 | *
|
|---|
| 33 | * @param {Object} settings user defined settings
|
|---|
| 34 | * @param {Object} options global options
|
|---|
| 35 | * @param {Object} config the config object to be updated
|
|---|
| 36 | * @param {Function} callback that receives complete config
|
|---|
| 37 | */
|
|---|
| 38 | function load(settings, options, config, callback) {
|
|---|
| 39 | config.loaded = [];
|
|---|
| 40 | // first load the root nodemon.json
|
|---|
| 41 | loadFile(options, config, utils.home, function (options) {
|
|---|
| 42 | // then load the user's local configuration file
|
|---|
| 43 | if (settings.configFile) {
|
|---|
| 44 | options.configFile = path.resolve(settings.configFile);
|
|---|
| 45 | }
|
|---|
| 46 | loadFile(options, config, process.cwd(), function (options) {
|
|---|
| 47 | // Then merge over with the user settings (parsed from the cli).
|
|---|
| 48 | // Note that merge protects and favours existing values over new values,
|
|---|
| 49 | // and thus command line arguments get priority
|
|---|
| 50 | options = utils.merge(settings, options);
|
|---|
| 51 |
|
|---|
| 52 | // legacy support
|
|---|
| 53 | if (!Array.isArray(options.ignore)) {
|
|---|
| 54 | options.ignore = [options.ignore];
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if (!options.ignoreRoot) {
|
|---|
| 58 | options.ignoreRoot = defaults.ignoreRoot;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | // blend the user ignore and the default ignore together
|
|---|
| 62 | if (options.ignoreRoot && options.ignore) {
|
|---|
| 63 | if (!Array.isArray(options.ignoreRoot)) {
|
|---|
| 64 | options.ignoreRoot = [options.ignoreRoot];
|
|---|
| 65 | }
|
|---|
| 66 | options.ignore = options.ignoreRoot.concat(options.ignore);
|
|---|
| 67 | } else {
|
|---|
| 68 | options.ignore = defaults.ignore.concat(options.ignore);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | // add in any missing defaults
|
|---|
| 72 | options = utils.merge(options, defaults);
|
|---|
| 73 |
|
|---|
| 74 | if (!options.script && !options.exec) {
|
|---|
| 75 | var found = findAppScript();
|
|---|
| 76 | if (found) {
|
|---|
| 77 | if (!options.args) {
|
|---|
| 78 | options.args = [];
|
|---|
| 79 | }
|
|---|
| 80 | // if the script is found as a result of not being on the command
|
|---|
| 81 | // line, then we move any of the pre double-dash args in execArgs
|
|---|
| 82 | const n =
|
|---|
| 83 | options.scriptPosition === null
|
|---|
| 84 | ? options.args.length
|
|---|
| 85 | : options.scriptPosition;
|
|---|
| 86 |
|
|---|
| 87 | options.execArgs = (options.execArgs || []).concat(
|
|---|
| 88 | options.args.splice(0, n)
|
|---|
| 89 | );
|
|---|
| 90 | options.scriptPosition = null;
|
|---|
| 91 |
|
|---|
| 92 | options.script = found;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | mutateExecOptions(options);
|
|---|
| 97 |
|
|---|
| 98 | if (options.quiet) {
|
|---|
| 99 | utils.quiet();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | if (options.verbose) {
|
|---|
| 103 | utils.debug = true;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | // simplify the ready callback to be called after the rules are normalised
|
|---|
| 107 | // from strings to regexp through the rules lib. Note that this gets
|
|---|
| 108 | // created *after* options is overwritten twice in the lines above.
|
|---|
| 109 | var ready = function (options) {
|
|---|
| 110 | normaliseRules(options, callback);
|
|---|
| 111 | };
|
|---|
| 112 |
|
|---|
| 113 | ready(options);
|
|---|
| 114 | });
|
|---|
| 115 | });
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | function normaliseRules(options, ready) {
|
|---|
| 119 | // convert ignore and watch options to rules/regexp
|
|---|
| 120 | rules.watch.add(options.watch);
|
|---|
| 121 | rules.ignore.add(options.ignore);
|
|---|
| 122 |
|
|---|
| 123 | // normalise the watch and ignore arrays
|
|---|
| 124 | options.watch = options.watch === false ? false : rules.rules.watch;
|
|---|
| 125 | options.ignore = rules.rules.ignore;
|
|---|
| 126 |
|
|---|
| 127 | ready(options);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /**
|
|---|
| 131 | * Looks for a config in the current working directory, and a config in the
|
|---|
| 132 | * user's home directory, merging the two together, giving priority to local
|
|---|
| 133 | * config. This can then be overwritten later by command line arguments
|
|---|
| 134 | *
|
|---|
| 135 | * @param {Function} ready callback to pass loaded settings to
|
|---|
| 136 | */
|
|---|
| 137 | function loadFile(options, config, dir, ready) {
|
|---|
| 138 | if (!ready) {
|
|---|
| 139 | ready = function () {};
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | var callback = function (settings) {
|
|---|
| 143 | // prefer the local nodemon.json and fill in missing items using
|
|---|
| 144 | // the global options
|
|---|
| 145 | ready(utils.merge(settings, options));
|
|---|
| 146 | };
|
|---|
| 147 |
|
|---|
| 148 | if (!dir) {
|
|---|
| 149 | return callback({});
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | var filename = options.configFile || path.join(dir, 'nodemon.json');
|
|---|
| 153 |
|
|---|
| 154 | if (config.loaded.indexOf(filename) !== -1) {
|
|---|
| 155 | // don't bother re-parsing the same config file
|
|---|
| 156 | return callback({});
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | fs.readFile(filename, 'utf8', function (err, data) {
|
|---|
| 160 | if (err) {
|
|---|
| 161 | if (err.code === 'ENOENT') {
|
|---|
| 162 | if (!options.configFile && dir !== utils.home) {
|
|---|
| 163 | // if no specified local config file and local nodemon.json
|
|---|
| 164 | // doesn't exist, try the package.json
|
|---|
| 165 | return loadPackageJSON(config, callback);
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 | return callback({});
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | var settings = {};
|
|---|
| 172 |
|
|---|
| 173 | try {
|
|---|
| 174 | settings = JSON.parse(data.toString('utf8').replace(/^\uFEFF/, ''));
|
|---|
| 175 | if (!filename.endsWith('package.json') || settings.nodemonConfig) {
|
|---|
| 176 | config.loaded.push(filename);
|
|---|
| 177 | }
|
|---|
| 178 | } catch (e) {
|
|---|
| 179 | utils.log.fail('Failed to parse config ' + filename);
|
|---|
| 180 | console.error(e);
|
|---|
| 181 | process.exit(1);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | // options values will overwrite settings
|
|---|
| 185 | callback(settings);
|
|---|
| 186 | });
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | function loadPackageJSON(config, ready) {
|
|---|
| 190 | if (!ready) {
|
|---|
| 191 | ready = () => {};
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | const dir = process.cwd();
|
|---|
| 195 | const filename = path.join(dir, 'package.json');
|
|---|
| 196 | const packageLoadOptions = { configFile: filename };
|
|---|
| 197 | return loadFile(packageLoadOptions, config, dir, (settings) => {
|
|---|
| 198 | ready(settings.nodemonConfig || {});
|
|---|
| 199 | });
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | function mutateExecOptions(options) {
|
|---|
| 203 | // work out the execOptions based on the final config we have
|
|---|
| 204 | options.execOptions = exec(
|
|---|
| 205 | {
|
|---|
| 206 | script: options.script,
|
|---|
| 207 | exec: options.exec,
|
|---|
| 208 | args: options.args,
|
|---|
| 209 | scriptPosition: options.scriptPosition,
|
|---|
| 210 | nodeArgs: options.nodeArgs,
|
|---|
| 211 | execArgs: options.execArgs,
|
|---|
| 212 | ext: options.ext,
|
|---|
| 213 | env: options.env,
|
|---|
| 214 | },
|
|---|
| 215 | options.execMap
|
|---|
| 216 | );
|
|---|
| 217 |
|
|---|
| 218 | // clean up values that we don't need at the top level
|
|---|
| 219 | delete options.scriptPosition;
|
|---|
| 220 | delete options.script;
|
|---|
| 221 | delete options.args;
|
|---|
| 222 | delete options.ext;
|
|---|
| 223 |
|
|---|
| 224 | return options;
|
|---|
| 225 | }
|
|---|