-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathbase.ts
More file actions
102 lines (82 loc) · 2.94 KB
/
base.ts
File metadata and controls
102 lines (82 loc) · 2.94 KB
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
import * as fs from 'fs-extra';
import * as path from 'path';
import { CodeMaker } from 'codemaker';
import { mkdtemp } from '../util';
import * as srcmak from 'jsii-srcmak';
export enum Language {
TYPESCRIPT = 'typescript',
PYTHON = 'python',
DOTNET = 'dotnet',
JAVA = 'java',
}
export const LANGUAGES = [ Language.TYPESCRIPT, Language.PYTHON ];
export interface ImportOptions {
readonly moduleNamePrefix?: string;
readonly targetLanguage: Language;
readonly outdir: string;
/**
* Path to copy the output .jsii file.
* @default - jsii file is not emitted
*/
readonly outputJsii?: string;
}
export abstract class ImportBase {
public abstract get moduleNames(): string[];
protected abstract async generateTypeScript(code: CodeMaker, moduleName?: string): Promise<void>;
public async import(options: ImportOptions) {
const code = new CodeMaker();
const outdir = path.resolve(options.outdir);
await fs.mkdirp(outdir);
const isTypescript = options.targetLanguage === Language.TYPESCRIPT
const { moduleNamePrefix } = options;
if (this.moduleNames.length === 0) {
console.error(`warning: no definitions to import`);
}
for (const name of this.moduleNames) {
// output the name of the imported resource
console.log(name);
const fileName = moduleNamePrefix ? `${moduleNamePrefix}-${name}.ts` : `${name}.ts`;
code.openFile(fileName);
code.indentation = 2;
await this.generateTypeScript(code, name);
code.closeFile(fileName);
if (isTypescript) {
await code.save(outdir);
}
if (!isTypescript || options.outputJsii) {
await mkdtemp(async staging => {
// this is not typescript, so we generate in a staging directory and
// use jsii-srcmak to compile and extract the language-specific source
// into our project.
await code.save(staging);
// these are the module dependencies we compile against
const deps = [ '@types/node', 'constructs', 'cdk8s' ];
const opts: srcmak.Options = {
entrypoint: fileName,
deps: deps.map(dep => path.dirname(require.resolve(`${dep}/package.json`))),
};
// used for testing.
if (options.outputJsii) {
opts.jsii = { path: options.outputJsii };
}
// python!
if (options.targetLanguage === Language.PYTHON) {
opts.python = {
outdir: outdir,
moduleName: moduleNamePrefix ? `${moduleNamePrefix}.${name}` : name
};
}
// java!
if (options.targetLanguage === Language.JAVA) {
const javaName = name.replace(/\//g, '.')
opts.java = {
outdir: '.',
package: `imports.${moduleNamePrefix ? moduleNamePrefix + '.' + javaName : javaName}`
};
}
await srcmak.srcmak(staging, opts);
});
}
}
}
}