makit
Version:
Make in JavaScript done right!
80 lines (79 loc) • 2.67 kB
JavaScript
#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const yargs_1 = __importDefault(require("yargs"));
const makefile_1 = require("../makefile/makefile");
const fs_1 = require("fs");
const path_1 = require("path");
const logger_1 = require("../utils/logger");
const io_1 = require("../io");
const config_1 = require("../config");
const argv = yargs_1.default.usage('$0 [OPTION] <TARGET>...')
.option('makefile', {
alias: 'm',
type: 'string',
description: 'makefile path, defaults to "makefile.js"'
})
.option('database', {
alias: 'd',
type: 'string',
description: 'database file, will be used for cache invalidation, defaults to "./.makit.db"'
})
.option('require', {
alias: 'r',
type: 'array',
string: true,
description: 'require a module before loading makefile.js or makefile.ts'
})
.option('reporter', {
type: 'string',
choices: ['dot', 'text'],
default: 'dot',
description: '"dot", "text"'
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'set loglevel to verbose'
})
.option('debug', {
alias: 'v',
type: 'boolean',
description: 'set loglevel to debug'
})
.option('loglevel', {
alias: 'l',
choices: [0, 1, 2, 3, 4],
description: 'error, warning, info, verbose, debug'
})
.option('graph', {
alias: 'g',
type: 'boolean',
description: 'output dependency graph, defaults to false'
})
.help('help')
.conflicts('loglevel', 'verbose')
.argv;
async function main() {
const pkgjson = path_1.join(process.cwd(), 'package.json');
const conf = config_1.parse(argv, fs_1.existsSync(pkgjson) ? require(pkgjson) : {});
logger_1.Logger.getOrCreate(conf.loglevel);
io_1.IO.getOrCreateDataBase(conf.database);
logger_1.Logger.getOrCreate().info(chalk_1.default['cyan']('CONF'), conf.makefile);
for (const specifier of conf.require || []) {
require(require.resolve(specifier, { paths: [process.cwd()] }));
}
const makefile = global['makit'] = new makefile_1.Makefile(process.cwd(), conf.reporter);
require(conf.makefile);
const targets = argv._;
await Promise.all(targets.length ? targets.map((target) => makefile.make(target)) : [makefile.make()]);
if (conf.graph) {
console.log(chalk_1.default['cyan']('TREE'));
console.log(makefile.dependencyGraphString());
}
}
main().catch(err => console.error(err.stack));