-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompile.ts
110 lines (89 loc) · 2.91 KB
/
compile.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as crypto from 'crypto';
import * as path from 'path';
import * as fs from 'fs-extra';
import { Options } from './options';
import { exec, validateOptions } from './util';
const compilerModule = require.resolve('jsii/bin/jsii');
/**
* Compiles the source files in `workdir` with jsii.
*/
export async function compile(workdir: string, options: Options) {
validateOptions(options);
const args = ['--silence-warnings', 'reserved-word'];
const entrypoint = options.entrypoint ?? 'index.ts';
if (path.extname(entrypoint) !== '.ts') {
throw new Error(`jsii entrypoint must be a .ts file: ${entrypoint}`);
}
if (!(await fs.pathExists(path.join(workdir, entrypoint)))) {
throw new Error(`unable to find typescript entrypoint: ${path.join(workdir, entrypoint)}`);
}
// path to entrypoint without extension
const basepath = path.join(path.dirname(entrypoint), path.basename(entrypoint, '.ts'));
const moduleKey = options.moduleKey?.replace(/\./g, '').replace(/\//g, '') ?? crypto.createHash('sha256').update(basepath, 'utf8').digest('hex');
// jsii modules to include
const moduleDirs = options.deps ?? [];
const targets: Record<string, any> = { };
const deps: Record<string, string> = { };
for (const dir of moduleDirs) {
// read module metadata
const metadata = await fs.readJson(path.join(dir, 'package.json'));
const moduleName: string = metadata.name;
const moduleVersion: string = metadata.version;
const targetdir = path.join(path.join(workdir, 'node_modules'), moduleName);
await fs.mkdirp(path.dirname(targetdir));
await fs.copy(dir, targetdir);
// add to "deps" and "peer deps"
if (!moduleName.startsWith('@types/')) {
deps[moduleName] = moduleVersion;
}
}
const pkg = {
name: moduleKey,
version: '0.0.0',
author: '[email protected]',
main: `${basepath}.js`,
types: `${basepath}.d.ts`,
license: 'UNLICENSED',
repository: { url: 'http://generated', type: 'git' },
jsii: {
outdir: 'dist',
targets: targets,
},
dependencies: deps,
peerDependencies: deps,
};
if (options.exports) {
(pkg as Record<string, any>).exports = options.exports;
}
if (options.python) {
targets.python = {
distName: 'generated',
module: options.python.moduleName,
};
}
if (options.java) {
targets.java = {
package: options.java.package,
maven: {
groupId: 'generated',
artifactId: 'generated',
},
};
}
if (options.csharp) {
targets.dotnet = {
namespace: options.csharp.namespace,
packageId: options.csharp.namespace,
};
}
if (options.golang) {
targets.go = {
moduleName: options.golang.moduleName,
packageName: options.golang.packageName,
};
}
await fs.writeFile(path.join(workdir, 'package.json'), JSON.stringify(pkg, undefined, 2));
await exec(compilerModule, args, {
cwd: workdir,
});
}