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

discover all packages in workspaces #1159

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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 @@ -14,6 +14,7 @@ export interface Configuration {
cacheDir?: string;
nextVersion: string | undefined;
nextVersionFromMetadata?: boolean;
packages: [{ name: string; path: string }] | [];
}

export interface ConfigLoaderOptions {
Expand All @@ -26,6 +27,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 @@ -37,6 +66,8 @@ export function fromPath(rootPath: string, options: ConfigLoaderOptions = {}): C
// Step 2: fill partial config with defaults
let { repo, nextVersion, labels, cacheDir, ignoreCommitters } = config;

const packages = getPackages(rootPath);

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

Expand Down