es-node-runner
Version:
Node runner that transpiles typescript or es modules using blazing fast ⚡ esbuild and restarts the process automatically on change. Suitable for node development server.
73 lines (72 loc) • 2.06 kB
JavaScript
import {resolve} from 'path';
import DEBUG from 'debug';
import {createRequire} from 'module';
const arg = process.argv.slice(2);
const idx = arg.indexOf('--debug');
if (idx !== -1) {
DEBUG.enable(arg[idx + 1] ?? '*');
}
const debug = DEBUG('es-node-runner:config');
const isObject = (obj) => {
return Boolean(obj) && typeof obj === 'object' && !Array.isArray(obj);
};
const overrideTargetObj = (target, source) => {
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(target, key)) {
if (isObject(source[key])) {
overrideTargetObj(target[key], source[key]);
} else {
target[key] = source[key] ?? target[key];
}
}
}
};
const loadConfig = async () => {
debug('loading config');
const cwd = process.cwd();
const defaultConfig = {
buildOptions: {
entry: 'src/index.ts',
outdir: 'node_modules/.cache/es-node-runner',
outfile: 'bundle.js',
target: 'node14',
format: 'cjs',
sourcemap: true,
},
watchOptions: {
watch: 'src',
ignore: undefined,
},
spawnOptions: {
delay: 1000,
restartCmd: 'rs',
clearTerminal: false,
autoRestart: true,
logging: true,
args: [],
},
};
let userConfig;
try {
debug('looking for es-node-runner.config file');
userConfig = (
await import('file://' + resolve(cwd, 'es-node-runner.config.js'))
).default;
} catch (error) {
debug(
'es-node-runner.config file not found...looking for config in package.json'
);
const require = createRequire(import.meta.url);
const packageJson = require(resolve(cwd, 'package.json'));
userConfig = packageJson['es-node-runner'] ?? {};
}
debug(
Object.keys(userConfig).length === 0
? 'user config not found...using default config'
: 'overriding default config with user config'
);
overrideTargetObj(defaultConfig, userConfig);
debug('config loaded');
return defaultConfig;
};
export const {buildOptions, watchOptions, spawnOptions} = await loadConfig();