UNPKG

11.1 kBJavaScriptView Raw
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5var inquirer = _interopDefault(require('inquirer'));
6var chalk = _interopDefault(require('chalk'));
7var path = _interopDefault(require('path'));
8var childProcess = _interopDefault(require('child_process'));
9var fs = _interopDefault(require('fs'));
10var ncu = _interopDefault(require('npm-check-updates'));
11var tape = _interopDefault(require('tape'));
12var babelCore = _interopDefault(require('babel-core'));
13
14var exec = childProcess.exec;
15// ----------------------------------------------------------------
16var runTests = (function (dir, entry) {
17 console.log(chalk.yellow("Running project tests..."));
18 exec("./node_modules/.bin/nyc --require ./node_modules/babel-register --reporter=lcov --reporter=text ./node_modules/.bin/tape '" + path.resolve(dir) + "/**/*.js' | ./node_modules/.bin/faucet", function (error, stdout, stderr) {
19 if (error !== null) {
20 console.log("exec error: " + error);
21 }
22 console.log(stdout);
23 console.log(stderr);
24 });
25})
26
27var exec$1 = childProcess.exec;
28// ----------------------------------------------------------------
29var rollupFiles = (function (file, output) {
30 console.log("Rolling files into bundle...");
31 exec$1("./node_modules/.bin/rollup --config ./node_modules/ezy/vendor/rollup.config.js " + path.resolve(file) + " -o " + path.resolve(output), function (error, stdout, stderr) {
32 if (error !== null) {
33 console.log("exec error: " + error);
34 }
35 console.log("Rolled " + file + " into bundle at " + output + ".");
36 });
37})
38// --config ../vendor/rollup.config.js
39
40var exec$2 = childProcess.exec;
41// ----------------------------------------------------------------
42var addBabelPreset = (function () {
43 fs.stat(path.resolve("./node_modules/babel-preset-es2015"), function (err, stats) {
44 if (err && err.code !== "ENOENT") console.log(err);
45 if (err && err.code === "ENOENT") {
46 console.log(chalk.yellow("No Babel preset detected..."));
47 console.log(chalk.blue("Installing ES2015 Babel preset via NPM..."));
48 exec$2("npm i -D babel-preset-es2015", function (error, stdout, stderr) {
49 console.log(chalk.green("Added ES2015 Babel preset."));
50 if (error !== null) {
51 console.log("exec error: " + error);
52 }
53 });
54 } else {
55 console.log(chalk.green("Babel preset already exists."));
56 }
57 });
58
59 fs.stat(path.resolve(".babelrc"), function (err, stats) {
60 if (err && err.code !== "ENOENT") console.log(err);
61 if (err && err.code === "ENOENT") {
62 console.log(chalk.yellow("No .babelrc file detected..."));
63 console.log(chalk.blue("Creating .babelrc file..."));
64
65 fs.writeFile(path.resolve(".babelrc"), "{\n \"presets\": [\"es2015\"]\n}", function (err) {
66 if (err) throw err;
67 console.log(chalk.green("Added preset to .babelrc file."));
68 });
69 } else {
70 console.log(chalk.green(".babelrc already exists."));
71 }
72 });
73})
74
75// ----------------------------------------------------------------
76var addWallabyConfig = (function () {
77
78 fs.stat(path.resolve("wallaby.js"), function (err, stats) {
79 if (err && err.code !== "ENOENT") console.log(err);
80 if (err && err.code === "ENOENT") {
81 console.log(chalk.yellow("No wallaby.js file detected..."));
82 console.log(chalk.blue("Creating wallaby.js file..."));
83 var oldFile = fs.createReadStream(path.resolve("./node_modules/ezy/vendor/wallaby.js"));
84 var newFile = fs.createWriteStream(path.resolve("wallaby.js"));
85 oldFile.pipe(newFile);
86 console.log(chalk.green("wallaby.js file created."));
87 } else {
88 console.log(chalk.green("wallaby.js already exists."));
89 }
90 });
91})
92
93// ----------------------------------------------------------------
94var addEzyConfig = (function () {
95
96 fs.stat(path.resolve("ezy.config.json"), function (err, stats) {
97 if (err && err.code !== "ENOENT") console.log(err);
98 if (err && err.code === "ENOENT") {
99 console.log(chalk.yellow("No ezy.config.json file detected..."));
100 console.log(chalk.blue("Creating ezy.config.json file..."));
101 var oldFile = fs.createReadStream(path.resolve("./node_modules/ezy/vendor/ezy.config.json"));
102 var newFile = fs.createWriteStream(path.resolve("ezy.config.json"));
103 oldFile.pipe(newFile);
104 console.log(chalk.green("ezy.config.json file created."));
105 } else {
106 console.log(chalk.green("ezy.config.json already exists."));
107 }
108 });
109})
110
111// ----------------------------------------------------------------
112var addESLint = (function () {
113
114 fs.stat(path.resolve(".eslintrc"), function (err, stats) {
115 if (err && err.code !== "ENOENT") console.log(err);
116 if (err && err.code === "ENOENT") {
117 console.log(chalk.yellow("No .eslintrc file detected..."));
118 console.log(chalk.blue("Creating .eslintrc file..."));
119
120 // fs.writeFile(path.resolve(".babelrc"), "{\n \"presets\": [\"es2015\"]\n}", (err) => {
121 // if (err) throw err;
122 // console.log(chalk.green("Added preset to .babelrc file."));
123 // });
124 var oldFile = fs.createReadStream(path.resolve("./node_modules/ezy/vendor/.eslintrc"));
125 var newFile = fs.createWriteStream(path.resolve(".eslintrc"));
126 oldFile.pipe(newFile);
127 console.log(chalk.green(".eslintrc file created."));
128 } else {
129 console.log(chalk.green(".eslintrc already exists."));
130 }
131 });
132})
133
134var exec$6 = childProcess.exec;
135// ----------------------------------------------------------------
136var viewCoverage = (function () {
137 console.log(chalk.yellow("Opening coverage report in the browser..."));
138 exec$6("open " + path.resolve("coverage/lcov-report/index.html"), function (error, stdout, stderr) {
139 if (error !== null) {
140 console.log("exec error: " + error);
141 }
142 });
143})
144
145var exec$7 = childProcess.exec;
146// ----------------------------------------------------------------
147var runProject = {
148 node: function node(file) {
149 console.log(chalk.yellow("Running project with Node..."));
150 exec$7("node " + path.resolve(file), function (error, stdout, stderr) {
151 if (error !== null) {
152 console.log("exec error: " + error);
153 }
154 console.log(stdout);
155 console.log(stderr);
156 });
157 },
158 browser: function browser(file) {
159 console.log(chalk.yellow("Running project with BrowserSync..."));
160 exec$7("./node_modules/.bin/browser-sync start --server " + path.resolve(file) + " --files " + path.resolve(file), function (error, stdout, stderr) {
161 if (error !== null) {
162 console.log("exec error: " + error);
163 }
164 console.log(stdout);
165 console.log(stderr);
166 });
167 }
168};
169
170// ----------------------------------------------------------------
171var updateDependencies = {
172 check: function check() {
173 console.log(chalk.yellow("Checking for new versions of your dependencies..."));
174 ncu.run({
175 packageFile: path.resolve("package.json"),
176 jsonUpgraded: true,
177 upgrade: false
178 }).then(function (toUpgrade) {
179 if (Object.getOwnPropertyNames(toUpgrade).length < 1) {
180 console.log(chalk.green("Everything is up to date. :)"));
181 } else {
182 console.log(toUpgrade);
183 }
184 });
185 },
186 run: function run() {
187 console.log(chalk.yellow("Updating your dependencies..."));
188 ncu.run({
189 packageFile: path.resolve("package.json"),
190 jsonUpgraded: true,
191 upgrade: true
192 }).then(function (toUpgrade) {
193 if (Object.getOwnPropertyNames(toUpgrade).length < 1) {
194 console.log(chalk.green("Everything is up to date. :)"));
195 } else {
196 console.log(toUpgrade);
197 }
198 });
199 }
200
201};
202
203// ----------------------------------------------------------------
204
205var questions = [{
206 type: "list",
207 name: "inputValue",
208 message: "What would you like to do?",
209 choices: ["1. Run Project", new inquirer.Separator(), "2. Test Project", "3. View Coverage", new inquirer.Separator(), "4. Bundle Project", new inquirer.Separator(), "5. Check Updates", "6. Run Updates", "7. Add Babel Preset", "8. Add Wallaby Config", "9. Add ESLint", "10. Add ezy Config", new inquirer.Separator()]
210}];
211
212var taskList = (function (configure) {
213 var config = require(configure); // Weird Require.
214 var entry, entryDir, outputDir, output, testDir;
215 if (config.entry) {
216 entry = config.entry;
217 } else {
218 entry = "src/index.js";
219 }
220 if (config.entryDir) {
221 entryDir = config.entryDir;
222 } else {
223 entryDir = "src";
224 }
225 if (config.outputDir) {
226 outputDir = config.outputDir;
227 } else {
228 outputDir = "dist";
229 }
230 if (config.output) {
231 output = config.output;
232 } else {
233 output = "bundle.js";
234 }
235 if (config.testDir) {
236 testDir = config.testDir;
237 } else {
238 testDir = "tests";
239 }
240 // --------------------------------
241 inquirer.prompt(questions).then(function (answers) {
242 var a = answers.inputValue;
243
244 if (a === "1. Run Project") {
245 inquirer.prompt([{
246 type: "list",
247 name: "runWhere",
248 message: "Where would you like to run the project?",
249 choices: ["Node", "Browser"]
250 }]).then(function (answers) {
251 if (answers.runWhere === "Node") {
252 runProject.node(outputDir + "/" + output);
253 } else if (answers.runWhere === "Browser") {
254 runProject.browser(outputDir);
255 }
256 });
257 } else if (a === "2. Test Project") {
258 runTests(testDir, entryDir);
259 } else if (a === "3. View Coverage") {
260 viewCoverage();
261 } else if (a === "4. Bundle Project") {
262 rollupFiles(entryDir + "/" + entry, outputDir + "/" + output);
263 } else if (a === "5. Check Updates") {
264 updateDependencies.check();
265 } else if (a === "6. Run Updates") {
266 updateDependencies.run();
267 } else if (a === "7. Add Babel Preset") {
268 addBabelPreset();
269 } else if (a === "8. Add Wallaby Config") {
270 addWallabyConfig();
271 } else if (a === "9. Add ESLint") {
272 addESLint();
273 } else if (a === "10. Add ezy Config") {
274 addEzyConfig();
275 }
276 });
277})
278
279// ----------------------------------------------------------------
280var bin = function bin() {
281 // TODO: Check for ezy.config.json, chck for ezy section in package json,
282 // else suggest config file is made.
283 fs.stat(path.resolve("ezy.config.json"), function (err, stats) {
284 if (err && err.code !== "ENOENT") console.log(err);
285 if (err && err.code === "ENOENT") {
286 console.log(chalk.red("No ezy.config.json file has been found."));
287 inquirer.prompt([{
288 type: "confirm",
289 name: "install",
290 message: "Create an ezy.config.json file now?",
291 default: true
292 }]).then(function (answers) {
293 if (answers.install === true) {
294 addEzyConfig();
295 } else {
296 console.log("Please create a config file to use ezy.");
297 }
298 });
299 } else {
300 taskList(path.resolve("ezy.config.json"));
301 }
302 });
303};
304
305var test = tape;
306var color = chalk;
307var babel = babelCore;
308
309exports.bin = bin;
310exports.test = test;
311exports.color = color;
312exports.babel = babel;
\No newline at end of file