-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·120 lines (105 loc) · 2.65 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env node
const fs = require('fs');
const { promisify } = require('util');
const { homedir } = require('os');
const { join } = require('path');
const inquirer = require('inquirer');
const prettier = require('prettier');
const merge = require('deepmerge');
const { defaults, questions } = require('./config');
const { installDev } = require('./install-dependencies');
const writeFile = promisify(fs.writeFile);
const configFileName = join(homedir(), '.prettiereslint.config.json');
const format = (filename, contents, prettierConfig) => {
const isJS = filename.endsWith('.js');
return prettier.format(
`${isJS ? 'module.exports = ' : ''}${JSON.stringify(contents)}`,
{
filepath: filename,
...prettierConfig,
fileWidth: 60, // force it not to be written on a single line
},
);
};
async function readConfigOrQuestion() {
if (fs.existsSync(configFileName)) {
console.log('Config file found. Using that');
const config = require(configFileName);
return config;
} else {
return inquirer.prompt(questions);
}
}
const createFile = async (filename, contents, prettierConfig) => {
try {
await writeFile(filename, format(filename, contents, prettierConfig));
console.log('\x1b[32m', '✔', '\x1b[0m', `Created ${filename}`);
} catch (error) {
console.log('\x1b[31m', '✘', '\x1b[0m', `Error creating ${filename}\n`);
console.log(error);
}
};
(async () => {
const {
prettier,
filenames,
saveGlobalConfig,
useReact,
eslint,
} = await readConfigOrQuestion();
const prettierConfig = {
...defaults.prettier,
...prettier,
};
const eslintConfig = merge.all([
defaults.eslint,
{
env: {
browser: eslint.env.includes('browser'),
node: eslint.env.includes('node'),
mocha: eslint.env.includes('mocha'),
jest: eslint.env.includes('jest'),
},
},
useReact
? {
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
extends: ['plugin:react/recommended'],
plugins: ['react'],
}
: {},
]);
const writeEslintConfig = createFile(
filenames.eslint,
eslintConfig,
prettierConfig,
);
const writePrettierConfig = createFile(
filenames.prettier,
prettierConfig,
prettierConfig,
);
const promises = [writeEslintConfig, writePrettierConfig];
if (saveGlobalConfig) {
promises.push(
createFile(configFileName, {
prettierOverrides: prettierConfig,
filenames,
}),
);
}
await Promise.all(promises);
console.log('Installing dependencies…');
const requiredDependencies = [
'eslint',
'prettier',
'eslint-config-prettier',
'eslint-plugin-prettier',
...(useReact ? ['eslint-plugin-react'] : []),
];
return await installDev(requiredDependencies);
})();