-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
128 lines (111 loc) · 3.05 KB
/
setup.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
121
122
123
124
125
126
127
128
/* eslint-env node */
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs/promises');
const path = require('path');
const readline = require('readline');
const resolveToFiles = (directory) => async (dirent) => {
if (
dirent.name === '.git' ||
dirent.name === 'setup.js' ||
dirent.name === 'node_modules'
)
return [];
if (dirent.isFile()) return [path.resolve(directory, dirent.name)];
if (dirent.isDirectory())
return await getFiles(path.resolve(directory, dirent.name));
return [];
};
const getFiles = async (directory) => {
const filesWithTypes = await fs.readdir(directory, {
encoding: 'utf-8',
withFileTypes: true,
});
const resolutions = filesWithTypes.map(resolveToFiles(directory));
const files = await Promise.all(resolutions);
return files.flat(1);
};
const gettingFiles = getFiles(path.resolve(__dirname));
const replaceInFile = (matcher, replacer) => async (path) => {
const contents = await fs.readFile(path, {encoding: 'utf-8'});
await fs.writeFile(path, contents.replace(matcher, replacer));
};
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const replaceVariable = async (variable, value) => {
const files = await gettingFiles;
const matcher = new RegExp(`\\[${variable}\\]`, 'g');
await Promise.all(files.map(replaceInFile(matcher, value)));
};
const fillTemplateVariable = (variable, question, defaultValue = '') =>
new Promise((resolve) => {
rl.question(question, async (receivedValue) => {
const value = receivedValue?.trim() || defaultValue;
replaceVariable(variable, value);
rl.write(`Replaced ${variable} to "${value}".\n`);
resolve();
});
});
replaceVariable('year', new Date().getFullYear())
.then(() =>
fillTemplateVariable(
'guebbitJavascriptLibrary',
`
Library name (can have upper/lower case letters, spaces, numbers and special characters)
Ex. React.js, Vue.js, Fetch Interceptors, GraphQL Loader for Webpack etc.
> `,
),
)
.then(() =>
fillTemplateVariable(
'guebbit-javascript-library',
`
Module name (only lower case letters, "." or "-") and optionally with scope.
Ex. react, vue, fetch-interceptors, @webpack/graphql-loader etc.
> `,
),
)
.then(() =>
fillTemplateVariable(
'guebbitJavascriptLibrary',
`
What is you library name in camel-case for UMD bundles?
Ex. React, Vue, fetchInterceptors, graphqlLoaderForWebpack etc.
> `,
),
)
.then(() =>
fillTemplateVariable(
'Guebbit',
`
What is your nickname on GitHub?
> `,
'',
),
)
.then(() =>
fillTemplateVariable(
'guebbit-javascript-library',
`
What will be this repository name on GitHub?
> `,
),
)
.then(() =>
fillTemplateVariable(
'Andrea Guerzoni',
`
What is your full name? (used in MIT License)
> `,
'',
),
)
.then(() => {
rl.close();
fs.unlink(path.resolve(__filename));
})
.catch((error) => {
console.error(error?.message ?? error ?? 'Unknown error.');
rl.close();
});