From 7bba040e7af33ccf2e711dc29256066be5af1045 Mon Sep 17 00:00:00 2001 From: ocean-gao Date: Wed, 27 Nov 2024 17:28:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20create-fes-app=20=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eslint.config.js | 1 + packages/create-fes-app/package.json | 28 ++++++++-------- packages/create-fes-app/src/cli.js | 18 +++++----- packages/create-fes-app/src/index.js | 32 ++++++++++-------- packages/create-fes-app/src/utils.js | 7 ++-- .../templates/plugin/scripts/build.js | 33 +++++++++++-------- .../templates/plugin/scripts/randomColor.js | 3 +- .../templates/plugin/types.d.ts | 11 +++---- 8 files changed, 73 insertions(+), 60 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index cf4a93c8..6066eb0c 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -32,5 +32,6 @@ export default await antfu({ multilineDetection: 'brackets', }, ], + 'no-console': 'off', }, }); diff --git a/packages/create-fes-app/package.json b/packages/create-fes-app/package.json index f7ad9a2c..df6c96fa 100644 --- a/packages/create-fes-app/package.json +++ b/packages/create-fes-app/package.json @@ -2,30 +2,30 @@ "name": "@fesjs/create-fes-app", "version": "3.0.4", "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" - }, + "author": "qlin", + "license": "MIT", + "homepage": "https://github.com/WeBankFinTech/fes.js#readme", "repository": { "type": "git", "url": "git+https://github.com/WeBankFinTech/fes.js.git", "directory": "packages/create-fes-app" }, + "bugs": { + "url": "https://github.com/WeBankFinTech/fes.js/issues" + }, "keywords": [ "fes" ], "sideEffects": false, - "author": "qlin", - "license": "MIT", - "bugs": { - "url": "https://github.com/WeBankFinTech/fes.js/issues" + "main": "lib/index.js", + "bin": { + "create-fes-app": "bin/create-fes-app.js" }, - "homepage": "https://github.com/WeBankFinTech/fes.js#readme", + "files": [ + "bin", + "lib", + "templates/**/*" + ], "publishConfig": { "access": "public" }, diff --git a/packages/create-fes-app/src/cli.js b/packages/create-fes-app/src/cli.js index 5835133a..699643f2 100644 --- a/packages/create-fes-app/src/cli.js +++ b/packages/create-fes-app/src/cli.js @@ -1,7 +1,7 @@ +import { existsSync } from 'node:fs'; +import { join } from 'node:path'; +import process from 'node:process'; import { chalk, yParser } from '@fesjs/utils'; -import { existsSync } from 'fs'; -import { join } from 'path'; - const args = yParser(process.argv.slice(2), { alias: { @@ -9,9 +9,9 @@ const args = yParser(process.argv.slice(2), { help: ['h'], force: ['f'], merge: ['m'], - proxy: ['x'] + proxy: ['x'], }, - boolean: ['version', 'help', 'merge', 'force'] + boolean: ['version', 'help', 'merge', 'force'], }); if (args._.length > 1) { @@ -25,7 +25,8 @@ if (args.version && !args._[0]) { : ''; const { name, version } = require('../package.json'); console.log(`${name}@${version}${local}`); -} else if (args.help && !args._[0]) { +} +else if (args.help && !args._[0]) { console.log(` Usage: create-fes-app @@ -36,11 +37,12 @@ Options: -m, --merge Merge target directory if it exists -x, --proxy Use specified proxy when creating project `); -} else { +} +else { require('.') .default({ cwd: process.cwd(), - args + args, }) .catch((err) => { console.error(`Create failed, ${err.message}`); diff --git a/packages/create-fes-app/src/index.js b/packages/create-fes-app/src/index.js index 17644640..caeb9bec 100644 --- a/packages/create-fes-app/src/index.js +++ b/packages/create-fes-app/src/index.js @@ -1,12 +1,13 @@ -import path from 'path'; +import path from 'node:path'; +import process from 'node:process'; import { chalk } from '@fesjs/utils'; -import validateProjectName from 'validate-npm-package-name'; import fs from 'fs-extra'; import inquirer from 'inquirer'; +import validateProjectName from 'validate-npm-package-name'; -import { clearConsole } from './utils'; import AppGenerator from './generator/App'; import PluginGenerator from './generator/Plugin'; +import { clearConsole } from './utils'; export default async ({ cwd, args }) => { if (args.proxy) { @@ -20,20 +21,21 @@ export default async ({ cwd, args }) => { 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}`)); - }); + 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.pathExistsSync(targetDir) && !args.merge) { if (args.force) { await fs.remove(targetDir); - } else if (inCurrent) { + } + else if (inCurrent) { clearConsole(); const { ok } = await inquirer.prompt([ { @@ -45,7 +47,8 @@ export default async ({ cwd, args }) => { if (!ok) { return null; } - } else { + } + else { clearConsole(); const { action } = await inquirer.prompt([ { @@ -98,7 +101,8 @@ export default async ({ cwd, args }) => { console.log('$ pnpm i'); console.log('$ pnpm dev'); console.log(); - } else if (template === 'plugin') { + } + else if (template === 'plugin') { const generator = new PluginGenerator({ cwd, args, diff --git a/packages/create-fes-app/src/utils.js b/packages/create-fes-app/src/utils.js index 00183a34..adac0ed7 100644 --- a/packages/create-fes-app/src/utils.js +++ b/packages/create-fes-app/src/utils.js @@ -1,6 +1,7 @@ -import readline from 'readline'; +import process from 'node:process'; +import readline from 'node:readline'; -export const clearConsole = (title) => { +export function clearConsole(title) { if (process.stdout.isTTY) { const blank = '\n'.repeat(process.stdout.rows); console.log(blank); @@ -10,4 +11,4 @@ export const clearConsole = (title) => { console.log(title); } } -}; +} diff --git a/packages/create-fes-app/templates/plugin/scripts/build.js b/packages/create-fes-app/templates/plugin/scripts/build.js index 98b34f29..5c11b0e1 100644 --- a/packages/create-fes-app/templates/plugin/scripts/build.js +++ b/packages/create-fes-app/templates/plugin/scripts/build.js @@ -1,17 +1,18 @@ // 关闭 import 规则 /* eslint import/no-extraneous-dependencies: 0 */ -const fs = require('fs'); -const fse = require('fs-extra'); -const path = require('path'); -const merge = require('deepmerge'); -const chokidar = require('chokidar'); +const fs = require('node:fs'); +const path = require('node:path'); +const process = require('node:process'); const chalk = require('chalk'); +const chokidar = require('chokidar'); +const merge = require('deepmerge'); +const fse = require('fs-extra'); const argv = require('yargs-parser')(process.argv.slice(2)); +const pkg = require('../package.json'); const compiler = require('./compiler'); const randomColor = require('./randomColor'); -const pkg = require('../package.json'); const ESM_OUTPUT_DIR = 'es'; const NODE_CJS_OUTPUT_DIR = 'lib'; @@ -76,10 +77,12 @@ function transformFile(filePath, outputPath, config, log) { const type = config.target === 'browser' ? ESM_OUTPUT_DIR : NODE_CJS_OUTPUT_DIR; log(`Transform to ${type} for ${config.target === 'browser' ? chalk.yellow(shortFilePath) : chalk.blue(shortFilePath)}`); fse.outputFileSync(outputPath, transformedCode); - } catch (error) { + } + catch (error) { console.error(error); } - } else { + } + else { fse.copySync(filePath, outputPath); } } @@ -92,10 +95,12 @@ function compilerPkg(codeDir, outputDir, config, log) { const fileStats = fs.lstatSync(filePath); if (config.copy.includes(file)) { fse.copySync(filePath, outputFilePath); - } else if (fileStats.isDirectory(filePath) && !/__tests__/.test(file)) { + } + else if (fileStats.isDirectory(filePath) && !/__tests__/.test(file)) { fse.ensureDirSync(outputFilePath); compilerPkg(filePath, outputFilePath, config, log); - } else if (fileStats.isFile(filePath)) { + } + else if (fileStats.isFile(filePath)) { transformFile(filePath, outputFilePath, config, log); } }); @@ -112,11 +117,13 @@ function watchFile(dir, outputDir, config, log) { const outputPath = changeFile.replace(dir, outputDir); const stat = fs.lstatSync(changeFile); log(`[${event}] ${shortChangeFile}`); - if (config.resolveCopy.some((item) => changeFile.startsWith(item))) { + if (config.resolveCopy.some(item => changeFile.startsWith(item))) { fse.copySync(changeFile, outputPath); - } else if (stat.isFile()) { + } + else if (stat.isFile()) { transformFile(changeFile, outputPath, config, log); - } else if (stat.isDirectory()) { + } + else if (stat.isDirectory()) { compilerPkg(changeFile, outputPath, config); } }); diff --git a/packages/create-fes-app/templates/plugin/scripts/randomColor.js b/packages/create-fes-app/templates/plugin/scripts/randomColor.js index bca8e2de..1b533950 100644 --- a/packages/create-fes-app/templates/plugin/scripts/randomColor.js +++ b/packages/create-fes-app/templates/plugin/scripts/randomColor.js @@ -27,7 +27,8 @@ module.exports = function (pkg) { cache[pkg] = str; if (index === colors.length - 1) { index = 0; - } else { + } + else { index += 1; } } diff --git a/packages/create-fes-app/templates/plugin/types.d.ts b/packages/create-fes-app/templates/plugin/types.d.ts index d05ac3db..82bb6613 100644 --- a/packages/create-fes-app/templates/plugin/types.d.ts +++ b/packages/create-fes-app/templates/plugin/types.d.ts @@ -1,10 +1,7 @@ import {} from '@fesjs/fes'; -declare module "@fesjs/fes" { - interface PluginBuildConfig { - - } - interface PluginRuntimeConfig { - - } +declare module '@fesjs/fes' { + interface PluginBuildConfig {} + + interface PluginRuntimeConfig {} }