UNPKG

@relative-ci/agent

Version:

Send bundle stats and CI build information to RelativeCI

54 lines (51 loc) 2.5 kB
import path from 'path'; import get from 'lodash/get'; import { pathExistsSync, readJSONSync } from 'fs-extra'; import { cosmiconfigSync } from 'cosmiconfig'; import yargs from 'yargs/yargs'; import { hideBin } from 'yargs/helpers'; import { CLI_MISSING_CONFIGURATION_ERROR, CLI_INVALID_CONFIGURATION_ERROR, CLI_MISSING_STATS_FILE_ERROR } from './locales/en.js'; import { SOURCE_WEBPACK_STATS } from './constants.js'; import { filterArtifacts } from './artifacts/filter-artifacts.js'; import { validateWebpackStats } from './artifacts/validate-webpack-stats.js'; import { debug } from './utils/debug.js'; import { logResponse } from './utils/log-response.js'; import { normalizeParams } from './utils/normalize-params.js'; import ingest from './ingest/ingest.js'; async function cli(processArgs) { const args = await yargs(hideBin(processArgs)) .usage('Usage: $0 OPTIONS') .option('config-dir', { describe: 'Config directory', default: '', alias: 'c' }) .option('commit', { describe: 'Commit SHA', default: '' }) .option('commit-message', { describe: 'Commit message', default: '', alias: 'commitMessage' }) .option('branch', { describe: 'Branch name', default: '' }) .option('pr', { describe: 'Pull Request number', default: '' }) .option('slug', { describe: 'Project slug', default: '' }) .help() .argv; const searchConfig = cosmiconfigSync('relativeci', { searchStrategy: 'global', }).search('config-dir' in args ? args['config-dir'] : undefined); debug('Config', searchConfig); if (!searchConfig) { throw new Error(CLI_MISSING_CONFIGURATION_ERROR); } const { config } = searchConfig; if (!get(config, 'webpack.stats')) { throw new Error(CLI_INVALID_CONFIGURATION_ERROR); } // Load webpack stats file relative to the config file const webpackArtifactFilepath = path.join(path.dirname(searchConfig.filepath), get(config, 'webpack.stats')); if (!pathExistsSync(webpackArtifactFilepath)) { throw new Error(CLI_MISSING_STATS_FILE_ERROR); } const data = readJSONSync(webpackArtifactFilepath); validateWebpackStats(data); debug('CLI arguments', args); const params = normalizeParams(args, config); const artifactsData = filterArtifacts([{ key: SOURCE_WEBPACK_STATS, data }]); const response = await ingest(artifactsData, params, config); logResponse(response); } export { cli as default }; //# sourceMappingURL=cli.js.map