create-dxcli
Version:
dxcli: create your own CLI
36 lines (35 loc) • 1.83 kB
JavaScript
"use strict";
// tslint:disable no-floating-promises
// tslint:disable no-console
const _ = require("lodash");
const path = require("path");
const Generator = require("yeoman-generator");
const yosay = require("yosay");
const { version } = require('../../../package.json');
class CommandGenerator extends Generator {
constructor(args, options) {
super(args, options);
this.options = options;
}
get _path() { return this.options.name.split(':').join('/'); }
get _ts() { return this.pjson.devDependencies.typescript; }
get _ext() { return this._ts ? 'ts' : 'js'; }
get _mocha() { return this.pjson.devDependencies.mocha; }
async prompting() {
this.pjson = this.fs.readJSON('package.json');
if (!this.pjson)
throw new Error('not in a project directory');
this.log(yosay(`Adding a command to ${this.pjson.name} Version: ${version}`));
}
writing() {
this.sourceRoot(path.join(__dirname, '../../../templates'));
this.fs.copyTpl(this.templatePath(`command.${this._ext}.ejs`), this.destinationPath(`src/commands/${this._path}.${this._ext}`), Object.assign({}, this.options, { _ }));
// this.fs.copyTpl(this.templatePath(`plugin/src/hooks/init.${this._ext}`), this.destinationPath(`src/hooks/init.${this._ext}`), this)
if (this._mocha) {
// this.fs.copyTpl(this.templatePath(`plugin/test/hooks/init.test.${this._ext}`), this.destinationPath(`test/hooks/init.test.${this._ext}`), this)
this.fs.copyTpl(this.templatePath(`command.test.${this._ext}.ejs`), this.destinationPath(`test/commands/${this._path}.test.${this._ext}`), Object.assign({}, this.options, { _ }));
}
// this.fs.writeJSON(this.destinationPath('./package.json'), this.pjson)
}
}
module.exports = CommandGenerator;