Skip to content

Commit 6a77536

Browse files
committed
fix relative path
1 parent 0f630a7 commit 6a77536

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

packages/cli/__tests__/compile-stylex-folder-test.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ describe('cache mechanism works as expected', () => {
258258
copiedNodeModules: false,
259259
},
260260
};
261+
262+
beforeAll(async () => {
263+
await fs.rm(cachePath, { recursive: true, force: true });
264+
});
265+
261266
beforeEach(() => {
262267
writeSpy = jest.spyOn(cacheModule, 'writeCache');
263268
});
@@ -327,7 +332,8 @@ describe('cache mechanism works as expected', () => {
327332

328333
describe('CLI works with a custom cache path', () => {
329334
let writeSpy;
330-
const customCachePath = path.join(__dirname, '__custom_cache__');
335+
const projectRoot = path.resolve(__dirname, '../../../');
336+
const customCachePath = path.join(projectRoot, '__custom_cache__');
331337
const config: TransformConfig = {
332338
input: path.resolve('./source'),
333339
output: path.resolve('./src'),
@@ -369,31 +375,20 @@ describe('CLI works with a custom cache path', () => {
369375
test('uses the custom cache path for caching', async () => {
370376
await compileDirectory(config);
371377

372-
const customFilePath = path.join(config.input, 'index.js');
373-
374-
const cacheFilePath = path.join(
375-
customCachePath,
376-
path.relative(config.input, customFilePath) + '.json',
377-
);
378-
379-
expect(
380-
await fs
381-
.access(customCachePath)
382-
.then(() => true)
383-
.catch(() => false),
384-
).toBe(true);
385-
expect(
386-
await fs
387-
.access(cacheFilePath)
388-
.then(() => true)
389-
.catch(() => false),
390-
).toBe(true);
378+
const cacheFiles = await fs.readdir(customCachePath);
379+
expect(cacheFiles.length).toEqual(3);
391380

392-
const cacheData = JSON.parse(await fs.readFile(cacheFilePath, 'utf-8'));
393-
expect(cacheData).toHaveProperty('inputHash');
394-
expect(cacheData).toHaveProperty('outputHash');
395-
expect(cacheData).toHaveProperty('collectedCSS');
381+
for (const cacheFile of cacheFiles) {
382+
const cacheFilePath = path.join(customCachePath, cacheFile);
383+
const cacheContent = JSON.parse(
384+
await fs.readFile(cacheFilePath, 'utf-8'),
385+
);
386+
expect(cacheContent).toHaveProperty('inputHash');
387+
expect(cacheContent).toHaveProperty('outputHash');
388+
expect(cacheContent).toHaveProperty('collectedCSS');
389+
}
396390
});
391+
397392
test('skips transformation when cache is valid', async () => {
398393
await compileDirectory(config);
399394

packages/cli/src/cache.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ export function getDefaultCachePath() {
1818
}
1919

2020
export async function getCacheFilePath(cachePath, filePath) {
21-
const projectRoot = path.join(__dirname, '../../../');
22-
const relativePath = path.relative(projectRoot, filePath);
21+
const projectRoot = path.resolve(__dirname, '../../../');
22+
const absoluteFilePath = path.resolve(filePath);
23+
const relativePath = path.relative(projectRoot, absoluteFilePath);
2324
const fileName = relativePath.replace(/[\\/]/g, '__');
2425
return path.join(cachePath, `${fileName}.json`);
2526
}

0 commit comments

Comments
 (0)