Skip to content

Commit

Permalink
Merge pull request #1 from mansona/load-packages-ef4
Browse files Browse the repository at this point in the history
discover all packages in workspaces
  • Loading branch information
mansona authored Dec 3, 2023
2 parents aa39347 + 7ba8d75 commit adcfee5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/__mocks__/changelog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const defaultConfig = {
ignoreCommitters: [],
cacheDir: ".changelog",
nextVersion: "Unreleased",
packages: [],
};

class MockedChangelog extends Changelog {
Expand Down
28 changes: 28 additions & 0 deletions src/changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,34 @@ describe("Changelog", () => {
}
});

describe("packageFromPath with custom packages", () => {
const MockedChangelog = require("./changelog").default;

const TESTS = [
["", ""],
["/some/path/to/repo/foo.js", ""],
["/some/path/to/repo/packages/foo.js", ""],
["/some/path/to/repo/packages/tests/foo/face.js", ""],
["/some/path/to/repo/packages/tests/yup/face.js", "another-one"],
["/some/path/to/repo/funky-package/foo/bar/baz.js", ""],
["/some/path/to/repo/packages/funky-package/foo/bar/baz.js", ""],
["/some/path/to/repo/over-here/foo/bar/baz.js", "funky-package"],
];

for (let [input, expected] of TESTS) {
it(`${input} -> ${expected}`, () => {
const changelog = new MockedChangelog({
rootPath: "/some/path/to/repo/",
packages: [
{ name: "funky-package", path: "/some/path/to/repo/over-here" },
{ name: "another-one", path: "/some/path/to/repo/packages/tests/yup" },
],
});
expect(changelog.packageFromPath(input)).toEqual(expected);
});
}
});

describe("getCommitInfos", () => {
beforeEach(() => {
require("./fetch").__resetMockResponses();
Expand Down
29 changes: 22 additions & 7 deletions src/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const pMap = require("p-map");
const { resolve } = require("path");

import progressBar from "./progress-bar";
import { Configuration } from "./configuration";
Expand Down Expand Up @@ -79,16 +80,30 @@ export default class Changelog {
}

private packageFromPath(path: string): string {
const parts = path.split("/");
if (parts[0] !== "packages" || parts.length < 3) {
if (this.config.packages.length) {
// use the discovered packages
const absolutePath = resolve(this.config.rootPath, path);

const foundPackage = this.config.packages.find(p => absolutePath.startsWith(p.path));

if (foundPackage) {
return foundPackage.name;
}

return "";
}
} else {
// if we did not find any packages then default to
const parts = path.split("/");
if (parts[0] !== "packages" || parts.length < 3) {
return "";
}

if (parts.length >= 4 && parts[1][0] === "@") {
return `${parts[1]}/${parts[2]}`;
}
if (parts.length >= 4 && parts[1][0] === "@") {
return `${parts[1]}/${parts[2]}`;
}

return parts[1];
return parts[1];
}
}

private getListOfCommits(from: string, to: string): Git.CommitListItem[] {
Expand Down
32 changes: 32 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface Configuration {
nextVersion: string | undefined;
nextVersionFromMetadata?: boolean;
wildcardLabel?: string;
packages: [{ name: string; path: string }] | [];
}

export interface ConfigLoaderOptions {
Expand All @@ -27,6 +28,34 @@ export function load(options: ConfigLoaderOptions = {}): Configuration {
return fromPath(rootPath, options);
}

function getPackages(rootPath: string): [{ name: string; path: string }] | [] {
let packages = [];

if (fs.existsSync(path.join(rootPath, "package-lock.json"))) {
const result = execa.sync("npm", ["query", ".workspace"], { cwd: rootPath });
const workspaceQuery = JSON.parse(result.stdout);

packages = workspaceQuery.map((item: any) => ({ name: item.name, path: item.path }));
} else if (fs.existsSync(path.join(rootPath, "pnpm-lock.yaml"))) {
const result = execa.sync(`pnpm`, ["m", "ls", "--json", "--depth=-1"], { cwd: rootPath });
const workspaceJson = JSON.parse(result.stdout);

packages = workspaceJson
.filter((item: any) => item.name && item.path)
.map((item: any) => ({ name: item.name, path: item.path }));
} else if (fs.existsSync(path.join(rootPath, "yarn.lock"))) {
const result = execa.sync(`yarn`, ["--silent", "workspaces", "info", "--json"], { cwd: rootPath });
const workspaceMap = JSON.parse(result.stdout);

packages = Object.keys(workspaceMap).map(key => ({
name: key,
path: path.resolve(rootPath, workspaceMap[key].location),
}));
}

return packages;
}

export function fromPath(rootPath: string, options: ConfigLoaderOptions = {}): Configuration {
// Step 1: load partial config from `package.json` or `lerna.json`
let config = fromPackageConfig(rootPath) || fromLernaConfig(rootPath) || {};
Expand All @@ -38,6 +67,8 @@ export function fromPath(rootPath: string, options: ConfigLoaderOptions = {}): C
// Step 2: fill partial config with defaults
let { repo, nextVersion, labels, cacheDir, ignoreCommitters, wildcardLabel } = config;

const packages = getPackages(rootPath);

if (!repo) {
repo = findRepo(rootPath);
if (!repo) {
Expand Down Expand Up @@ -87,6 +118,7 @@ export function fromPath(rootPath: string, options: ConfigLoaderOptions = {}): C
ignoreCommitters,
cacheDir,
wildcardLabel,
packages,
};
}

Expand Down

0 comments on commit adcfee5

Please sign in to comment.