feat(cli): add create command

This commit is contained in:
陈嘉涵
2020-01-16 17:06:22 +08:00
parent 38001e59f0
commit 62a1b39b2b
31 changed files with 677 additions and 258 deletions

View File

@@ -0,0 +1,15 @@
import { GENERATOR_DIR, CWD } from '../common/constant';
import { VantCliGenerator } from '../compiler/vant-cli-generator';
export async function create() {
const generator = new VantCliGenerator([], {
env: {
cwd: CWD
},
resolved: GENERATOR_DIR
});
return new Promise(resolve => {
generator.run(resolve);
});
}

View File

@@ -33,6 +33,7 @@ export const CACHE_DIR = join(ROOT, 'node_modules/.cache');
// Relative paths
export const DIST_DIR = join(__dirname, '../../dist');
export const CONFIG_DIR = join(__dirname, '../config');
export const GENERATOR_DIR = join(__dirname, '../../generators');
// Dist files
export const PACKAGE_ENTRY_FILE = join(DIST_DIR, 'package-entry.js');

View File

@@ -0,0 +1,72 @@
import chalk from 'chalk';
import { join } from 'path';
import { consola, slimPath } from '../common/logger';
import { CWD, GENERATOR_DIR } from '../common/constant';
import Generator from 'yeoman-generator';
const TEMPLATES = join(GENERATOR_DIR, 'templates');
const PROMPTS = [
{
type: 'input',
name: 'name',
message: 'Your package name'
}
];
export class VantCliGenerator extends Generator {
inputs = {
name: ''
};
async prompting() {
return this.prompt(PROMPTS).then(inputs => {
this.inputs = inputs as any;
});
}
writing() {
consola.info(`Creating project in ${slimPath(CWD)}\n`);
const copy = (from: string, to?: string) => {
this.fs.copy(join(TEMPLATES, from), this.destinationPath(to || from));
};
const copyTpl = (name: string) => {
this.fs.copyTpl(
join(TEMPLATES, name),
this.destinationPath(name),
this.inputs
);
};
copy('.gitignore');
copy('.eslintignore');
copy('babel.config.js');
copy('src/**/*', 'src');
copy('docs/**/*', 'docs');
copyTpl('package.json');
copyTpl('vant.config.js');
}
install() {
console.log();
consola.info('Install dependencies...\n');
this.installDependencies({
npm: false,
bower: false,
yarn: true,
skipMessage: true
});
}
end() {
console.log();
consola.success(
`Successfully created project ${chalk.yellow(this.inputs.name)}.`
);
consola.success(
`Now you can run ${chalk.yellow('yarn dev')} to start development!`
);
}
}

View File

@@ -6,6 +6,7 @@ import { lint } from './commands/lint';
import { test } from './commands/jest';
import { clean } from './commands/clean';
import { build } from './commands/build';
import { create } from './commands/create';
import { release } from './commands/release';
import { changelog } from './commands/changelog';
import { buildSite } from './commands/build-site';
@@ -40,6 +41,10 @@ command('build')
.option('--watch', 'Watch file change')
.action(build);
command('create')
.description('Create a new project')
.action(create);
command('release')
.description('Compile components and release it')
.action(release);