-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnewRoute.js
109 lines (89 loc) · 3.68 KB
/
newRoute.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
'use strict';
const chalk = require('chalk'),
monumentCheck = require('./checkForProject'),
path = require('path'),
fs = require('fs'),
existingRoute = require('../templates/routes/existingRoute'),
routeTemplate = require('../templates/routes/route'),
routeTest = require('../templates/routes/newRoute.test'),
routeTestHeader = require('../templates/routes/newRouteHeader.test'),
routeTestEOF = require('../templates/routes/newRouteEOF.test'),
getFinalFileName = (fileName, route) => {
if (route === '/' || route.match(/^\/:/)){
return 'main';
} else if (fileName[0] === '') {
return fileName[1];
} else {
console.log(chalk.yellow('You have a route that doesn\'t begin with "/"... you should fix that'));
return fileName[0];
}
},
getLocalRoute = (fileName, route) => {
if (fileName[0] === '' || (route === '/' || route.match(/^\/:/))) {
return route;
}
return `/${route}`;
};
module.exports = () => {
let routes,
routeReadHolder,
routeTestHolder;
console.log('Updating your applications route handlers');
if (monumentCheck()) {
try {
fs.statSync(path.join(process.cwd(), 'routes.json'));
routes = require(path.join(process.cwd(), 'routes.json'));
Object.keys(routes).forEach((route) => {
const fileName = route.split('/'),
finalFileName = getFinalFileName(fileName, route),
localRoute = getLocalRoute(fileName, route),
targetPath = path.join(process.cwd(), `/routes/${finalFileName}.js`),
targetTestPath = path.join(process.cwd(), `/routes/${finalFileName}.test.js`),
chalkedFileName = chalk.cyan(`/routes/${finalFileName}.js`);
try {
fs.statSync(targetPath);
routeReadHolder = fs.readFileSync(targetPath, 'utf-8');
// check for all the verbs...
routes[route].forEach((verb) => {
const regex = new RegExp(`route:${localRoute}:${verb.toLowerCase()}`),
chalkedVerb = chalk.green(verb),
chalkedRoute = chalk.green(localRoute);
if (!routeReadHolder.match(regex)){
// the route does not yet exist... stub it out
routeReadHolder += `\r\n\r\n${existingRoute({
routePath: localRoute,
routeVerb: verb
})}`;
fs.writeFileSync(targetPath, routeReadHolder);
console.log(`${chalkedFileName} updated with the ${chalkedRoute} handler for ${chalkedVerb} requests`);
}
});
} catch (err) {
routeReadHolder = routeTemplate();
routeTestHolder = routeTestHeader({ fileName: localRoute });
routes[route].forEach((verb) => {
routeReadHolder += `\r\n\r\n${existingRoute({
routePath: localRoute,
routeVerb: verb
})}`;
routeTestHolder += `\r\n\r\n${routeTest({
routePath: localRoute,
routeVerb: verb
})}`;
});
routeTestHolder += routeTestEOF();
fs.writeFileSync(targetPath, routeReadHolder);
fs.writeFileSync(targetTestPath, routeTestHolder);
console.log(`${chalkedFileName} created`);
}
});
} catch (err) {
console.log(err);
console.log('\n\nCouldn\'t find a routes.json to base your application on...');
}
} else {
console.log('\n\nWait a minute...');
console.log(` are you sure this is a ${chalk.cyan('monument')} project folder?`);
console.log(' Maybe you ran this from the wrong directory?');
}
};