UNPKG

4.18 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.findLocalBuildDirs = exports.Target = void 0;
4const fs = require("fs-extra");
5const path = require("path");
6const spdx = require("spdx-license-list/full");
7const dependency_graph_1 = require("./dependency-graph");
8const logging = require("./logging");
9class Target {
10 constructor(options) {
11 this.arguments = options.arguments;
12 this.assembly = options.assembly;
13 this.fingerprint = options.fingerprint ?? true;
14 this.force = options.force ?? false;
15 this.packageDir = options.packageDir;
16 this.rosetta = options.rosetta;
17 this.runtimeTypeChecking = options.runtimeTypeChecking;
18 this.targetName = options.targetName;
19 }
20 /**
21 * Emits code artifacts.
22 *
23 * @param outDir the directory where the generated source will be placed.
24 */
25 async generateCode(outDir, tarball) {
26 await this.generator.load(this.packageDir, this.assembly);
27 if (this.force || !(await this.generator.upToDate(outDir))) {
28 this.generator.generate(this.fingerprint);
29 const licenseFile = path.join(this.packageDir, 'LICENSE');
30 const license = (await fs.pathExists(licenseFile))
31 ? await fs.readFile(licenseFile, 'utf8')
32 : spdx[this.assembly.license]?.licenseText;
33 const noticeFile = path.join(this.packageDir, 'NOTICE');
34 const notice = (await fs.pathExists(noticeFile))
35 ? await fs.readFile(noticeFile, 'utf8')
36 : undefined;
37 await this.generator.save(outDir, tarball, { license, notice });
38 }
39 else {
40 logging.info(`Generated code for ${this.targetName} was already up-to-date in ${outDir} (use --force to re-generate)`);
41 }
42 }
43 /**
44 * A utility to copy files from one directory to another.
45 *
46 * @param sourceDir the directory to copy from.
47 * @param targetDir the directory to copy into.
48 */
49 async copyFiles(sourceDir, targetDir) {
50 // Preemptively create target directory, to avoid unsafely racing on it's creation.
51 await fs.mkdirp(targetDir);
52 await fs.copy(sourceDir, targetDir, { recursive: true });
53 }
54 /**
55 * Traverses the dep graph and returns a list of pacmak output directories
56 * available locally for this specific target. This allows target builds to
57 * take local dependencies in case a dependency is checked-out.
58 *
59 * @param packageDir The directory of the package to resolve from.
60 */
61 async findLocalDepsOutput(rootPackageDir) {
62 return findLocalBuildDirs(rootPackageDir, this.targetName);
63 }
64}
65exports.Target = Target;
66/**
67 * Traverses the dep graph and returns a list of pacmak output directories
68 * available locally for this specific target. This allows target builds to
69 * take local dependencies in case a dependency is checked-out.
70 *
71 * @param packageDir The directory of the package to resolve from.
72 */
73async function findLocalBuildDirs(rootPackageDir, targetName) {
74 const results = new Set();
75 await (0, dependency_graph_1.traverseDependencyGraph)(rootPackageDir, processPackage);
76 return Array.from(results);
77 async function processPackage(packageDir, pkg, isRoot) {
78 // no jsii or jsii.outdir - either a misconfigured jsii package or a non-jsii dependency. either way, we are done here.
79 if (!pkg.jsii || !pkg.jsii.outdir) {
80 return false;
81 }
82 if (isRoot) {
83 // This is the root package - no need to register it's outdir
84 return true;
85 }
86 // if an output directory exists for this module, then we add it to our
87 // list of results (unless it's the root package, which we are currently building)
88 const outdir = path.join(packageDir, pkg.jsii.outdir, targetName);
89 if (await fs.pathExists(outdir)) {
90 logging.debug(`Found ${outdir} as a local dependency output`);
91 results.add(outdir);
92 }
93 return true;
94 }
95}
96exports.findLocalBuildDirs = findLocalBuildDirs;
97//# sourceMappingURL=target.js.map
\No newline at end of file