-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
Β·241 lines (187 loc) Β· 7.23 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env node
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Required modules
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const fs = require('fs');
const { execSync } = require('child_process');
const readline = require('readline');
const chalk = require('chalk');
const cliSelect = require('cli-select');
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Files to create
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
let packageFile = 'package.json';
let webpackFile = 'webpack.config.js';
let babelFile = '.babelrc';
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Ask a question to the user
//
// query: string
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const askQuestion = (query) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise(resolve => rl.question(chalk.yellow(query), ans => {
rl.close();
resolve(ans);
}));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Creates config files
//
// componentName: string
// componentDesc: string
// authorName: string
// authorWebsite: string
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const createConfigFiles = (mainComponent, componentName, componentDesc, authorName, authorWebsite) => {
let packageConfig = {
"name": `${componentName}`,
"version": "1.0.0",
"description": `${componentDesc}`,
"main": "./build/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": {
"name": `${authorName}`,
"url": `${authorWebsite}`
},
"license": "MIT"
}
let webpackConfig = {
"entry": [`./src/${mainComponent}`],
"module": {
"rules": [
{
"test": "/\.(js|jsx)$/",
"exclude": "/node_modules/",
"use": ["babel-loader"]
},
{
"test": "/(\.css$)/",
"loaders": ["style-loader", "css-loader"]
}
]
},
"externals": {
"react": {
"root": "React",
"commonjs2": "react",
"commonjs": "react",
"amd": "react"
},
"react-dom": {
"root": "ReactDOM",
"commonjs2": "react-dom",
"commonjs": "react-dom",
"amd": "react-dom"
}
},
"resolve": {
"extensions": ["*", ".js", ".jsx"]
},
"output": {
"publicPath": "/",
"filename": "index.js",
"libraryTarget": "commonjs2"
},
"devServer": {
"contentBase": "./build"
}
};
let babelConfig = {
"presets": ["@babel/preset-env", "@babel/preset-react"]
};
let jsonPackageFile = JSON.stringify(packageConfig, null, 4);
let jsonWebpackFile = JSON.stringify(webpackConfig, null, 4);
let jsonBabelFile = JSON.stringify(babelConfig, null, 4);
fs.openSync(packageFile, 'w');
fs.writeFileSync(packageFile, jsonPackageFile);
console.log(chalk.green(`β
${packageFile} has been created.`));
fs.openSync(webpackFile, 'w');
fs.writeFileSync(webpackFile, 'module.exports = ');
jsonWebpackFile = jsonWebpackFile.replace(`"/\.(js|jsx)$/"`, `/\.(js|jsx)$/`).replace(`"/(\.css$)/"`, `/(\.css$)/`);
fs.appendFileSync(webpackFile, jsonWebpackFile);
console.log(chalk.green(`β
${webpackFile} has been created.`));
fs.openSync(babelFile, 'w');
fs.writeFileSync(babelFile, jsonBabelFile);
console.log(chalk.green(`β
${babelFile} has been created.`));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Creates config files
//
// dependencies: array
// tag: string
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const installDependencies = (dependencies, tag) => {
console.log(chalk.red(`β³ Installing ${tag} dependencies...`));
execSync(`npm i --save-dev ${dependencies.join(' ')}`);
console.log(chalk.green(`β
${tag} dependencies have been installed.`));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Building packages
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const buildPackage = () => {
console.log(chalk.red(`β³ Building package`));
execSync(`npm run build`);
execSync(`rm -rf build`)
execSync(`mv dist build`);
console.log(chalk.green(`β
Package built successfully!`));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Check for src folder
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const checkSrcFolder = () => {
try{
if(fs.readdirSync('./src').length > 0){
console.log(`\n \n Please select the main file of your component: \n \n`);
return fs.readdirSync('./src').filter(file => file.endsWith('js'));
}
else
throw 'error';
}
catch(err){
console.log(chalk.red(`β Error checking /src: Create a /src folder and place your component on there.`));
process.exit(1);
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Main thread
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
const main = async () => {
// Printing name of the app
console.log(chalk.green(`
ββββ¬ββββββββββ¬ββββ β¬βββββββββββββ¬β ββββββββ¬β
β ββ¬βββ€ βββ€ β ββ€βββββ¬βββ€ βββ€β βββββββββββββ
ββββ΄ββββββ΄ β΄ β΄ βββ β΄ββββββ΄ β΄βββ β΄ ββββ΄ β΄ β΄
`));
// Checking if there is src folder and returns the files in it
let files = checkSrcFolder();
// User needs to select the main component
let mainComponent = (await cliSelect({values: files})).value;
console.log(chalk.green(`\n β
You have selected ${mainComponent} as the main component. \n`));
// Asking the user some questions
let componentName = await askQuestion("What is the name of the main component? Example: react-awesomeComponent \n");
let componentDesc = await askQuestion("Describe your component. Example: This is the BeSt coMponEnt EveR \n");
let authorName = await askQuestion("What is the name of the author? Example: Elon Musk \n");
let authorWebsite = await askQuestion("What is the webiste of the author? Example: elonmusk.com \n");
// Creating config files depending on answers
createConfigFiles(mainComponent, componentName, componentDesc, authorName, authorWebsite);
// Installing dependencies
await installDependencies(['webpack', 'webpack-dev-server', 'webpack-cli'], 'Webpack');
await installDependencies(['@babel/core', '@babel/preset-env', '@babel/preset-react'], 'Babel');
await installDependencies(['babel-loader', 'style-loader', 'css-loader'], 'Loader');
// Building the package
buildPackage();
// Ready to publish
console.log(chalk.blue('To publish your component: \n npm publish'));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Starting main
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main();