UNPKG

mili

Version:

Scaffolding with continuous control over the development of the project.

146 lines (145 loc) 6.26 kB
#!/usr/bin/env node "use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const ajv8_1 = require("ajv8"); const commander_1 = require("commander"); const path = require("path"); const fs = require("fs-extra"); const milircSchema = require("./schema/milirc.json"); const cosmiconfig_1 = require("cosmiconfig"); const package_json_1 = require("../package.json"); const init_1 = require("./init"); const upgrade_1 = require("./upgrade"); const update_1 = require("./update"); const clean_1 = require("./clean"); const check_1 = require("./check"); const logger = require("./util/logger"); const pretty_error_1 = require("./util/pretty-error"); const ajv = new ajv8_1.default(); const validate = ajv.compile(milircSchema); const explorer = (0, cosmiconfig_1.cosmiconfig)('mili'); function getConfig(cwd = process.cwd()) { return __awaiter(this, void 0, void 0, function* () { const result = yield explorer.search(cwd); if (!result) throw new Error('Cannot find .milirc.yml or .milirc.json'); const config = result.config; const valid = validate(config); if (!valid) throw new Error(ajv.errorsText(validate.errors, { dataVar: 'milirc' })); return config; }); } function main() { return __awaiter(this, void 0, void 0, function* () { commander_1.program .version(package_json_1.version); const absolutize = (val) => path.resolve(val); commander_1.program .command('init <repository>') .usage('[options] <repository>') .description('initialize the project') .option('-f --force') .option('-v --version <version>', 'Set the template version') .option('--registry <registry>', 'Set the npm registry') .option('--cwd <cwd>', 'Set the current work directory', absolutize) .action((repository, option) => __awaiter(this, void 0, void 0, function* () { const { force = false, cwd, version, registry } = option; if (cwd) yield fs.ensureDir(cwd); yield (0, init_1.init)({ cwd, template: repository, registry, force, version, }); })); commander_1.program .command('upgrade') .description('upgrade the template') .option('-f --force') .option('--cwd [cwd]', 'Set the current work directory', absolutize) .option('-v --version <version>', 'Set the template version') .action((options) => __awaiter(this, void 0, void 0, function* () { const config = yield getConfig(options.cwd); yield (0, upgrade_1.upgrade)({ template: config.template, version: config.version, registry: config.registry, cwd: options.cwd, force: options.force, answers: config.answers, }, options.version); })); commander_1.program .command('update') .description('Update the project with the current version of the template') .option('-f --force') .option('--cwd [cwd]', 'Set the current work directory', absolutize) .action((option) => __awaiter(this, void 0, void 0, function* () { const { force = false } = option; const cwd = option.cwd; if (cwd && !fs.pathExistsSync(cwd)) throw new Error(`No such directory: ${cwd}`); const config = yield getConfig(cwd); yield (0, update_1.update)({ template: config.template, version: config.version, registry: config.registry, cwd, force, answers: config.answers, }); })); commander_1.program .command('clean') .description('Clean the cache of mili') .action(() => __awaiter(this, void 0, void 0, function* () { yield (0, clean_1.clean)(); })); commander_1.program .command('check [files...]') .description('Check if the project file meets the template requirements') .option('--cwd [cwd]', 'Set the current work directory', absolutize) .option('-d --diff', 'Show file difference') .option('--fold', 'fold undifferentiated code') .action((files, options) => __awaiter(this, void 0, void 0, function* () { const { cwd, diff, fold } = options; if (cwd && !fs.pathExistsSync(cwd)) { throw new Error(`No such directory: ${cwd}`); } const config = yield getConfig(options.cwd); yield (0, check_1.check)({ template: config.template, version: config.version, registry: config.registry, answers: config.answers, cwd, showDiff: diff, fold, }); })); commander_1.program.on('command:*', function (operands) { logger.error(`error: unknown command '${operands[0]}'`); process.exitCode = 1; }); try { yield commander_1.program.parseAsync(process.argv); } catch (e) { logger.error((0, pretty_error_1.prettyError)(e)); process.exitCode = 1; } }); } void main();