2024-04-30 20:57:13 +08:00
|
|
|
"use strict";
|
2022-06-14 14:37:10 +08:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
exports.getProductionDependencies = void 0;
|
2024-04-30 20:57:13 +08:00
|
|
|
const fs = require("fs");
|
2022-06-14 14:37:10 +08:00
|
|
|
const path = require("path");
|
|
|
|
const cp = require("child_process");
|
|
|
|
const parseSemver = require('parse-semver');
|
2024-04-30 20:57:13 +08:00
|
|
|
const root = fs.realpathSync(path.dirname(path.dirname(__dirname)));
|
2022-06-14 14:37:10 +08:00
|
|
|
function asYarnDependency(prefix, tree) {
|
|
|
|
let parseResult;
|
|
|
|
try {
|
|
|
|
parseResult = parseSemver(tree.name);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
err.message += `: ${tree.name}`;
|
|
|
|
console.warn(`Could not parse semver: ${tree.name}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// not an actual dependency in disk
|
|
|
|
if (parseResult.version !== parseResult.range) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const name = parseResult.name;
|
|
|
|
const version = parseResult.version;
|
|
|
|
const dependencyPath = path.join(prefix, name);
|
|
|
|
const children = [];
|
|
|
|
for (const child of (tree.children || [])) {
|
|
|
|
const dep = asYarnDependency(path.join(prefix, name, 'node_modules'), child);
|
|
|
|
if (dep) {
|
|
|
|
children.push(dep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { name, version, path: dependencyPath, children };
|
|
|
|
}
|
2024-04-30 20:57:13 +08:00
|
|
|
function getYarnProductionDependencies(folderPath) {
|
|
|
|
const raw = cp.execSync('yarn list --json', { cwd: folderPath, encoding: 'utf8', env: { ...process.env, NODE_ENV: 'production' }, stdio: [null, null, 'inherit'] });
|
2022-06-14 14:37:10 +08:00
|
|
|
const match = /^{"type":"tree".*$/m.exec(raw);
|
|
|
|
if (!match || match.length !== 1) {
|
|
|
|
throw new Error('Could not parse result of `yarn list --json`');
|
|
|
|
}
|
|
|
|
const trees = JSON.parse(match[0]).data.trees;
|
|
|
|
return trees
|
2024-04-30 20:57:13 +08:00
|
|
|
.map(tree => asYarnDependency(path.join(folderPath, 'node_modules'), tree))
|
2022-06-14 14:37:10 +08:00
|
|
|
.filter((dep) => !!dep);
|
|
|
|
}
|
2024-04-30 20:57:13 +08:00
|
|
|
function getProductionDependencies(folderPath) {
|
2022-06-14 14:37:10 +08:00
|
|
|
const result = [];
|
2024-04-30 20:57:13 +08:00
|
|
|
const deps = getYarnProductionDependencies(folderPath);
|
2022-06-14 14:37:10 +08:00
|
|
|
const flatten = (dep) => { result.push({ name: dep.name, version: dep.version, path: dep.path }); dep.children.forEach(flatten); };
|
|
|
|
deps.forEach(flatten);
|
2024-04-30 20:57:13 +08:00
|
|
|
// Account for distro npm dependencies
|
|
|
|
const realFolderPath = fs.realpathSync(folderPath);
|
|
|
|
const relativeFolderPath = path.relative(root, realFolderPath);
|
|
|
|
const distroPackageJsonPath = `${root}/.build/distro/npm/${relativeFolderPath}/package.json`;
|
|
|
|
if (fs.existsSync(distroPackageJsonPath)) {
|
|
|
|
const distroPackageJson = JSON.parse(fs.readFileSync(distroPackageJsonPath, 'utf8'));
|
|
|
|
const distroDependencyNames = Object.keys(distroPackageJson.dependencies ?? {});
|
|
|
|
for (const name of distroDependencyNames) {
|
|
|
|
result.push({
|
|
|
|
name,
|
|
|
|
version: distroPackageJson.dependencies[name],
|
|
|
|
path: path.join(realFolderPath, 'node_modules', name)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [...new Set(result)];
|
2022-06-14 14:37:10 +08:00
|
|
|
}
|
|
|
|
exports.getProductionDependencies = getProductionDependencies;
|
|
|
|
if (require.main === module) {
|
|
|
|
console.log(JSON.stringify(getProductionDependencies(root), null, ' '));
|
|
|
|
}
|
2024-04-30 20:57:13 +08:00
|
|
|
//# sourceMappingURL=dependencies.js.map
|