-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
sdk.ts
76 lines (74 loc) · 2.89 KB
/
sdk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { ChildProcess } from '@bcms/selfhosted-utils/child-process';
import path from 'path';
import { FS } from '@bcms/selfhosted-utils/fs';
import { buildCjs, buildMjs } from './utils/build';
import { getBackendVersion, getUtilsVersion } from './utils/versions';
import { packageJsonExport } from './utils/package-json';
export async function packSdk() {
await ChildProcess.advancedExec('npm pack', {
cwd: path.join(process.cwd(), 'ui', 'dist-sdk'),
onChunk(type, chunk) {
process[type].write(chunk);
},
}).awaiter;
}
export async function buildSdk() {
const basePath = path.join(process.cwd(), 'ui');
const localFs = new FS(basePath);
if (await localFs.exist(['dist-sdk'])) {
await localFs.deleteDir(['dist-sdk']);
}
await buildMjs(localFs, basePath, 'build:sdk:mjs', 'dist-sdk', async () => {
const delDirs = ['backend'];
for (let i = 0; i < delDirs.length; i++) {
const delDir = delDirs[i];
await localFs.deleteDir(['dist-sdk', 'mjs', delDir]);
}
const files = await localFs.fileTree(
['dist-sdk', 'mjs', 'ui', 'src', 'sdk'],
'',
);
for (let i = 0; i < files.length; i++) {
const fileInfo = files[i];
await localFs.move(
['dist-sdk', 'mjs', 'ui', 'src', 'sdk', fileInfo.path.rel],
['dist-sdk', 'mjs', fileInfo.path.rel],
);
}
});
await buildCjs(localFs, basePath, 'build:sdk:cjs', 'dist-sdk', async () => {
const delDirs = ['backend'];
for (let i = 0; i < delDirs.length; i++) {
const delDir = delDirs[i];
await localFs.deleteDir(['dist-sdk', 'cjs', delDir]);
}
const files = await localFs.fileTree(
['dist-sdk', 'cjs', 'ui', 'src', 'sdk'],
'',
);
for (let i = 0; i < files.length; i++) {
const fileInfo = files[i];
await localFs.move(
['dist-sdk', 'cjs', 'ui', 'src', 'sdk', fileInfo.path.rel],
['dist-sdk', 'cjs', fileInfo.path.rel],
);
}
});
await localFs.copy('SDK-README.md', ['dist-sdk', 'README.md']);
await localFs.copy(['..', 'LICENSE'], ['dist-sdk', 'LICENSE']);
const packageJson = JSON.parse(
await localFs.readString('sdk.package.json'),
);
packageJson.devDependencies = undefined;
packageJson.scripts = {};
const backendVersion = await getBackendVersion();
const utilsVersion = await getUtilsVersion();
packageJson.dependencies[backendVersion[0]] = '^' + backendVersion[1];
packageJson.dependencies[utilsVersion[0]] = '^' + utilsVersion[1];
let files = await localFs.fileTree(['dist-sdk'], '');
packageJsonExport(files, packageJson);
await localFs.save(
['dist-sdk', 'package.json'],
JSON.stringify(packageJson, null, 4),
);
}