-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove-version.js
150 lines (129 loc) · 4.67 KB
/
remove-version.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
/* eslint-disable no-console */
const path = require('path');
const fs = require('fs');
const { program } = require('commander');
// eslint-disable-next-line no-promise-executor-return
const delay = (time) => new Promise((resolve) => setTimeout(resolve, time));
program
.name('')
.requiredOption('-t, --target <version to remove>', 'a version to remove is required');
program.parse();
const options = program.opts();
const friendlyLog = async (logMessage) => {
console.log(' ');
console.log(logMessage);
await delay(200);
};
const assertFolder = (fullPath, exists) => {
if (fs.existsSync(fullPath)) {
if (exists) {
console.log('directory exists!');
} else {
console.log('directory existence unexpected');
console.log('\x1b[41m', `error: ${fullPath} exists but should not ... exiting`);
process.exit(1);
}
} else if (exists) {
console.log('directory not found.');
console.log('\x1b[41m', `error: ${fullPath} not found ... exiting`);
process.exit(1);
} else {
console.log('directory not found!');
}
};
const assertFile = (fullPath, exists) => {
if (fs.existsSync(fullPath)) {
if (exists) {
console.log('file exists!');
} else {
console.log('file existence unexpected');
console.log('\x1b[41m', `error: ${fullPath} exists but should not ... exiting`);
process.exit(1);
}
} else if (exists) {
console.log('file not found.');
console.log('\x1b[41m', `error: ${fullPath} not found ... exiting`);
process.exit(1);
} else {
console.log('file not found!');
}
};
const verifyFolders = (newVersion, exists) => {
// controller folder
console.log(`searching for controller folder controllers/${newVersion}`);
const targetControllerFolder = path.join(__dirname, 'controllers', newVersion);
assertFolder(targetControllerFolder, exists);
// views folder
console.log(`searching for view folder views/${newVersion}`);
const targetViewsFolder = path.join(__dirname, 'views', newVersion);
assertFolder(targetViewsFolder, exists);
};
const verifyFiles = (newVersion, exists) => {
// routes file
console.log(`searching for routes file routes/${newVersion}.js`);
const routesFileFullPath = path.join(__dirname, 'routes', `${newVersion}.js`);
console.log(routesFileFullPath);
assertFile(routesFileFullPath, exists);
// app.js file
console.log('searching for file app.js');
const appjsFullPath = path.join(__dirname, 'app.js');
console.log(appjsFullPath);
assertFile(appjsFullPath, exists);
};
const removeLine = (filePath, lineToRemove) => {
fs.readFile(filePath, 'utf8', (err, data) => {
const newData = data.split('\n');
if (err) return console.log(err);
if (newData.indexOf(lineToRemove) !== -1) {
newData.splice(newData.indexOf(lineToRemove), 1);
}
fs.writeFile(filePath, newData.join('\n'), 'utf8', (writeErr) => {
if (writeErr) return console.log(writeErr);
return 0;
});
return 0;
});
};
const removeLines = (filePath, startLine, endLine) => {
fs.readFile(filePath, 'utf8', (err, data) => {
const newData = data.split('\n');
if (err) return console.log(err);
const startIndex = newData.indexOf(startLine);
const endIndex = newData.indexOf(endLine);
if ((startIndex !== -1) && (endIndex !== -1)) {
newData.splice(
startIndex,
endIndex - startIndex + 2,
);
}
fs.writeFile(filePath, newData.join('\n'), 'utf8', (writeErr) => {
if (writeErr) return console.log(writeErr);
return 0;
});
return 0;
});
};
const run = async () => {
await friendlyLog(`Removing prototype version ${options.target}`);
await friendlyLog('Beginning pre-requisite checks');
verifyFolders(options.target, true);
verifyFiles(options.target, true);
await friendlyLog(`Pre-requisite checks complete .... ${options.target} -> X`);
await friendlyLog('Removing folders');
fs.rmSync(path.join(__dirname, 'controllers', options.target), { recursive: true });
fs.rmSync(path.join(__dirname, 'views', options.target), { recursive: true });
await friendlyLog('Removing files');
fs.rmSync(path.join(__dirname, 'routes', `${options.target}.js`));
await friendlyLog('Removing route from app.js');
removeLine(
path.join(__dirname, 'app.js'),
`app.use('/${options.target}/', setVersionMiddleware('/${options.target}/'), require('./routes/${options.target}'));`,
);
await friendlyLog('Removing start button from homepage');
removeLines(
path.join(__dirname, 'views', 'index.njk'),
` {# [${options.target}-start] (do not delete this comment it is used in automation) #}`,
` {# [${options.target}-end] (do not delete this comment it is used in automation) #}`,
);
};
run();