Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vx: Improve dependency resolution based on package.json #928

Merged
merged 2 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion vx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"dotenv": "^16.0.1",
"fs-extra": "^10.1.0",
"glob": "^8.0.3",
"madge": "^5.0.1",
"onchange": "^7.1.0",
"rollup": "^2.48.0",
"rollup-plugin-terser": "^7.0.2",
Expand Down
68 changes: 30 additions & 38 deletions vx/scripts/release/depsTree.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,45 @@
const path = require('path');

const { memoize } = require('lodash');
const madge = require('madge');
const opts = require('vx/opts');
const vxPath = require('vx/vxPath');
const packageNames = require('vx/packageNames');
const packageJson = require('vx/util/packageJson');

// Takes import map and turns it into a dependency map
const buildDepsTree = memoize(async function buildDepsTree() {
const baseTree = (await getAllDeps()).tree;

const depTree = {};
for (const file in baseTree) {
const [packageName] = file.split(path.sep);
depTree[packageName] = depTree[packageName] ?? {};
baseTree[file].forEach(dep => {
const [depName] = dep.split(path.sep);

if (depName === packageName) {
return;
}
depTree[depName] = depTree[depName] || {};

if (!depTree[depName][packageName]) {
depTree[depName][packageName] = depTree[packageName];
}
});
}
return depTree;
const buildDepsMemo = memoize(function (package, deps) {
const pkgJson = packageJson(package);

// Is circular object ok?
deps[package] = deps[package] || {};

const dependencies = Object.keys(pkgJson.dependencies || {});

dependencies.forEach(dependency => {
deps[dependency] = deps[dependency] || {};
deps[dependency][package] =
deps[package] ?? buildDepsMemo(dependency, deps);
});

return deps;
});

function buildDepsTree() {
return packageNames.list.reduce(
(deps, packageName) => buildDepsMemo(packageName, deps),
{}
);
}

// Sorts an array of packages by their dependency depth
async function sortDependencies(packagesList) {
const deps = await buildDepsTree();
function sortDependencies(packagesList) {
const deps = buildDepsTree();

return packagesList.sort(
(a, b) => countMaxDepth(deps[b]) - countMaxDepth(deps[a])
);
}

module.exports = { buildDepsTree, sortDependencies };

// Uses madge to get all the packages. Note: must be run from the root of the repo.
const getAllDeps = memoize(function getAllDeps() {
return madge(vxPath.PACKAGES_PATH, {
excludeRegExp: [`${opts.dir.TESTS}|.d.ts|shared`, /^((?!src).)*$/],
fileExtensions: ['ts'],
tsConfig: vxPath.TSCONFIG_PATH,
});
});
module.exports = {
buildDepsTree,
sortDependencies,
};

// Counts max dependency depth
const countMaxDepth = memoize(function countMaxDepth(node) {
Expand Down
11 changes: 2 additions & 9 deletions vx/scripts/release/github/listAllChangedPackages.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const path = require('path');

const opts = require('vx/opts');
const packageNames = require('vx/packageNames');
const vxPath = require('vx/vxPath');

const listAllChangesSinceStableBranch = require('./listAllChangesSinceStableBranch');

Expand All @@ -10,12 +8,7 @@ function listAllChangedPackages() {
return Object.keys(
changes.reduce((packages, { files = [] }) => {
return files.reduce((packages, file) => {
if (!file.startsWith(opts.dir.PACKAGES)) {
return packages;
}

const [, packageName] = file.split(path.sep);

const packageName = vxPath.packageNameFromPath(file);
if (!packageNames.names[packageName]) {
return packages;
}
Expand Down
30 changes: 27 additions & 3 deletions vx/scripts/release/packagesToRelease.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
const logger = require('vx/logger');
const {
buildDepsTree,
sortDependencies,
} = require('vx/scripts/release/depsTree');
const listAllChangedPackages = require('vx/scripts/release/github/listAllChangedPackages');

// Gets all the packages that need to be released in the correct order
async function packagesToRelease() {
const deps = await buildDepsTree();
function packagesToRelease() {
const deps = buildDepsTree();

const changedPackages = listAllChangedPackages();

logger.info(
`💡 The following packages were changed: \n - ${changedPackages.join(
'\n - '
)}\n`
);

const queue = [...changedPackages];
const release = new Set();

const unchangedDependents = new Set();

while (queue.length) {
const name = queue.shift();

Expand All @@ -24,12 +33,27 @@ async function packagesToRelease() {

for (const dep in dependents) {
queue.push(dep);
unchangedDependents.add(dep);
}

release.add(name);
}

return await sortDependencies([...release]);
logger.info(
`🧱 The following packages did not change, but will be released because they depend on changed packages: \n - ${[
...unchangedDependents,
].join('\n - ')} \n`
);

const allPackagesToRelease = sortDependencies([...release]);

logger.info(
`✅ The packages will be released in the following order: \n - ${allPackagesToRelease.join(
'\n - '
)}\n`
);

return allPackagesToRelease;
}

module.exports = packagesToRelease;
Loading