-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·201 lines (190 loc) · 5.06 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
#!/usr/bin/env node
"use strict";
// get command line arguments
const inquirer = require("inquirer"),
ora = require("ora"),
createConfig = require("./createConfig"),
chalk = require("chalk"),
exec = require('child_process').exec,
path = require('path'),
commandLineArgs = require('command-line-args'),
optionDefinitions = [
{ name: 'output', alias: 'o', type: String, default: './' }
],
options = commandLineArgs(optionDefinitions),
CONFIG_DIRECTORY = options.output ? path.resolve(process.cwd(), options.output.trim().replace(' ', '-')) : path.resolve(process.cwd());
// initialise spinner
let spinner = ora("Setting up webpack");
spinner.color = "blue";
let config = null;
inquirer
.prompt([
{
type: "list",
message: "Select main library",
name: "library",
choices: [
{
name: "React"
},
{
name: "Vue"
},
{
name: "Custom config"
}
],
validate: function(answer) {
if (answer.length < 1) {
return "You must choose at least one library.";
}
return true;
}
},
{
type: "confirm",
message: "Do you want to transpile using Babel?",
name: "babel",
default: true,
when: function (answers) {
return answers.library === 'Custom config';
}
},
{
type: "confirm",
message: "Are you using Typescript?",
name: "typescript",
default: false
},
{
type: "input",
message: "Entry file",
name: "entry",
default: "./src/index.js"
},
{
type: "input",
message: "Output path",
name: "path",
default: "dist"
},
{
type: "input",
message: "Output file name",
name: "filename",
default: "./bundle.js"
},
{
type: "checkbox",
message: "Styling",
name: "styling",
choices: [
{
name: "CSS"
},
{
name: "CSS Modules"
},
{
name: "SASS"
}
]
},
{
type: "confirm",
message: "Load images (svg/png/jpg/gif) using webpack?",
name: "image"
},
{
type: "confirm",
message: "Load fonts (woff/woff2/eot/ttf) using webpack?",
name: "fonts"
},
{
type: "list",
message: "Package manager [ we will install dependencies for you :-) ]",
name: "packageManager",
choices: [
{
name: "npm"
},
{
name: "yarn"
}
],
validate: function(answer) {
if (answer.length < 1) {
return "You must choose at least one package manager.";
}
return true;
}
}
])
.then(answers => {
config = answers;
if (answers.library === "Custom config") {
inquirer
.prompt([
{
type: "checkbox",
message: "Select files to compile",
name: "customLoaders",
choices: [
{
name: "JavaScript"
},
{
name: "HTML"
}
]
}
])
.then(answers => {
config = { ...config, ...answers };
cliCreateConfig();
});
} else {
cliCreateConfig();
}
});
function cliCreateConfig() {
spinner.start();
const npmConfig = createConfig(config, CONFIG_DIRECTORY);
spinner.stop();
console.log(chalk.green.bold('\n\nConfig created! Installing dependencies...\n\n'));
spinner = ora("Installing dependencies");
spinner.color = "blue";
spinner.start();
setTimeout(() => {
spinner.text = "Still installing dependencies...";
spinner.color = "yellow";
}, 10000);
setTimeout(() => {
spinner.text = "Almost done...";
spinner.color = "green";
}, 30000)
const packageManager = config.packageManager;
if (packageManager === 'npm') {
exec(`cd "${CONFIG_DIRECTORY}" && npm init -y && npm i --save-dev ${npmConfig.dev.join(" ")} && npm i --save ${npmConfig.save.join(" ")}`, (err) => {
spinner.stop();
if (err) {
console.log(chalk.red.bold('Failed to install dependencies.\n\n'));
console.log(err);
console.log(chalk.yellow(`\n\nPlease run the command "npm i --save-dev ${npmConfig.dev.join(' ')} && npm i --save ${npmConfig.save.join(' ')}" to manually install dependencies.\n\n`));
return;
}
console.log(chalk.green.bold("Dependencies installed. Enjoy! :)\n\n"));
});
} else if (packageManager === 'yarn') {
exec(`cd "${CONFIG_DIRECTORY}" && yarn init -y && yarn add ${npmConfig.dev.join(" ")} --dev && yarn add ${npmConfig.save.join(" ")}`, (err) => {
spinner.stop();
if (err) {
console.log(chalk.red.bold('Failed to install dependencies.\n\n'));
console.log(err);
console.log(chalk.yellow(`\n\nPlease run the command "npm i --save-dev ${npmConfig.dev.join(' ')} && npm i --save ${npmConfig.save.join(' ')}" to manually install dependencies.\n\n`));
return;
}
console.log(chalk.green.bold("Dependencies installed. Enjoy! :)\n\n"));
});
}
}