mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-05 03:05:07 +08:00
feat: 改为使用create-fes-app创建项目
This commit is contained in:
parent
a388c64889
commit
342670c878
@ -14,7 +14,8 @@ const headPkgs = [
|
||||
"fes-plugin-model",
|
||||
"fes-plugin-layout",
|
||||
"fes-plugin-icon",
|
||||
"fes-plugin-locale"
|
||||
"fes-plugin-locale",
|
||||
"create-fes-app"
|
||||
];
|
||||
const tailPkgs = [];
|
||||
// const otherPkgs = readdirSync(join(__dirname, 'packages')).filter(
|
||||
|
1
packages/create-fes-app/.local
Normal file
1
packages/create-fes-app/.local
Normal file
@ -0,0 +1 @@
|
||||
Used in bin/create-fes-app.js to determine if it is in the local debug state.
|
21
packages/create-fes-app/LICENSE
Normal file
21
packages/create-fes-app/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020-present webank
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
3
packages/create-fes-app/bin/create-fes-app.js
Executable file
3
packages/create-fes-app/bin/create-fes-app.js
Executable file
@ -0,0 +1,3 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('../lib/cli');
|
37
packages/create-fes-app/package.json
Normal file
37
packages/create-fes-app/package.json
Normal file
@ -0,0 +1,37 @@
|
||||
{
|
||||
"name": "@webank/create-fes-app",
|
||||
"version": "2.0.0-alpha.0",
|
||||
"description": "create a app base on fes.js",
|
||||
"main": "lib/index.js",
|
||||
"files": [
|
||||
"lib",
|
||||
"bin",
|
||||
"templates"
|
||||
],
|
||||
"bin": {
|
||||
"create-fes-app": "bin/create-fes-app.js"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/WeBankFinTech/fes.js.git",
|
||||
"directory": "packages/create-fes-app"
|
||||
},
|
||||
"keywords": [
|
||||
"fes"
|
||||
],
|
||||
"sideEffects": false,
|
||||
"author": "qlin",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/WeBankFinTech/fes.js/issues"
|
||||
},
|
||||
"homepage": "https://github.com/WeBankFinTech/fes.js#readme",
|
||||
"dependencies": {
|
||||
"@umijs/utils": "3.3.3",
|
||||
"inquirer": "^7.3.3",
|
||||
"tar": "^6.1.0",
|
||||
"validate-npm-package-name": "^3.0.0",
|
||||
"fs-extra": "^9.0.1",
|
||||
"readline": "^1.3.0"
|
||||
}
|
||||
}
|
49
packages/create-fes-app/src/cli.js
Normal file
49
packages/create-fes-app/src/cli.js
Normal file
@ -0,0 +1,49 @@
|
||||
import { chalk, yParser } from '@umijs/utils';
|
||||
import { existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
|
||||
const args = yParser(process.argv.slice(2), {
|
||||
alias: {
|
||||
version: ['v'],
|
||||
help: ['h'],
|
||||
force: ['f'],
|
||||
merge: ['m'],
|
||||
proxy: ['x']
|
||||
},
|
||||
boolean: ['version', 'help', 'merge', 'force']
|
||||
});
|
||||
|
||||
if (args._.length > 1) {
|
||||
console.log(chalk.yellow('\n Info: You provided more than one argument. The first one will be used as the app\'s name, the rest are ignored.'));
|
||||
}
|
||||
|
||||
if (args.version && !args._[0]) {
|
||||
args._[0] = 'version';
|
||||
const local = existsSync(join(__dirname, '../.local'))
|
||||
? chalk.cyan('@local')
|
||||
: '';
|
||||
const { name, version } = require('../package.json');
|
||||
console.log(`${name}@${version}${local}`);
|
||||
} else if (args.help && !args._[0]) {
|
||||
console.log(`
|
||||
Usage: create-fes-app <name>
|
||||
|
||||
Options:
|
||||
-v, --version Output the current version
|
||||
-h, --help Display help for command
|
||||
-f, --force Overwrite target directory if it exists
|
||||
-m, --merge Merge target directory if it exists
|
||||
-x, --proxy <proxyUrl> Use specified proxy when creating project
|
||||
`);
|
||||
} else {
|
||||
require('.')
|
||||
.default({
|
||||
cwd: process.cwd(),
|
||||
args
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(`Create failed, ${err.message}`);
|
||||
console.error(err);
|
||||
});
|
||||
}
|
106
packages/create-fes-app/src/index.js
Normal file
106
packages/create-fes-app/src/index.js
Normal file
@ -0,0 +1,106 @@
|
||||
import path from 'path';
|
||||
import { chalk } from '@umijs/utils';
|
||||
import validateProjectName from 'validate-npm-package-name';
|
||||
import fs from 'fs-extra';
|
||||
import { execSync } from 'child_process';
|
||||
import inquirer from 'inquirer';
|
||||
import tar from 'tar';
|
||||
import { clearConsole } from './utils';
|
||||
|
||||
export default async ({ cwd, args }) => {
|
||||
if (args.proxy) {
|
||||
process.env.HTTP_PROXY = args.proxy;
|
||||
}
|
||||
const projectName = args._[0];
|
||||
const inCurrent = projectName === '.';
|
||||
const name = inCurrent ? path.relative('../', cwd) : projectName;
|
||||
const targetDir = path.resolve(cwd, projectName || '.');
|
||||
|
||||
const result = validateProjectName(name);
|
||||
if (!result.validForNewPackages) {
|
||||
console.error(chalk.red(`Invalid project name: "${name}"`));
|
||||
result.errors && result.errors.forEach((err) => {
|
||||
console.error(chalk.red.dim(`Error: ${err}`));
|
||||
});
|
||||
result.warnings && result.warnings.forEach((warn) => {
|
||||
console.error(chalk.red.dim(`Warning: ${warn}`));
|
||||
});
|
||||
throw new Error('Process exited');
|
||||
}
|
||||
if (fs.existsSync(targetDir) && !args.merge) {
|
||||
if (args.force) {
|
||||
await fs.remove(targetDir);
|
||||
} else if (inCurrent) {
|
||||
clearConsole();
|
||||
const { ok } = await inquirer.prompt([
|
||||
{
|
||||
name: 'ok',
|
||||
type: 'confirm',
|
||||
message: 'Generate project in current directory?'
|
||||
}
|
||||
]);
|
||||
if (!ok) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
clearConsole();
|
||||
const { action } = await inquirer.prompt([
|
||||
{
|
||||
name: 'action',
|
||||
type: 'list',
|
||||
message: `Target directory ${chalk.cyan(targetDir)} already exists. Pick an action:`,
|
||||
choices: [
|
||||
{ name: 'Overwrite', value: 'overwrite' },
|
||||
{ name: 'Merge', value: 'merge' },
|
||||
{ name: 'Cancel', value: false }
|
||||
]
|
||||
}
|
||||
]);
|
||||
if (!action) {
|
||||
return null;
|
||||
}
|
||||
if (action === 'overwrite') {
|
||||
console.log(`\nRemoving ${chalk.cyan(targetDir)}...`);
|
||||
await fs.remove(targetDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
clearConsole();
|
||||
const { template } = await inquirer.prompt([
|
||||
{
|
||||
name: 'template',
|
||||
type: 'list',
|
||||
message: 'Pick an template:',
|
||||
choices: [
|
||||
{ name: 'PC, suitable for management desk front-end applications', value: 'pc' },
|
||||
{ name: 'H5, suitable for mobile applications', value: 'h5' },
|
||||
{ name: 'Cancel', value: false }
|
||||
]
|
||||
}
|
||||
]);
|
||||
|
||||
if (template) {
|
||||
const map = {
|
||||
pc: '@webank/fes-template',
|
||||
h5: '@webank/fes-template-h5'
|
||||
};
|
||||
fs.mkdirSync(targetDir);
|
||||
const stdout = execSync(`npm pack ${map[template]}`, { encoding: 'utf8', stdio: [null] });
|
||||
const tempFilePath = path.resolve(cwd, stdout.replace('\n', ''));
|
||||
fs.createReadStream(tempFilePath).pipe(
|
||||
tar.x({
|
||||
strip: 1,
|
||||
C: targetDir
|
||||
})
|
||||
).on('finish', () => {
|
||||
fs.removeSync(tempFilePath);
|
||||
console.log();
|
||||
console.log(chalk.green(`project ${projectName} created successfully, please execute the following command to use:`));
|
||||
console.log(`$ cd ${projectName}`);
|
||||
console.log('$ yarn');
|
||||
console.log('$ yarn dev');
|
||||
console.log();
|
||||
});
|
||||
}
|
||||
};
|
13
packages/create-fes-app/src/utils.js
Normal file
13
packages/create-fes-app/src/utils.js
Normal file
@ -0,0 +1,13 @@
|
||||
import readline from 'readline';
|
||||
|
||||
export const clearConsole = (title) => {
|
||||
if (process.stdout.isTTY) {
|
||||
const blank = '\n'.repeat(process.stdout.rows);
|
||||
console.log(blank);
|
||||
readline.cursorTo(process.stdout, 0, 0);
|
||||
readline.clearScreenDown(process.stdout);
|
||||
if (title) {
|
||||
console.log(title);
|
||||
}
|
||||
}
|
||||
};
|
@ -33,12 +33,8 @@
|
||||
"@vue/preload-webpack-plugin": "1.1.2",
|
||||
"@webank/fes-compiler": "^2.0.0-alpha.0",
|
||||
"cliui": "6.0.0",
|
||||
"fs-extra": "^9.0.1",
|
||||
"html-webpack-plugin": "^3.2.0",
|
||||
"html-webpack-tags-plugin": "2.0.17",
|
||||
"inquirer": "^7.3.3",
|
||||
"tar": "^6.1.0",
|
||||
"validate-npm-package-name": "^3.0.0",
|
||||
"vue-loader": "^16.1.2",
|
||||
"webpack-bundle-analyzer": "4.3.0"
|
||||
}
|
||||
|
@ -50,7 +50,6 @@ export default function () {
|
||||
require.resolve('./plugins/misc/route'),
|
||||
|
||||
// commands
|
||||
require.resolve('./plugins/commands/create'),
|
||||
require.resolve('./plugins/commands/build'),
|
||||
require.resolve('./plugins/commands/dev')
|
||||
]
|
||||
|
@ -1,115 +0,0 @@
|
||||
import path from 'path';
|
||||
import { chalk } from '@umijs/utils';
|
||||
import validateProjectName from 'validate-npm-package-name';
|
||||
import fs from 'fs-extra';
|
||||
import { execSync } from 'child_process';
|
||||
import { Logger } from '@webank/fes-compiler';
|
||||
import inquirer from 'inquirer';
|
||||
import tar from 'tar';
|
||||
|
||||
const logger = new Logger('fes:plugin-built-in');
|
||||
|
||||
export default function (api) {
|
||||
api.registerCommand({
|
||||
name: 'create',
|
||||
description: 'create a new project',
|
||||
async fn({ args }) {
|
||||
if (args.proxy) {
|
||||
process.env.HTTP_PROXY = args.proxy;
|
||||
}
|
||||
const cwd = args.cwd || process.cwd();
|
||||
const projectName = args._[0];
|
||||
const inCurrent = projectName === '.';
|
||||
const name = inCurrent ? path.relative('../', cwd) : projectName;
|
||||
const targetDir = path.resolve(cwd, projectName || '.');
|
||||
|
||||
const result = validateProjectName(name);
|
||||
if (!result.validForNewPackages) {
|
||||
console.error(chalk.red(`Invalid project name: "${name}"`));
|
||||
result.errors && result.errors.forEach((err) => {
|
||||
console.error(chalk.red.dim(`Error: ${err}`));
|
||||
});
|
||||
result.warnings && result.warnings.forEach((warn) => {
|
||||
console.error(chalk.red.dim(`Warning: ${warn}`));
|
||||
});
|
||||
throw new Error('Process exited');
|
||||
}
|
||||
if (fs.existsSync(targetDir) && !args.merge) {
|
||||
if (args.force) {
|
||||
await fs.remove(targetDir);
|
||||
} else {
|
||||
logger.clearConsole();
|
||||
if (inCurrent) {
|
||||
const { ok } = await inquirer.prompt([
|
||||
{
|
||||
name: 'ok',
|
||||
type: 'confirm',
|
||||
message: 'Generate project in current directory?'
|
||||
}
|
||||
]);
|
||||
if (!ok) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
const { action } = await inquirer.prompt([
|
||||
{
|
||||
name: 'action',
|
||||
type: 'list',
|
||||
message: `Target directory ${chalk.cyan(targetDir)} already exists. Pick an action:`,
|
||||
choices: [
|
||||
{ name: 'Overwrite', value: 'overwrite' },
|
||||
{ name: 'Merge', value: 'merge' },
|
||||
{ name: 'Cancel', value: false }
|
||||
]
|
||||
}
|
||||
]);
|
||||
if (!action) {
|
||||
return null;
|
||||
}
|
||||
if (action === 'overwrite') {
|
||||
console.log(`\nRemoving ${chalk.cyan(targetDir)}...`);
|
||||
await fs.remove(targetDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const { template } = await inquirer.prompt([
|
||||
{
|
||||
name: 'template',
|
||||
type: 'list',
|
||||
message: 'Pick an template:',
|
||||
choices: [
|
||||
{ name: 'PC, suitable for management desk front-end applications', value: 'pc' },
|
||||
{ name: 'H5, suitable for mobile applications', value: 'h5' },
|
||||
{ name: 'Cancel', value: false }
|
||||
]
|
||||
}
|
||||
]);
|
||||
|
||||
if (template) {
|
||||
const map = {
|
||||
pc: '@webank/fes-template',
|
||||
h5: '@webank/fes-template-h5'
|
||||
};
|
||||
fs.mkdirSync(targetDir);
|
||||
const stdout = execSync(`npm pack ${map[template]}`, { encoding: 'utf8', stdio: [null] });
|
||||
const tempFilePath = path.resolve(cwd, stdout.replace('\n', ''));
|
||||
fs.createReadStream(tempFilePath).pipe(
|
||||
tar.x({
|
||||
strip: 1,
|
||||
C: targetDir
|
||||
})
|
||||
).on('finish', () => {
|
||||
fs.removeSync(tempFilePath);
|
||||
console.log();
|
||||
console.log(chalk.green(`project ${projectName} created successfully, please execute the following command to use:`));
|
||||
console.log(`$ cd ${projectName}`);
|
||||
console.log('$ yarn');
|
||||
console.log('$ yarn dev');
|
||||
console.log();
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
@ -49,31 +49,6 @@ program
|
||||
.usage('<command> [options]')
|
||||
.description(fesPkg.description);
|
||||
|
||||
program
|
||||
.command('create <app-name>')
|
||||
.description('create a new project powered by fes.js')
|
||||
.option('-f, --force', 'Overwrite target directory if it exists')
|
||||
.option('--merge', 'Merge target directory if it exists')
|
||||
.option('-x, --proxy <proxyUrl>', 'Use specified proxy when creating project')
|
||||
.action(async () => {
|
||||
if (args._.length > 2) {
|
||||
console.log(chalk.yellow('\n Info: You provided more than one argument. The first one will be used as the app\'s name, the rest are ignored.'));
|
||||
}
|
||||
try {
|
||||
await new Service({
|
||||
cwd: getCwd(),
|
||||
pkg: getPkg(process.cwd())
|
||||
}).run({
|
||||
name: 'create',
|
||||
args
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(chalk.red(e.message));
|
||||
console.error(e.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
program
|
||||
.command('dev')
|
||||
.description('run local http service for development')
|
||||
|
Loading…
x
Reference in New Issue
Block a user