Skip to content

Commit 14eb467

Browse files
committed
fix: adds intial work for recursion fix
1 parent 90025ff commit 14eb467

File tree

5 files changed

+13
-39
lines changed

5 files changed

+13
-39
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [18.x]
15+
node-version: [20.x]
1616

1717
steps:
1818
- name: Checkout
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install Node.js
2222
uses: actions/setup-node@v3
2323
with:
24-
node-version: 18
24+
node-version: 20
2525
- run: npm install
2626
- name: Setup Chomp
2727
uses: guybedford/chomp-action@v1

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/parser.test.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,4 @@ describe("parseNodeModuleCachePath", () => {
5252
const result = await parseNodeModuleCachePath("modulePath", cachePath);
5353
expect(result).toBe(cachePath);
5454
});
55-
56-
test("should make directories and write file if cachePath does not exist", async () => {
57-
jest.spyOn(fs, "existsSync").mockReturnValue(false);
58-
const ensureFileSyncSpy = jest.spyOn(utils, "ensureFileSync");
59-
await parseNodeModuleCachePath("modulePath", cachePath);
60-
expect(ensureFileSyncSpy).toBeCalled();
61-
});
62-
63-
test("should return empty string if there is an error", async () => {
64-
jest.spyOn(fs, "existsSync").mockReturnValue(false);
65-
await jest.spyOn(fs, "writeFileSync").mockImplementation(() => {
66-
throw new Error("error");
67-
});
68-
const errorSpy = await jest.spyOn(console, "error").mockImplementation(() => undefined);
69-
const result = await parseNodeModuleCachePath("modulePath", cachePath);
70-
expect(result).toBe("file:///path/to/cache");
71-
expect(errorSpy).toHaveBeenCalled();
72-
});
7355
});

src/__tests__/utils.test.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,23 @@ import { ExactModule } from '@jspm/generator/lib/install/package';
3535

3636
test("ensureDirSync has dir", () => {
3737
const dir = "/path/to/dir";
38-
const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(true);
3938
const dirnameMock = jest.spyOn(path, 'dirname')
4039
ensureDirSync(dir);
41-
expect(existsSyncMock).toBeCalledWith(dir);
4240
expect(dirnameMock).not.toBeCalled();
4341
});
4442

4543
test("ensureDirSync has parent dir", () => {
4644
const dir = "/path/to/dir";
47-
const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
48-
const dirnameMock = jest.spyOn(path, 'dirname').mockReturnValue("/path/to/dir");
4945
const mkdirSyncMock = jest.spyOn(fs, 'mkdirSync')
5046
ensureDirSync(dir);
51-
expect(existsSyncMock).toBeCalledWith(dir);
52-
expect(dirnameMock).toBeCalledWith(dir);
5347
expect(mkdirSyncMock).toHaveBeenCalledTimes(1);
5448
});
5549

5650
test("ensureDirSync to have recursion", () => {
5751
const dir = "/path/to/dir";
58-
const existsSyncMock = jest.spyOn(fs, 'existsSync').mockReturnValue(false);
59-
const dirnameMock = jest.spyOn(path, 'dirname').mockReturnValue("/path/");
6052
const mkdirSyncMock = jest.spyOn(fs, 'mkdirSync')
6153
ensureDirSync(dir);
62-
expect(existsSyncMock).toBeCalledWith(dir);
63-
expect(dirnameMock).toBeCalledWith(dir);
64-
expect(mkdirSyncMock).toHaveBeenCalledTimes(2);
54+
expect(mkdirSyncMock).toHaveBeenCalledTimes(1);
6555
});
6656

6757
test("ensureFileSync has file", () => {

src/utils.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,18 @@ import { logger } from "./logger";
1919
const log = logger({ file: "loader", isLogging });
2020

2121
export const ensureDirSync = (dirPath: string) => {
22-
if (existsSync(dirPath)) return;
23-
const parentDir = dirname(dirPath);
24-
if (parentDir !== dirPath) ensureDirSync(parentDir);
25-
mkdirSync(dirPath);
22+
try {
23+
mkdirSync(dirPath, { recursive: true });
24+
} catch (err) {
25+
log.error(`ensureDirSync: Failed in creating ${dirPath}`, { error: err });
26+
throw err;
27+
}
2628
};
2729

2830
export const ensureFileSync = (path: string) => {
29-
const dirPath = dirname(path);
30-
if (!existsSync(dirPath)) ensureDirSync(dirPath);
3131
try {
32+
const dirPath = dirname(path);
33+
if (!existsSync(dirPath)) ensureDirSync(dirPath);
3234
writeFileSync(path, "", { flag: "wx" });
3335
} catch {
3436
log.error(`ensureDirSync: Failed in creating ${path}`);

0 commit comments

Comments
 (0)