-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·475 lines (410 loc) · 13.1 KB
/
index.ts
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#!/usr/bin/env node
import simpleGit from 'simple-git';
import axios, { AxiosError } from 'axios';
import { parse } from 'uri-js';
import { existsSync, realpathSync, readFileSync, mkdirSync, writeFileSync } from 'fs';
import { spawn } from 'child_process';
import yargs from 'yargs';
import { dump } from 'js-yaml';
import PQueue from 'p-queue';
import { join, sep } from 'path';
import { tmpdir } from 'os';
import { moveSync } from 'fs-extra';
const MAX_RETRIES = 120;
const WAIT_FOR = 5000; // milliseconds
const VERSION_FILE = 'version.json';
const VERSIONS_FILE = '.openapis';
const { DNT } = process.env;
type CompileOpts = { prepareCommand?: string; command: string; compiledDirectory: string };
type FrameworkAliases = 'angular' | 'axios' | 'axios-js';
type FrameworkMap = {
[key in FrameworkAliases]: { generator: string; properties: string[]; compile?: CompileOpts };
};
const frameworks: FrameworkMap = {
angular: {
generator: 'typescript-angular',
properties: ['-p apiModulePrefix={serviceNamePascalCase}'],
},
axios: {
generator: 'typescript-axios',
properties: ['-p modelNamePrefix={serviceNamePascalCase}'],
},
'axios-js': {
generator: 'typescript-axios',
properties: ['-p modelNamePrefix={serviceNamePascalCase} -p npmName=api'],
compile: {
prepareCommand: 'npm install',
command: 'npm run build',
compiledDirectory: 'dist',
},
},
};
const pascalCase = (str: string) => {
return str
.replace(/(\w)(\w*)/g, (_g0: string, g1: string, g2: string) => {
return g1.toUpperCase() + g2.toLowerCase();
})
.replace(/[_-]/g, '');
};
const repoInfo = async () => {
const log = await simpleGit().log({ maxCount: 1 });
if (!log.latest) {
throw new Error('Unable to fetch git log');
}
const sha = log.latest.hash;
const remotes = await simpleGit().getRemotes(true);
const origin = remotes.find((remote) => remote.name === 'origin');
if (!origin) {
throw new Error("Unable to find remote with name 'origin'");
}
const { path: pushPath } = parse(origin.refs.push);
if (!pushPath) {
throw new Error(`Unable to extract path from ${origin.refs.push}`);
}
const organization = pushPath.split('/')[pushPath.startsWith('/') ? 1 : 0];
if (!organization) {
throw new Error(`Unable to extract organization from ${pushPath}`);
}
let repo = pushPath.split('/')[pushPath.startsWith('/') ? 2 : 1];
if (!repo) {
throw new Error(`Unable to extract repo from ${pushPath}`);
}
if (repo.endsWith('.git')) {
repo = repo.replace(/(.+)(.git)$/gm, '$1');
}
const info = { organization, repo, sha };
return info;
};
const exec = (command: string, cwd?: string) => {
return new Promise((resolve, reject) => {
let stdout = '';
let stderr = '';
console.log(`Running Command: ${command}`);
const parts = command.split(' ');
const p = spawn(parts[0], parts.slice(1), {
shell: true,
cwd,
env: {
...process.env,
},
});
p.on('error', (err) => {
reject(err);
});
p.on('exit', (code) => {
if (code === 0) {
resolve({
stdout,
stderr,
});
return;
}
reject(new Error(`Command '${command}' exited with code ${code}`));
});
p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stdout); // Pipe stderr to stdout too
p.stdout.on('data', (chunk) => {
stdout = `${stdout}${chunk}`;
});
p.stderr.on('data', (chunk) => {
stderr = `${stderr}${chunk}`;
});
});
};
const event = (org: string, repo: string, action: string) => {
if (DNT) {
return;
}
axios
.post(
`https://api.segment.io/v1/track`,
{
userId: org,
event: `generate-${action}`,
properties: { script: 'openapi-generator' },
context: { repo },
},
{ auth: { username: 'RvjEAi2NrzWFz3SL0bNwh5yVwrwWr0GA', password: '' } },
)
.then(() => {})
.catch((error: AxiosError) => {
console.error('Event Log Error', error);
});
};
type Service = {
openapiUrl: string;
serviceName: string;
serviceNamePascalCase: string;
outputDirectory: string;
required: boolean;
};
const fetchServiceMap = async (
inputDirectory: string,
outputDirectory: string,
required: string[] = [],
) => {
if (inputDirectory.indexOf('/$NODE_ENV') !== -1) {
inputDirectory = inputDirectory.replace('/$NODE_ENV', `${sep}${process.env.NODE_ENV || ''}`);
}
if (!existsSync(inputDirectory)) {
throw new Error(`Missing directory: ${inputDirectory}`);
}
const inDir = realpathSync(inputDirectory);
const servicesFile = join(inDir, 'services.json');
const envVarsFile = join(inDir, 'env-vars.json');
if (!existsSync(servicesFile)) {
throw new Error(`Missing file: ${servicesFile}`);
}
if (!existsSync(envVarsFile)) {
throw new Error(`Missing file: ${envVarsFile}`);
}
console.log(`Using services.json: ${servicesFile}`);
console.log(`Using env-vars.json: ${envVarsFile}`);
const services = JSON.parse(readFileSync(servicesFile).toString());
const envVars = JSON.parse(readFileSync(envVarsFile).toString());
const serviceMap = Object.entries(services).reduce((acc, [key, value]) => {
const key$ = key as string;
const value$ = value as Record<string, string>;
const baseUrl = value$.base_url || value$['base-url'];
const serviceName = value$.service_name || value$['service-name'];
if (envVars.SERVICE_NAME === serviceName || envVars['service-name'] === serviceName) {
console.log(`Skipping ${serviceName}, that's this project!`);
return acc;
}
const openapiUrl = `${baseUrl}/openapi.json`;
const serviceNamePascalCase = pascalCase(serviceName);
const outDir = join(outputDirectory, serviceName);
console.log(`Discovered ${serviceName} service (${openapiUrl})`);
acc[key$] = {
openapiUrl,
serviceName,
serviceNamePascalCase,
outputDirectory: outDir,
required: required.find((r) => r === '+all' || r.toLowerCase() === serviceName)
? true
: false,
};
return acc;
}, {} as { [key: string]: Service });
return serviceMap;
};
const openUrl = (url: string, required = false, ttl = MAX_RETRIES) => {
return new Promise((resolve, reject) => {
ttl = ttl - 1;
if (ttl <= 0) {
reject(new Error(`Exceeded maximum retries after ${MAX_RETRIES - ttl} attempts`));
return;
}
axios
.get(url, {
validateStatus: (status) => status >= 200 && status < 500,
})
.then(({ status, data }) => {
if (status < 300 && data && data.components) {
resolve(data);
return;
}
if (!required) {
reject(new Error(`Not found, status was: ${status}`));
return;
}
console.log(`[Attempt ${MAX_RETRIES - ttl}] Retrying! Status was ${status}: ${url}`);
setTimeout(() => {
openUrl(url, required, ttl).then((data2) => {
resolve(data2);
});
}, WAIT_FOR);
return;
})
.catch((e) => {
reject(e);
return;
});
});
};
type OpenApiDoc = {
info?: {
version?: string;
};
};
type OpenApiVersion = {
match: boolean;
old?: string;
new?: string;
};
const checkVersion = (
serviceName: string,
openApi: OpenApiDoc,
outputDirectory: string,
): OpenApiVersion => {
const ret: OpenApiVersion = { match: false };
if (!openApi || !openApi.info || !openApi.info.version) {
console.log(`Unable to determine version for ${serviceName}: Missing metadata from OpenAPI`);
return ret;
}
const { info } = openApi;
const { version: openApiVersion } = info;
if (openApiVersion) {
ret.new = openApiVersion;
}
if (!existsSync(outputDirectory)) {
console.log(`Unable to determine version for ${serviceName}: Output directory does not exist`);
return ret;
}
try {
const { version } = JSON.parse(
readFileSync(join(realpathSync(outputDirectory), VERSION_FILE)).toString(),
);
if (version) {
ret.old = version;
}
} catch (e) {
console.log(`Unable to determine version for ${serviceName}: Unable to read version file`);
}
if (ret.old && ret.new && ret.old === ret.new) {
ret.match = true;
return ret;
} else {
console.log(`Updating ${serviceName} from ${ret.old} to ${ret.new}`);
}
return ret;
};
const generateApi = async (generatorAlias: FrameworkAliases, service: Service, force = false) => {
const { generator, properties, compile } = frameworks[generatorAlias];
let version = null;
try {
const openApi = (await openUrl(service.openapiUrl, service.required)) as OpenApiDoc;
const { match: versionMatch, new: newVersion } = checkVersion(
service.serviceName,
openApi,
service.outputDirectory,
);
if (versionMatch && !force) {
console.log(
`Skipping ${service.serviceName}: Version (${newVersion}) from OpenAPI and local matches (use -f to force regeneration)`,
);
return { serviceName: service.serviceName, version: newVersion };
} else {
version = newVersion;
}
} catch (e: any) {
console.log(`Skipping ${service.serviceName} using ${service.openapiUrl}: ${e.message}`);
return { serviceName: service.serviceName, version };
}
mkdirSync(service.outputDirectory, { recursive: true });
const commands = [`npx @openapitools/openapi-generator-cli`];
commands.push('generate');
commands.push(`-g ${generator}`);
commands.push(`-i ${service.openapiUrl}`);
const tempDir = join(tmpdir(), service.serviceNamePascalCase);
let outDir = service.outputDirectory;
if (compile) {
outDir = tempDir;
}
commands.push(`-o ${outDir}`);
commands.push(
...properties.map((p) => {
return p.replace(`{serviceNamePascalCase}`, service.serviceNamePascalCase);
}),
);
try {
await exec(commands.join(' '));
console.log(`Generated library for ${service.serviceName} at ${outDir}`);
if (compile) {
if (compile.prepareCommand) {
await exec(compile.prepareCommand, outDir);
}
await exec(compile.command, outDir);
const compileDir = join(outDir, compile.compiledDirectory);
moveSync(compileDir, service.outputDirectory, { overwrite: true });
console.log(`Compiled library for ${service.serviceName} to ${service.outputDirectory}`);
}
if (version) {
writeFileSync(
join(realpathSync(service.outputDirectory), VERSION_FILE),
JSON.stringify({ version }),
);
}
} catch (e) {
console.log(`Error generating: `, e);
}
return { serviceName: service.serviceName, version };
};
const run = async (
generator: FrameworkAliases,
inputDirectory: string,
outputDirectory: string,
required: string[],
force: boolean,
) => {
try {
const { organization, repo } = await repoInfo();
event(organization, repo, generator);
} catch (e: any) {
console.warn('Unable to get repo info', e.message);
}
if (!Object.keys(frameworks).includes(generator)) {
throw new Error(`Unknown generator: ${generator}`);
}
const serviceMap = await fetchServiceMap(inputDirectory, outputDirectory, required);
const promises = Object.values(serviceMap).map((properties) => {
return async () => generateApi(generator, properties, force);
});
const queue = new PQueue({ concurrency: 1 });
const versions = await queue.addAll(promises);
mkdirSync(outputDirectory, { recursive: true });
const yamlStr = dump(versions);
let realPath = VERSIONS_FILE;
try {
realPath = realpathSync(realPath);
} catch (e) {
//pass
}
writeFileSync(
realPath,
`
# Do not edit this file, it is managed by @scaffoldly/openapi-generator
#
# This file assists caching of auto-generated APIs in \`${outputDirectory}\` during builds
#
# This file is *safe* to add to source control and will increase the speed of builds
---
${yamlStr}
`,
);
};
(async () => {
try {
const argv = await yargs(process.argv.slice(2))
.usage('Usage: $0 [options]')
.describe('g', `Generator, one of: [${Object.keys(frameworks)}]`)
.default('g', 'axios')
.describe('i', `Input directory`)
.default('i', '.scaffoldly')
.describe('o', `Output directory`)
.describe('f', 'Force generation (exclude version checks)')
.boolean('f')
.default('f', false)
.describe(
'r',
"Require a response from these services(s), use '+all' to require all services",
)
.array('r')
.example(
'$0 -g angular -o src/app/openapi -r +all',
'Generate Angular client libraries into src/app/openapi/{service-name}. Retries until all services are loaded',
)
.example(
'$0 -g axios -o src/app/openapi -r auth -r foo',
'Generate Axios client libraries into src/app/openapi/{service-name}. Retries until auth and foo are loaded',
)
.example(
'$0 -g angular -o src/app/openapi',
'Generate Angular client libraries into src/app/openapi/{service-name}. No retries',
)
.demandOption(['g', 'o']).argv;
await run(argv.g as FrameworkAliases, argv.i, argv.o as string, argv.r as string[], argv.f);
} catch (e) {
console.error(e);
}
})();