2024-03-24 19:38:18 +02:00
|
|
|
#!/usr/bin/env node
|
2023-08-01 20:09:47 +02:00
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Updates the SDK Typescript definitions located in the file sdk.d.ts
|
|
|
|
* to match the current definitions of functions of sdk.mjs.
|
|
|
|
*/
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
import {execSync} from 'node:child_process';
|
2023-08-01 20:09:47 +02:00
|
|
|
import fs from 'node:fs/promises';
|
|
|
|
import path from 'node:path';
|
2024-03-24 19:38:18 +02:00
|
|
|
import process from 'node:process';
|
|
|
|
import {getDirnameFromImportMeta} from '../../sdk.mjs';
|
2023-08-01 20:09:47 +02:00
|
|
|
|
|
|
|
const __dirname = getDirnameFromImportMeta(import.meta.url);
|
2024-03-24 19:38:18 +02:00
|
|
|
const rootDirectory = path.resolve(__dirname, '..', '..');
|
2023-08-01 20:09:47 +02:00
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const sdkTs = path.resolve(rootDirectory, 'sdk.d.ts');
|
|
|
|
const sdkMts = path.resolve(rootDirectory, 'sdk.d.mts');
|
|
|
|
const sdkMjs = path.resolve(rootDirectory, 'sdk.mjs');
|
2023-08-01 20:09:47 +02:00
|
|
|
|
|
|
|
const generateSdkMts = async () => {
|
2024-03-24 19:38:18 +02:00
|
|
|
// Remove temporally type definitions imported with comments
|
2023-08-01 20:09:47 +02:00
|
|
|
// in sdk.mjs to avoid circular imports
|
2024-03-24 19:38:18 +02:00
|
|
|
const originalSdkMjsContent = await fs.readFile(sdkMjs, 'utf8');
|
|
|
|
const temporarySdkMjsContent = originalSdkMjsContent
|
2023-08-01 20:09:47 +02:00
|
|
|
.split('\n')
|
|
|
|
.filter((line) => {
|
|
|
|
return !line.startsWith(' * @typedef {import("./sdk")');
|
|
|
|
})
|
|
|
|
.join('\n');
|
2024-03-24 19:38:18 +02:00
|
|
|
await fs.writeFile(sdkMjs, temporarySdkMjsContent);
|
2023-08-01 20:09:47 +02:00
|
|
|
try {
|
|
|
|
execSync(
|
|
|
|
'npx tsc sdk.mjs' +
|
|
|
|
' --declaration --emitDeclarationOnly --allowJs --removeComments',
|
|
|
|
);
|
|
|
|
} catch (error) {
|
2023-08-04 03:27:20 +02:00
|
|
|
await fs.writeFile(sdkMjs, originalSdkMjsContent);
|
2023-08-01 20:09:47 +02:00
|
|
|
console.log(
|
|
|
|
`Error ${error.status} generating Typescript` +
|
|
|
|
` definitions: '${error.message}'`,
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2024-03-24 19:38:18 +02:00
|
|
|
|
2023-08-01 20:09:47 +02:00
|
|
|
await fs.writeFile(sdkMjs, originalSdkMjsContent);
|
|
|
|
};
|
|
|
|
|
|
|
|
const generateSdkTs = async () => {
|
2023-08-08 06:38:52 +02:00
|
|
|
const fileExists = await fs
|
|
|
|
.access(sdkMts)
|
|
|
|
.then(() => true)
|
|
|
|
.catch(() => false);
|
2024-03-24 19:38:18 +02:00
|
|
|
if (fileExists) await fs.unlink(sdkMts);
|
2023-08-01 20:09:47 +02:00
|
|
|
await generateSdkMts();
|
|
|
|
|
2024-03-24 19:38:18 +02:00
|
|
|
const autogeneratedMessage =
|
|
|
|
'/* The next code is autogenerated from sdk.mjs */';
|
2023-08-01 20:09:47 +02:00
|
|
|
const newSdkTsContent =
|
2024-03-24 19:38:18 +02:00
|
|
|
// eslint-disable-next-line unicorn/no-await-expression-member
|
|
|
|
(await fs.readFile(sdkTs, 'utf8')).split(autogeneratedMessage)[0] +
|
|
|
|
`${autogeneratedMessage}\n\n${await fs.readFile(sdkMts, 'utf8')}`;
|
2023-08-01 20:09:47 +02:00
|
|
|
|
|
|
|
await fs.writeFile(sdkTs, newSdkTsContent);
|
|
|
|
await fs.unlink(sdkMts);
|
|
|
|
|
|
|
|
try {
|
|
|
|
execSync('npx prettier -w sdk.d.ts');
|
|
|
|
} catch (error) {
|
|
|
|
console.log(
|
|
|
|
`Error ${error.status} executing Prettier` +
|
2024-03-28 23:44:08 +02:00
|
|
|
` to prettify SDK TS definitions: '${error.message}'`,
|
2023-08-01 20:09:47 +02:00
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
await generateSdkTs();
|