1 | const colors = require('colors');
|
2 | const commandLineArgs = require('command-line-args');
|
3 | const prompt = require('prompt');
|
4 | const main = require('./lib');
|
5 | const pkg = require('../package.json');
|
6 |
|
7 | const log = main.log;
|
8 | const projectDefaults = {
|
9 | name: 'my-project',
|
10 | desc: '',
|
11 | author: '',
|
12 | license: 'MIT',
|
13 | version: '0.0.1',
|
14 | port: 8084
|
15 | };
|
16 | let project = {};
|
17 |
|
18 | const help = `
|
19 | Usage: freshpack <project-directory> [options]
|
20 | or: freshpack --conf
|
21 |
|
22 | Options:
|
23 | -h, --help usage information
|
24 | -v, --version version number
|
25 |
|
26 | -q, --quiet disable verbose logging
|
27 | -c, --conf prompt for configuration (not completed yet!)
|
28 | -p, --port port (default is 8084)
|
29 | -i, --install install dependencies
|
30 |
|
31 | -f, --flow include 'flow' - static type checker
|
32 | -l, --lint include 'eslint' - pattern identifying and reporting
|
33 | -t, --test include 'jest' and 'enzyme' - testing toolset
|
34 | -d, --dev alias for '--flow --lint --test'
|
35 |
|
36 | -m, --mobx include 'mobx' - simple, scalable state management
|
37 | -r, --redux include 'redux' - predictable state container
|
38 |
|
39 | -o, --router include 'react-router' - declarative routing
|
40 |
|
41 | -a, --sass include 'sass'
|
42 | -y, --styled include 'styled-components'
|
43 | `;
|
44 |
|
45 | const optionDefinitions = [
|
46 | { name: 'dir', type: String, defaultOption: true },
|
47 | { name: 'port', alias: 'p', type: String },
|
48 | { name: 'version', alias: 'v', type: String },
|
49 | { name: 'help', alias: 'h', type: Boolean },
|
50 | { name: 'install', alias: 'i', type: Boolean },
|
51 | { name: 'lint', alias: 'l', type: Boolean },
|
52 | { name: 'flow', alias: 'f', type: Boolean },
|
53 | { name: 'test', alias: 't', type: Boolean },
|
54 | { name: 'mobx', alias: 'm', type: Boolean },
|
55 | { name: 'redux', alias: 'r', type: Boolean },
|
56 | { name: 'router', alias: 'o', type: Boolean },
|
57 | { name: 'sass', alias: 'a', type: Boolean },
|
58 | { name: 'styled', alias: 'y', type: Boolean },
|
59 | { name: 'quiet', alias: 'q', type: Boolean },
|
60 | { name: 'conf', alias: 'c', type: Boolean },
|
61 | { name: 'dev', alias: 'd', type: Boolean }
|
62 | ];
|
63 |
|
64 | const args = commandLineArgs(optionDefinitions);
|
65 |
|
66 | const startWithPromt = (callback) => {
|
67 | const promptLines = [
|
68 | 'project name (' + (args.dir || projectDefaults.name) + '):',
|
69 | 'project description:',
|
70 | 'project author:',
|
71 | 'project version: (' + projectDefaults.version + '):',
|
72 | 'project licence: (' + projectDefaults.license + '):',
|
73 | 'localhost port (' + (args.port || projectDefaults.port) + '):'
|
74 | ];
|
75 | prompt.start();
|
76 |
|
77 | prompt.delimiter = '';
|
78 | prompt.get(
|
79 | promptLines,
|
80 | (err, result) => {
|
81 | const name = result[promptLines[0]].replace(/\s/g, '-').trim();
|
82 | const description = result[promptLines[1]];
|
83 | const author = result[promptLines[2]];
|
84 | const version = result[promptLines[3]];
|
85 | const licence = result[promptLines[4]];
|
86 | const port = result[promptLines[5]];
|
87 |
|
88 | project = {
|
89 | name: name !== '' ? name : projectDefaults.name,
|
90 | desc: description !== '' ? description : projectDefaults.desc,
|
91 | author: author !== '' ? author : projectDefaults.author,
|
92 | version: version !== '' ? version : projectDefaults.version,
|
93 | license: licence !== '' ? licence : projectDefaults.license,
|
94 | port: port !== '' ? port : projectDefaults.port
|
95 | };
|
96 |
|
97 | callback(project, args);
|
98 | }
|
99 | );
|
100 | };
|
101 |
|
102 | module.exports = (callback) => {
|
103 | if (!args.dir && !args.conf) {
|
104 | console.log(help);
|
105 | return;
|
106 | } else if (args.version) {
|
107 | console.log('v' + pkg.version);
|
108 | return;
|
109 | } else if (args.sass && args.styled ) {
|
110 | console.log('Please use only ONE of the style options:');
|
111 | console.log('--sass OR --styled');
|
112 | return;
|
113 | } else if (args.mobx && args.redux ) {
|
114 | console.log('Please use only ONE of the state management options:');
|
115 | console.log('--mobx OR --redux');
|
116 | return;
|
117 | }
|
118 |
|
119 | log('');
|
120 | log(colors.white('freshpack v' + pkg.version));
|
121 |
|
122 | if (args.conf) {
|
123 | startWithPromt(callback);
|
124 | } else {
|
125 | if (args.dev) {
|
126 | args.lint = true;
|
127 | args.test = true;
|
128 | args.flow = true;
|
129 | }
|
130 |
|
131 | project = {
|
132 | name: args.dir || projectDefaults.name,
|
133 | desc: projectDefaults.desc,
|
134 | author: projectDefaults.author,
|
135 | version: projectDefaults.version,
|
136 | license: projectDefaults.license,
|
137 | port: args.port || projectDefaults.port
|
138 | };
|
139 |
|
140 | callback(project, args);
|
141 | }
|
142 | };
|