Skip to content

Commit

Permalink
feat: vite中安装eslint插件时基于配置推荐提示
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiden-FE committed May 10, 2024
1 parent 574e50a commit 8bf166c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/adm-zip": "^0.5.5",
"@types/figlet": "^1.5.8",
"@types/inquirer": "^9.0.7",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.11.27",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
Expand All @@ -75,13 +76,17 @@
"@compass-aiden/helpers": "^0.2.0",
"@compass-aiden/telegram": "^2.3.1",
"@inquirer/prompts": "^4.3.0",
"acorn": "^8.11.3",
"acorn-walk": "^8.3.2",
"adm-zip": "^0.5.12",
"astring": "^1.8.6",
"axios": "^1.6.7",
"chalk": "^5.3.0",
"commander": "^12.0.0",
"figlet": "^1.7.0",
"handlebars": "^4.7.8",
"inquirer": "^9.2.16",
"lodash-es": "^4.17.21",
"ora": "^8.0.1",
"yaml": "^2.4.1"
}
Expand Down
36 changes: 35 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default () => {
.usage('<command> [option]')
.on('--help', () => {
// eslint-disable-next-line no-console
console.log(`\r\n${figlet.textSync('Compass CLI')}`);
console.log(`\r\n${chalk.cyan(figlet.textSync('Compass CLI'))}`);
// 新增说明信息
// eslint-disable-next-line no-console
console.log(`\r\nRun ${chalk.cyan(`compass <command> --help`)} show details\r\n`);
Expand Down
38 changes: 38 additions & 0 deletions src/utils/get-ast-tree-of-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import * as acorn from 'acorn';

/**
* 获取文件的ast语法树
* @param filePath 文件路径
* @param options 配置项
* @returns {acorn.Program}
*/
export default function getASTTreeOfFile(
filePath: string,
options?: {
cwd?: string;
} & Partial<acorn.Options>,
): acorn.Program {
const { cwd, encoding } = {
encoding: 'utf8' as const,
cwd: process.cwd(),
...options,
};
const target = typeof cwd !== 'undefined' ? resolve(cwd, filePath) : filePath;
const code = readFileSync(target, { encoding });
const comments: acorn.Comment[] = [];
const tokens: acorn.Token[] = [];
const ast = acorn.parse(code, {
ecmaVersion: options?.ecmaVersion || 7,
onComment: comments,
onToken: tokens,
locations: true,
...options,
});
// @ts-expect-error -- expected
ast.comments = comments;
// @ts-expect-error -- expected
ast.tokens = tokens;
return ast;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export { default as getLibraryVersionFromNpmRegisty } from './get-library-versio
export { default as downloadRepoFromGithub } from './download-repo-from-github';
export { default as getFilePathsInFolder } from './get-file-paths-in-folder';
export { default as deleteFoldersSync } from './delete-folder-sync';
export { default as getASTTreeOfFile } from './get-ast-tree-of-file';
export * from './delete-files-sync';
27 changes: 27 additions & 0 deletions src/utils/plugins/eslint.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,40 @@ export async function addEslintPlugin(options?: { pkgManager?: PkgManager; cwd?:
eslintFileData.extends.push(answer);
writeFileSync(eslintFilePath, JSON.stringify(eslintFileData, null, 2), 'utf-8');
}

// Vite 相关配置
let hasViteConfig = false;
if (
isFileOrFolderExists(join(cwd || process.cwd(), 'vite.config.ts')) ||
isFileOrFolderExists(join(cwd || process.cwd(), 'vite.config.js'))
) {
hasViteConfig = true;
}

// 在package.json内写入lint命令
execSync(`npm pkg set scripts.lint="${pluginConfig.lint}"`, execOption);
execSync(
`${pkgManager} add -D @compass-aiden/eslint-config eslint eslint-plugin-import ${pluginConfig.deps.join(' ')}`,
execOption,
);

if (hasViteConfig) {
Logger.info(`在Vite项目中推荐使用vite-plugin-checker插件,示例如下:
import checker from 'vite-plugin-checker';
export default defineConfig({
plugins: [
// ......其他插件
checker({
eslint: {
lintCommand: 'eslint "./src/**/*.{ts,tsx,vue,js,jsx,cjs,mjs}" --fix',
},
}),
],
});
`);
}
Logger.success(`${isCreateFile ? 'Created' : 'Updated'} file at ${eslintFilePath}`);
Logger.success(
`Installed @compass-aiden/eslint-config eslint eslint-plugin-import ${pluginConfig.deps.join(' ')} at ${cwd || process.cwd()}`,
Expand Down

0 comments on commit 8bf166c

Please sign in to comment.