forked from cdklabs/jsii-srcmak
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.ts
More file actions
75 lines (65 loc) · 2.41 KB
/
util.ts
File metadata and controls
75 lines (65 loc) · 2.41 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
import * as fs from 'fs-extra';
import * as path from 'path';
import * as os from 'os';
import { spawn, SpawnOptions } from 'child_process';
import { Options } from './options';
export async function mkdtemp(closure: (dir: string) => Promise<void>) {
const workdir = await fs.mkdtemp(path.join(os.tmpdir(), 'temp-'));
try {
await closure(workdir);
if (!process.env.RETAIN_TMP) {
await fs.remove(workdir);
} else {
console.error(`NOTE: Temp directory retained (RETAIN_TMP=1): ${workdir}`);
}
} catch(e) {
console.error(`NOTE: Temp directory retained due to an error: ${workdir}`);
throw e;
}
}
export async function exec(moduleName: string, args: string[] = [], options: SpawnOptions = { }) {
return new Promise((ok, fail) => {
const opts: SpawnOptions = {
...options,
stdio: [ 'inherit', 'pipe', 'pipe' ],
};
const child = spawn(process.execPath, [ moduleName, ...args ], opts);
const data = new Array<Buffer>();
child.stdout?.on('data', chunk => data.push(chunk));
child.stderr?.on('data', chunk => data.push(chunk));
const newError = (message: string) => new Error([
message,
' | ' + Buffer.concat(data).toString('utf-8').split('\n').filter(x => x).join('\n | '),
' +----------------------------------------------------------------------------------',
` | Command: ${moduleName} ${args.join(' ')}`,
` | Workdir: ${path.resolve(options.cwd ?? '.')}`,
' +----------------------------------------------------------------------------------',
].join('\n'));
child.once('error', err => {
throw newError(`jsii compilation failed. error: ${err.message}`);
});
child.once('exit', code => {
if (code === 0) {
return ok();
}
else {
return fail(newError(`jsii compilation failed with non-zero exit code: ${code}`));
}
});
});
}
/**
* This validates that the Python module name and Java package name
* conform to language-specific constraints.
*
* @param options Options set by the consumer
* @throws error if options do not conform
*/
export function validateOptions(options: Options) {
if (options.python?.moduleName.includes('-')) {
throw new Error(`Python moduleName [${options.python.moduleName}] may not contain "-"`);
}
if (options.java?.package.includes('-')) {
throw new Error(`Java package [${options.java.package}] may not contain "-"`);
}
}