1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.JsiiModule = exports.DEFAULT_PACK_COMMAND = void 0;
|
4 | const fs = require("fs-extra");
|
5 | const os = require("os");
|
6 | const path = require("path");
|
7 | const logging = require("../lib/logging");
|
8 | const util_1 = require("./util");
|
9 | exports.DEFAULT_PACK_COMMAND = 'npm pack';
|
10 | class JsiiModule {
|
11 | constructor(options) {
|
12 | this.name = options.name;
|
13 | this.moduleDirectory = options.moduleDirectory;
|
14 | this.availableTargets = options.availableTargets;
|
15 | this.outputDirectory = options.defaultOutputDirectory;
|
16 | this.dependencyNames = options.dependencyNames ?? [];
|
17 | }
|
18 | |
19 |
|
20 |
|
21 | async npmPack(packCommand = exports.DEFAULT_PACK_COMMAND) {
|
22 | this._tarball = await util_1.Scratch.make(async (tmpdir) => {
|
23 | const args = [];
|
24 | if (packCommand === exports.DEFAULT_PACK_COMMAND) {
|
25 |
|
26 |
|
27 | args.push(JSON.stringify(this.moduleDirectory));
|
28 | if (logging.level.valueOf() >= logging.LEVEL_VERBOSE) {
|
29 | args.push('--loglevel=verbose');
|
30 | }
|
31 | }
|
32 | else {
|
33 |
|
34 | await fs.copy(this.moduleDirectory, tmpdir, { dereference: true });
|
35 | }
|
36 | const out = await (0, util_1.shell)(packCommand, args, {
|
37 | cwd: tmpdir,
|
38 | });
|
39 |
|
40 |
|
41 |
|
42 | const lines = out.trim().split(os.EOL);
|
43 | const lastLine = lines[lines.length - 1].trim();
|
44 | if (!lastLine.endsWith('.tgz') && !lastLine.endsWith('.tar.gz')) {
|
45 | throw new Error(`${packCommand} did not produce tarball from ${this.moduleDirectory} into ${tmpdir} (output was ${JSON.stringify(lines)})`);
|
46 | }
|
47 | return path.resolve(tmpdir, lastLine);
|
48 | });
|
49 | }
|
50 | get tarball() {
|
51 | if (!this._tarball) {
|
52 | throw new Error('Tarball not available yet, call npmPack() first');
|
53 | }
|
54 | return this._tarball.object;
|
55 | }
|
56 | async load(system, validate = true) {
|
57 | return system
|
58 | .loadModule(this.moduleDirectory, { validate })
|
59 | .then((assembly) => (this._assembly = assembly));
|
60 | }
|
61 | get assembly() {
|
62 | if (!this._assembly) {
|
63 | throw new Error('Assembly not available yet, call load() first');
|
64 | }
|
65 | return this._assembly;
|
66 | }
|
67 | async cleanup() {
|
68 | if (this._tarball) {
|
69 | await this._tarball.cleanup();
|
70 | }
|
71 | }
|
72 | }
|
73 | exports.JsiiModule = JsiiModule;
|
74 |
|
\ | No newline at end of file |