UNPKG

think-cli

Version:

A simple CLI for scaffolding Thinkjs projects.

90 lines (75 loc) 2.34 kB
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 argv = require('minimist')(process.argv.slice(2)); const Run = require('../../lib/run'); /** * Usage. */ program .usage('<adapter> [module-name]'); /** * Help. */ program.on('--help', function() { console.log(); console.log(' Examples:'); console.log(); console.log(chalk.gray(' # create a adapter with the name ' + chalk.gray.underline.bold('user'))); console.log(' $ thinkjs adapter user'); console.log(); console.log(chalk.gray(' # create a adapter with the name ' + chalk.gray.underline.bold('base') + ' type ' + chalk.gray.underline.bold('user'))); console.log(' $ thinkjs adapter user/base'); console.log(); console.log(chalk.gray(' # create a adapter with the name ' + chalk.gray.underline.bold('user') + ' in multi-module mode')); console.log(' $ thinkjs adapter user home'); console.log(); }); program.parse(process.argv); if (program.args.length < 1) return program.help(); /** * 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 adapter = program.args[0].split('/'); const actionName = adapter[1] || 'base'; const context = Object.assign(thinkjsInfo.metadata, argv, { type: adapter[0], action: utils.getActionName(actionName), moduleName: program.args[1] || thinkjsInfo.metadata.defaultModule, actionPrefix: utils.getPrefix(actionName), ROOT_PATH: appPath, APP_NAME: thinkjsInfo.projectName }); const run = new Run({ template: thinkjsInfo.template, targetPath: appPath, options: { name: thinkjsInfo.metadata.name, command: 'adapter', maps: 'adapter', context }, done(err, files) { if (err) return logger.error(err); Object .keys(files) .forEach(file => { logger.success('Create: %s', path.normalize(file)); }); } }); run.start();