-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
261 lines (222 loc) · 10.4 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env node
var program = require("commander");
var fs = require("fs");
var child_process = require("child_process");
var chalk = require("chalk");
function editStoreFile(storeFileContents) {
var newContents = storeFileContents;
var middlewareStrings = [];
if(program.thunk) {
newContents = newContents.replace("//<thunkImportLine>", "import thunk from 'redux-thunk';");
middlewareStrings.push("thunk");
}
else {
newContents = newContents.replace("//<thunkImportLine>", "");
}
if(program.promise) {
newContents = newContents.replace("//<promiseImportLine>", "import promise from 'redux-promise-middleware';");
middlewareStrings.push("promise()");
}
else {
newContents = newContents.replace("//<promiseImportLine>", "");
}
if(program.logger) {
newContents = newContents.replace("//<loggerImportLine>", "import logger from 'redux-logger';");
middlewareStrings.push("logger");
}
else {
newContents = newContents.replace("//<loggerImportLine>", "");
}
if(program.ducks) {
newContents = newContents.replace("//<reducersImportLine>", "import reducers from './ducks';");
}
else {
newContents = newContents.replace("//<reducersImportLine>", "import reducers from './reducers';");
}
newContents = newContents.replace("//<middlewareLine>", "const middleware = applyMiddleware(" + middlewareStrings.toString().replace(/,/g, ", ") + ");");
return newContents;
}
function editActionsFile(actionsFileContents) {
var newContents = actionsFileContents;
if(!program.thunk) {
var asyncIndex = newContents.indexOf("//This is an example async action using redux-thunk.");
newContents = newContents.substring(0, asyncIndex);
}
return newContents;
}
function editComponentFile(componentFileContents) {
var newContents = componentFileContents;
if(program.ducks) {
newContents = newContents.replace("//<actionsImportLine>", "import { increment } from './ducks/sampleDuck';");
}
else {
newContents = newContents.replace("//<actionsImportLine>", "import { increment } from './actions';");
}
return newContents;
}
function addDependency(path, packageName, yarnExists) {
console.log(chalk.cyan("Adding dependency '" + packageName + "'..."));
var output = "";
if(yarnExists) {
output = child_process.execSync("yarn add " + packageName, {cwd: path}).toString("utf8");
}
else {
output = child_process.execSync("npm install --save " + packageName, {cwd: path}).toString("utf8");
}
console.log(chalk.cyan("Output from installation: " + output));
}
function addFileFromTemplate(path, templatePath, textProcessingFunc) {
var templateContents = fs.readFileSync(templatePath, "utf8");
if(textProcessingFunc) {
templateContents = textProcessingFunc(templateContents);
}
console.log(chalk.cyan("Creating file " + path + "..."));
fs.writeFileSync(path, templateContents, function(err) {
console.error(err);
});
}
function checkOrMakeDir(path) {
var dirMade = false;
try {
var stat = fs.statSync(path);
if(stat.isDirectory()) {
console.warn(chalk.yellow("WARN: The directory '" + path + "' already exists. Skipping adding files to it..."));
}
else {
console.error(chalk.yellow("WARN: A file with the path '" + path + "' already exists. Cannot create a file with the same name!"));
}
}
catch(err) {
console.log(chalk.cyan("Making directory " + path + "..."));
try {
fs.mkdirSync(path);
console.log(chalk.green("Successfully created the directory '" + path + "'!"));
dirMade = true;
}
catch(err) {
console.error(chalk.red("ERR: Failed to make directory '" + path + "'!"));
}
}
return dirMade;
}
program
.version("0.6.0")
.arguments("<root_project_directory>")
.option("-r, --react", "Installs and saves 'react-redux' to the project dependencies. Also prompts the user with basic steps to integrate Redux with React.")
.option("-l, --no-logger", "Skips installing 'redux-logger' middleware.")
.option("-t, --no-thunk", "Skips installing 'redux-thunk' middleware.")
.option("-p, --no-promise", "Skips installing 'redux-promise-middleware' middleware.")
.option("-e, --examples", "Adds an 'redux_examples/' directory to the project root. This directory includes examples for integrating redux with different types of projects.")
.option("-d, --ducks", "Adds a 'ducks/' directory to the project root. This directory contains an example modular redux file (also known as a duck).")
.option("-s, --src <directory>", "Specifies the source directory for the project. This is where the boilerplate code will reside. Defaults to the root project directory specified by the <path> argument.")
.action(function(path) {
try {
var fullPath = process.cwd() + "/" + path.replace("/", "");
var stat = fs.statSync(fullPath);
if(stat.isDirectory()) {
try {
var packageStat = fs.statSync(fullPath + "/package.json");
if(packageStat.isFile()) {
console.log(chalk.cyan("Adding redux to " + path + "..."));
var yarnExists = false;
try {
var yarnLockStat = fs.statSync(fullPath + "/yarn.lock");
if(yarnLockStat.isFile()) {
yarnExists = true;
console.log(chalk.cyan("Detected yarn.lock, using yarn to install dependencies."));
}
else {
console.log(chalk.cyan("No yarn.lock detected, using npm to install dependencies."));
}
}
catch(err) {
console.log(chalk.cyan("No yarn.lock detected, using npm to install dependencies."));
}
fullPath = program.src ? (fullPath + "/" + program.src) : fullPath;
try {
var pathStat = fs.statSync(fullPath);
if(pathStat.isDirectory()) {
//Create redux directories
var actionsDirMade = !program.ducks ? checkOrMakeDir(fullPath + "/actions") : false;
var reducersDirMade = !program.ducks ? checkOrMakeDir(fullPath + "/reducers") : false;
//Add boilerplate code to the redux directories
if(actionsDirMade) {
addFileFromTemplate(fullPath + "/actions/index.js", __dirname + "/templates/actions_template.js", editActionsFile);
}
if(reducersDirMade) {
addFileFromTemplate(fullPath + "/reducers/sampleReducers.js", __dirname + "/templates/sample_reducers_template.js");
addFileFromTemplate(fullPath + "/reducers/index.js", __dirname + "/templates/reducers_template.js");
}
if(program.examples) {
var examplesDirMade = checkOrMakeDir(fullPath + "/redux_examples");
if(examplesDirMade) {
var reactDirMade = checkOrMakeDir(fullPath + "/redux_examples/react_example");
if(reactDirMade) {
addFileFromTemplate(fullPath + "/redux_examples/react_example/App.js", __dirname + "/templates/react_example_template.js");
addFileFromTemplate(fullPath + "/redux_examples/react_example/TestComponent.js", __dirname + "/templates/react_example_component_template.js", editComponentFile);
}
}
}
if(program.ducks) {
var ducksDirMade = checkOrMakeDir(fullPath + "/ducks");
if(ducksDirMade) {
addFileFromTemplate(fullPath + "/ducks/index.js", __dirname + "/templates/ducks_index_template.js");
addFileFromTemplate(fullPath + "/ducks/sampleDuck.js", __dirname + "/templates/ducks_template.js");
}
}
//Add store boilerplate code in the main project directory
addFileFromTemplate(fullPath + "/store.js", __dirname + "/templates/store_template.js", editStoreFile);
//Install dependencies
addDependency(fullPath, "redux", yarnExists);
if(program.logger) {
addDependency(fullPath, "redux-logger", yarnExists);
}
if(program.thunk) {
addDependency(fullPath, "redux-thunk", yarnExists);
}
if(program.promise) {
addDependency(fullPath, "redux-promise-middleware", yarnExists);
}
if(program.react) {
addDependency(fullPath, "react-redux", yarnExists);
console.log(chalk.green("Remember to perform the following steps to successfully integrate Redux with your React app (assuming you are using ES6 syntax):"));
console.log("");
console.log(chalk.green("In your main file:"));
console.log(chalk.green("1. import { Provider } from 'react-redux';"));
console.log(chalk.green("2. import store from './store';"));
console.log(chalk.green("3. Wrap the outermost component with <Provider store={store}></Provider>"));
console.log("");
console.log(chalk.green("In your individual components:"));
console.log(chalk.green("1. import { connect } from 'react-redux';"));
console.log(chalk.green("2. import { <actionNames> } from 'actions';"));
console.log(chalk.green("3. Create const mapStateToProps = (state) => { return {propName: state.reducerName.variableName} }"));
console.log(chalk.green("4. Create const mapDispatchToProps = (dispatch) => { return {propName: () => { dispatch(actionFunc()) }} }"));
console.log(chalk.green("5. Insert the mapped props and actions into your component's render() function."));
console.log(chalk.green("6. Wrap the component class with the connect(mapStateToProps, mapDispatchToProps)(Component) function and export the resulting component."));
}
}
else {
console.error(chalk.red("ERR: '" + fullPath + "' is not a valid subdirectory in the project!"));
}
}
catch(err) {
console.error(chalk.red("ERR: '" + fullPath + "' is not a valid subdirectory in the project!"));
}
}
else {
console.error(chalk.red("ERR: '" + fullPath + "' is not a valid npm project! No package.json file found!"));
}
}
catch(err) {
console.error(chalk.red("ERR: '" + fullPath + "' is not a valid npm project! No package.json file found!"));
}
}
else {
console.error(chalk.red("ERR: '" + fullPath + "' is not a valid directory!"));
}
}
catch(err) {
console.error(chalk.red("ERR: '" + fullPath + "' is not a valid directory!"));
}
})
.parse(process.argv);