UNPKG

5.14 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.updateAllNpmIgnores = exports.findJsiiModules = void 0;
4const spec = require("@jsii/spec");
5const fs = require("fs-extra");
6const path = require("path");
7const logging = require("../lib/logging");
8const packaging_1 = require("./packaging");
9const toposort_1 = require("./toposort");
10const util_1 = require("./util");
11/**
12 * Find all modules that need to be packagerd
13 *
14 * If the input list is empty, include the current directory.
15 *
16 * The result is topologically sorted.
17 */
18async function findJsiiModules(directories, recurse) {
19 const ret = [];
20 const visited = new Set();
21 const toVisit = directories.length > 0 ? directories : ['.'];
22 await Promise.all(toVisit.map((dir) => visitPackage(dir, true)));
23 return (0, toposort_1.topologicalSort)(ret, (m) => m.name, (m) => m.dependencyNames);
24 async function visitPackage(dir, isRoot) {
25 const realPath = await fs.realpath(dir);
26 if (visited.has(realPath)) {
27 return;
28 } // Already visited
29 visited.add(realPath);
30 const pkg = await fs.readJson(path.join(realPath, 'package.json'));
31 if (!pkg.jsii?.outdir || !pkg.jsii?.targets) {
32 if (isRoot) {
33 throw new Error(`Invalid "jsii" section in ${realPath}. Expecting "outdir" and "targets"`);
34 }
35 else {
36 return; // just move on, this is not a jsii package
37 }
38 }
39 if (!pkg.name) {
40 throw new Error(`package.json does not have a 'name' field: ${JSON.stringify(pkg, undefined, 2)}`);
41 }
42 const dependencyNames = [
43 ...Object.keys(pkg.dependencies ?? {}),
44 ...Object.keys(pkg.peerDependencies ?? {}),
45 ...Object.keys(pkg.devDependencies ?? {}),
46 ];
47 // if --recurse is set, find dependency dirs and build them.
48 if (recurse) {
49 await Promise.all(dependencyNames.flatMap(async (dep) => {
50 if ((0, util_1.isBuiltinModule)(dep)) {
51 return [];
52 }
53 try {
54 const depDir = await (0, util_1.findDependencyDirectory)(dep, realPath);
55 return [await visitPackage(depDir, false)];
56 }
57 catch (e) {
58 // Some modules like `@types/node` cannot be require()d, but we also don't need them.
59 if (!['MODULE_NOT_FOUND', 'ERR_PACKAGE_PATH_NOT_EXPORTED'].includes(e.code)) {
60 throw e;
61 }
62 return [];
63 }
64 }));
65 }
66 // outdir is either by package.json/jsii.outdir (relative to package root) or via command line (relative to cwd)
67 const outputDirectory = pkg.jsii.outdir && path.resolve(realPath, pkg.jsii.outdir);
68 const targets = [...Object.keys(pkg.jsii.targets), 'js']; // "js" is an implicit target.
69 ret.push(new packaging_1.JsiiModule({
70 name: pkg.name,
71 moduleDirectory: realPath,
72 defaultOutputDirectory: outputDirectory,
73 availableTargets: targets,
74 dependencyNames,
75 }));
76 }
77}
78exports.findJsiiModules = findJsiiModules;
79async function updateAllNpmIgnores(packages) {
80 await Promise.all(packages.map((pkg) => updateNpmIgnore(pkg.moduleDirectory, pkg.outputDirectory)));
81}
82exports.updateAllNpmIgnores = updateAllNpmIgnores;
83async function updateNpmIgnore(packageDir, excludeOutdir) {
84 const npmIgnorePath = path.join(packageDir, '.npmignore');
85 let lines = new Array();
86 let modified = false;
87 if (await fs.pathExists(npmIgnorePath)) {
88 lines = (await fs.readFile(npmIgnorePath)).toString().split('\n');
89 }
90 // if this is a fresh .npmignore, we can be a bit more opinionated
91 // otherwise, we add just add stuff that's critical
92 if (lines.length === 0) {
93 excludePattern('Exclude typescript source and config', '*.ts', 'tsconfig.json', '*.tsbuildinfo');
94 includePattern('Include javascript files and typescript declarations', '*.js', '*.d.ts');
95 }
96 if (excludeOutdir) {
97 excludePattern('Exclude jsii outdir', path.relative(packageDir, excludeOutdir));
98 }
99 includePattern('Include .jsii and .jsii.gz', spec.SPEC_FILE_NAME, spec.SPEC_FILE_NAME_COMPRESSED);
100 if (modified) {
101 await fs.writeFile(npmIgnorePath, `${lines.join('\n')}\n`);
102 logging.info('Updated .npmignore');
103 }
104 function includePattern(comment, ...patterns) {
105 excludePattern(comment, ...patterns.map((p) => `!${p}`));
106 }
107 function excludePattern(comment, ...patterns) {
108 let first = true;
109 for (const pattern of patterns) {
110 if (lines.includes(pattern)) {
111 return; // already in .npmignore
112 }
113 modified = true;
114 if (first) {
115 lines.push('');
116 lines.push(`# ${comment}`);
117 first = false;
118 }
119 lines.push(pattern);
120 }
121 }
122}
123//# sourceMappingURL=npm-modules.js.map
\No newline at end of file