@weclapp/sdk 2.0.0-dev.23 → 2.0.0-dev.24
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/cli.js +43 -51
- package/package.json +29 -23
- package/{tsconfig.lib.json → tsconfig.sdk.json} +7 -8
package/dist/cli.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { fileURLToPath } from 'url';
|
|
2
2
|
import { resolve, dirname } from 'path';
|
|
3
|
-
import { rmdir, stat, readFile, writeFile, rm, cp, mkdir } from 'fs/promises';
|
|
4
3
|
import { rollup } from 'rollup';
|
|
5
4
|
import terser from '@rollup/plugin-terser';
|
|
6
|
-
import ts from 'rollup
|
|
5
|
+
import ts from '@rollup/plugin-typescript';
|
|
7
6
|
import indentString from 'indent-string';
|
|
8
7
|
import { snakeCase, pascalCase, camelCase } from 'change-case';
|
|
9
8
|
import chalk from 'chalk';
|
|
10
9
|
import { OpenAPIV3 } from 'openapi-types';
|
|
11
10
|
import { createHash } from 'crypto';
|
|
11
|
+
import { stat, readFile, rm, cp, writeFile, mkdir } from 'fs/promises';
|
|
12
12
|
import { config } from 'dotenv';
|
|
13
13
|
import yargs from 'yargs';
|
|
14
14
|
import { hideBin } from 'yargs/helpers';
|
|
15
|
+
import pkg from '../package.json' with { type: 'json' };
|
|
15
16
|
import prettyMs from 'pretty-ms';
|
|
16
17
|
|
|
17
18
|
var Target;
|
|
@@ -39,7 +40,7 @@ const currentDirname = () => {
|
|
|
39
40
|
return fileURLToPath(new URL('..', import.meta.url));
|
|
40
41
|
};
|
|
41
42
|
|
|
42
|
-
const tsconfig = resolve(currentDirname(), './tsconfig.
|
|
43
|
+
const tsconfig = resolve(currentDirname(), './tsconfig.sdk.json');
|
|
43
44
|
const resolveGlobals = (...globals) => Object.fromEntries(globals.map(v => [v, '*']));
|
|
44
45
|
const generateOutput = (config) => ({
|
|
45
46
|
sourcemap: true,
|
|
@@ -61,35 +62,22 @@ const bundle = async (workingDirectory, target) => {
|
|
|
61
62
|
globals: resolveGlobals('node-fetch', 'url')
|
|
62
63
|
})
|
|
63
64
|
];
|
|
64
|
-
// Remove build dir
|
|
65
|
-
await rmdir(dist()).catch(() => void 0);
|
|
66
65
|
const bundles = {
|
|
67
66
|
[Target.BROWSER_PROMISES]: () => ({
|
|
68
|
-
input: src('
|
|
67
|
+
input: src('index.ts'),
|
|
68
|
+
plugins: [ts({ tsconfig, declarationDir: dist() }), terser()],
|
|
69
69
|
output: [
|
|
70
|
-
generateOutput({
|
|
71
|
-
file: dist('index.cjs'),
|
|
72
|
-
name: 'Weclapp',
|
|
73
|
-
format: 'umd'
|
|
74
|
-
}),
|
|
75
70
|
generateOutput({
|
|
76
71
|
file: dist('index.js'),
|
|
77
72
|
format: 'es'
|
|
78
73
|
})
|
|
79
|
-
]
|
|
80
|
-
plugins: [ts({ tsconfig }), terser()]
|
|
74
|
+
]
|
|
81
75
|
}),
|
|
82
76
|
[Target.BROWSER_RX]: () => ({
|
|
77
|
+
input: src('index.ts'),
|
|
78
|
+
plugins: [ts({ tsconfig, declarationDir: dist() }), terser()],
|
|
83
79
|
external: ['rxjs'],
|
|
84
|
-
input: src('browser.rx.ts'),
|
|
85
|
-
plugins: [ts({ tsconfig }), terser()],
|
|
86
80
|
output: [
|
|
87
|
-
generateOutput({
|
|
88
|
-
file: dist('index.cjs'),
|
|
89
|
-
name: 'Weclapp',
|
|
90
|
-
format: 'umd',
|
|
91
|
-
globals: resolveGlobals('rxjs')
|
|
92
|
-
}),
|
|
93
81
|
generateOutput({
|
|
94
82
|
file: dist('index.js'),
|
|
95
83
|
format: 'es',
|
|
@@ -98,16 +86,16 @@ const bundle = async (workingDirectory, target) => {
|
|
|
98
86
|
]
|
|
99
87
|
}),
|
|
100
88
|
[Target.NODE_PROMISES]: () => ({
|
|
101
|
-
input: src('
|
|
102
|
-
|
|
89
|
+
input: src('index.ts'),
|
|
90
|
+
plugins: [ts({ tsconfig, declarationDir: dist() }), terser()],
|
|
103
91
|
external: ['node-fetch', 'url'],
|
|
104
|
-
|
|
92
|
+
output: generateNodeOutput()
|
|
105
93
|
}),
|
|
106
94
|
[Target.NODE_RX]: () => ({
|
|
107
|
-
input: src('
|
|
108
|
-
|
|
95
|
+
input: src('index.ts'),
|
|
96
|
+
plugins: [ts({ tsconfig, declarationDir: dist() }), terser()],
|
|
109
97
|
external: ['node-fetch', 'url', 'rxjs'],
|
|
110
|
-
|
|
98
|
+
output: generateNodeOutput()
|
|
111
99
|
})
|
|
112
100
|
};
|
|
113
101
|
const config = bundles[target]();
|
|
@@ -1205,7 +1193,7 @@ const hash = (content, algorithm = 'sha256') => {
|
|
|
1205
1193
|
|
|
1206
1194
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
1207
1195
|
const cli = async () => {
|
|
1208
|
-
const
|
|
1196
|
+
const version = pkg.version;
|
|
1209
1197
|
const { argv } = yargs(hideBin(process.argv))
|
|
1210
1198
|
.scriptName('build-weclapp-sdk')
|
|
1211
1199
|
.usage('Usage: $0 <source> [flags]')
|
|
@@ -1274,7 +1262,7 @@ const cli = async () => {
|
|
|
1274
1262
|
return Promise.reject();
|
|
1275
1263
|
}
|
|
1276
1264
|
if (await stat(src).catch(() => false)) {
|
|
1277
|
-
logger.infoLn(`Source is a file
|
|
1265
|
+
logger.infoLn(`Source is a file`);
|
|
1278
1266
|
const content = JSON.parse(await readFile(src, 'utf-8'));
|
|
1279
1267
|
return { cache, content, options };
|
|
1280
1268
|
}
|
|
@@ -1300,42 +1288,46 @@ const cli = async () => {
|
|
|
1300
1288
|
return { cache, content, options };
|
|
1301
1289
|
};
|
|
1302
1290
|
|
|
1303
|
-
const
|
|
1304
|
-
const
|
|
1291
|
+
const workingDir = resolve(currentDirname(), './sdk');
|
|
1292
|
+
const cacheDir = resolve(currentDirname(), './.cache');
|
|
1305
1293
|
void (async () => {
|
|
1306
1294
|
const start = process.hrtime.bigint();
|
|
1307
|
-
const { default: { version } } = await import('../package.json', { assert: { type: 'json' } });
|
|
1308
1295
|
const { content: doc, cache: useCache, options } = await cli();
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
const dist = (...paths) => resolve(workingDirectory, ...paths);
|
|
1313
|
-
const tmp = async (...paths) => {
|
|
1314
|
-
const fullPath = resolve(cacheDir, ...paths);
|
|
1315
|
-
await mkdir(dirname(fullPath), { recursive: true }).catch(() => null);
|
|
1296
|
+
const workingDirPath = async (...paths) => {
|
|
1297
|
+
const fullPath = resolve(workingDir, ...paths);
|
|
1298
|
+
await mkdir(dirname(fullPath), { recursive: true });
|
|
1316
1299
|
return fullPath;
|
|
1317
1300
|
};
|
|
1301
|
+
// Resolve cache dir and key
|
|
1302
|
+
const cacheKey = hash([pkg.version, JSON.stringify(doc), JSON.stringify(options)]).slice(-8);
|
|
1303
|
+
const cachedSdkDir = resolve(cacheDir, cacheKey);
|
|
1304
|
+
// Remove old SDK
|
|
1305
|
+
await rm(workingDir, { recursive: true, force: true });
|
|
1318
1306
|
if (useCache) {
|
|
1319
1307
|
logger.infoLn(`Cache ID: ${cacheKey}`);
|
|
1320
1308
|
}
|
|
1321
|
-
if (useCache && await stat(
|
|
1322
|
-
|
|
1309
|
+
if (useCache && await stat(cachedSdkDir).catch(() => false)) {
|
|
1310
|
+
// Copy cached SDK to working dir
|
|
1311
|
+
logger.successLn(`Cache match! (${cachedSdkDir})`);
|
|
1312
|
+
await cp(cachedSdkDir, workingDir, { recursive: true });
|
|
1323
1313
|
}
|
|
1324
1314
|
else {
|
|
1325
|
-
//
|
|
1326
|
-
await writeFile(await
|
|
1315
|
+
// Write swagger.json file
|
|
1316
|
+
await writeFile(await workingDirPath('openapi.json'), JSON.stringify(doc, null, 2));
|
|
1327
1317
|
logger.infoLn(`Generate sdk (target: ${options.target})`);
|
|
1328
|
-
// Generate
|
|
1318
|
+
// Generate and write SDK
|
|
1329
1319
|
const sdk = generate(doc, options);
|
|
1330
|
-
await writeFile(await
|
|
1331
|
-
// Bundle
|
|
1320
|
+
await writeFile(await workingDirPath('src', 'index.ts'), sdk.trim() + '\n');
|
|
1321
|
+
// Bundle and write SDK
|
|
1332
1322
|
logger.infoLn('Bundle... (this may take some time)');
|
|
1333
|
-
await bundle(
|
|
1334
|
-
|
|
1335
|
-
|
|
1323
|
+
await bundle(workingDir, options.target);
|
|
1324
|
+
if (useCache) {
|
|
1325
|
+
// Copy SDK to cache
|
|
1326
|
+
logger.successLn(`Caching SDK: (${cachedSdkDir})`);
|
|
1327
|
+
await mkdir(cachedSdkDir, { recursive: true });
|
|
1328
|
+
await cp(workingDir, cachedSdkDir, { recursive: true });
|
|
1329
|
+
}
|
|
1336
1330
|
}
|
|
1337
|
-
// Copy bundled SDK
|
|
1338
|
-
await cp(cacheDir, workingDirectory, { recursive: true });
|
|
1339
1331
|
// Print job summary
|
|
1340
1332
|
const duration = (process.hrtime.bigint() - start) / 1000000n;
|
|
1341
1333
|
logger.successLn(`SDK built in ${prettyMs(Number(duration))}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weclapp/sdk",
|
|
3
|
-
"version": "2.0.0-dev.
|
|
3
|
+
"version": "2.0.0-dev.24",
|
|
4
4
|
"description": "SDK generator based on a weclapp api swagger file",
|
|
5
5
|
"author": "weclapp",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"files": [
|
|
19
19
|
"bin",
|
|
20
20
|
"dist",
|
|
21
|
-
"tsconfig.
|
|
21
|
+
"tsconfig.sdk.json"
|
|
22
22
|
],
|
|
23
23
|
"engines": {
|
|
24
24
|
"node": ">=20",
|
|
@@ -36,37 +36,43 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
39
|
+
"build": "rollup --config rollup.config.ts --configPlugin typescript={tsconfig:\\'tsconfig.node.json\\'} --configImportAttributesKey with",
|
|
40
|
+
"build:watch": "npm run build -- --watch",
|
|
41
|
+
"cli:browser": "./bin/cli.js test/openapi.json --target browser",
|
|
42
|
+
"cli:browser:cache": "./bin/cli.js test/openapi.json --target browser --cache",
|
|
43
|
+
"cli:browser.rx": "./bin/cli.js test/openapi.json --target browser.rx",
|
|
44
|
+
"cli:browser.rx:cache": "./bin/cli.js test/openapi.json --target browser.rx --cache",
|
|
45
|
+
"cli:node": "./bin/cli.js test/openapi.json --target node",
|
|
46
|
+
"cli:node:cache": "./bin/cli.js test/openapi.json --target node --cache",
|
|
47
|
+
"cli:node.rx": "./bin/cli.js test/openapi.json --target node.rx",
|
|
48
|
+
"cli:node.rx:cache": "./bin/cli.js test/openapi.json --target node.rx --cache",
|
|
42
49
|
"lint": "eslint src/**/*.ts",
|
|
43
50
|
"lint:fix": "npm run lint -- --fix",
|
|
44
|
-
"ci
|
|
51
|
+
"ci": "npm run lint && npm run build && npm run cli:browser",
|
|
45
52
|
"release": "standard-version"
|
|
46
53
|
},
|
|
47
54
|
"devDependencies": {
|
|
48
|
-
"@
|
|
49
|
-
"@typescript-eslint/
|
|
55
|
+
"@rollup/pluginutils": "5.1.4",
|
|
56
|
+
"@typescript-eslint/eslint-plugin": "8.25.0",
|
|
57
|
+
"@typescript-eslint/parser": "8.25.0",
|
|
50
58
|
"eslint": "^8.46.0",
|
|
51
59
|
"rollup-plugin-string": "^3.0.0",
|
|
52
60
|
"standard-version": "^9.5.0"
|
|
53
61
|
},
|
|
54
62
|
"dependencies": {
|
|
55
|
-
"@rollup/plugin-
|
|
56
|
-
"@rollup/plugin-
|
|
57
|
-
"@types/fs-extra": "
|
|
58
|
-
"@types/yargs": "
|
|
59
|
-
"chalk": "
|
|
60
|
-
"change-case": "
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"typescript": "^5.1.6",
|
|
69
|
-
"yargs": "^17.7.2"
|
|
63
|
+
"@rollup/plugin-terser": "0.4.4",
|
|
64
|
+
"@rollup/plugin-typescript": "12.1.2",
|
|
65
|
+
"@types/fs-extra": "11.0.1",
|
|
66
|
+
"@types/yargs": "17.0.33",
|
|
67
|
+
"chalk": "5.3.0",
|
|
68
|
+
"change-case": "4.1.2",
|
|
69
|
+
"dotenv": "16.3.1",
|
|
70
|
+
"indent-string": "5.0.0",
|
|
71
|
+
"openapi-types": "12.1.3",
|
|
72
|
+
"pretty-ms": "8.0.0",
|
|
73
|
+
"rollup": "4.34.8",
|
|
74
|
+
"typescript": "5.7.3",
|
|
75
|
+
"yargs": "17.7.2"
|
|
70
76
|
},
|
|
71
77
|
"peerDependencies": {
|
|
72
78
|
"rxjs": "^7.8.0"
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"compilerOptions": {
|
|
3
|
+
"baseUrl": ".",
|
|
3
4
|
"declaration": true,
|
|
4
|
-
"strict": true,
|
|
5
|
-
"noImplicitAny": true,
|
|
6
5
|
"esModuleInterop": true,
|
|
6
|
+
"module": "ESNext",
|
|
7
7
|
"moduleResolution": "node",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"baseUrl": "."
|
|
8
|
+
"noImplicitAny": true,
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"strict": true,
|
|
11
|
+
"target": "ES2022"
|
|
13
12
|
},
|
|
14
13
|
"include": [
|
|
15
|
-
"
|
|
14
|
+
"sdk/**/*.ts"
|
|
16
15
|
]
|
|
17
16
|
}
|