Skip to content

Commit fbd16ea

Browse files
committed
fix relative path
1 parent 0f630a7 commit fbd16ea

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

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

Lines changed: 21 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
});
@@ -280,6 +285,7 @@ describe('cache mechanism works as expected', () => {
280285
expect(writeSpy).toHaveBeenCalledTimes(3);
281286

282287
const cacheFiles = await fs.readdir(cachePath);
288+
console.log('cacheFiles', cacheFiles)
283289
expect(cacheFiles.length).toEqual(3);
284290

285291
for (const cacheFile of cacheFiles) {
@@ -327,7 +333,8 @@ describe('cache mechanism works as expected', () => {
327333

328334
describe('CLI works with a custom cache path', () => {
329335
let writeSpy;
330-
const customCachePath = path.join(__dirname, '__custom_cache__');
336+
const projectRoot = path.resolve(__dirname, '../../../');
337+
const customCachePath = path.join(projectRoot, '__custom_cache__');
331338
const config: TransformConfig = {
332339
input: path.resolve('./source'),
333340
output: path.resolve('./src'),
@@ -369,31 +376,21 @@ describe('CLI works with a custom cache path', () => {
369376
test('uses the custom cache path for caching', async () => {
370377
await compileDirectory(config);
371378

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);
379+
const cacheFiles = await fs.readdir(customCachePath);
380+
console.log('cacheFiles', cacheFiles)
381+
expect(cacheFiles.length).toEqual(3);
391382

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');
383+
for (const cacheFile of cacheFiles) {
384+
const cacheFilePath = path.join(customCachePath, cacheFile);
385+
const cacheContent = JSON.parse(
386+
await fs.readFile(cacheFilePath, 'utf-8'),
387+
);
388+
expect(cacheContent).toHaveProperty('inputHash');
389+
expect(cacheContent).toHaveProperty('outputHash');
390+
expect(cacheContent).toHaveProperty('collectedCSS');
391+
}
396392
});
393+
397394
test('skips transformation when cache is valid', async () => {
398395
await compileDirectory(config);
399396

packages/cli/src/cache.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,31 @@ export function getDefaultCachePath() {
1717
return path.join('node_modules', '.stylex-cache');
1818
}
1919

20+
// function findProjectRoot(startDir = process.cwd()) {
21+
// let currentDir = startDir;
22+
23+
// while (currentDir !== path.parse(currentDir).root) {
24+
// if (fs.existsSync(path.join(currentDir, 'package.json'))) {
25+
// return currentDir;
26+
// }
27+
// currentDir = path.dirname(currentDir);
28+
// }
29+
30+
// throw new Error('Project root not found');
31+
// }
32+
33+
// export async function getCacheFilePath(cachePath, filePath) {
34+
// const projectRoot = path.join(__dirname);
35+
// //findProjectRoot(); // Dynamically find the true project root
36+
// const relativePath = path.relative(projectRoot, filePath);
37+
// const fileName = relativePath.replace(/[\\/]/g, '__');
38+
// return path.join(cachePath, `${fileName}.json`);
39+
// }
40+
2041
export async function getCacheFilePath(cachePath, filePath) {
21-
const projectRoot = path.join(__dirname, '../../../');
22-
const relativePath = path.relative(projectRoot, filePath);
42+
const projectRoot = path.resolve(__dirname, '../../../');
43+
const absoluteFilePath = path.resolve(filePath);
44+
const relativePath = path.relative(projectRoot, absoluteFilePath);
2345
const fileName = relativePath.replace(/[\\/]/g, '__');
2446
return path.join(cachePath, `${fileName}.json`);
2547
}

packages/cli/src/transform.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ export async function compileFile(
135135

136136
const newOutputHash = await computeHash(outputFilePath);
137137

138+
console.log('writefilepathh: ', filePath)
139+
138140
await writeCache(cachePath, filePath, {
139141
inputHash,
140142
outputHash: newOutputHash,

0 commit comments

Comments
 (0)