UNPKG

3.09 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.JsiiModule = exports.DEFAULT_PACK_COMMAND = void 0;
4const fs = require("fs-extra");
5const os = require("os");
6const path = require("path");
7const logging = require("../lib/logging");
8const util_1 = require("./util");
9exports.DEFAULT_PACK_COMMAND = 'npm pack';
10class 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 * Prepare an NPM package from this source module
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 // Quoting (JSON-stringifying) the module directory in order to avoid
26 // problems if there are spaces or other special characters in the path.
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 // Ensure module is copied to tmpdir to ensure parallel execution does not contend on generated tarballs
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 // Take only the last line of npm pack which should contain the
40 // tarball name. otherwise, there can be a lot of extra noise there
41 // from scripts that emit to STDOUT.
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}
73exports.JsiiModule = JsiiModule;
74//# sourceMappingURL=packaging.js.map
\No newline at end of file