UNPKG

7.17 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21var __importDefault = (this && this.__importDefault) || function (mod) {
22 return (mod && mod.__esModule) ? mod : { "default": mod };
23};
24Object.defineProperty(exports, "__esModule", { value: true });
25exports.getLatestVersion = exports.getDependencies = exports.detectYarn = void 0;
26const path = __importStar(require("path"));
27const fs = __importStar(require("fs"));
28const cp = __importStar(require("child_process"));
29const parse_semver_1 = __importDefault(require("parse-semver"));
30const util_1 = require("./util");
31const exists = (file) => fs.promises.stat(file).then(_ => true, _ => false);
32function parseStdout({ stdout }) {
33 return stdout.split(/[\r\n]/).filter(line => !!line)[0];
34}
35function exec(command, options = {}, cancellationToken) {
36 return new Promise((c, e) => {
37 let disposeCancellationListener = null;
38 const child = cp.exec(command, { ...options, encoding: 'utf8' }, (err, stdout, stderr) => {
39 if (disposeCancellationListener) {
40 disposeCancellationListener();
41 disposeCancellationListener = null;
42 }
43 if (err) {
44 return e(err);
45 }
46 c({ stdout, stderr });
47 });
48 if (cancellationToken) {
49 disposeCancellationListener = cancellationToken.subscribe((err) => {
50 child.kill();
51 e(err);
52 });
53 }
54 });
55}
56async function checkNPM(cancellationToken) {
57 const { stdout } = await exec('npm -v', {}, cancellationToken);
58 const version = stdout.trim();
59 if (/^3\.7\.[0123]$/.test(version)) {
60 throw new Error(`npm@${version} doesn't work with vsce. Please update npm: npm install -g npm`);
61 }
62}
63function getNpmDependencies(cwd) {
64 return checkNPM()
65 .then(() => exec('npm list --production --parseable --depth=99999 --loglevel=error', { cwd, maxBuffer: 5000 * 1024 }))
66 .then(({ stdout }) => stdout.split(/[\r\n]/).filter(dir => path.isAbsolute(dir)));
67}
68function asYarnDependency(prefix, tree, prune) {
69 if (prune && /@[\^~]/.test(tree.name)) {
70 return null;
71 }
72 let name;
73 try {
74 const parseResult = (0, parse_semver_1.default)(tree.name);
75 name = parseResult.name;
76 }
77 catch (err) {
78 name = tree.name.replace(/^([^@+])@.*$/, '$1');
79 }
80 const dependencyPath = path.join(prefix, name);
81 const children = [];
82 for (const child of tree.children || []) {
83 const dep = asYarnDependency(path.join(prefix, name, 'node_modules'), child, prune);
84 if (dep) {
85 children.push(dep);
86 }
87 }
88 return { name, path: dependencyPath, children };
89}
90function selectYarnDependencies(deps, packagedDependencies) {
91 const index = new (class {
92 constructor() {
93 this.data = Object.create(null);
94 for (const dep of deps) {
95 if (this.data[dep.name]) {
96 throw Error(`Dependency seen more than once: ${dep.name}`);
97 }
98 this.data[dep.name] = dep;
99 }
100 }
101 find(name) {
102 let result = this.data[name];
103 if (!result) {
104 throw new Error(`Could not find dependency: ${name}`);
105 }
106 return result;
107 }
108 })();
109 const reached = new (class {
110 constructor() {
111 this.values = [];
112 }
113 add(dep) {
114 if (this.values.indexOf(dep) < 0) {
115 this.values.push(dep);
116 return true;
117 }
118 return false;
119 }
120 })();
121 const visit = (name) => {
122 let dep = index.find(name);
123 if (!reached.add(dep)) {
124 // already seen -> done
125 return;
126 }
127 for (const child of dep.children) {
128 visit(child.name);
129 }
130 };
131 packagedDependencies.forEach(visit);
132 return reached.values;
133}
134async function getYarnProductionDependencies(cwd, packagedDependencies) {
135 const raw = await new Promise((c, e) => cp.exec('yarn list --prod --json', { cwd, encoding: 'utf8', env: { ...process.env }, maxBuffer: 5000 * 1024 }, (err, stdout) => (err ? e(err) : c(stdout))));
136 const match = /^{"type":"tree".*$/m.exec(raw);
137 if (!match || match.length !== 1) {
138 throw new Error('Could not parse result of `yarn list --json`');
139 }
140 const usingPackagedDependencies = Array.isArray(packagedDependencies);
141 const trees = JSON.parse(match[0]).data.trees;
142 let result = trees
143 .map(tree => asYarnDependency(path.join(cwd, 'node_modules'), tree, !usingPackagedDependencies))
144 .filter(util_1.nonnull);
145 if (usingPackagedDependencies) {
146 result = selectYarnDependencies(result, packagedDependencies);
147 }
148 return result;
149}
150async function getYarnDependencies(cwd, packagedDependencies) {
151 const result = new Set([cwd]);
152 const deps = await getYarnProductionDependencies(cwd, packagedDependencies);
153 const flatten = (dep) => {
154 result.add(dep.path);
155 dep.children.forEach(flatten);
156 };
157 deps.forEach(flatten);
158 return [...result];
159}
160async function detectYarn(cwd) {
161 for (const name of ['yarn.lock', '.yarnrc', '.yarnrc.yaml', '.pnp.cjs', '.yarn']) {
162 if (await exists(path.join(cwd, name))) {
163 if (!process.env['VSCE_TESTS']) {
164 util_1.log.info(`Detected presence of ${name}. Using 'yarn' instead of 'npm' (to override this pass '--no-yarn' on the command line).`);
165 }
166 return true;
167 }
168 }
169 return false;
170}
171exports.detectYarn = detectYarn;
172async function getDependencies(cwd, dependencies, packagedDependencies) {
173 if (dependencies === 'none') {
174 return [cwd];
175 }
176 else if (dependencies === 'yarn' || (dependencies === undefined && (await detectYarn(cwd)))) {
177 return await getYarnDependencies(cwd, packagedDependencies);
178 }
179 else {
180 return await getNpmDependencies(cwd);
181 }
182}
183exports.getDependencies = getDependencies;
184function getLatestVersion(name, cancellationToken) {
185 return checkNPM(cancellationToken)
186 .then(() => exec(`npm show ${name} version`, {}, cancellationToken))
187 .then(parseStdout);
188}
189exports.getLatestVersion = getLatestVersion;
190//# sourceMappingURL=npm.js.map
\No newline at end of file