Skip to content

Commit

Permalink
fix(#49): 缩减日志 & 安装husky进行错误拦截
Browse files Browse the repository at this point in the history
fix: 缩减日志 & 安装husky进行错误拦截
  • Loading branch information
GGwujun committed May 28, 2021
2 parents 4f57522 + d1d5f04 commit fea92a2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 49 deletions.
10 changes: 6 additions & 4 deletions packages/cli-plugin-lint/src/add-prettier-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,19 @@ const createPrettierrc = (prettier: string, dir: string = process.cwd()) => {

const configPrettierRC = async (pmTool?: string) => {
try {
Logger.info(chalk.green("安装prettier相关依赖"));
for (const dep in prettierDeps) {
installOraInstance.start(dep + "@" + prettierDeps[dep]);
installOraInstance.start("安装 prettier 依赖\n" + dep + "@" + prettierDeps[dep]);
await installSaveDev(dep, prettierDeps[dep], pmTool);
installOraInstance.succeed(dep + "@" + prettierDeps[dep]);
installOraInstance.clear();
}

installOraInstance.succeed("安装 prettier 依赖");

// const answer = await prettierQuestions();
// if (answer) {
// const { run } = answer;
const run = "prettier"; // run prettier By the Prettier CLI/plugin default
Logger.info(chalk.green("Adding prettier config."));
// Logger.debug(chalk.green("Adding prettier config."));
const file = getFilenameForDirectory(process.cwd());
const eslint = loadConfigFile({ filePath: file }) || {
extends: [],
Expand Down
49 changes: 34 additions & 15 deletions packages/cli-plugin-lint/src/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { installSaveDev } from "./utils/npm-utils";
import { winfeCiDeps, huskyCiDeps, lintstagedCiDeps } from "./config";
import { huskyCiDeps, lintstagedCiDeps } from "./config";
import fs from "fs";
import spawn from "cross-spawn";
import { resolve } from "path";
Expand Down Expand Up @@ -78,22 +78,32 @@ function getConfigPackage() {
async function initHusky(pmTool: string) {
const HUSKY_CONFIG_PATH = resolve(process.cwd(), "./.husky");
const PRE_COMMIT_PATH = `${HUSKY_CONFIG_PATH}/pre-commit`;

// install huksy
for (const dep in huskyCiDeps) {
installOraInstance.start(dep + "@" + huskyCiDeps[dep]);
installOraInstance.start(
"安装 husky 依赖\n" + dep + "@" + huskyCiDeps[dep]
);
await installSaveDev(dep, huskyCiDeps[dep], pmTool);
installOraInstance.succeed(dep + "@" + huskyCiDeps[dep]);
installOraInstance.clear();
}

installOraInstance.succeed("安装 husky 依赖");

fs.rmdirSync(HUSKY_CONFIG_PATH, { recursive: true });

//install husky & init husky
spawn.sync("npx", ["husky", "install"], {
stdio: "inherit",
let spawnHinstall = spawn.sync("npx", ["husky", "install"], {
stdio: ["inherit", "inherit", "pipe"],
encoding: "utf-8",
});

if (spawnHinstall.stderr) {
throw new Error(`husky 初始化失败 \n ${spawnHinstall.stderr}`);
}

// add pre-commit
spawn.sync(
let spawnHadd = spawn.sync(
"npx",
[
"husky",
Expand All @@ -102,9 +112,13 @@ async function initHusky(pmTool: string) {
`npx lint-staged --config ./.lintstagedrc.js`,
],
{
stdio: "inherit",
stdio: ["inherit", "inherit", "pipe"],
encoding: "utf-8",
}
);
if (spawnHadd.stderr) {
throw new Error(`husky add pre-commit hook failed \n ${spawnHadd.stderr}`);
}

Logger.info(chalk.yellow(`\n👏 husky 配置完成, please check for sure. \n`));
}
Expand All @@ -116,14 +130,14 @@ async function initLintstaged(
) {
// install huksy
for (const dep in lintstagedCiDeps) {
installOraInstance.start(dep + "@" + huskyCiDeps[dep]);
await installSaveDev(dep, huskyCiDeps[dep], pmTool);
installOraInstance.succeed(dep + "@" + huskyCiDeps[dep]);
installOraInstance.start(dep + "@" + lintstagedCiDeps[dep]);
await installSaveDev(dep, lintstagedCiDeps[dep], pmTool);
installOraInstance.clear();
}

//init lint-staged config
const suffix = ["js"];
projectType === "vue" && suffix.push("vue");
projectType === "browser" && suffix.push("vue");
supportTypeScript && suffix.push("ts");
const lintScript = suffix.length > 1 ? `{${suffix.join(",")}}` : suffix[0];
const eslintPath = `**/*.${lintScript}`;
Expand All @@ -134,7 +148,7 @@ async function initLintstaged(
...oldStagedConfig,
[eslintPath]: [
"prettier -c --write --config ./.prettierrc.js",
"eslint --config ./.eslintrc.js --fix",
"eslint --config ./.eslintrc.js --fix",
],
};

Expand Down Expand Up @@ -168,15 +182,20 @@ export default async function interEslintToCI(
supportTypeScript: boolean,
pmTool: string = "yarn"
) {
Logger.info(chalk.green("开始安装 git hook 配置..."));
Logger.info(chalk.green("配置 git hook"));
if (hookEngine === HookEngine["winfe"]) {
Logger.info(
chalk.yellow(
`\n winfe hook has not yet been implemented ,please check for sure. \n`
)
);
} else {
await initHusky(pmTool);
await initLintstaged(projectType, supportTypeScript, pmTool);
try {
await initHusky(pmTool);
await initLintstaged(projectType, supportTypeScript, pmTool);
} catch (error) {
Logger.error(chalk.red(`${error}`))
process.exit(0);
}
}
}
42 changes: 29 additions & 13 deletions packages/cli-plugin-lint/src/deps-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,62 @@ import ora from "ora";
const installOraInstance = ora("install");

export const installDeps = async (options: PluginOptions) => {
Logger.info(chalk.green("正在安装 eslint 相关依赖 ..."));
try {
for (const dep in commonDeps) {
installOraInstance.start(dep + "@" + commonDeps[dep]);
installOraInstance.start(
"安装 eslint 依赖\n" + dep + "@" + commonDeps[dep]
);
await installSaveDev(dep, commonDeps[dep]);
installOraInstance.succeed(dep + "@" + commonDeps[dep]);
installOraInstance.clear();
}

Logger.info(chalk.green("正在安装配置集 eslint..."));
installOraInstance.succeed("安装 eslint 依赖");

// 安装默认的es6配置集
for (const dep in configDeps.default) {
installOraInstance.start(dep + "@" + configDeps.default[dep]);
installOraInstance.start(
"安装 eslint es6 配置集\n" + dep + "@" + configDeps.default[dep]
);
await installSaveDev(dep, configDeps.default[dep]);
installOraInstance.succeed(dep + "@" + configDeps.default[dep]);
installOraInstance.clear();
}

// 安装weining官方统一eslint配置集
for (const dep in DeafultSharedEslintConfig) {
installOraInstance.start(dep + "@" + DeafultSharedEslintConfig[dep]);
installOraInstance.start(
"安装 eslint @winfe 配置集\n" +
dep +
"@" +
DeafultSharedEslintConfig[dep]
);
await installSaveDev(dep, DeafultSharedEslintConfig[dep]);
installOraInstance.succeed(dep + "@" + DeafultSharedEslintConfig[dep]);
installOraInstance.clear();
}

// 安装项目特有的配置集
const proPlugin = pluginDeps[options.env === "node" ? "node" : "vue"];
for (const dep in proPlugin) {
installOraInstance.start(dep + "@" + proPlugin[dep]);
installOraInstance.start(
"安装 eslint env 配置集\n" + dep + "@" + proPlugin[dep]
);
await installSaveDev(dep, proPlugin[dep]);
installOraInstance.succeed(dep + "@" + proPlugin[dep]);
installOraInstance.clear();
}

// 安装ts相关的配置集
if (options.typescript) {
for (const dep in tsDeps) {
installOraInstance.start(dep + "@" + tsDeps[dep]);
installOraInstance.start(
"安装 eslint ts 配置集\n" + dep + "@" + tsDeps[dep]
);
await installSaveDev(dep, tsDeps[dep]);
installOraInstance.succeed(dep + "@" + tsDeps[dep]);
installOraInstance.clear();
}
}

installOraInstance.succeed("安装 eslint 配置集");
} catch (error) {
Logger.error(chalk.green(`正在安装相关依赖失败,${error}`));
Logger.error(chalk.red(`${error}`));
process.exit(0);
}
};
9 changes: 4 additions & 5 deletions packages/cli-plugin-lint/src/install-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ async function configEslintRC(projectType: string, supportTypeScript: boolean) {
});

if (answer.eslint) {
Logger.info(chalk.green("合并配置,更新 .eslintrc.js 配置文件..."));
Logger.info(chalk.green("合并配置,更新 .eslintrc.js 配置"));

let newFileJSON = {};
for (const oldFile of checkResult) {
Expand Down Expand Up @@ -144,19 +144,18 @@ async function configEslintRC(projectType: string, supportTypeScript: boolean) {
// process.exit(0);
// }
} else {
Logger.info(chalk.red("放弃升级eslint配置,请手动进行eslint配置"));
Logger.info(chalk.red("放弃升级eslint配置,请手动进行eslint配置."));
}
} else {
// 不存在 eslint 配置文件, copy 模板到新建 eslintrc.js 文件
Logger.info(chalk.green("检测到该项目尚无 eslintrc.js 配置文件"));
Logger.info(chalk.green("复制标准 eslintrc.js 配置模板到项目空间..."));
Logger.info(chalk.green("项目尚无 eslintrc.js 配置文件,执行标准 eslintrc.js 配置."));
fs.writeFileSync(
`${process.cwd()}/.eslintrc.js`,
eslintConfigContent,
"utf-8"
);
Logger.info(
chalk.yellow(`\n👏 eslint配置更新完成, please check for sure. \n`)
chalk.yellow(`\n👏 eslint配置更新完成, please check for sure.\n`)
);
}
}
Expand Down
27 changes: 15 additions & 12 deletions packages/cli-plugin-lint/src/utils/config-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
* @description Helper to locate and load configuration files.
*/

import { Logger } from "../logger";
// import { Logger } from "../logger";
import fs from "fs";
import path from "path";
import stripComments from "strip-json-comments";
//@ts-ignore
import stringify from "json-stable-stringify-without-jsonify";
import importFresh from "import-fresh";


// todo Logger.debug目前没实现通过参数控制,后续实现

export type configName =
| ".eslintrc.yaml"
| ".eslintrc.yml"
Expand Down Expand Up @@ -50,15 +53,15 @@ function readFile(filePath: string) {
* Loads a YAML configuration from a file.
*/
function loadYAMLConfigFile(filePath: string) {
Logger.debug(`Loading YAML config file: ${filePath}`);
// Logger.debug(`Loading YAML config file: ${filePath}`);
// lazy load YAML to improve performance when not used
const yaml = require("js-yaml");

try {
// empty YAML file can be null, so always use
return yaml.safeLoad(readFile(filePath)) || {};
} catch (e) {
Logger.debug(`Error reading YAML file: ${filePath}`);
// Logger.debug(`Error reading YAML file: ${filePath}`);
e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
throw e;
}
Expand All @@ -68,12 +71,12 @@ function loadYAMLConfigFile(filePath: string) {
* Loads a JSON configuration from a file.
*/
function loadJSONConfigFile(filePath: string) {
Logger.debug(`Loading JSON config file: ${filePath}`);
// Logger.debug(`Loading JSON config file: ${filePath}`);

try {
return JSON.parse(stripComments(readFile(filePath)));
} catch (e) {
Logger.debug(`Error reading JSON file: ${filePath}`);
// Logger.debug(`Error reading JSON file: ${filePath}`);
e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
e.messageTemplate = "failed-to-read-json";
e.messageData = {
Expand All @@ -88,7 +91,7 @@ function loadJSONConfigFile(filePath: string) {
* Loads a legacy (.eslintrc) configuration from a file.
*/
function loadLegacyConfigFile(filePath: string) {
Logger.debug(`Loading config file: ${filePath}`);
// Logger.debug(`Loading config file: ${filePath}`);

// lazy load YAML to improve performance when not used
const yaml = require("js-yaml");
Expand All @@ -99,7 +102,7 @@ function loadLegacyConfigFile(filePath: string) {
/* istanbul ignore next */ {}
);
} catch (e) {
Logger.debug(`Error reading YAML file: ${filePath}`);
// Logger.debug(`Error reading YAML file: ${filePath}`);
e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
throw e;
}
Expand All @@ -109,11 +112,11 @@ function loadLegacyConfigFile(filePath: string) {
* Loads a JavaScript configuration from a file.
*/
function loadJSConfigFile(filePath: string) {
Logger.debug(`Loading JS config file: ${filePath}`);
// Logger.debug(`Loading JS config file: ${filePath}`);
try {
return importFresh(filePath);
} catch (e) {
Logger.debug(`Error reading JavaScript file: ${filePath}`);
// Logger.debug(`Error reading JavaScript file: ${filePath}`);
e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
throw e;
}
Expand All @@ -123,11 +126,11 @@ function loadJSConfigFile(filePath: string) {
* Loads a configuration from a package.json file.
*/
function loadPackageJSONConfigFile(filePath: any) {
Logger.debug(`Loading package.json config file: ${filePath}`);
// Logger.debug(`Loading package.json config file: ${filePath}`);
try {
return loadJSONConfigFile(filePath).eslintConfig || null;
} catch (e) {
Logger.debug(`Error reading package.json file: ${filePath}`);
// Logger.debug(`Error reading package.json file: ${filePath}`);
e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
throw e;
}
Expand Down Expand Up @@ -200,7 +203,7 @@ export function loadConfigFile(file: {
* Writes a configuration file in JavaScript format.
*/
function writeJSConfigFile(config: object, filePath: any) {
Logger.debug(`Writing JS config file: ${filePath}`);
// Logger.debug(`Writing JS config file: ${filePath}`);

const stringifiedContent = `module.exports = ${stringify(config, {
cmp: sortByKey,
Expand Down

0 comments on commit fea92a2

Please sign in to comment.