@scaleway/generate-react-sdk 0.2.3 → 0.2.5
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.
- package/dist/buildAPI.js +3 -8
- package/dist/generateAPI/emitFiles.d.ts +2 -2
- package/dist/generateAPI/{generate.d.ts → generate-metadata.d.ts} +1 -5
- package/dist/generateAPI/generate-metadata.js +120 -0
- package/dist/generateAPI/generateType.d.ts +2 -2
- package/dist/generateAPI/generateType.js +4 -2
- package/dist/generateAPI/getFileContent.d.ts +1 -1
- package/dist/generateAPI/getFileContent.js +6 -5
- package/dist/generateAPI/index.d.ts +1 -1
- package/dist/generateAPI/index.js +1 -1
- package/dist/metadata-types.d.ts +37 -0
- package/dist/metadata-types.js +5 -0
- package/dist/parseArgsCLI.d.ts +1 -1
- package/dist/parseArgsCLI.js +3 -7
- package/package.json +11 -12
- package/dist/generateAPI/config.d.ts +0 -3
- package/dist/generateAPI/config.js +0 -5
- package/dist/generateAPI/generate.js +0 -57
- package/dist/generateAPI/processDeclarationFile.d.ts +0 -2
- package/dist/generateAPI/processDeclarationFile.js +0 -73
- package/dist/generateAPI/resolveModule.d.ts +0 -2
- package/dist/generateAPI/resolveModule.js +0 -25
- package/dist/generateAPI/types.d.ts +0 -13
- package/dist/generateAPI/types.js +0 -1
package/dist/buildAPI.js
CHANGED
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { generateAPI } from './generateAPI/index.js';
|
|
3
3
|
import { parseArgsCLI } from './parseArgsCLI.js';
|
|
4
|
-
const requiresValueArgs = [
|
|
5
|
-
'dir-gen-name',
|
|
6
|
-
'sdk-factory-path',
|
|
7
|
-
'package-name-filter',
|
|
8
|
-
];
|
|
4
|
+
const requiresValueArgs = ['dir-gen-name', 'sdk-factory-path', 'package-name-filter'];
|
|
9
5
|
// Parse CLI arguments
|
|
10
6
|
const { cliArgs } = parseArgsCLI({ requiresValueArgs });
|
|
11
7
|
const dirGenName = cliArgs['dir-gen-name'] || 'src/__generated__';
|
|
12
|
-
const sdkFactoryPath = cliArgs['sdk-factory-path'] ||
|
|
13
|
-
'../contexts/SDKCacheProvider/sdkFactory';
|
|
8
|
+
const sdkFactoryPath = cliArgs['sdk-factory-path'] || '../contexts/SDKCacheProvider/sdkFactory';
|
|
14
9
|
const packageNameFilter = cliArgs['package-name-filter'] || '@scaleway/sdk-';
|
|
15
|
-
generateAPI({
|
|
10
|
+
await generateAPI({
|
|
16
11
|
dirGenName,
|
|
17
12
|
sdkFactoryPath,
|
|
18
13
|
packageNameFilter,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ProcessedMetadata } from '../metadata-types.ts';
|
|
2
2
|
export declare const emitFiles: ({ res, sourceFolderGen, sdkFactoryPath, }: {
|
|
3
|
-
res:
|
|
3
|
+
res: ProcessedMetadata;
|
|
4
4
|
sourceFolderGen: string;
|
|
5
5
|
sdkFactoryPath: string;
|
|
6
6
|
}) => void;
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
export declare const directoryOfSrcFolder: string;
|
|
2
1
|
export declare const generateAPI: ({ dirGenName, sdkFactoryPath, packageNameFilter, }: {
|
|
3
2
|
dirGenName: string;
|
|
4
3
|
sdkFactoryPath: string;
|
|
5
|
-
/**
|
|
6
|
-
* Name of the package starting with
|
|
7
|
-
*/
|
|
8
4
|
packageNameFilter: string;
|
|
9
|
-
}) => void
|
|
5
|
+
}) => Promise<void>;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
import { exec } from 'node:child_process';
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync } from 'node:fs';
|
|
11
|
+
import { createRequire } from 'node:module';
|
|
12
|
+
import { dirname, join, resolve } from 'node:path';
|
|
13
|
+
import { exit, stdout } from 'node:process';
|
|
14
|
+
import { emitFiles } from './emitFiles.js';
|
|
15
|
+
import { generateType } from './generateType.js';
|
|
16
|
+
const directoryOfSrcFolder = resolve('./');
|
|
17
|
+
const require = createRequire(resolve('./package.json'));
|
|
18
|
+
function discoverSdkPackages(packageNameFilter) {
|
|
19
|
+
const pkgJsonPath = resolve('package.json');
|
|
20
|
+
if (!existsSync(pkgJsonPath)) {
|
|
21
|
+
stdout.write('⚠️ No package.json found in current directory\n');
|
|
22
|
+
return new Map();
|
|
23
|
+
}
|
|
24
|
+
const pkgJson = JSON.parse(readFileSync(pkgJsonPath, 'utf-8'));
|
|
25
|
+
const allDeps = {
|
|
26
|
+
...pkgJson.dependencies,
|
|
27
|
+
...pkgJson.devDependencies,
|
|
28
|
+
...pkgJson.peerDependencies,
|
|
29
|
+
};
|
|
30
|
+
const packages = new Map();
|
|
31
|
+
for (const [name] of Object.entries(allDeps)) {
|
|
32
|
+
if (name.startsWith(packageNameFilter)) {
|
|
33
|
+
packages.set(name, name);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return packages;
|
|
37
|
+
}
|
|
38
|
+
async function loadVersions(packageName) {
|
|
39
|
+
try {
|
|
40
|
+
const resolvedPath = require.resolve(`${packageName}/metadata`);
|
|
41
|
+
const metadataModule = await import(__rewriteRelativeImportExtension(resolvedPath));
|
|
42
|
+
const versions = metadataModule?.pkgMetadata?.versions || metadataModule?.default?.versions || [];
|
|
43
|
+
return versions;
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
stdout.write(`⚠️ Could not load metadata from ${packageName}: ${error}\n`);
|
|
47
|
+
return [];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async function loadMetadata(packageName, version) {
|
|
51
|
+
try {
|
|
52
|
+
// Try the exported path first
|
|
53
|
+
try {
|
|
54
|
+
const resolvedPath = require.resolve(`${packageName}/${version}/metadata`);
|
|
55
|
+
const metadataModule = await import(__rewriteRelativeImportExtension(resolvedPath));
|
|
56
|
+
return metadataModule.queriesMetadata;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
stdout.write(`⚠️ Error loading metadata from ${packageName}/${version}/metadata \n Using dist fallback \n`);
|
|
60
|
+
// Fallback: construct path from node_modules
|
|
61
|
+
const pkgDir = join(dirname(resolve('package.json')), 'node_modules', packageName);
|
|
62
|
+
const distMetadataPath = join(pkgDir, 'dist', version, 'metadata.gen.js');
|
|
63
|
+
const metadataModule = await import(__rewriteRelativeImportExtension(distMetadataPath));
|
|
64
|
+
return metadataModule.queriesMetadata;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
stdout.write(`⚠️ Error loading metadata from ${packageName}/${version}/metadata: ${error}\n`);
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
export const generateAPI = async ({ dirGenName, sdkFactoryPath, packageNameFilter, }) => {
|
|
73
|
+
let result = {};
|
|
74
|
+
const dir = join(directoryOfSrcFolder, dirGenName);
|
|
75
|
+
// Create directory if it doesn't exist (don't delete existing files)
|
|
76
|
+
mkdirSync(dir, { recursive: true });
|
|
77
|
+
const sdkPackages = discoverSdkPackages(packageNameFilter);
|
|
78
|
+
if (sdkPackages.size === 0) {
|
|
79
|
+
stdout.write('⚠️ No SDK packages found in dependencies\n');
|
|
80
|
+
}
|
|
81
|
+
const skipPackages = new Set(['@scaleway/sdk-test', '@scaleway/sdk-std']);
|
|
82
|
+
for (const [packageName] of sdkPackages) {
|
|
83
|
+
if (skipPackages.has(packageName)) {
|
|
84
|
+
stdout.write(`⚠️ Skipping ${packageName}: excluded package\n`);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
const versions = await loadVersions(packageName);
|
|
88
|
+
if (versions.length === 0) {
|
|
89
|
+
stdout.write(`⚠️ Skipping ${packageName}: no versions with metadata found\n`);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
for (const version of versions) {
|
|
93
|
+
const metadata = await loadMetadata(packageName, version);
|
|
94
|
+
if (!metadata) {
|
|
95
|
+
stdout.write(`⚠️ Skipping ${packageName}/${version}: no queriesMetadata found\n`);
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const namespace = metadata.folderName || metadata.namespace;
|
|
99
|
+
const apis = metadata.services
|
|
100
|
+
.map((service) => service.apiClass)
|
|
101
|
+
.filter((apiClass) => apiClass && apiClass.length > 0);
|
|
102
|
+
if (apis.length > 0) {
|
|
103
|
+
result = {
|
|
104
|
+
...result,
|
|
105
|
+
[namespace]: {
|
|
106
|
+
packageName,
|
|
107
|
+
apis,
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
emitFiles({ res: result, sourceFolderGen: dir, sdkFactoryPath });
|
|
114
|
+
generateType(result);
|
|
115
|
+
exec('cd ../.. && pnpm run format').on('error', () => {
|
|
116
|
+
stdout.write('❌ Error during format !\n');
|
|
117
|
+
exit(1);
|
|
118
|
+
});
|
|
119
|
+
stdout.write('✅ files formatted !\n');
|
|
120
|
+
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export declare const generateType: (res:
|
|
1
|
+
import type { ProcessedMetadata } from '../metadata-types.ts';
|
|
2
|
+
export declare const generateType: (res: ProcessedMetadata) => void;
|
|
@@ -6,15 +6,17 @@ export const generateType = (res) => {
|
|
|
6
6
|
const template = ['//this file is generated \n\n'];
|
|
7
7
|
// import types
|
|
8
8
|
for (const [name, { packageName }] of Object.entries(res)) {
|
|
9
|
-
const
|
|
9
|
+
const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
|
|
10
|
+
const imp = `import type { ${capitalizedName} } from "${packageName}"\n`;
|
|
10
11
|
template.push(imp);
|
|
11
12
|
}
|
|
12
13
|
// export
|
|
13
14
|
template.push('\n export type APISdk = {\n');
|
|
14
15
|
for (const [name, { apis }] of Object.entries(res)) {
|
|
16
|
+
const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
|
|
15
17
|
for (const api of apis) {
|
|
16
18
|
const key = `${lowerCaseFirstLetter(name + api.replace('API', ''))}`;
|
|
17
|
-
const type = `${
|
|
19
|
+
const type = `${capitalizedName}.${api}`;
|
|
18
20
|
template.push(`${key}:${type},\n`);
|
|
19
21
|
}
|
|
20
22
|
}
|
|
@@ -4,5 +4,5 @@ type getContentProps = {
|
|
|
4
4
|
packageName: string;
|
|
5
5
|
sdkFactoryPath: string;
|
|
6
6
|
};
|
|
7
|
-
export declare const getFileContent: ({ packageName, name, api, sdkFactoryPath
|
|
7
|
+
export declare const getFileContent: ({ packageName, name, api, sdkFactoryPath }: getContentProps) => string;
|
|
8
8
|
export {};
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import { lowerCaseFirstLetter } from './helpers.js';
|
|
2
|
-
export const getFileContent = ({ packageName, name, api, sdkFactoryPath
|
|
3
|
-
const
|
|
2
|
+
export const getFileContent = ({ packageName, name, api, sdkFactoryPath }) => {
|
|
3
|
+
const capitalizedName = name.charAt(0).toUpperCase() + name.slice(1);
|
|
4
|
+
const apiName = capitalizedName + api;
|
|
4
5
|
const keyValue = name + api.replace('API', '');
|
|
5
6
|
const template = [
|
|
6
|
-
`import { ${
|
|
7
|
+
`import { ${capitalizedName} } from "${packageName}"`,
|
|
7
8
|
`import { createSDKFactory } from "${sdkFactoryPath}"`,
|
|
8
9
|
'',
|
|
9
10
|
'// This file is generated by scripts placed in `@scaleway/sdk:tools/generate-react-sdk` all modifications will be erased',
|
|
10
11
|
`export const use${apiName} = createSDKFactory(`,
|
|
11
|
-
|
|
12
|
-
`'${lowerCaseFirstLetter(keyValue)}'`,
|
|
12
|
+
` ${capitalizedName}.${api},`,
|
|
13
|
+
` '${lowerCaseFirstLetter(keyValue)}'`,
|
|
13
14
|
')',
|
|
14
15
|
].join('\n');
|
|
15
16
|
return template.toString();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { generateAPI } from './generate.ts';
|
|
1
|
+
export { generateAPI } from './generate-metadata.ts';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { generateAPI } from './generate.js';
|
|
1
|
+
export { generateAPI } from './generate-metadata.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for metadata-based generation.
|
|
3
|
+
* These types mirror the structure of metadata.gen.ts files in each SDK package.
|
|
4
|
+
*/
|
|
5
|
+
/** A single API method from metadata */
|
|
6
|
+
export interface MetadataMethod {
|
|
7
|
+
methodName: string;
|
|
8
|
+
protoName: string;
|
|
9
|
+
paramsType: string;
|
|
10
|
+
returnType: string;
|
|
11
|
+
isList: boolean;
|
|
12
|
+
paginationType?: 'offset' | 'cursor' | 'none';
|
|
13
|
+
pageParamKey?: string;
|
|
14
|
+
listItemType?: string;
|
|
15
|
+
isPrivate: boolean;
|
|
16
|
+
description?: string;
|
|
17
|
+
hasWaiter?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** A service class (e.g. "API", "ZonedAPI") from metadata */
|
|
20
|
+
export interface MetadataService {
|
|
21
|
+
apiClass: string;
|
|
22
|
+
methods: MetadataMethod[];
|
|
23
|
+
}
|
|
24
|
+
/** Top-level metadata from metadata.gen.ts */
|
|
25
|
+
export interface Metadata {
|
|
26
|
+
namespace: string;
|
|
27
|
+
version: string;
|
|
28
|
+
folderName: string;
|
|
29
|
+
services: MetadataService[];
|
|
30
|
+
}
|
|
31
|
+
/** Processed result for code generation */
|
|
32
|
+
export type ProcessedMetadata = {
|
|
33
|
+
[namespace: string]: {
|
|
34
|
+
packageName: string;
|
|
35
|
+
apis: string[];
|
|
36
|
+
};
|
|
37
|
+
};
|
package/dist/parseArgsCLI.d.ts
CHANGED
package/dist/parseArgsCLI.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Parse CLI arguments
|
|
2
|
-
export const parseArgsCLI = ({ requiresValueArgs
|
|
2
|
+
export const parseArgsCLI = ({ requiresValueArgs }) => {
|
|
3
3
|
const args = process.argv.slice(2);
|
|
4
4
|
const cliArgs = {};
|
|
5
5
|
for (let i = 0; i < args.length; i++) {
|
|
@@ -9,17 +9,13 @@ export const parseArgsCLI = ({ requiresValueArgs, }) => {
|
|
|
9
9
|
const key = parts[0];
|
|
10
10
|
let value = parts.length > 1 ? parts[1] : true;
|
|
11
11
|
// Check if the next argument is a value for this flag (handles --arg value format)
|
|
12
|
-
if (value === true &&
|
|
13
|
-
i + 1 < args.length &&
|
|
14
|
-
args[i + 1] &&
|
|
15
|
-
!args[i + 1]?.startsWith('--')) {
|
|
12
|
+
if (value === true && i + 1 < args.length && args[i + 1] && !args[i + 1]?.startsWith('--')) {
|
|
16
13
|
value = args[i + 1];
|
|
17
14
|
i++; // Skip the next argument since we've consumed it
|
|
18
15
|
}
|
|
19
16
|
if (key) {
|
|
20
17
|
// For arguments that require values, if no value is provided, skip them or use default
|
|
21
|
-
if (requiresValueArgs?.includes(key) &&
|
|
22
|
-
(value === true || value === undefined)) {
|
|
18
|
+
if (requiresValueArgs?.includes(key) && (value === true || value === undefined)) {
|
|
23
19
|
console.log(`⚠️ Warning: --${key} requires a value, using default`);
|
|
24
20
|
continue;
|
|
25
21
|
}
|
package/package.json
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scaleway/generate-react-sdk",
|
|
3
|
-
"version": "0.2.
|
|
4
|
-
"description": "generate react sdk",
|
|
5
|
-
"type": "module",
|
|
3
|
+
"version": "0.2.5",
|
|
6
4
|
"private": false,
|
|
5
|
+
"description": "generate react sdk",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"directory": "tools/generate-react-sdk"
|
|
9
|
+
},
|
|
7
10
|
"bin": {
|
|
8
11
|
"generate-react-sdk": "./dist/buildAPI.js"
|
|
9
12
|
},
|
|
10
13
|
"files": [
|
|
11
14
|
"dist"
|
|
12
15
|
],
|
|
13
|
-
"
|
|
14
|
-
"node": ">=20.19.6"
|
|
15
|
-
},
|
|
16
|
+
"type": "module",
|
|
16
17
|
"exports": {
|
|
17
18
|
"./*": {
|
|
18
19
|
"types": "./dist/*.d.ts",
|
|
19
20
|
"default": "./dist/*.js"
|
|
20
21
|
}
|
|
21
22
|
},
|
|
22
|
-
"repository": {
|
|
23
|
-
"type": "git",
|
|
24
|
-
"directory": "tools/generate-react-sdk"
|
|
25
|
-
},
|
|
26
23
|
"devDependencies": {
|
|
27
|
-
"@types/node": "20.19.35"
|
|
28
|
-
|
|
24
|
+
"@types/node": "20.19.35"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20.19.6"
|
|
29
28
|
},
|
|
30
29
|
"scripts": {
|
|
31
30
|
"build": "tsgo -p tsconfig.build.json",
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { exec } from 'node:child_process';
|
|
2
|
-
import { existsSync, readFileSync, rmSync } from 'node:fs';
|
|
3
|
-
import { join, resolve } from 'node:path';
|
|
4
|
-
import { exit, stdout } from 'node:process';
|
|
5
|
-
import { project } from './config.js';
|
|
6
|
-
import { emitFiles } from './emitFiles.js';
|
|
7
|
-
import { generateType } from './generateType.js';
|
|
8
|
-
import { processDeclarationFile } from './processDeclarationFile.js';
|
|
9
|
-
export const directoryOfSrcFolder = join(resolve('./'));
|
|
10
|
-
// Read package.json
|
|
11
|
-
const packageJson = JSON.parse(readFileSync(resolve(resolve('./'), 'package.json'), 'utf8'));
|
|
12
|
-
export const generateAPI = ({ dirGenName, sdkFactoryPath, packageNameFilter, }) => {
|
|
13
|
-
let result = {};
|
|
14
|
-
const dir = join(directoryOfSrcFolder, dirGenName);
|
|
15
|
-
if (existsSync(dir)) {
|
|
16
|
-
rmSync(dir, {
|
|
17
|
-
recursive: true,
|
|
18
|
-
force: true,
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
// Generate SDK paths from dependencies
|
|
22
|
-
const sdkPaths = [
|
|
23
|
-
...Object.keys(packageJson.dependencies ?? {}),
|
|
24
|
-
...Object.keys(packageJson.devDependencies ?? {}),
|
|
25
|
-
...Object.keys(packageJson.peerDependencies ?? {}),
|
|
26
|
-
]
|
|
27
|
-
.filter(dep => dep.startsWith(packageNameFilter))
|
|
28
|
-
.filter(dep => !['@scaleway/sdk-client'].includes(dep))
|
|
29
|
-
.map(dep => {
|
|
30
|
-
const declarationFile = 'dist/index.gen.d.ts';
|
|
31
|
-
return [
|
|
32
|
-
dep,
|
|
33
|
-
resolve(resolve('./'), `./node_modules/${dep}/${declarationFile}`),
|
|
34
|
-
];
|
|
35
|
-
});
|
|
36
|
-
for (const [packageName, sdkPath] of sdkPaths) {
|
|
37
|
-
if (!(packageName && sdkPath)) {
|
|
38
|
-
throw new Error('packageName or sdkPath is undefined');
|
|
39
|
-
}
|
|
40
|
-
const path = project.addSourceFileAtPath(sdkPath);
|
|
41
|
-
const res = processDeclarationFile({
|
|
42
|
-
namespace: '',
|
|
43
|
-
packageName,
|
|
44
|
-
result,
|
|
45
|
-
sourceFile: path,
|
|
46
|
-
});
|
|
47
|
-
result = { ...result, ...res };
|
|
48
|
-
}
|
|
49
|
-
emitFiles({ res: result, sourceFolderGen: dir, sdkFactoryPath });
|
|
50
|
-
generateType(result);
|
|
51
|
-
// // Finally we format all files to prevent updated files just because format is different
|
|
52
|
-
exec('cd ../.. && pnpm run format').on('error', () => {
|
|
53
|
-
stdout.write('❌ Error during format !\n');
|
|
54
|
-
exit(1);
|
|
55
|
-
});
|
|
56
|
-
stdout.write('✅ files formatted !\n');
|
|
57
|
-
};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { resolveModule } from './resolveModule.js';
|
|
2
|
-
// Recursive function to dive in all files
|
|
3
|
-
export function processDeclarationFile({ sourceFile, namespace, result, packageName, }) {
|
|
4
|
-
let copyOfResult = { ...result };
|
|
5
|
-
// Export processing
|
|
6
|
-
for (const exportDecl of sourceFile.getExportDeclarations()) {
|
|
7
|
-
const moduleSpecifier = exportDecl.getModuleSpecifierValue();
|
|
8
|
-
if (!moduleSpecifier) {
|
|
9
|
-
break;
|
|
10
|
-
}
|
|
11
|
-
const resolvedFile = resolveModule(sourceFile, moduleSpecifier);
|
|
12
|
-
if (!resolvedFile) {
|
|
13
|
-
break;
|
|
14
|
-
}
|
|
15
|
-
copyOfResult = {
|
|
16
|
-
...copyOfResult,
|
|
17
|
-
...processDeclarationFile({
|
|
18
|
-
namespace: `${namespace}_${exportDecl.getNamespaceExport()?.getName() ?? ''}`,
|
|
19
|
-
packageName,
|
|
20
|
-
result: copyOfResult,
|
|
21
|
-
sourceFile: resolvedFile,
|
|
22
|
-
}),
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
// Import processing
|
|
26
|
-
for (const importDecl of sourceFile.getImportDeclarations()) {
|
|
27
|
-
const moduleSpecifier = importDecl.getModuleSpecifierValue();
|
|
28
|
-
if (!moduleSpecifier) {
|
|
29
|
-
break;
|
|
30
|
-
}
|
|
31
|
-
const resolvedFile = resolveModule(sourceFile, moduleSpecifier);
|
|
32
|
-
if (!resolvedFile) {
|
|
33
|
-
break;
|
|
34
|
-
}
|
|
35
|
-
// const newNS = `${namespace}_${importDecl.getNamespaceImport()?.getText() ?? ''}`
|
|
36
|
-
copyOfResult = {
|
|
37
|
-
...copyOfResult,
|
|
38
|
-
...processDeclarationFile({
|
|
39
|
-
namespace,
|
|
40
|
-
packageName,
|
|
41
|
-
result: copyOfResult,
|
|
42
|
-
sourceFile: resolvedFile,
|
|
43
|
-
}),
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
for (const classDecl of sourceFile.getClasses()) {
|
|
47
|
-
let className = classDecl.getName();
|
|
48
|
-
if (className === 'InstanceV1UtilsAPI') {
|
|
49
|
-
// instanceAPI is not well exported
|
|
50
|
-
className = 'API';
|
|
51
|
-
}
|
|
52
|
-
if (className &&
|
|
53
|
-
// theses API are manually set inside the SDK himself ...
|
|
54
|
-
!['K8SUtilsAPI', 'InstanceV1UtilsAPI'].includes(className)) {
|
|
55
|
-
const prettyNamespace = namespace.split('_').filter(aNamespace => aNamespace.length > 0)[0] ??
|
|
56
|
-
'';
|
|
57
|
-
// Take what ends with `API` and is not part of the ignored APIs array.
|
|
58
|
-
const namespaceApi = {
|
|
59
|
-
[prettyNamespace]: {
|
|
60
|
-
apis: [
|
|
61
|
-
...new Set([
|
|
62
|
-
...(copyOfResult[prettyNamespace]?.apis ?? []),
|
|
63
|
-
className,
|
|
64
|
-
]),
|
|
65
|
-
],
|
|
66
|
-
packageName,
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
Object.assign(copyOfResult, namespaceApi);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return copyOfResult;
|
|
73
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
|
-
import { dirname, resolve } from 'node:path/posix';
|
|
3
|
-
// Resolve the referenced file in import/export
|
|
4
|
-
export function resolveModule(sourceFile, moduleSpecifier) {
|
|
5
|
-
const currentDir = dirname(sourceFile.getFilePath());
|
|
6
|
-
let resolvedPath = resolve(currentDir, moduleSpecifier.replace('.js', ''));
|
|
7
|
-
// Ajouter les extensions .ts ou .d.ts si pas presente dans le path
|
|
8
|
-
if (!(resolvedPath.endsWith('.ts') || resolvedPath.endsWith('.d.ts'))) {
|
|
9
|
-
if (existsSync(`${resolvedPath}.ts`)) {
|
|
10
|
-
resolvedPath = `${resolvedPath}.ts`;
|
|
11
|
-
}
|
|
12
|
-
else if (existsSync(`${resolvedPath}.d.ts`)) {
|
|
13
|
-
resolvedPath = `${resolvedPath}.d.ts`;
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
return null;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
try {
|
|
20
|
-
return (sourceFile.getProject().addSourceFileAtPathIfExists(resolvedPath) ?? null);
|
|
21
|
-
}
|
|
22
|
-
catch {
|
|
23
|
-
return null;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { SourceFile } from 'ts-morph';
|
|
2
|
-
export type ProcessDeclarationFileProps = {
|
|
3
|
-
sourceFile: SourceFile;
|
|
4
|
-
namespace: string;
|
|
5
|
-
result: ProcessDeclaration;
|
|
6
|
-
packageName: string;
|
|
7
|
-
};
|
|
8
|
-
export type ProcessDeclaration = {
|
|
9
|
-
[x: string]: {
|
|
10
|
-
packageName: string;
|
|
11
|
-
apis: string[];
|
|
12
|
-
};
|
|
13
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|