UNPKG

nss-run

Version:

nss-run (not so simple run) is a very simplistic build tool.

75 lines (74 loc) 2.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = loadConfig; var _promises = _interopRequireDefault(require("fs/promises")); var _path = _interopRequireDefault(require("path")); var _chalk = _interopRequireDefault(require("chalk")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } // eslint-disable-next-line no-underscore-dangle let import_; try { // Node < 13.3 doesn't support import() syntax. // eslint-disable-next-line global-require import_ = require('./import').default; } catch (e) {/* empty */} function assemblePath(fileName) { return _path.default.resolve('.', fileName); } async function fileExists(fileName) { try { await _promises.default.access(assemblePath(fileName), _promises.default.constants.R_OK); return true; } catch (ignored) { return false; } } function loadCjs(file) { // eslint-disable-next-line import/no-dynamic-require,global-require require(assemblePath(file)); } async function loadMjs(file) { if (!import_) { throw new Error("Internal error: Native ECMAScript modules aren't supported" + ' by this platform.\n'); } await import_(`file://${assemblePath(file)}`); } async function loadCjsThenMjs(file) { try { loadCjs(file); } catch (e) { await loadMjs(file); } } function loadBabelJs(file) { try { require(_path.default.resolve('.', 'node_modules', '@babel', 'register')); // eslint-disable-line global-require,import/no-dynamic-require } catch (ignored) { console.log(_chalk.default.red("Found '") + _chalk.default.cyan(file) + _chalk.default.red("' but can not find '") + _chalk.default.cyan('@babel/register') + _chalk.default.red("'. Make sure you add @babel/register to your projects dependencies!")); process.exit(-2); } loadCjs(file); } const CONFIG_FILENAMES = { 'nss-runfile.js': loadCjsThenMjs, 'nss-runfile.cjs': loadCjs, 'nss-runfile.mjs': loadMjs, 'nss-runfile.babel.js': loadBabelJs }; async function loadConfig() { // eslint-disable-next-line no-restricted-syntax for (const [file, loader] of Object.entries(CONFIG_FILENAMES)) { // eslint-disable-next-line no-await-in-loop const exists = await fileExists(file); if (exists) { console.log(_chalk.default.blue("Detected '") + _chalk.default.cyan(file) + _chalk.default.blue("'...")); // eslint-disable-next-line no-await-in-loop await loader(file); return; } } console.log(_chalk.default.red("Could not find a '") + Object.keys(CONFIG_FILENAMES).map(f => _chalk.default.cyan(f)).join(_chalk.default.red("' or '")) + _chalk.default.red("' in the current folder.")); process.exit(-1); }