UNPKG

bundlewatch

Version:
87 lines (86 loc) 3.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _fs = _interopRequireDefault(require("fs")); var _path = _interopRequireDefault(require("path")); var _readPkgUp = _interopRequireDefault(require("read-pkg-up")); var _ValidationError = _interopRequireDefault(require("../app/errors/ValidationError")); var _logger = _interopRequireDefault(require("../logger")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } const readConfigFile = configFilePath => { try { return _fs.default.readFileSync(configFilePath, 'utf8'); } catch (error) { _logger.default.error(`Exception while trying to read JSON config file`, error); return null; } }; const getConfigFileJson = configFilePath => { const configFileContents = readConfigFile(configFilePath); if (!configFileContents) { throw new _ValidationError.default(`Could not find JSON config file: ${configFilePath}}`); } try { return JSON.parse(configFileContents); } catch (error) { _logger.default.error(`Exception while parsing JSON config`, error); throw new _ValidationError.default(`Could not parse JSON config file: ${configFilePath}`); } }; const getConfigFileJS = configFilePath => { const projectDir = _path.default.resolve(_fs.default.realpathSync(process.cwd())); const fullPath = `${projectDir}/${configFilePath}`; try { return require(fullPath); // eslint-disable-line global-require } catch (error) { _logger.default.error(`Exception while loading JS config`, error); throw new _ValidationError.default(`Exception while loading JS config: ${fullPath}`); } }; const getConfigFileContents = configFilePath => { if (configFilePath.endsWith('.js') || configFilePath.endsWith('.cjs') || configFilePath.endsWith('.mjs')) { return getConfigFileJS(configFilePath); } return getConfigFileJson(configFilePath); }; const determineConfig = cliOptions => { const pkgJson = (_readPkgUp.default.sync() || {}).packageJson; let pkgJsonbundlewatch = pkgJson.bundlewatch; if (cliOptions.args && cliOptions.args.length > 0) { if (pkgJsonbundlewatch) { _logger.default.warn(`CLI files supplied, config in package.json will be ignored`); } if (cliOptions.configFilePath) { _logger.default.warn(`CLI files supplied, configFilePath will be ignored (this must be used on its own)`); } const files = cliOptions.args.map(filePathGlob => { return { path: filePathGlob, maxSize: cliOptions.maxSize }; }); return { files, defaultCompression: cliOptions.compression || 'gzip', normalizeFilenames: cliOptions.normalize }; } if (cliOptions.config) { if (pkgJsonbundlewatch) { _logger.default.warn(`configFilePath supplied, config in package.json will be ignored`); } return getConfigFileContents(cliOptions.config); } if (pkgJsonbundlewatch) { if (Array.isArray(pkgJsonbundlewatch)) { return { files: pkgJsonbundlewatch }; } return pkgJsonbundlewatch; } return {}; }; var _default = exports.default = determineConfig;