Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
56 changes: 42 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12489,30 +12489,54 @@ var auto_changelog_dist = __nccwpck_require__(9272);
const MANIFEST_FILE_NAME = 'package.json';
const CHANGELOG_FILE_NAME = 'CHANGELOG.md';
/**
* Finds the package manifest for each workspace, and collects
* Recursively finds the package manifest for each workspace, and collects
* metadata for each package.
*
* @param workspaces - The list of workspace patterns given in the root manifest.
* @param rootDir - The monorepo root directory.
* @param parentDir - The parent directory of the current package.
* @returns The metadata for all packages in the monorepo.
*/
async function getMetadataForAllPackages(workspaces, rootDir = WORKSPACE_ROOT) {
async function getMetadataForAllPackages(workspaces, rootDir = WORKSPACE_ROOT, parentDir = '') {
const workspaceLocations = await (0,dist.getWorkspaceLocations)(workspaces, rootDir);
const result = {};
await Promise.all(workspaceLocations.map(async (workspaceDirectory) => {
return workspaceLocations.reduce(async (promise, workspaceDirectory) => {
const result = await promise;
const fullWorkspacePath = external_path_default().join(rootDir, workspaceDirectory);
if ((await external_fs_.promises.lstat(fullWorkspacePath)).isDirectory()) {
const rawManifest = await (0,dist.getPackageManifest)(fullWorkspacePath);
// If the package is a sub-workspace, resolve all packages in the sub-workspace and add them
// to the result.
if (dist.ManifestFieldNames.Workspaces in rawManifest) {
const rootManifest = (0,dist.validatePackageManifestVersion)(rawManifest, workspaceDirectory);
const manifest = (0,dist.validateMonorepoPackageManifest)(rootManifest, workspaceDirectory);
const name = manifest[dist.ManifestFieldNames.Name];
if (!name) {
throw new Error('Expected sub-workspace to have a name.');
}
return {
...result,
...(await getMetadataForAllPackages(manifest.workspaces, workspaceDirectory, workspaceDirectory)),
[name]: {
dirName: external_path_default().basename(workspaceDirectory),
manifest,
name,
dirPath: external_path_default().join(parentDir, workspaceDirectory),
},
};
}
const manifest = (0,dist.validatePolyrepoPackageManifest)(rawManifest, workspaceDirectory);
result[manifest.name] = {
dirName: external_path_default().basename(workspaceDirectory),
manifest,
name: manifest.name,
dirPath: workspaceDirectory,
return {
...result,
[manifest.name]: {
dirName: external_path_default().basename(workspaceDirectory),
manifest,
name: manifest.name,
dirPath: external_path_default().join(parentDir, workspaceDirectory),
},
};
}
}));
return result;
return result;
}, Promise.resolve({}));
}
/**
* @param allPackages - The metadata of all packages in the monorepo.
Expand Down Expand Up @@ -12596,8 +12620,12 @@ async function updatePackageChangelog(packageMetadata, updateSpecification, root
changelogContent = await external_fs_.promises.readFile(changelogPath, 'utf-8');
}
catch (error) {
console.error(`Failed to read changelog in "${projectRootDirectory}".`);
throw error;
// If the error is not a file not found error, throw it
if (error.code !== 'ENOENT') {
console.error(`Failed to read changelog in "${projectRootDirectory}".`);
throw error;
}
return console.warn(`Failed to read changelog in "${projectRootDirectory}".`);
}
const newChangelogContent = await (0,auto_changelog_dist.updateChangelog)({
changelogContent,
Expand All @@ -12610,7 +12638,7 @@ async function updatePackageChangelog(packageMetadata, updateSpecification, root
const packageName = packageMetadata.manifest.name;
throw new Error(`"updateChangelog" returned an empty value for package ${packageName ? `"${packageName}"` : `at "${packagePath}"`}.`);
}
await external_fs_.promises.writeFile(changelogPath, newChangelogContent);
return await external_fs_.promises.writeFile(changelogPath, newChangelogContent);
}
/**
* Updates the given manifest per the update specification as follows:
Expand Down
2 changes: 1 addition & 1 deletion src/package-operations.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs';
import cloneDeep from 'lodash.clonedeep';
import * as actionUtils from '@metamask/action-utils/dist/file-utils';
import * as actionUtils from '@metamask/action-utils';
import {
ManifestDependencyFieldNames,
ManifestFieldNames,
Expand Down
85 changes: 70 additions & 15 deletions src/package-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import {
getPackageManifest,
getWorkspaceLocations,
ManifestDependencyFieldNames,
ManifestFieldNames,
PackageManifest,
MonorepoPackageManifest,
validateMonorepoPackageManifest,
validatePackageManifestVersion,
validatePolyrepoPackageManifest,
writeJsonFile,
} from '@metamask/action-utils';
Expand All @@ -14,7 +18,7 @@ import { WORKSPACE_ROOT } from './utils';

export interface PackageMetadata {
readonly dirName: string;
readonly manifest: PackageManifest;
readonly manifest: PackageManifest | MonorepoPackageManifest;
readonly name: string;
readonly dirPath: string;
}
Expand All @@ -34,39 +38,83 @@ const MANIFEST_FILE_NAME = 'package.json';
const CHANGELOG_FILE_NAME = 'CHANGELOG.md';

/**
* Finds the package manifest for each workspace, and collects
* Recursively finds the package manifest for each workspace, and collects
* metadata for each package.
*
* @param workspaces - The list of workspace patterns given in the root manifest.
* @param rootDir - The monorepo root directory.
* @param parentDir - The parent directory of the current package.
* @returns The metadata for all packages in the monorepo.
*/
export async function getMetadataForAllPackages(
workspaces: string[],
rootDir: string = WORKSPACE_ROOT,
parentDir = '',
): Promise<Record<string, PackageMetadata>> {
const workspaceLocations = await getWorkspaceLocations(workspaces, rootDir);

const result: Record<string, PackageMetadata> = {};
await Promise.all(
workspaceLocations.map(async (workspaceDirectory) => {
return workspaceLocations.reduce<Promise<Record<string, PackageMetadata>>>(
async (promise, workspaceDirectory) => {
const result = await promise;

const fullWorkspacePath = pathUtils.join(rootDir, workspaceDirectory);
if ((await fs.lstat(fullWorkspacePath)).isDirectory()) {
const rawManifest = await getPackageManifest(fullWorkspacePath);

// If the package is a sub-workspace, resolve all packages in the sub-workspace and add them
// to the result.
if (ManifestFieldNames.Workspaces in rawManifest) {
const rootManifest = validatePackageManifestVersion(
rawManifest,
workspaceDirectory,
);

const manifest = validateMonorepoPackageManifest(
rootManifest,
workspaceDirectory,
);

const name = manifest[ManifestFieldNames.Name];
if (!name) {
throw new Error('Expected sub-workspace to have a name.');
}

return {
...result,
...(await getMetadataForAllPackages(
manifest.workspaces,
workspaceDirectory,
workspaceDirectory,
)),
[name]: {
dirName: pathUtils.basename(workspaceDirectory),
manifest,
name,
dirPath: pathUtils.join(parentDir, workspaceDirectory),
},
};
}

const manifest = validatePolyrepoPackageManifest(
rawManifest,
workspaceDirectory,
);
result[manifest.name] = {
dirName: pathUtils.basename(workspaceDirectory),
manifest,
name: manifest.name,
dirPath: workspaceDirectory,

return {
...result,
[manifest.name]: {
dirName: pathUtils.basename(workspaceDirectory),
manifest,
name: manifest.name,
dirPath: pathUtils.join(parentDir, workspaceDirectory),
},
};
}
}),

return result;
},
Promise.resolve({}),
);
return result;
}

/**
Expand Down Expand Up @@ -179,8 +227,15 @@ async function updatePackageChangelog(
try {
changelogContent = await fs.readFile(changelogPath, 'utf-8');
} catch (error) {
console.error(`Failed to read changelog in "${projectRootDirectory}".`);
throw error;
// If the error is not a file not found error, throw it
if (error.code !== 'ENOENT') {
console.error(`Failed to read changelog in "${projectRootDirectory}".`);
throw error;
}

return console.warn(
`Failed to read changelog in "${projectRootDirectory}".`,
);
Comment on lines +232 to +240
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since not all sub-workflow packages in snaps-skunkworks have a CHANGELOG.md, I changed this to ignore the package if the file doesn't exist.

}

const newChangelogContent = await updateChangelog({
Expand All @@ -199,7 +254,7 @@ async function updatePackageChangelog(
);
}

await fs.writeFile(changelogPath, newChangelogContent);
return await fs.writeFile(changelogPath, newChangelogContent);
}

/**
Expand Down