Skip to content

Commit

Permalink
refactor: use topo as paths resolver, join cli and runner modules
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed May 25, 2023
1 parent a79e784 commit 8a2aea9
Show file tree
Hide file tree
Showing 29 changed files with 307 additions and 458 deletions.
28 changes: 26 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import meow from "meow";
import process from "process";
import { toPairs, set } from "lodash-es";
import runner from "./runner.js";

const cli = meow(
`
Expand Down Expand Up @@ -84,7 +83,7 @@ const processFlags = (flags) => {
return set(m, k, v.split(","));
}

// FIXME Smth wrong with negate parser.
// FIXME Something is wrong with the default negate parser.
if (flags[`no${k[0].toUpperCase()}${k.slice(1)}`]) {
flags[k] = false;
return set(m, k, false);
Expand All @@ -94,4 +93,29 @@ const processFlags = (flags) => {
}, {});
};

const runner = async (cliFlags) => {
// Catch errors.
try {
// Imports.
const multiSemanticRelease = (await import("../lib/multiSemanticRelease.js")).default;

// Do multirelease (log out any errors).
multiSemanticRelease(null, {}, {}, cliFlags).then(
() => {
// Success.
process.exit(0);
},
(error) => {
// Log out errors.
console.error(`[multi-semantic-release]:`, error);
process.exit(1);
}
);
} catch (error) {
// Log out errors.
console.error(`[multi-semantic-release]:`, error);
process.exit(1);
}
};

runner(processFlags(cli.flags));
55 changes: 0 additions & 55 deletions bin/runner.js

This file was deleted.

57 changes: 0 additions & 57 deletions lib/getPackagePaths.js

This file was deleted.

49 changes: 34 additions & 15 deletions lib/multiSemanticRelease.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import semanticRelease from "semantic-release";
import { uniq, template } from "lodash-es";
import { uniq, template, sortBy } from "lodash-es";
import { topo } from "@semrel-extra/topo";
import { dirname, join } from "path";

import { check } from "./blork.js";
import getLogger from "./getLogger.js";
import getConfig from "./getConfig.js";
import getConfigMultiSemrel from "./getConfigMultiSemrel.js";
import getConfigSemantic from "./getConfigSemantic.js";
import getManifest from "./getManifest.js";
import cleanPath from "./cleanPath.js";
import RescopedStream from "./RescopedStream.js";
import createInlinePluginCreator from "./createInlinePluginCreator.js";
import { createRequire } from "module";

/**
* The multirelease context.
Expand Down Expand Up @@ -52,31 +53,48 @@ async function multiSemanticRelease(
_flags
) {
// Check params.
check(paths, "paths: string[]");
if (paths) {
check(paths, "paths: string[]");
}
check(cwd, "cwd: directory");
check(env, "env: objectlike");
check(stdout, "stdout: stream");
check(stderr, "stderr: stream");
cwd = cleanPath(cwd);

// Start.
const logger = getLogger({ stdout, stderr });
logger.complete(`Started multirelease! Loading ${paths.length} packages...`);
const flags = normalizeFlags(await getConfigMultiSemrel(cwd, _flags));
const silent = flags.silent && !flags.debug;
const require = createRequire(import.meta.url);
const multisemrelPkgJson = require("../package.json");
const semrelPkgJson = require("semantic-release/package.json");

if (!silent) {
console.log(`multi-semantic-release version: ${multisemrelPkgJson.version}`);
console.log(`semantic-release version: ${semrelPkgJson.version}`);
console.log(`flags: ${JSON.stringify(flags, null, 2)}`);
}
if (flags.debug) {
require("debug").enable("msr:*");
}

// Vars.
const flags = normalizeFlags(_flags);
const globalOptions = await getConfig(cwd);
const multiContext = { globalOptions, inputOptions, cwd, env, stdout, stderr };
const workspaces = [
...(getManifest(join(cwd, "package.json")).workspaces || []),
...(Array.isArray(flags.ignorePackages) ? flags.ignorePackages.map((p) => `!${p}`) : []),
];
const { queue } = await topo({
const { queue, packages: _packages } = await topo({
cwd,
workspaces,
filter: ({ manifest }) => !flags.ignorePrivate || !manifest.private,
workspacesExtra: Array.isArray(flags.ignorePackages) ? flags.ignorePackages.map((p) => `!${p}`) : [],
filter: ({ manifest, manifestAbsPath, manifestRelPath }) =>
(!flags.ignorePrivate || !manifest.private) &&
(paths ? paths.includes(manifestAbsPath) || paths.includes(manifestRelPath) : true),
});

// Get list of package.json paths according to workspaces.
paths = paths || Object.values(_packages).map((pkg) => pkg.manifestPath);

// Start.
const logger = getLogger({ stdout, stderr });
logger.complete(`Started multirelease! Loading ${paths.length} packages...`);

// Load packages from paths.
const packages = await Promise.all(paths.map((path) => getPackage(path, multiContext)));
packages.forEach((pkg) => {
Expand Down Expand Up @@ -107,7 +125,8 @@ async function multiSemanticRelease(

// Return packages list.
logger.complete(`Released ${released} of ${queue.length} packages, semantically!`);
return packages;

return sortBy(packages, ({ name }) => queue.indexOf(name));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/updateDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ const updateManifestDeps = (pkg) => {

// Cannot establish version.
if (!release || !release.version)
throw Error(`Cannot release because dependency ${d.name} has not been released`);
throw Error(`Cannot release ${pkg.name} because dependency ${d.name} has not been released yet`);
});

if (!auditManifestChanges(manifest, path)) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
"testTimeout": 50000
},
"dependencies": {
"@manypkg/get-packages": "^1.1.3",
"@semrel-extra/topo": "^1.3.0",
"@semrel-extra/topo": "^1.13.0",
"blork": "^9.3.0",
"cosmiconfig": "^7.0.1",
"debug": "^4.3.4",
Expand Down
1 change: 0 additions & 1 deletion test/fixtures/boltWorkspaces/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "msr-test-a",
"version": "0.0.0",
"peerDependencies": {
"msr-test-c": "*",
"left-pad": "latest"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/boltWorkspaces/packages/b/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"msr-test-a": "*"
},
"devDependencies": {
"msr-test-c": "*",
"msr-test-d": "*",
"left-pad": "latest"
}
}
1 change: 0 additions & 1 deletion test/fixtures/boltWorkspacesIgnore/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "msr-test-a",
"version": "0.0.0",
"peerDependencies": {
"msr-test-c": "*",
"left-pad": "latest"
}
}
3 changes: 1 addition & 2 deletions test/fixtures/boltWorkspacesIgnore/packages/b/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

{
"name": "msr-test-b",
"version": "0.0.0",
"dependencies": {
"msr-test-a": "*"
},
"devDependencies": {
"msr-test-c": "*",
"msr-test-d": "*",
"left-pad": "latest"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/boltWorkspacesIgnore/packages/d/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "msr-test-d",
"version": "0.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "msr-test-a",
"version": "0.0.0",
"peerDependencies": {
"msr-test-c": "*",
"left-pad": "latest"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"msr-test-a": "*"
},
"devDependencies": {
"msr-test-c": "*",
"msr-test-d": "*",
"left-pad": "latest"
}
}
1 change: 0 additions & 1 deletion test/fixtures/pnpmWorkspace/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "msr-test-a",
"version": "0.0.0",
"peerDependencies": {
"msr-test-c": "*",
"left-pad": "latest"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/pnpmWorkspace/packages/b/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"msr-test-a": "*"
},
"devDependencies": {
"msr-test-c": "*",
"msr-test-d": "*",
"left-pad": "latest"
}
}
1 change: 0 additions & 1 deletion test/fixtures/pnpmWorkspaceIgnore/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "msr-test-a",
"version": "0.0.0",
"peerDependencies": {
"msr-test-c": "*",
"left-pad": "latest"
}
}
3 changes: 1 addition & 2 deletions test/fixtures/pnpmWorkspaceIgnore/packages/b/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

{
"name": "msr-test-b",
"version": "0.0.0",
"dependencies": {
"msr-test-a": "*"
},
"devDependencies": {
"msr-test-c": "*",
"msr-test-d": "*",
"left-pad": "latest"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/pnpmWorkspaceIgnore/packages/d/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "msr-test-d",
"version": "0.0.0"
}
}
1 change: 0 additions & 1 deletion test/fixtures/yarnWorkspaces/packages/a/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "msr-test-a",
"version": "0.0.0",
"peerDependencies": {
"msr-test-c": "*",
"left-pad": "latest"
}
}
Loading

0 comments on commit 8a2aea9

Please sign in to comment.