-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·213 lines (199 loc) · 6.37 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
#!/usr/bin/env node
let shell = require("shelljs");
let colors = require("colors");
let appName = process.argv[2];
let appDirectory = `${process.cwd()}/${appName}`;
let fs = require("fs-extra");
const emptyAndCopyDirectory = () => {
fs.emptyDir(`${appDirectory}/src/`, function(err) {
if (err) return console.error(err);
console.log("deletedDirectory!");
});
fs.copy(`${__dirname}/template/src`, `${appDirectory}/src`, function(err) {
if (err) return console.error(err);
console.log("success!");
});
fs.copy(
`${__dirname}/template/config-overrides.js`,
`${appDirectory}/config-overrides.js`,
function(err) {
if (err) return console.error(err);
console.log("success!");
}
);
fs.copy(`${__dirname}/template/.vscode`, `${appDirectory}/.vscode`, function(
err
) {
if (err) return console.error(err);
console.log("success!");
});
};
const createEntity = async entityName => {
const name = entityName.charAt(0).toLowerCase() + entityName.slice(1);
const uName = entityName.charAt(0).toUpperCase() + entityName.slice(1);
fs.access("./src/store/", fs.constants.F_OK, err => {
if (err) {
console.log(
"current directory does not have the src/store folder. Make sure the folder exists before continuing"
.red
);
return;
}
fs.copy(`${__dirname}/NewModelTemplate`, `./src/store/${uName}`, err => {
if (err) return console.error(err);
fs.renameSync(
`./src/store/${uName}/NewModelActions.js`,
`./src/store/${uName}/${uName}Actions.js`,
err => {}
);
fs.renameSync(
`./src/store/${uName}/NewModelConsts.js`,
`./src/store/${uName}/${uName}Consts.js`,
err => {}
);
fs.renameSync(
`./src/store/${uName}/NewModelModel.js`,
`./src/store/${uName}/${uName}Model.js`,
err => {}
);
fs.renameSync(
`./src/store/${uName}/NewModelReducer.js`,
`./src/store/${uName}/${uName}Reducer.js`,
err => {}
);
prependFiles(name, `${uName}Reducer`);
console.log("Template copied successfully".green);
});
});
};
const prependFiles = (entityName, fileName) => {
const uName = entityName.charAt(0).toUpperCase() + entityName.slice(1);
const allCapsName = entityName.toUpperCase();
addToReducer(entityName);
const data = fs.readFileSync(`./src/store/${uName}/${uName}Reducer.js`);
const fd = fs.openSync(`./src/store/${uName}/${uName}Reducer.js`, "w+");
const newData = data
.toString()
.replace(/SET_NewModel/g, `SET_${allCapsName}`)
.replace(/NewModel/g, `${uName}`);
fs.writeSync(fd, newData, 0, newData.length);
const constData = fs.readFileSync(`./src/store/${uName}/${uName}Consts.js`);
const constOpenSync = fs.openSync(
`./src/store/${uName}/${uName}Consts.js`,
"w+"
);
const newConstData = constData
.toString()
.replace(/NewModel/g, `${allCapsName}`);
fs.writeSync(constOpenSync, newConstData, 0, newConstData.length);
const actionData = fs.readFileSync(`./src/store/${uName}/${uName}Actions.js`);
const actionOpenSync = fs.openSync(
`./src/store/${uName}/${uName}Actions.js`,
"w+"
);
const newActionData = actionData
.toString()
.replace(/SET_NewModel/g, `SET_${allCapsName}`)
.replace(/NewModel/g, `${uName}`);
fs.writeSync(actionOpenSync, newActionData, 0, newActionData.length);
};
const addToReducer = entityName => {
const uName = entityName.charAt(0).toUpperCase() + entityName.slice(1);
const data = fs.readFileSync(`./src/store/reducers.js`);
const fd = fs.openSync(`./src/store/reducers.js`, "w+");
const insert = new Buffer(
`import ${entityName} from './${uName}/${uName}Reducer';\r\n`
);
const newData = data.toString().replace(/}([^}]*)$/, `,${entityName}\r\n}`);
fs.writeSync(fd, insert, 0, insert.length, 0);
fs.writeSync(fd, newData, insert.length, newData.length, insert.length);
fs.close(fd, err => {
if (err) throw err;
});
};
const run = async () => {
switch (appName) {
case "--add-store-item":
let entityName = process.argv[3];
createEntity(entityName);
console.log(appName, entityName);
break;
default:
let success = await createReactApp();
if (!success) {
console.log(
"Something went wrong while trying to create a new React app using create-react-app"
.red
);
return false;
}
cdIntoNewApp();
emptyAndCopyDirectory();
console.log("template copied successfully!!");
updatePackageScripts();
console.log("package.json updated successfully!!");
console.log("installing all packages. This could be a while.");
await installPackages();
console.log("All done");
break;
}
};
const updatePackageScripts = async () => {
let packagePath = `${appDirectory}/package.json`;
let packageJson = require(packagePath);
packageJson.scripts = {
start: "react-app-rewired start",
build: "react-app-rewired build",
test: "react-app-rewired test",
eject: "react-scripts eject"
};
fs.writeFile(packagePath, JSON.stringify(packageJson, null, 2), function(
err
) {
if (err) return console.log(err);
console.log("writing to " + packagePath);
});
};
const createReactApp = () => {
return new Promise(resolve => {
if (appName) {
shell.exec(`create-react-app ${appName}`, code => {
console.log("Exited with code ", code);
console.log("Created react app");
resolve(true);
});
} else {
console.log("\nNo app name was provided.".red);
console.log("\nProvide an app name in the following format: ");
console.log("\ncreate-react-redux-router-app ", "app-name\n".cyan);
resolve(false);
}
});
};
const installPackages = () => {
return new Promise(resolve => {
const libraries = [
"redux",
"react-router",
"react-redux@5.1.1",
"redux-thunk",
"react-router-dom",
"dw-app-wrapper",
"babel-plugin-import",
"react-app-rewire-less",
"react-app-rewired"
];
console.log(`\nInstalling ${[...libraries].join(", ")}\n`.cyan);
shell.exec(`npm install -D ${[...libraries].join(" ")}`, () => {
console.log("\nFinished installing packages\n".green);
resolve();
});
});
};
const cdIntoNewApp = () => {
return new Promise(resolve => {
shell.cd(appDirectory);
resolve();
});
};
run();