-
Notifications
You must be signed in to change notification settings - Fork 246
/
js.ts
134 lines (122 loc) · 3.49 KB
/
js.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as spec from '@jsii/spec';
import { Generator, Legalese } from '../generator';
import { PackageInfo, Target } from '../target';
import { toReleaseVersion } from './version-utils';
import { TargetName } from '.';
export default class JavaScript extends Target {
public static toPackageInfos(assm: spec.Assembly): {
[language: string]: PackageInfo;
} {
const releaseVersion = toReleaseVersion(
assm.version,
TargetName.JAVASCRIPT,
);
const packageInfo: PackageInfo = {
repository: 'NPM',
url: `https://www.npmjs.com/package/${assm.name}/v/${releaseVersion}`,
usage: {
'package.json': {
language: 'js',
code: JSON.stringify({ [assm.name]: `^${releaseVersion}` }, null, 2),
},
npm: {
language: 'console',
code: `$ npm i ${assm.name}@${releaseVersion}`,
},
yarn: {
language: 'console',
code: `$ yarn add ${assm.name}@${releaseVersion}`,
},
},
};
return { typescript: packageInfo, javascript: packageInfo };
}
public static toNativeReference(type: spec.Type) {
const [, ...name] = type.fqn.split('.');
const resolvedName = name.join('.');
const result: { typescript: string; javascript?: string } = {
typescript: `import { ${resolvedName} } from '${type.assembly}';`,
};
if (!spec.isInterfaceType(type)) {
result.javascript = `const { ${resolvedName} } = require('${type.assembly}');`;
} else {
result.javascript = `// ${resolvedName} is an interface`;
}
return result;
}
protected readonly generator = new PackOnly();
public async build(sourceDir: string, outDir: string) {
return this.copyFiles(sourceDir, outDir);
}
}
// ##################
// # CODE GENERATOR #
// ##################
class PackOnly extends Generator {
public constructor() {
// NB: This does not generate code, so runtime type checking is irrelevant
super({ runtimeTypeChecking: false });
}
public async save(outdir: string, tarball: string, _: Legalese) {
// Intentionally ignore the Legalese field here... it's not useful here.
return super.save(outdir, tarball, {});
}
protected getAssemblyOutputDir(_mod: spec.Assembly) {
return '.';
}
protected onBeginInterface(_ifc: spec.InterfaceType) {
return;
}
protected onEndInterface(_ifc: spec.InterfaceType) {
return;
}
protected onInterfaceMethod(_ifc: spec.InterfaceType, _method: spec.Method) {
return;
}
protected onInterfaceMethodOverload(
_ifc: spec.InterfaceType,
_overload: spec.Method,
_originalMethod: spec.Method,
) {
return;
}
protected onInterfaceProperty(
_ifc: spec.InterfaceType,
_prop: spec.Property,
) {
return;
}
protected onProperty(_cls: spec.ClassType, _prop: spec.Property) {
return;
}
protected onStaticProperty(_cls: spec.ClassType, _prop: spec.Property) {
return;
}
protected onUnionProperty(
_cls: spec.ClassType,
_prop: spec.Property,
_union: spec.UnionTypeReference,
) {
return;
}
protected onMethod(_cls: spec.ClassType, _method: spec.Method) {
return;
}
protected onMethodOverload(
_cls: spec.ClassType,
_overload: spec.Method,
_originalMethod: spec.Method,
) {
return;
}
protected onStaticMethod(_cls: spec.ClassType, _method: spec.Method) {
return;
}
protected onStaticMethodOverload(
_cls: spec.ClassType,
_overload: spec.Method,
_originalMethod: spec.Method,
) {
return;
}
}