eslint-prettier-config
Version:
Eslint+Airbnb+Preitter+TS+Vue JS代码规范自动格式化方案
80 lines (75 loc) • 2.34 kB
JavaScript
const commander = require('commander');
const chalk = require('chalk');
const spawn = require('cross-spawn');
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const pkg = require('../package.json');
const crossSpawn = require('../utils/crossSpawn.js');
const inquirer = require('inquirer') // 提供人机交互
const ora = require('ora') //等待提醒
// --init commander
const program = new commander.Command();
program
.option('-i, --init', 'init config files')
program.parse(process.argv);
if (program.init) {
installPackage()
} else {
console.log('not found commander');
process.exit(1);
}
//指定目录下安装npm 包
function installPackage() {
const spinner = ora(chalk.yellow(`Installing packages. This migth take a couple of minutes...`)).start();
const allDependencies = ['eslint', 'prettier','eslint-config-airbnb-base','eslint-config-prettier','eslint-loader',
'eslint-plugin-prettier', 'eslint-plugin-vue', '@typescript-eslint/eslint-plugin', '@typescript-eslint/parser',
]
console.log('');
console.log(`Install ${chalk.cyan(allDependencies.join(' '))}`)
new Promise((resolve, reject) => {
spinner.text = chalk.blueBright(`Installing packages...`)
let command = 'npm';
const args = ['install'];
[].push.apply(args, allDependencies);
let child = crossSpawn(command, args, { stido: 'inherit' });
child.on('close', code => {
console.log(code)
if(code !== 0){
reject({
command: `${command} ${args.join(' ')}`
});
return;
}
resolve();
})
}).then(() => {
spinner.stop();
console.log();
//在根目录下生成配置文件
copyFile()
}).catch(error => {
console.log(error)
})
}
// copyFile()
// 在根目录下生成 .editorConfig .eslintrc.js .prettierrc.js
function copyFile() {
const templatePath = path.resolve(__dirname, "..", 'template')
const targetPath = process.cwd();
//遍历文件夹下的文件
fs.readdir(templatePath, function (err, paths) {
if (err) {
throw err;
}
paths.forEach((pathItem) => {
fs.exists(pathItem, async (exists) => {
// if(!exists) {
fs.copyFileSync(path.resolve(templatePath, pathItem), path.resolve(targetPath, pathItem))
// }
})
})
console.log("Congratulations, it's done successfully!")
})
}
//package.json 配置eslint prettier命令