mirror of
https://github.com/youzan/vant.git
synced 2025-10-20 10:44:59 +00:00
feat(cli): add create command
This commit is contained in:
15
packages/vant-cli/src/commands/create.ts
Normal file
15
packages/vant-cli/src/commands/create.ts
Normal 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);
|
||||
});
|
||||
}
|
@@ -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');
|
||||
|
72
packages/vant-cli/src/compiler/vant-cli-generator.ts
Normal file
72
packages/vant-cli/src/compiler/vant-cli-generator.ts
Normal 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!`
|
||||
);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user