@accelbyte/codegen 2.0.1 → 2.1.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.
|
@@ -49,9 +49,6 @@ class CliParser {
|
|
|
49
49
|
static getResolvedSwaggersOutputPath = () => {
|
|
50
50
|
return path.resolve(CliParser.getSwaggersOutputPath());
|
|
51
51
|
};
|
|
52
|
-
static isAdmin = () => {
|
|
53
|
-
return CliParser.instance().argv.admin;
|
|
54
|
-
};
|
|
55
52
|
static getSnippetOutputPath = () => {
|
|
56
53
|
return CliParser.instance().argv.snippetOutput;
|
|
57
54
|
};
|
|
@@ -80,7 +77,9 @@ const generateImports = (body, importStatements, makeNewImportVarMap2, getImport
|
|
|
80
77
|
}
|
|
81
78
|
}
|
|
82
79
|
}
|
|
83
|
-
const generatedImports = Object.keys(usedImportVarMap).sort().map((moduleSource) =>
|
|
80
|
+
const generatedImports = Object.keys(usedImportVarMap).sort().map((moduleSource) => {
|
|
81
|
+
return `import { ${usedImportVarMap[moduleSource].sort().join(", ")} } from '${moduleSource}'`;
|
|
82
|
+
}).join("\n");
|
|
84
83
|
return `${generatedImports}
|
|
85
84
|
${importStatements.sort().join("\n")}`;
|
|
86
85
|
};
|
|
@@ -192,8 +191,6 @@ class VersionHelpers {
|
|
|
192
191
|
};
|
|
193
192
|
}
|
|
194
193
|
|
|
195
|
-
const codegenPackageJsonPath = path__default.join(__dirname, "../package.json");
|
|
196
|
-
JSON.parse(fs__default.readFileSync(codegenPackageJsonPath, "utf-8"));
|
|
197
194
|
const UNDEFINED_SWAGGER_SEMVER = "0.0.0";
|
|
198
195
|
const REMOVED_KEYWORDS = [
|
|
199
196
|
"/admin/",
|
|
@@ -289,7 +286,7 @@ class ParserUtils {
|
|
|
289
286
|
return null;
|
|
290
287
|
}
|
|
291
288
|
const type = ParserUtils.parseRefType($ref);
|
|
292
|
-
return `import { ${type} } from '
|
|
289
|
+
return `import { ${type} } from '../../generated-definitions/${type}.js'`;
|
|
293
290
|
};
|
|
294
291
|
static parseRefType = ($ref) => {
|
|
295
292
|
let ref = $ref.replace(".", "/");
|
|
@@ -481,7 +478,7 @@ class ParserUtils {
|
|
|
481
478
|
static writeApiFile(distDir, apiName, apiBuffer, imports, returnMethods) {
|
|
482
479
|
const newImports = [];
|
|
483
480
|
imports.forEach((el, index) => {
|
|
484
|
-
newImports.push(el.replace("
|
|
481
|
+
newImports.push(el.replace("../../generated-definitions", "../generated-definitions"));
|
|
485
482
|
});
|
|
486
483
|
const fileContent = templateApiClass(apiName, apiBuffer, newImports, returnMethods);
|
|
487
484
|
fs__default.writeFileSync(`${distDir}/${apiName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
@@ -499,9 +496,9 @@ class ParserUtils {
|
|
|
499
496
|
ParserUtils.mkdirIfNotExist(distDir);
|
|
500
497
|
fs__default.writeFileSync(path__default.join(distDir, `${name}.ts`), ParserUtils.prependCopyrightHeader(buffer));
|
|
501
498
|
}
|
|
502
|
-
static writeAllImportsFile(distDir, buffer
|
|
499
|
+
static writeAllImportsFile(distDir, buffer) {
|
|
503
500
|
ParserUtils.mkdirIfNotExist(distDir);
|
|
504
|
-
fs__default.writeFileSync(path__default.join(distDir,
|
|
501
|
+
fs__default.writeFileSync(path__default.join(distDir, "all-imports.ts"), ParserUtils.prependCopyrightHeader(buffer));
|
|
505
502
|
}
|
|
506
503
|
static syncPackageVersion(apiInfo, skipVersionSync, prereleaseId) {
|
|
507
504
|
if (skipVersionSync)
|
|
@@ -920,8 +917,9 @@ const templateApiIndex = (serviceName, serviceNameTitle, apiList) => {
|
|
|
920
917
|
let imports = "";
|
|
921
918
|
let returnStatement = "";
|
|
922
919
|
for (const cl of apiList) {
|
|
920
|
+
const dir = cl.toLowerCase().includes("admin") ? "generated-admin" : "generated-public";
|
|
923
921
|
imports += `
|
|
924
|
-
import { ${cl} } from './${
|
|
922
|
+
import { ${cl} } from './${dir}/${cl}.js'`;
|
|
925
923
|
returnStatement += `
|
|
926
924
|
${cl}, `;
|
|
927
925
|
}
|
|
@@ -1322,7 +1320,7 @@ class SwaggerReaderHelpers {
|
|
|
1322
1320
|
tagToClassImportsRecord[className] = tagToClassImportsRecord[className] ? tagToClassImportsRecord[className] : {};
|
|
1323
1321
|
if (responseClass) {
|
|
1324
1322
|
const importTypeClass = ParserUtils.parseRefType(responseClass);
|
|
1325
|
-
tagToClassImportsRecord[className][importTypeClass] = `import { ${importTypeClass} } from '
|
|
1323
|
+
tagToClassImportsRecord[className][importTypeClass] = `import { ${importTypeClass} } from '../../generated-definitions/${importTypeClass}.js'`;
|
|
1326
1324
|
}
|
|
1327
1325
|
if (responseClass && responseClass.endsWith("Array")) {
|
|
1328
1326
|
arrayDefinitions.push(responseClass);
|
|
@@ -1387,39 +1385,30 @@ class SwaggerReaderHelpers {
|
|
|
1387
1385
|
}
|
|
1388
1386
|
|
|
1389
1387
|
class CodeGenerator {
|
|
1390
|
-
static
|
|
1391
|
-
static
|
|
1392
|
-
static getGeneratedAdminFolder = () => `${CliParser.getOutputPath()}/generated-admin`;
|
|
1388
|
+
static srcFolder = () => CliParser.getOutputPath();
|
|
1389
|
+
static getGeneratedFolder = (isAdmin) => isAdmin ? `${CodeGenerator.srcFolder()}/generated-admin` : `${CodeGenerator.srcFolder()}/generated-public`;
|
|
1393
1390
|
static getGeneratedSnippetsFolder = () => `${CliParser.getSnippetOutputPath()}/generated-snippets`;
|
|
1394
|
-
static
|
|
1391
|
+
static prepareDirs = (DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS) => {
|
|
1392
|
+
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
1393
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR(true));
|
|
1394
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR(false));
|
|
1395
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(true));
|
|
1396
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(false));
|
|
1397
|
+
};
|
|
1395
1398
|
static main = async (nameArray) => {
|
|
1396
1399
|
const serviceName = nameArray[0];
|
|
1397
1400
|
const sdkName = nameArray[1];
|
|
1398
1401
|
const swaggerFile = nameArray[2];
|
|
1399
1402
|
const parser = new SwaggerParser();
|
|
1400
|
-
const generatedFolder = CliParser.isAdmin() ? CodeGenerator.getGeneratedAdminFolder() : CodeGenerator.getGeneratedPublicFolder();
|
|
1401
|
-
const DIST_DIR = `${generatedFolder}/${serviceName}`;
|
|
1402
|
-
const DIST_ENDPOINTS_DIR = path__default.join(DIST_DIR, "endpoints");
|
|
1403
|
-
const DIST_DEFINITION_DIR = path__default.join(DIST_DIR, "definitions");
|
|
1404
1403
|
const swaggerFilePath = `${CliParser.getSwaggersOutputPath()}/${swaggerFile}`;
|
|
1405
1404
|
const api = await parser.parse(swaggerFilePath);
|
|
1405
|
+
const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
|
|
1406
1406
|
const indexImportsSet = /* @__PURE__ */ new Set();
|
|
1407
1407
|
const apiInfo = { ...api.info, "x-version": api["x-version"]?.version };
|
|
1408
1408
|
console.log("----------\nGenerating API:", { title: apiInfo.title, version: apiInfo.version });
|
|
1409
|
-
ParserUtils.mkdirIfNotExist(DIST_DIR);
|
|
1410
|
-
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
1411
|
-
ParserUtils.mkdirIfNotExist(DIST_ENDPOINTS_DIR);
|
|
1412
1409
|
ParserUtils.syncPackageVersion(apiInfo, CliParser.skipVersionSync(), process.env.PRERELEASE_ID);
|
|
1410
|
+
ParserUtils.writeXVersion(CodeGenerator.srcFolder(), api["x-version"], api.info);
|
|
1413
1411
|
const parsedInformation = await SwaggerReaderHelpers.parseAllEndpoints({ api, sdkName, serviceName });
|
|
1414
|
-
const parsedInformationByType = CliParser.isAdmin() ? parsedInformation.admin : parsedInformation.public;
|
|
1415
|
-
const {
|
|
1416
|
-
arrayDefinitions,
|
|
1417
|
-
tagToClassImportsRecord,
|
|
1418
|
-
tagToEndpointClassesRecord,
|
|
1419
|
-
tagToSdkClientRecord,
|
|
1420
|
-
tagToSdkFunctionNamesRecord,
|
|
1421
|
-
tagToSdkImportsRecord
|
|
1422
|
-
} = parsedInformationByType;
|
|
1423
1412
|
if (CliParser.getSnippetOutputPath()) {
|
|
1424
1413
|
try {
|
|
1425
1414
|
ParserUtils.writeSnippetFile(CodeGenerator.getGeneratedSnippetsFolder(), api.info.title, JSON.stringify({
|
|
@@ -1430,55 +1419,69 @@ class CodeGenerator {
|
|
|
1430
1419
|
console.log("Generating snippets", err);
|
|
1431
1420
|
}
|
|
1432
1421
|
}
|
|
1433
|
-
const
|
|
1434
|
-
const
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
const
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1422
|
+
const DIST_DIR = (isAdmin) => `${CodeGenerator.getGeneratedFolder(isAdmin)}`;
|
|
1423
|
+
const DIST_DIR_ENDPOINTS = (isAdmin) => path__default.join(DIST_DIR(isAdmin), "endpoints");
|
|
1424
|
+
const DIST_DEFINITION_DIR = path__default.join(CodeGenerator.srcFolder(), "generated-definitions");
|
|
1425
|
+
const targetSrcFolder = `${CodeGenerator.srcFolder()}/`;
|
|
1426
|
+
CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS);
|
|
1427
|
+
const mainApiList = [];
|
|
1428
|
+
const generatePublicOrAdmin = (isAdmin) => {
|
|
1429
|
+
const parsedInformationByType = isAdmin ? parsedInformation.admin : parsedInformation.public;
|
|
1430
|
+
const {
|
|
1431
|
+
arrayDefinitions,
|
|
1432
|
+
tagToClassImportsRecord,
|
|
1433
|
+
tagToEndpointClassesRecord,
|
|
1434
|
+
tagToSdkClientRecord,
|
|
1435
|
+
tagToSdkFunctionNamesRecord,
|
|
1436
|
+
tagToSdkImportsRecord
|
|
1437
|
+
} = parsedInformationByType;
|
|
1438
|
+
const writeApiEndpointFiles = (isAdminEndpoint) => {
|
|
1439
|
+
const apiList = [];
|
|
1440
|
+
for (const tag in tagToEndpointClassesRecord) {
|
|
1441
|
+
const { className, classGenName } = ParserUtils.generateClassName(tag, isAdminEndpoint);
|
|
1442
|
+
const classBuffer = tagToEndpointClassesRecord[tag];
|
|
1443
|
+
const imports = [.../* @__PURE__ */ new Set([...tagToSdkImportsRecord[tag], ...Object.values(tagToClassImportsRecord[className])])];
|
|
1444
|
+
const apiImports = [.../* @__PURE__ */ new Set([...tagToSdkImportsRecord[tag], ...Object.values(tagToClassImportsRecord[className])])];
|
|
1445
|
+
apiImports.push(`import { ${classGenName} } from './endpoints/${classGenName}.js'`);
|
|
1446
|
+
ParserUtils.writeClassFile(DIST_DIR_ENDPOINTS(isAdminEndpoint), classGenName, classBuffer, imports);
|
|
1447
|
+
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1448
|
+
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1449
|
+
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, apiImports, tagToSdkFunctionNamesRecord[tag]);
|
|
1450
|
+
apiList.push(apiGenName);
|
|
1451
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DIR_ENDPOINTS(isAdminEndpoint), `${classGenName}`), targetSrcFolder));
|
|
1452
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DIR(isAdminEndpoint), `${apiGenName}`), targetSrcFolder));
|
|
1453
|
+
}
|
|
1454
|
+
mainApiList.push(...apiList);
|
|
1455
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(CodeGenerator.srcFolder(), serviceNameTitle), targetSrcFolder));
|
|
1456
|
+
};
|
|
1457
|
+
const writeDefinitions = (api2) => {
|
|
1458
|
+
const duplicates = /* @__PURE__ */ new Map();
|
|
1459
|
+
const definitions = api2?.components?.schemas || api2.definitions;
|
|
1460
|
+
for (const ref in definitions) {
|
|
1461
|
+
const definition = definitions[ref];
|
|
1462
|
+
const fileName = ParserUtils.parseRefType(ref);
|
|
1463
|
+
const fileExist = fs__default.existsSync(path__default.join(DIST_DEFINITION_DIR, `${fileName}.ts`));
|
|
1464
|
+
if (fileExist) {
|
|
1465
|
+
const duplicateName = ParserUtils.toCamelCaseWord(ref).replace(".", "").replace(".", "");
|
|
1466
|
+
duplicates.set(ref, duplicateName);
|
|
1467
|
+
}
|
|
1468
|
+
const { buffer } = new TemplateZod().render(fileName, definition, /* @__PURE__ */ new Map());
|
|
1469
|
+
ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, fileName, buffer);
|
|
1470
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DEFINITION_DIR, fileName), targetSrcFolder));
|
|
1471
|
+
}
|
|
1472
|
+
for (const arrayClass of arrayDefinitions) {
|
|
1473
|
+
const buffer = new TemplateZodArray().render(arrayClass);
|
|
1474
|
+
ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, arrayClass, buffer);
|
|
1475
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path__default.join(DIST_DEFINITION_DIR, arrayClass), targetSrcFolder));
|
|
1476
|
+
}
|
|
1477
|
+
};
|
|
1478
|
+
writeApiEndpointFiles(isAdmin);
|
|
1479
|
+
writeDefinitions(api);
|
|
1480
|
+
};
|
|
1481
|
+
generatePublicOrAdmin(true);
|
|
1482
|
+
generatePublicOrAdmin(false);
|
|
1483
|
+
const apiIndexBuff = templateApiIndex(serviceName, serviceNameTitle, mainApiList);
|
|
1484
|
+
ParserUtils.writeApiMainFile(CodeGenerator.srcFolder(), serviceNameTitle, apiIndexBuff);
|
|
1482
1485
|
console.log("\nCOMPLETED\n----------\n\n");
|
|
1483
1486
|
return indexImportsSet;
|
|
1484
1487
|
};
|
|
@@ -1545,17 +1548,7 @@ class SwaggerDownloader {
|
|
|
1545
1548
|
};
|
|
1546
1549
|
}
|
|
1547
1550
|
|
|
1548
|
-
|
|
1549
|
-
CliParser.createInstance(yargs2);
|
|
1550
|
-
SwaggerDownloader.main();
|
|
1551
|
-
}).command("generate-code", "Generate code based on downloaded swagger files", async (yargs2) => {
|
|
1552
|
-
yargs2.check(({ output }) => {
|
|
1553
|
-
if (!output?.trim()) {
|
|
1554
|
-
throw new Error("output is required for generate-code");
|
|
1555
|
-
}
|
|
1556
|
-
return true;
|
|
1557
|
-
});
|
|
1558
|
-
CliParser.createInstance(yargs2);
|
|
1551
|
+
const generateSdk = async () => {
|
|
1559
1552
|
const arrayOfSets = await Promise.all(CliParser.getConfigFile().map((config) => CodeGenerator.main(config)));
|
|
1560
1553
|
const indexImportsSet = /* @__PURE__ */ new Set();
|
|
1561
1554
|
const filenamesSet = /* @__PURE__ */ new Set();
|
|
@@ -1572,7 +1565,20 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
|
1572
1565
|
const filesToImport = indexImportsArray.map((fileToImport) => {
|
|
1573
1566
|
return `export * from '${fileToImport.replace("\\", "/")}.js'`;
|
|
1574
1567
|
});
|
|
1575
|
-
ParserUtils.writeAllImportsFile(CliParser.getOutputPath(), filesToImport.join("\n")
|
|
1568
|
+
ParserUtils.writeAllImportsFile(CliParser.getOutputPath(), filesToImport.join("\n"));
|
|
1569
|
+
};
|
|
1570
|
+
yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
1571
|
+
CliParser.createInstance(yargs2);
|
|
1572
|
+
SwaggerDownloader.main();
|
|
1573
|
+
}).command("generate-code", "Generate code based on downloaded swagger files", async (yargs2) => {
|
|
1574
|
+
yargs2.check(({ output }) => {
|
|
1575
|
+
if (!output?.trim()) {
|
|
1576
|
+
throw new Error("output is required for generate-code");
|
|
1577
|
+
}
|
|
1578
|
+
return true;
|
|
1579
|
+
});
|
|
1580
|
+
CliParser.createInstance(yargs2);
|
|
1581
|
+
await generateSdk();
|
|
1576
1582
|
}).option("config", {
|
|
1577
1583
|
description: "Config file providing backend services URL.",
|
|
1578
1584
|
type: "string",
|
|
@@ -1584,10 +1590,5 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
|
1584
1590
|
}).option("output", {
|
|
1585
1591
|
description: "Output path for generated code. Required for generate-code",
|
|
1586
1592
|
type: "string"
|
|
1587
|
-
}).option("admin", {
|
|
1588
|
-
description: "Only generate /admin endpoints.",
|
|
1589
|
-
type: "boolean",
|
|
1590
|
-
default: false,
|
|
1591
|
-
demandOption: false
|
|
1592
1593
|
}).demandCommand(1).help().argv;
|
|
1593
1594
|
//# sourceMappingURL=accelbyte-codegen.mjs.map
|