1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.updateAllNpmIgnores = exports.findJsiiModules = void 0;
|
4 | const spec = require("@jsii/spec");
|
5 | const fs = require("fs-extra");
|
6 | const path = require("path");
|
7 | const logging = require("../lib/logging");
|
8 | const packaging_1 = require("./packaging");
|
9 | const toposort_1 = require("./toposort");
|
10 | const util_1 = require("./util");
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | async 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 | }
|
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;
|
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 |
|
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 |
|
59 | if (!['MODULE_NOT_FOUND', 'ERR_PACKAGE_PATH_NOT_EXPORTED'].includes(e.code)) {
|
60 | throw e;
|
61 | }
|
62 | return [];
|
63 | }
|
64 | }));
|
65 | }
|
66 |
|
67 | const outputDirectory = pkg.jsii.outdir && path.resolve(realPath, pkg.jsii.outdir);
|
68 | const targets = [...Object.keys(pkg.jsii.targets), 'js'];
|
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 | }
|
78 | exports.findJsiiModules = findJsiiModules;
|
79 | async function updateAllNpmIgnores(packages) {
|
80 | await Promise.all(packages.map((pkg) => updateNpmIgnore(pkg.moduleDirectory, pkg.outputDirectory)));
|
81 | }
|
82 | exports.updateAllNpmIgnores = updateAllNpmIgnores;
|
83 | async 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 |
|
91 |
|
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;
|
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 |
|
\ | No newline at end of file |