1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
13 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
14 | };
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | exports.installOnDemand = exports.getOnDemandDependencies = exports.onDemandDependenciesFromPackageJSON = exports.getPackageRoot = exports.getPackageJSONPath = exports.assertResolvable = void 0;
|
17 | const child_process_1 = require("child_process");
|
18 | const fs_1 = require("fs");
|
19 | const path_1 = __importDefault(require("path"));
|
20 | const pkg_install_1 = require("pkg-install");
|
21 | const pkg_up_1 = __importDefault(require("pkg-up"));
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | exports.assertResolvable = async (id) => {
|
31 | await new Promise(async (resolve, reject) => {
|
32 | child_process_1.exec(`${process.execPath} -e "require.resolve(process.env.ID)"`, {
|
33 | cwd: await exports.getPackageRoot() || process.cwd(),
|
34 | env: Object.assign(Object.assign({}, process.env), { ID: id })
|
35 | }, (error) => {
|
36 | if (error != null) {
|
37 | reject(error);
|
38 | return;
|
39 | }
|
40 | resolve();
|
41 | });
|
42 | });
|
43 | };
|
44 | exports.getPackageJSONPath = async () => {
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | return pkg_up_1.default({ cwd: __dirname });
|
52 | };
|
53 | exports.getPackageRoot = async () => {
|
54 | const packageJSONPath = await exports.getPackageJSONPath();
|
55 | return packageJSONPath != null ? path_1.default.dirname(packageJSONPath) : null;
|
56 | };
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | exports.onDemandDependenciesFromPackageJSON = (packageJSON) => {
|
62 | const onDemandDependencies = new Map();
|
63 | const onDemandList = (packageJSON === null || packageJSON === void 0 ? void 0 : packageJSON.installsOnDemand) || [];
|
64 | for (const packageName of onDemandList) {
|
65 | onDemandDependencies.set(packageName, '*');
|
66 | }
|
67 | return onDemandDependencies;
|
68 | };
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | exports.getOnDemandDependencies = (() => {
|
80 | let cached = null;
|
81 | return async () => {
|
82 | if (cached == null) {
|
83 | const packageJSONPath = await exports.getPackageJSONPath();
|
84 | if (packageJSONPath != null) {
|
85 | const rawPackageJSON = await fs_1.promises.readFile(packageJSONPath, {
|
86 | encoding: 'utf-8',
|
87 | });
|
88 | const packageJSON = JSON.parse(rawPackageJSON.toString());
|
89 | cached = exports.onDemandDependenciesFromPackageJSON(packageJSON);
|
90 | }
|
91 | }
|
92 | return cached;
|
93 | };
|
94 | })();
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | exports.installOnDemand = async (packageName) => {
|
106 | try {
|
107 | await exports.assertResolvable(packageName);
|
108 | return;
|
109 | }
|
110 | catch (_error) {
|
111 | }
|
112 | let dependencies = new Map();
|
113 | try {
|
114 | dependencies = await exports.getOnDemandDependencies();
|
115 | }
|
116 | catch (error) {
|
117 | console.error(error);
|
118 | }
|
119 | if (!dependencies.has(packageName)) {
|
120 | throw new Error(`Package "${packageName}" cannot be installed on demand. ${dependencies}`);
|
121 | }
|
122 | const version = dependencies.get(packageName);
|
123 | await pkg_install_1.install({ [packageName]: version }, {
|
124 | stdio: 'inherit',
|
125 | cwd: (await exports.getPackageRoot()) || process.cwd(),
|
126 | noSave: true,
|
127 | });
|
128 | console.log(`Package "${packageName}@${version} installed."`);
|
129 | };
|
130 |
|
\ | No newline at end of file |