enablement-build-template-generate 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +4 -0
  3. package/index.js +155 -0
  4. package/package.json +17 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Chris Doty
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,4 @@
1
+ # enablement-build-deploy-version
2
+ Gets package.json version and updates a deployment repo with the verison.
3
+
4
+
package/index.js ADDED
@@ -0,0 +1,155 @@
1
+ #!/usr/bin/env node
2
+ import * as url from 'url';
3
+ import path from 'path';
4
+ import { globby } from 'globby';
5
+ import { readFileSync, writeFileSync, existsSync } from "fs";
6
+
7
+ const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
8
+
9
+ function stripIt(ins) {
10
+ return ins.replace(/( |\r\n|\n|\r)/gm,"");
11
+ }
12
+
13
+ async function main(options) {
14
+ var configFile = path.join(__dirname, options.config);
15
+ const try1 = configFile;
16
+
17
+ if(!existsSync(configFile)) {
18
+ configFile = path.join(options.config);
19
+ }
20
+
21
+ if(!existsSync(configFile)) {
22
+ throw `enablement-build-template-generate: could not load config file ${configFile}, also tried ${try1}`;
23
+ }
24
+
25
+ const templatePath = path.dirname(configFile);
26
+
27
+ const config = JSON.parse(readFileSync(configFile,'utf8'));
28
+
29
+ const plist = [];
30
+
31
+ Object.keys(config).forEach((templateFilename)=>{
32
+
33
+ plist.push(new Promise(async (primaryResolve, reject) => {
34
+
35
+ const docTemplate = readFileSync(path.join(templatePath, templateFilename), "utf8");
36
+ const jlist = [];
37
+ Object.keys(config[templateFilename]).forEach((varname)=>{
38
+ const jobConfig = config[templateFilename][varname];
39
+ const job = new Promise(async (resolve, reject) => {
40
+ const jobTemplate = readFileSync(path.join(templatePath, jobConfig.template), "utf8");
41
+ const cwd = path.join(templatePath, jobConfig.rootPath);
42
+
43
+ globby(
44
+ [
45
+ `./**/package.json`,
46
+ `!**/node_modules`
47
+ ],
48
+ { cwd: cwd },
49
+ ).then((images)=>{
50
+ images.sort();
51
+ let jobs = [];
52
+ let exclude = jobConfig.exclude||[];
53
+ const allNames = [];
54
+
55
+ for (let i = 0; i < images.length; i++) {
56
+ let name = images[i].replace(/\/package\.json/g, '');
57
+ allNames.push(name);
58
+ if(name.startsWith("_")) continue;
59
+
60
+ const packageJson = JSON.parse(readFileSync(path.join(cwd,images[i]),'utf8'));
61
+ let fullName = packageJson.name;
62
+ let packageName = packageJson.name;
63
+ if(packageName.startsWith('@')) {
64
+ packageName = packageName.split("\/")[1];
65
+ }
66
+
67
+ let job = jobTemplate;
68
+ if(exclude.indexOf(name)>=0) continue;
69
+ let safe = name.replace(/-/g, '_');
70
+ job = job.replace(/{other}/g, `{other:${name}}`).replace(/{name}/g, name).replace(/{safe}/g, safe).replace(/{package}/g, packageName).replace(/{full}/g, fullName);
71
+ jobs.push(job);
72
+ }
73
+
74
+ jobs = jobs.join('\n');
75
+ for (let i = 0; i < allNames.length; i++) {
76
+ let cnames = allNames.slice();
77
+ cnames.splice(i,1);
78
+ cnames = cnames.join(`, ${cwd}/`);
79
+ let rex = `{other:${allNames[i]}}`;
80
+ rex=new RegExp(rex,'g');
81
+ jobs = jobs.replace(rex, `${cwd}/${cnames}`);
82
+ }
83
+
84
+ resolve({name:varname, text:jobs});
85
+ },reject);
86
+
87
+ });
88
+ jlist.push(job);
89
+ });
90
+
91
+ Promise.all(jlist).then((results)=>{
92
+ var output = docTemplate;
93
+
94
+ Object.keys(results).forEach((idx)=>{
95
+ const varname = results[idx].name;
96
+ let rex = new RegExp('\\{'+varname+'\\}',"g");
97
+ output = output.replace(rex, results[idx].text);
98
+ });
99
+
100
+ const line = "#######################################";
101
+ const donotedit = "### DO NOT EDIT - THIS IS GENERATED ###";
102
+ output = `${line}\n${donotedit}\n${line}\n\n${output}\n\n${line}\n${donotedit}\n${line}`;
103
+
104
+ if(stripIt(output)!==docTemplate) {
105
+ const outFile = path.join(templatePath, `../${templateFilename}`);
106
+ writeFileSync(path.join(templatePath, `../${templateFilename}`), output, "utf8");
107
+ }
108
+
109
+ primaryResolve(`generated template ${templateFilename}`);
110
+ },reject);
111
+ }));
112
+ })
113
+
114
+ return Promise.all(plist);
115
+ }
116
+
117
+ let options = {
118
+ config: "./.cicd/templates/templated.json",
119
+ debug: false,
120
+ push: true
121
+ }
122
+
123
+ let argv = process.argv;
124
+
125
+ for (let i = 2; i < argv.length; i++) {
126
+ if (argv[i] === "--debug") { options.debug = true; continue; }
127
+ if (argv[i].substring(0, 2) === "--") {
128
+ let name = argv[i].substring(2);
129
+ if (options[name] !== undefined) {
130
+ options[name] = argv[i + 1];
131
+ i++;
132
+ } else {
133
+ console.error('Expected a known option');
134
+ process.exit(1);
135
+ }
136
+ }
137
+ }
138
+
139
+ (async () => {
140
+ try {
141
+ console.log('\x1b[32m%s\x1b[0m', "Running enablement-build-template-generate with options:\n", JSON.stringify(options, null, 2));
142
+ main(options).then((results) => {
143
+ console.log(results.join('\n'));
144
+ if (options.debug) console.log("DONE");
145
+ process.exit(0);
146
+ }, (e) => {
147
+ console.log('\x1b[32m%s\x1b[0m',e);
148
+ if (options.debug) console.log("ERROR");
149
+ process.exit(1);
150
+ })
151
+ } catch (e) {
152
+ console.log('\x1b[32m%s\x1b[0m',e);
153
+ process.exit(1);
154
+ }
155
+ })();
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "enablement-build-template-generate",
3
+ "version": "1.0.0",
4
+ "description": "Generates build yaml for mono repos",
5
+ "type": "module",
6
+ "module": "./index.js",
7
+ "main": "./index.js",
8
+ "bin": "./index.js",
9
+ "dependencies": {
10
+ "globby": "^14.0.1"
11
+ },
12
+ "scripts": {
13
+ "start": "node index.js"
14
+ },
15
+ "license": "MIT",
16
+ "author": "Chris Doty"
17
+ }