@accelbyte/codegen 2.0.1 → 2.1.0-experimental.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.
|
@@ -72,9 +72,6 @@ class CliParser {
|
|
|
72
72
|
static getResolvedSwaggersOutputPath = () => {
|
|
73
73
|
return path__namespace.resolve(CliParser.getSwaggersOutputPath());
|
|
74
74
|
};
|
|
75
|
-
static isAdmin = () => {
|
|
76
|
-
return CliParser.instance().argv.admin;
|
|
77
|
-
};
|
|
78
75
|
static getSnippetOutputPath = () => {
|
|
79
76
|
return CliParser.instance().argv.snippetOutput;
|
|
80
77
|
};
|
|
@@ -103,7 +100,9 @@ const generateImports = (body, importStatements, makeNewImportVarMap2, getImport
|
|
|
103
100
|
}
|
|
104
101
|
}
|
|
105
102
|
}
|
|
106
|
-
const generatedImports = Object.keys(usedImportVarMap).sort().map((moduleSource) =>
|
|
103
|
+
const generatedImports = Object.keys(usedImportVarMap).sort().map((moduleSource) => {
|
|
104
|
+
return `import { ${usedImportVarMap[moduleSource].sort().join(", ")} } from '${moduleSource}'`;
|
|
105
|
+
}).join("\n");
|
|
107
106
|
return `${generatedImports}
|
|
108
107
|
${importStatements.sort().join("\n")}`;
|
|
109
108
|
};
|
|
@@ -215,8 +214,6 @@ class VersionHelpers {
|
|
|
215
214
|
};
|
|
216
215
|
}
|
|
217
216
|
|
|
218
|
-
const codegenPackageJsonPath = path.join(__dirname, "../package.json");
|
|
219
|
-
JSON.parse(fs.readFileSync(codegenPackageJsonPath, "utf-8"));
|
|
220
217
|
const UNDEFINED_SWAGGER_SEMVER = "0.0.0";
|
|
221
218
|
const REMOVED_KEYWORDS = [
|
|
222
219
|
"/admin/",
|
|
@@ -312,7 +309,7 @@ class ParserUtils {
|
|
|
312
309
|
return null;
|
|
313
310
|
}
|
|
314
311
|
const type = ParserUtils.parseRefType($ref);
|
|
315
|
-
return `import { ${type} } from '
|
|
312
|
+
return `import { ${type} } from '../../generated-definitions/${type}.js'`;
|
|
316
313
|
};
|
|
317
314
|
static parseRefType = ($ref) => {
|
|
318
315
|
let ref = $ref.replace(".", "/");
|
|
@@ -504,7 +501,7 @@ class ParserUtils {
|
|
|
504
501
|
static writeApiFile(distDir, apiName, apiBuffer, imports, returnMethods) {
|
|
505
502
|
const newImports = [];
|
|
506
503
|
imports.forEach((el, index) => {
|
|
507
|
-
newImports.push(el.replace("
|
|
504
|
+
newImports.push(el.replace("../../generated-definitions", "../generated-definitions"));
|
|
508
505
|
});
|
|
509
506
|
const fileContent = templateApiClass(apiName, apiBuffer, newImports, returnMethods);
|
|
510
507
|
fs.writeFileSync(`${distDir}/${apiName}.ts`, ParserUtils.prependCopyrightHeader(fileContent));
|
|
@@ -522,9 +519,9 @@ class ParserUtils {
|
|
|
522
519
|
ParserUtils.mkdirIfNotExist(distDir);
|
|
523
520
|
fs.writeFileSync(path.join(distDir, `${name}.ts`), ParserUtils.prependCopyrightHeader(buffer));
|
|
524
521
|
}
|
|
525
|
-
static writeAllImportsFile(distDir, buffer
|
|
522
|
+
static writeAllImportsFile(distDir, buffer) {
|
|
526
523
|
ParserUtils.mkdirIfNotExist(distDir);
|
|
527
|
-
fs.writeFileSync(path.join(distDir,
|
|
524
|
+
fs.writeFileSync(path.join(distDir, "all-imports.ts"), ParserUtils.prependCopyrightHeader(buffer));
|
|
528
525
|
}
|
|
529
526
|
static syncPackageVersion(apiInfo, skipVersionSync, prereleaseId) {
|
|
530
527
|
if (skipVersionSync)
|
|
@@ -943,8 +940,9 @@ const templateApiIndex = (serviceName, serviceNameTitle, apiList) => {
|
|
|
943
940
|
let imports = "";
|
|
944
941
|
let returnStatement = "";
|
|
945
942
|
for (const cl of apiList) {
|
|
943
|
+
const dir = cl.toLowerCase().includes("admin") ? "generated-admin" : "generated-public";
|
|
946
944
|
imports += `
|
|
947
|
-
import { ${cl} } from './${
|
|
945
|
+
import { ${cl} } from './${dir}/${cl}.js'`;
|
|
948
946
|
returnStatement += `
|
|
949
947
|
${cl}, `;
|
|
950
948
|
}
|
|
@@ -1345,7 +1343,7 @@ class SwaggerReaderHelpers {
|
|
|
1345
1343
|
tagToClassImportsRecord[className] = tagToClassImportsRecord[className] ? tagToClassImportsRecord[className] : {};
|
|
1346
1344
|
if (responseClass) {
|
|
1347
1345
|
const importTypeClass = ParserUtils.parseRefType(responseClass);
|
|
1348
|
-
tagToClassImportsRecord[className][importTypeClass] = `import { ${importTypeClass} } from '
|
|
1346
|
+
tagToClassImportsRecord[className][importTypeClass] = `import { ${importTypeClass} } from '../../generated-definitions/${importTypeClass}.js'`;
|
|
1349
1347
|
}
|
|
1350
1348
|
if (responseClass && responseClass.endsWith("Array")) {
|
|
1351
1349
|
arrayDefinitions.push(responseClass);
|
|
@@ -1410,39 +1408,30 @@ class SwaggerReaderHelpers {
|
|
|
1410
1408
|
}
|
|
1411
1409
|
|
|
1412
1410
|
class CodeGenerator {
|
|
1413
|
-
static
|
|
1414
|
-
static
|
|
1415
|
-
static getGeneratedAdminFolder = () => `${CliParser.getOutputPath()}/generated-admin`;
|
|
1411
|
+
static srcFolder = () => CliParser.getOutputPath();
|
|
1412
|
+
static getGeneratedFolder = (isAdmin) => isAdmin ? `${CodeGenerator.srcFolder()}/generated-admin` : `${CodeGenerator.srcFolder()}/generated-public`;
|
|
1416
1413
|
static getGeneratedSnippetsFolder = () => `${CliParser.getSnippetOutputPath()}/generated-snippets`;
|
|
1417
|
-
static
|
|
1414
|
+
static prepareDirs = (DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS) => {
|
|
1415
|
+
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
1416
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR(true));
|
|
1417
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR(false));
|
|
1418
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(true));
|
|
1419
|
+
ParserUtils.mkdirIfNotExist(DIST_DIR_ENDPOINTS(false));
|
|
1420
|
+
};
|
|
1418
1421
|
static main = async (nameArray) => {
|
|
1419
1422
|
const serviceName = nameArray[0];
|
|
1420
1423
|
const sdkName = nameArray[1];
|
|
1421
1424
|
const swaggerFile = nameArray[2];
|
|
1422
1425
|
const parser = new SwaggerParser();
|
|
1423
|
-
const generatedFolder = CliParser.isAdmin() ? CodeGenerator.getGeneratedAdminFolder() : CodeGenerator.getGeneratedPublicFolder();
|
|
1424
|
-
const DIST_DIR = `${generatedFolder}/${serviceName}`;
|
|
1425
|
-
const DIST_ENDPOINTS_DIR = path.join(DIST_DIR, "endpoints");
|
|
1426
|
-
const DIST_DEFINITION_DIR = path.join(DIST_DIR, "definitions");
|
|
1427
1426
|
const swaggerFilePath = `${CliParser.getSwaggersOutputPath()}/${swaggerFile}`;
|
|
1428
1427
|
const api = await parser.parse(swaggerFilePath);
|
|
1428
|
+
const serviceNameTitle = ParserUtils.convertDashesToTitleCase(serviceName);
|
|
1429
1429
|
const indexImportsSet = /* @__PURE__ */ new Set();
|
|
1430
1430
|
const apiInfo = { ...api.info, "x-version": api["x-version"]?.version };
|
|
1431
1431
|
console.log("----------\nGenerating API:", { title: apiInfo.title, version: apiInfo.version });
|
|
1432
|
-
ParserUtils.mkdirIfNotExist(DIST_DIR);
|
|
1433
|
-
ParserUtils.mkdirIfNotExist(DIST_DEFINITION_DIR);
|
|
1434
|
-
ParserUtils.mkdirIfNotExist(DIST_ENDPOINTS_DIR);
|
|
1435
1432
|
ParserUtils.syncPackageVersion(apiInfo, CliParser.skipVersionSync(), process.env.PRERELEASE_ID);
|
|
1433
|
+
ParserUtils.writeXVersion(CodeGenerator.srcFolder(), api["x-version"], api.info);
|
|
1436
1434
|
const parsedInformation = await SwaggerReaderHelpers.parseAllEndpoints({ api, sdkName, serviceName });
|
|
1437
|
-
const parsedInformationByType = CliParser.isAdmin() ? parsedInformation.admin : parsedInformation.public;
|
|
1438
|
-
const {
|
|
1439
|
-
arrayDefinitions,
|
|
1440
|
-
tagToClassImportsRecord,
|
|
1441
|
-
tagToEndpointClassesRecord,
|
|
1442
|
-
tagToSdkClientRecord,
|
|
1443
|
-
tagToSdkFunctionNamesRecord,
|
|
1444
|
-
tagToSdkImportsRecord
|
|
1445
|
-
} = parsedInformationByType;
|
|
1446
1435
|
if (CliParser.getSnippetOutputPath()) {
|
|
1447
1436
|
try {
|
|
1448
1437
|
ParserUtils.writeSnippetFile(CodeGenerator.getGeneratedSnippetsFolder(), api.info.title, JSON.stringify({
|
|
@@ -1453,55 +1442,69 @@ class CodeGenerator {
|
|
|
1453
1442
|
console.log("Generating snippets", err);
|
|
1454
1443
|
}
|
|
1455
1444
|
}
|
|
1456
|
-
const
|
|
1457
|
-
const
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
const
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1445
|
+
const DIST_DIR = (isAdmin) => `${CodeGenerator.getGeneratedFolder(isAdmin)}`;
|
|
1446
|
+
const DIST_DIR_ENDPOINTS = (isAdmin) => path.join(DIST_DIR(isAdmin), "endpoints");
|
|
1447
|
+
const DIST_DEFINITION_DIR = path.join(CodeGenerator.srcFolder(), "generated-definitions");
|
|
1448
|
+
const targetSrcFolder = `${CodeGenerator.srcFolder()}/`;
|
|
1449
|
+
CodeGenerator.prepareDirs(DIST_DEFINITION_DIR, DIST_DIR, DIST_DIR_ENDPOINTS);
|
|
1450
|
+
const mainApiList = [];
|
|
1451
|
+
const generatePublicOrAdmin = (isAdmin) => {
|
|
1452
|
+
const parsedInformationByType = isAdmin ? parsedInformation.admin : parsedInformation.public;
|
|
1453
|
+
const {
|
|
1454
|
+
arrayDefinitions,
|
|
1455
|
+
tagToClassImportsRecord,
|
|
1456
|
+
tagToEndpointClassesRecord,
|
|
1457
|
+
tagToSdkClientRecord,
|
|
1458
|
+
tagToSdkFunctionNamesRecord,
|
|
1459
|
+
tagToSdkImportsRecord
|
|
1460
|
+
} = parsedInformationByType;
|
|
1461
|
+
const writeApiEndpointFiles = (isAdminEndpoint) => {
|
|
1462
|
+
const apiList = [];
|
|
1463
|
+
for (const tag in tagToEndpointClassesRecord) {
|
|
1464
|
+
const { className, classGenName } = ParserUtils.generateClassName(tag, isAdminEndpoint);
|
|
1465
|
+
const classBuffer = tagToEndpointClassesRecord[tag];
|
|
1466
|
+
const imports = [.../* @__PURE__ */ new Set([...tagToSdkImportsRecord[tag], ...Object.values(tagToClassImportsRecord[className])])];
|
|
1467
|
+
const apiImports = [.../* @__PURE__ */ new Set([...tagToSdkImportsRecord[tag], ...Object.values(tagToClassImportsRecord[className])])];
|
|
1468
|
+
apiImports.push(`import { ${classGenName} } from './endpoints/${classGenName}.js'`);
|
|
1469
|
+
ParserUtils.writeClassFile(DIST_DIR_ENDPOINTS(isAdminEndpoint), classGenName, classBuffer, imports);
|
|
1470
|
+
const apiBuffer = tagToSdkClientRecord[tag];
|
|
1471
|
+
const { apiGenName } = ParserUtils.generateApiName(tag, isAdminEndpoint);
|
|
1472
|
+
ParserUtils.writeApiFile(DIST_DIR(isAdminEndpoint), apiGenName, apiBuffer, apiImports, tagToSdkFunctionNamesRecord[tag]);
|
|
1473
|
+
apiList.push(apiGenName);
|
|
1474
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DIR_ENDPOINTS(isAdminEndpoint), `${classGenName}`), targetSrcFolder));
|
|
1475
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DIR(isAdminEndpoint), `${apiGenName}`), targetSrcFolder));
|
|
1476
|
+
}
|
|
1477
|
+
mainApiList.push(...apiList);
|
|
1478
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(CodeGenerator.srcFolder(), serviceNameTitle), targetSrcFolder));
|
|
1479
|
+
};
|
|
1480
|
+
const writeDefinitions = (api2) => {
|
|
1481
|
+
const duplicates = /* @__PURE__ */ new Map();
|
|
1482
|
+
const definitions = api2?.components?.schemas || api2.definitions;
|
|
1483
|
+
for (const ref in definitions) {
|
|
1484
|
+
const definition = definitions[ref];
|
|
1485
|
+
const fileName = ParserUtils.parseRefType(ref);
|
|
1486
|
+
const fileExist = fs.existsSync(path.join(DIST_DEFINITION_DIR, `${fileName}.ts`));
|
|
1487
|
+
if (fileExist) {
|
|
1488
|
+
const duplicateName = ParserUtils.toCamelCaseWord(ref).replace(".", "").replace(".", "");
|
|
1489
|
+
duplicates.set(ref, duplicateName);
|
|
1490
|
+
}
|
|
1491
|
+
const { buffer } = new TemplateZod().render(fileName, definition, /* @__PURE__ */ new Map());
|
|
1492
|
+
ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, fileName, buffer);
|
|
1493
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DEFINITION_DIR, fileName), targetSrcFolder));
|
|
1494
|
+
}
|
|
1495
|
+
for (const arrayClass of arrayDefinitions) {
|
|
1496
|
+
const buffer = new TemplateZodArray().render(arrayClass);
|
|
1497
|
+
ParserUtils.writeDefinitionFile(DIST_DEFINITION_DIR, arrayClass, buffer);
|
|
1498
|
+
indexImportsSet.add(ParserUtils.getRelativePathToWebSdkSrcFolder(path.join(DIST_DEFINITION_DIR, arrayClass), targetSrcFolder));
|
|
1499
|
+
}
|
|
1500
|
+
};
|
|
1501
|
+
writeApiEndpointFiles(isAdmin);
|
|
1502
|
+
writeDefinitions(api);
|
|
1503
|
+
};
|
|
1504
|
+
generatePublicOrAdmin(true);
|
|
1505
|
+
generatePublicOrAdmin(false);
|
|
1506
|
+
const apiIndexBuff = templateApiIndex(serviceName, serviceNameTitle, mainApiList);
|
|
1507
|
+
ParserUtils.writeApiMainFile(CodeGenerator.srcFolder(), serviceNameTitle, apiIndexBuff);
|
|
1505
1508
|
console.log("\nCOMPLETED\n----------\n\n");
|
|
1506
1509
|
return indexImportsSet;
|
|
1507
1510
|
};
|
|
@@ -1568,17 +1571,7 @@ class SwaggerDownloader {
|
|
|
1568
1571
|
};
|
|
1569
1572
|
}
|
|
1570
1573
|
|
|
1571
|
-
|
|
1572
|
-
CliParser.createInstance(yargs2);
|
|
1573
|
-
SwaggerDownloader.main();
|
|
1574
|
-
}).command("generate-code", "Generate code based on downloaded swagger files", async (yargs2) => {
|
|
1575
|
-
yargs2.check(({ output }) => {
|
|
1576
|
-
if (!output?.trim()) {
|
|
1577
|
-
throw new Error("output is required for generate-code");
|
|
1578
|
-
}
|
|
1579
|
-
return true;
|
|
1580
|
-
});
|
|
1581
|
-
CliParser.createInstance(yargs2);
|
|
1574
|
+
const generateSdk = async () => {
|
|
1582
1575
|
const arrayOfSets = await Promise.all(CliParser.getConfigFile().map((config) => CodeGenerator.main(config)));
|
|
1583
1576
|
const indexImportsSet = /* @__PURE__ */ new Set();
|
|
1584
1577
|
const filenamesSet = /* @__PURE__ */ new Set();
|
|
@@ -1595,7 +1588,20 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
|
1595
1588
|
const filesToImport = indexImportsArray.map((fileToImport) => {
|
|
1596
1589
|
return `export * from '${fileToImport.replace("\\", "/")}.js'`;
|
|
1597
1590
|
});
|
|
1598
|
-
ParserUtils.writeAllImportsFile(CliParser.getOutputPath(), filesToImport.join("\n")
|
|
1591
|
+
ParserUtils.writeAllImportsFile(CliParser.getOutputPath(), filesToImport.join("\n"));
|
|
1592
|
+
};
|
|
1593
|
+
yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
1594
|
+
CliParser.createInstance(yargs2);
|
|
1595
|
+
SwaggerDownloader.main();
|
|
1596
|
+
}).command("generate-code", "Generate code based on downloaded swagger files", async (yargs2) => {
|
|
1597
|
+
yargs2.check(({ output }) => {
|
|
1598
|
+
if (!output?.trim()) {
|
|
1599
|
+
throw new Error("output is required for generate-code");
|
|
1600
|
+
}
|
|
1601
|
+
return true;
|
|
1602
|
+
});
|
|
1603
|
+
CliParser.createInstance(yargs2);
|
|
1604
|
+
await generateSdk();
|
|
1599
1605
|
}).option("config", {
|
|
1600
1606
|
description: "Config file providing backend services URL.",
|
|
1601
1607
|
type: "string",
|
|
@@ -1607,10 +1613,5 @@ yargs.command("download-swaggers", "Download swaggers JSON files", (yargs2) => {
|
|
|
1607
1613
|
}).option("output", {
|
|
1608
1614
|
description: "Output path for generated code. Required for generate-code",
|
|
1609
1615
|
type: "string"
|
|
1610
|
-
}).option("admin", {
|
|
1611
|
-
description: "Only generate /admin endpoints.",
|
|
1612
|
-
type: "boolean",
|
|
1613
|
-
default: false,
|
|
1614
|
-
demandOption: false
|
|
1615
1616
|
}).demandCommand(1).help().argv;
|
|
1616
1617
|
//# sourceMappingURL=accelbyte-codegen.js.map
|