-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVersion.js
374 lines (304 loc) · 10.7 KB
/
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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env node
/*****
* Semantic Versioning - and `package.json` Archiving Utility
* =====================
*
* The following Utility will archive `package.json` file(s), bump a VERSION lock file(s), propagate
* the changed version to the target `package.json` file(s), create or overwrite a `Version.js` file
* inside of `[src]` || `[source]` w/updated version(s), and most importantly, if found inside a
* nested Node JS project or inside of a git submodule, *recursively* perform all operations throughout
* each package.
*
* Setup & Usage
* - Instantiate a `VERSION` File
* >>> ./Version.js
*
* - Increment the Major `VERSION` & `package.json` Version(s)
* >>> ./Version.js --Increment --Major --Write
*
* - Increment the Minor `VERSION` & `package.json` Version(s)
* >>> ./Version.js --Increment --Minor --Write
*
* - Increment the Patch `VERSION` & `package.json` Version(s)
* >>> ./Version.js --Increment --Patch --Write
*
* Packaged Commands
* -----------------
* >>> "scripts": {
* >>> "patch": "node Version.js --Increment --Write --Patch",
* >>> "minor": "node Version.js --Increment --Write --Minor",
* >>> "major": "node Version.js --Increment --Write --Major"
* >>> }
*
* @returns Signal(0)
*/
const Version = {};
const Datetime = new Date();
// --> Standard-Library
const Runtime = require("process");
const Path = require("path");
const FS = require("fs");
const CLI = Runtime.argv;
const Parameters = CLI.splice(2);
const Entry = Runtime.env.npm_package_json;
const Throw = {
Trigger: false
};
(Entry !== undefined && FS.existsSync(Entry) && (
FS.statSync(Entry).isFile() || FS.statSync(Path.join(Entry, "package.json"))
.isFile())
) ? console.info("[DEBUG]",
"➜" + " " + "Package" + ":", Entry
) : Throw.Trigger = true;
if ( Throw.Trigger === true ) {
console.error("[ERROR]",
"➜" + " " + "Message" + ":", "Version.js must be interfaced via NPM",
"\n", " >>> \"scripts\": {",
"\n", " --------- ... \"patch\": \"node Version.js --Increment --Write --Patch\"",
"\n", " Example ... \"minor\": \"node Version.js --Increment --Write --Minor\"",
"\n", " --------- ... \"major\": \"node Version.js --Increment --Write --Major\"",
"\n", " ... }"
);
Runtime.exit(128);
}
const UID = require("crypto").randomBytes(256 / 8)
.toString("hex");
const Random = () => require("crypto").randomBytes(256 / 8)
.toString("hex");
const Directory = __dirname;
const Handler = FS.readFileSync(Entry);
const CWD = Runtime.cwd();
console.info("[DEBUG]",
"➜" + " " + "CWD" + ":", CWD
);
// Runtime.chdir(CWD);
console.info(
"[DEBUG]",
"➜" + " " + "Working Directory" + ":" + " " + Runtime.cwd()
);
const Data = JSON.parse(Handler);
const Today = Datetime.toISOString(Datetime.getTime()).slice(0, 10);
console.info(
"[DEBUG]",
"➜" + " " + "Date" + ":" + " " + Today
);
const Archive = Path.join(Directory, ".version");
const Folder = Path.join(Archive, Today);
const File = Path.join(Directory, "VERSION");
const Exportables = [];
const Source = () => {
let target;
target = Path.dirname(Entry) + Path.sep + "Source";
(FS.existsSync(target) && FS.lstatSync(target).isDirectory()) ?
Exportables.push([ target, Path.normalize(target) ]) : null;
target = Path.dirname(Entry) + Path.sep + "src";
(FS.existsSync(target) && FS.lstatSync(target).isDirectory()) ?
Exportables.push([ target, Path.normalize(target) ]) : null;
};
Source();
const Tree = {
Root: null,
Modules: []
};
/**
* Finds the pathname of the parent module's package descriptor file. If the
* directory is undefined (the default case), then it is set to the directory
* name of the parent module's filename. If no package.json file is found, then
* the parent directories are recursively searched until the file is found or
* the root directory is reached. Returns the pathname if found or null if not.
*/
function Package(directory) {
if ( !directory ) directory = Path.dirname(__dirname + Path.sep + "CLI");
var file = Path.resolve(directory, "package.json");
var module = Path.resolve(directory, "Module.js");
const Modular = FS.existsSync(module) && FS.statSync(module).isFile();
if ( FS.existsSync(file) && FS.statSync(file).isFile() || Modular ) {
Tree.Modules.push(directory);
const Target = (Modular) ? Path.join(Path.dirname(module), "..")
: Path.join(Path.dirname(file), "..");
return Package(Target);
}
var parent = Path.resolve(directory, "..");
if ( parent === directory ) return CWD;
return Package(parent);
}
Tree.Root = Package();
console.info(
"[DEBUG]",
"➜" + " " + "Package" + ":" + " " + Tree.Root
);
console.info(
"[DEBUG]",
"➜" + " " + "Modules" + ":" + " " + JSON.stringify(Tree.Modules, null, 4)
);
const Generation = {};
Tree.Modules.forEach((Element, Index) => {
const Directory = String(Element);
const Keys = Directory.split(Path.sep);
const Length = Keys.length;
const Key = Keys[Length - 1];
const Version = Path.join(Directory, "VERSION");
const Archive = Path.join(Directory, ".version");
const Target = Path.join(Archive, Today);
Generation[Key] = {};
if ( !(FS.existsSync(Path.join(Directory, "package.json"))) || FS.existsSync(Path.join(Directory, "Module.js")) ) {
const Components = Directory.split(Path.sep);
const Length = Components.length;
const Package = Components[Length - 1];
FS.writeFileSync(
Path.join(Directory, "package.json"),
JSON.stringify(
{
"name": Package,
"version": "0.0.0",
"private": true,
"author": "Jacob B. Sanders",
"license": "Proprietary",
"main": "./Module.js",
"description": "Module Path",
"scripts": {},
"dependencies": {},
"devDependencies": {},
"workspaces": [
"./*"
]
}, null, 4
)
);
}
const Buffer = FS.readFileSync(
Path.join(Directory, "package.json")
);
Generation[Key].Data = JSON.parse(Buffer);
// Generation[Key].Version = ...
if ( !FS.existsSync(Path.normalize(Version)) ) {
FS.writeFileSync(
Path.join(Version),
"0.0.0"
);
}
// Generation[Key].Date = ...
if ( !FS.existsSync(Path.normalize(Target)) ) {
FS.mkdirSync(Target);
}
const File = Path.join(Target, Random());
FS.writeFileSync(
File + "." + "JSON",
JSON.stringify(
Generation[Key].Data, null, 4
)
);
FS.writeFileSync(
Path.join(Version),
Generation[Key].Data.version
);
console.debug("[DEBUG] ➜ Successfully Created & Wrote to Archive", "(" + Path.normalize(Target) + ")");
Generation[Key].FS = {
Archive: Path.normalize(Archive),
Output: Path.normalize(Target),
File: File
};
});
if ( Parameters.includes("--Increment") || Parameters.includes("--increment") ) {
Version.Current = {};
Version.Target = {};
const Current = Data.version.split(".");
Version.Current.Major = Current[0];
Version.Current.Minor = Current[1];
Version.Current.Patch = Current[2];
const Major = () => {
Version.Target.Major = String(
Number(Version.Current.Major) + 1
);
Version.Target.Minor = String(Version.Current.Minor);
Version.Target.Patch = String(Version.Current.Patch);
};
const Minor = () => {
Version.Target.Major = String(Version.Current.Major);
Version.Target.Minor = String(
Number(Version.Current.Minor) + 1
);
Version.Target.Patch = String(Version.Current.Patch);
};
const Patch = () => {
Version.Target.Major = String(Version.Current.Major);
Version.Target.Minor = String(Version.Current.Minor);
Version.Target.Patch = String(
Number(Version.Current.Patch) + 1
);
};
(Parameters.includes("--Major") || Parameters.includes("--major"))
? Major() : console.debug("[DEBUG]"
+ " ➜ Skipping Major Incrementor");
(Parameters.includes("--Minor") || Parameters.includes("--minor"))
? Minor() : console.debug("[DEBUG]"
+ " ➜ Skipping Minor Incrementor");
(Parameters.includes("--Patch") || Parameters.includes("--patch"))
? Patch() : console.debug("[DEBUG]"
+ " ➜ Skipping Patch Incrementor");
console.debug(
"[DEBUG] ➜ Version "
+ String(Version.Current.Major)
+ "."
+ String(Version.Current.Minor)
+ "."
+ String(Version.Current.Patch)
+ " >>> "
+ String(Version.Target.Major)
+ "."
+ String(Version.Target.Minor)
+ "."
+ String(Version.Target.Patch)
);
}
Parameters.forEach((Value, Index) => {
switch ( Value ) {
case "--Write": {
Data.version = String(
String(Version.Target.Major)
+ "."
+ String(Version.Target.Minor)
+ "."
+ String(Version.Target.Patch)
);
FS.writeFileSync(Entry, JSON.stringify(Data, null, 4));
console.debug("[DEBUG] ➜ Successfully Wrote to Package Index");
FS.writeFileSync(File, Data.version);
console.debug("[DEBUG] ➜ Successfully Incremented VERSION Lock");
Exportables.forEach(
(Exportable, Index, _) => {
console.info("[DEBUG] ➜ Writing Version.js" + ":" + " " + Exportable[1]);
FS.writeFileSync(Path.join(Exportable[1], "Version.js"), "const Version = " + "\""
+ String(Data.version) + "\"" + ";\n" + "\n"
+ "export default Version;" + "\n"
);
}
);
console.debug("[DEBUG] ➜ Successfully Wrote Version.js File(s)");
Runtime.exit(0);
}
break;
case "--Archive":
Parameters.includes("--Archive") ?
FS.writeFileSync(
Folder + Path.sep + UID.toUpperCase() + ".JSON",
JSON.stringify(Data, null, 4)
) : console.info();
break;
default:
break;
}
});
module.exports.default = {
Tree: Tree,
Parent: Tree.Root,
Modules: Tree.Modules,
Parameters: Parameters,
Version: Generation
};
console.info(
"[DEBUG]",
"➜" + " " + "Exports" + ":" + " " + JSON.stringify(
module.exports.default, null, 4
)
);