Skip to content

Commit 3a7e6cb

Browse files
authored
fix: create-package script (#5123)
## Explanation <!-- Thanks for your contribution! Take a moment to answer these questions so that reviewers have the information they need to properly understand your changes: * What is the current state of things and why does it need to change? * What is the solution your changes offer and how does it work? * Are there any changes whose purpose might not obvious to those unfamiliar with the domain? * If your primary goal was to update one package but you found you had to update another one along the way, why did you do so? * If you had to upgrade a dependency, why did you do so? --> This PR addresses a bug in the yarn `create-package` script introduced in [this change](https://github.com/MetaMask/core/pull/3672/files#diff-3424bcceb4f9219f700e2bd2da507f2c742767c7a30f6be5512f62d3b44ffd7aR97), where `fstatSync` was replaced with `await fs.stat(packagePath)).isDirectory()`. The issue arises because fs.stat now throws an `ENOENT` error if the packagePath is empty. To resolve this, the script has been updated to handle empty paths gracefully, ensuring the process works as expected when creating new packages. ## References <!-- Are there any issues that this pull request is tied to? Are there other links that reviewers should consult to understand these changes better? Are there client or consumer pull requests to adopt any breaking changes? For example: * Fixes #12345 * Related to #67890 --> ## Changelog <!-- If you're making any consumer-facing changes, list those changes here as if you were updating a changelog, using the template below as a guide. (CATEGORY is one of BREAKING, ADDED, CHANGED, DEPRECATED, REMOVED, or FIXED. For security-related issues, follow the Security Advisory process.) Please take care to name the exact pieces of the API you've added or changed (e.g. types, interfaces, functions, or methods). If there are any breaking changes, make sure to offer a solution for consumers to follow once they upgrade to the changes. Finally, if you're only making changes to development scripts or tests, you may replace the template below with "None". --> ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate - [x] I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes
1 parent 9c4146d commit 3a7e6cb

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

scripts/create-package/utils.test.ts

+34-6
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ describe('create-package/utils', () => {
8787
nodeVersions: '>=18.0.0',
8888
};
8989

90-
(fs.promises.stat as jest.Mock).mockResolvedValueOnce({
91-
isDirectory: () => false,
92-
});
90+
const mockError = new Error('Not found') as NodeJS.ErrnoException;
91+
mockError.code = 'ENOENT';
92+
93+
jest.spyOn(fs.promises, 'stat').mockRejectedValue(mockError);
9394

9495
(fsUtils.readAllFiles as jest.Mock).mockResolvedValueOnce({
9596
'src/index.ts': 'export default 42;',
@@ -170,9 +171,7 @@ describe('create-package/utils', () => {
170171
nodeVersions: '20.0.0',
171172
};
172173

173-
(fs.promises.stat as jest.Mock).mockResolvedValueOnce({
174-
isDirectory: () => true,
175-
});
174+
(fs.promises.stat as jest.Mock).mockResolvedValue({});
176175

177176
await expect(
178177
finalizeAndWriteData(packageData, monorepoFileData),
@@ -181,5 +180,34 @@ describe('create-package/utils', () => {
181180
expect(fs.promises.mkdir).not.toHaveBeenCalled();
182181
expect(fs.promises.writeFile).not.toHaveBeenCalled();
183182
});
183+
184+
it('throws if fs.stat fails with an error other than ENOENT', async () => {
185+
const mockError = new Error('Permission denied') as NodeJS.ErrnoException;
186+
mockError.code = 'EACCES';
187+
188+
jest.spyOn(fs.promises, 'stat').mockRejectedValue(mockError);
189+
190+
const packageData: PackageData = {
191+
name: '@metamask/foo',
192+
description: 'A foo package.',
193+
directoryName: 'foo',
194+
nodeVersions: '20.0.0',
195+
currentYear: '2023',
196+
};
197+
198+
const monorepoFileData = {
199+
tsConfig: {
200+
references: [{ path: './packages/bar' }],
201+
},
202+
tsConfigBuild: {
203+
references: [{ path: './packages/bar' }],
204+
},
205+
nodeVersions: '20.0.0',
206+
};
207+
208+
await expect(
209+
finalizeAndWriteData(packageData, monorepoFileData),
210+
).rejects.toThrow('Permission denied');
211+
});
184212
});
185213
});

scripts/create-package/utils.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,13 @@ export async function finalizeAndWriteData(
9494
monorepoFileData: MonorepoFileData,
9595
) {
9696
const packagePath = path.join(PACKAGES_PATH, packageData.directoryName);
97-
if ((await fs.stat(packagePath)).isDirectory()) {
97+
try {
98+
await fs.stat(packagePath);
9899
throw new Error(`The package directory already exists: ${packagePath}`);
100+
} catch (error) {
101+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') {
102+
throw error;
103+
}
99104
}
100105

101106
console.log('Writing package and monorepo files...');
@@ -136,7 +141,7 @@ async function writeJsonFile(
136141
): Promise<void> {
137142
await fs.writeFile(
138143
filePath,
139-
prettierFormat(fileContent, { ...prettierRc, parser: 'json' }),
144+
await prettierFormat(fileContent, { ...prettierRc, parser: 'json' }),
140145
);
141146
}
142147

0 commit comments

Comments
 (0)