mili
Version:
Scaffolding with continuous control over the development of the project.
144 lines (143 loc) • 5.91 kB
JavaScript
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());
});
};
import Ajv from 'ajv8';
import { program } from 'commander';
import * as path from 'path';
import * as fs from 'fs-extra';
import * as milircSchema from './schema/milirc.json';
import { cosmiconfig } from 'cosmiconfig';
import { version } from '../package.json';
import { init } from './init';
import { upgrade } from './upgrade';
import { update } from './update';
import { clean } from './clean';
import { check } from './check';
import * as logger from "./util/logger";
import { prettyError } from "./util/pretty-error";
const ajv = new Ajv();
const validate = ajv.compile(milircSchema);
const explorer = 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* () {
program
.version(version);
const absolutize = (val) => path.resolve(val);
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 init({
cwd,
template: repository,
registry,
force,
version,
});
}));
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 upgrade({
template: config.template,
version: config.version,
registry: config.registry,
cwd: options.cwd,
force: options.force,
answers: config.answers,
}, options.version);
}));
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 update({
template: config.template,
version: config.version,
registry: config.registry,
cwd,
force,
answers: config.answers,
});
}));
program
.command('clean')
.description('Clean the cache of mili')
.action(() => __awaiter(this, void 0, void 0, function* () {
yield clean();
}));
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 check({
template: config.template,
version: config.version,
registry: config.registry,
answers: config.answers,
cwd,
showDiff: diff,
fold,
});
}));
program.on('command:*', function (operands) {
logger.error(`error: unknown command '${operands[0]}'`);
process.exitCode = 1;
});
try {
yield program.parseAsync(process.argv);
}
catch (e) {
logger.error(prettyError(e));
process.exitCode = 1;
}
});
}
void main();