forked from cdklabs/jsii-srcmak
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrcmak.ts
More file actions
48 lines (39 loc) · 1.6 KB
/
srcmak.ts
File metadata and controls
48 lines (39 loc) · 1.6 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
import * as fs from 'fs-extra';
import * as path from 'path';
import { exec, mkdtemp } from './util';
import { compile } from './compile';
import { Options } from './options';
import { ncp as _ncp } from 'ncp';
import { promisify } from 'util';
const ncp = promisify(_ncp);
const pacmakModule = require.resolve('jsii-pacmak/bin/jsii-pacmak');
export async function srcmak(srcdir: string, options: Options = { }) {
if (!(await fs.pathExists(srcdir))) {
throw new Error(`unable to find source directory ${srcdir}`);
}
await mkdtemp(async workdir => {
// copy sources to temp directory
await fs.copy(srcdir, workdir);
// perform jsii compilation
await compile(workdir, options);
// extract .jsii if requested
if (options.jsii) {
await fs.copy(path.join(workdir, '.jsii'), options.jsii.path);
}
// run pacmak to generate code
await exec(pacmakModule, [ '--code-only' ], { cwd: workdir });
// extract code based on selected languages
if (options.python) {
const reldir = options.python.moduleName.replace(/\./g, '/').replace(/-/g, '_'); // jsii replaces "." with "/"
const source = path.resolve(path.join(workdir, 'dist/python/src', reldir));
const target = path.join(options.python.outdir, reldir);
await fs.move(source, target, { overwrite: true });
}
if (options.java) {
const source = path.resolve(path.join(workdir, 'dist/java/src/'));
const target = path.join(options.java.outdir, 'src/');
await fs.mkdirp(target); // make sure target directory exists
await ncp(source, target, { clobber: false });
}
});
}