-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·103 lines (91 loc) · 2.85 KB
/
index.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
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const util = require('./util');
const prettierRc = { singleQuote: true, trailingComma: 'es5' };
const eslintRc = {
extends: ['react-app', 'plugin:prettier/recommended', 'prettier/react'],
overrides: [
{
files: ['**.ts', '**.tsx'],
extends: [
// root 'extends' from above is inherited and applied to these files too.
// If order turns out to matter, these should probably go right after 'react-app'.
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
],
},
],
};
const dotEnv = `BROWSER=none
GENERATE_SOURCEMAP=false`;
async function main() {
const name = process.argv[2];
if (!name) {
console.error('You must provide an app name.');
process.exit(1);
}
try {
fs.accessSync(name);
console.error(`${name} already exists.`);
process.exit(1);
} catch (err) {
// empty
}
try {
await util.execSyncWithOutput(`yarn create react-app ${name} --typescript`);
console.log('\n');
process.chdir(name);
const filesInSrc = [
'index.css',
'App.css',
'logo.svg',
'serviceWorker.ts',
].map(file => path.join('src', file));
const filesToDelete = ['README.md', ...filesInSrc];
filesToDelete.forEach(file => {
try {
fs.unlinkSync(file);
} catch (err) {
console.error(`${file} suspiciously missing, can't delete`);
}
});
// CRA file modifications
const sedPatterns = [
{ file: 'src/App.tsx', pattern: '/css/d; /logo/d' },
{ file: 'src/index.tsx', pattern: '/css/d; /serviceWorker/d' },
];
for (let idx = 0; idx < sedPatterns.length; idx++) {
const { file, pattern } = sedPatterns[idx];
await util.runSed(file, pattern);
}
console.log('\n');
// Rename default scripts
await util.modifyJson('package.json', obj => {
obj.scripts['dev'] = obj.scripts['start'];
delete obj.scripts['start'];
delete obj.scripts['eject'];
});
// Write config files
fs.writeFileSync('.prettierrc', JSON.stringify(prettierRc, null, 2));
fs.writeFileSync(
'.eslintrc.js',
`module.exports = ${JSON.stringify(eslintRc, null, 2)}`
);
fs.writeFileSync('.env', dotEnv);
// Enable absolute imports
await util.modifyJson('tsconfig.json', obj => {
obj.compilerOptions.baseUrl = 'src';
});
} catch (err) {
console.error(err);
process.exit(1);
}
// "Gaeron's policy" (?): it's static output anyway so don't bother with peerDeps
await util.execSyncWithOutput(
'yarn add prettier eslint-config-prettier eslint-plugin-prettier'
);
}
main();
// If the typescript version is too new it prints a version when running through eslint, and this somehow
// prevents vim-ale from fixing (but not linting) files even though the exit code is 0.