think-cli
Version:
A simple CLI for scaffolding Thinkjs projects.
86 lines (71 loc) • 1.99 kB
JavaScript
const program = require('commander');
const chalk = require('chalk');
const path = require('path');
const helper = require('think-helper');
const utils = require('../../lib/utils');
const logger = require('../../lib/logger');
const Run = require('../../lib/run');
const argv = require('minimist')(process.argv.slice(2));
/**
* Usage.
*/
program
.usage('<module-name>');
/**
* Help.
*/
program.on('--help', function() {
console.log();
console.log(' Examples:');
console.log();
console.log(chalk.gray(' # create a module with the ' + chalk.gray.underline.bold('default name')));
console.log(' $ thinkjs module');
console.log();
console.log(chalk.gray(' # create a module with the name ' + chalk.gray.underline.bold('user')));
console.log(' $ thinkjs module user');
console.log();
});
program.parse(process.argv);
/**
* Padding.
*/
console.log();
process.on('exit', function() {
console.log();
});
/**
* Start.
*/
const appPath = path.join(path.resolve('./'));
if (!utils.isThinkApp(appPath)) {
logger.error(
'Please execute the command in the ' +
chalk.yellow.underline.bold('thinkjs project') +
' root directory'
);
}
const thinkjsInfo = require(path.join(appPath, 'package.json')).thinkjs;
const isMultiModule = thinkjsInfo.isMultiModule;
if (!isMultiModule) {
logger.error('app mode is not module, can not create module.');
}
const context = Object.assign(thinkjsInfo.metadata, argv, {
actionPrefix: './',
moduleName: program.args[0] || thinkjsInfo.metadata.defaultModule,
ROOT_PATH: appPath,
APP_NAME: thinkjsInfo.projectName
});
const run = new Run({
template: thinkjsInfo.template,
targetPath: appPath,
options: { name: thinkjsInfo.metadata.name, command: 'module', maps: 'module', context },
done(err, files) {
if (err) return logger.error(err);
Object
.keys(files)
.forEach(file => {
logger.success('Create: %s', path.normalize(file));
});
}
});
run.start();