From fbd16eaaf4084fc8666ec7f3bb4758ed44fcc0f8 Mon Sep 17 00:00:00 2001 From: Melissa Liu Date: Mon, 13 Jan 2025 13:41:53 -0800 Subject: [PATCH] fix relative path --- .../__tests__/compile-stylex-folder-test.js | 45 +++++++++---------- packages/cli/src/cache.js | 26 ++++++++++- packages/cli/src/transform.js | 2 + 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/packages/cli/__tests__/compile-stylex-folder-test.js b/packages/cli/__tests__/compile-stylex-folder-test.js index ba2a5acbe..8de8e0454 100644 --- a/packages/cli/__tests__/compile-stylex-folder-test.js +++ b/packages/cli/__tests__/compile-stylex-folder-test.js @@ -258,6 +258,11 @@ describe('cache mechanism works as expected', () => { copiedNodeModules: false, }, }; + + beforeAll(async () => { + await fs.rm(cachePath, { recursive: true, force: true }); + }); + beforeEach(() => { writeSpy = jest.spyOn(cacheModule, 'writeCache'); }); @@ -280,6 +285,7 @@ describe('cache mechanism works as expected', () => { expect(writeSpy).toHaveBeenCalledTimes(3); const cacheFiles = await fs.readdir(cachePath); + console.log('cacheFiles', cacheFiles) expect(cacheFiles.length).toEqual(3); for (const cacheFile of cacheFiles) { @@ -327,7 +333,8 @@ describe('cache mechanism works as expected', () => { describe('CLI works with a custom cache path', () => { let writeSpy; - const customCachePath = path.join(__dirname, '__custom_cache__'); + const projectRoot = path.resolve(__dirname, '../../../'); + const customCachePath = path.join(projectRoot, '__custom_cache__'); const config: TransformConfig = { input: path.resolve('./source'), output: path.resolve('./src'), @@ -369,31 +376,21 @@ describe('CLI works with a custom cache path', () => { test('uses the custom cache path for caching', async () => { await compileDirectory(config); - const customFilePath = path.join(config.input, 'index.js'); - - const cacheFilePath = path.join( - customCachePath, - path.relative(config.input, customFilePath) + '.json', - ); - - expect( - await fs - .access(customCachePath) - .then(() => true) - .catch(() => false), - ).toBe(true); - expect( - await fs - .access(cacheFilePath) - .then(() => true) - .catch(() => false), - ).toBe(true); + const cacheFiles = await fs.readdir(customCachePath); + console.log('cacheFiles', cacheFiles) + expect(cacheFiles.length).toEqual(3); - const cacheData = JSON.parse(await fs.readFile(cacheFilePath, 'utf-8')); - expect(cacheData).toHaveProperty('inputHash'); - expect(cacheData).toHaveProperty('outputHash'); - expect(cacheData).toHaveProperty('collectedCSS'); + for (const cacheFile of cacheFiles) { + const cacheFilePath = path.join(customCachePath, cacheFile); + const cacheContent = JSON.parse( + await fs.readFile(cacheFilePath, 'utf-8'), + ); + expect(cacheContent).toHaveProperty('inputHash'); + expect(cacheContent).toHaveProperty('outputHash'); + expect(cacheContent).toHaveProperty('collectedCSS'); + } }); + test('skips transformation when cache is valid', async () => { await compileDirectory(config); diff --git a/packages/cli/src/cache.js b/packages/cli/src/cache.js index 1a8718b11..c1ac950f2 100644 --- a/packages/cli/src/cache.js +++ b/packages/cli/src/cache.js @@ -17,9 +17,31 @@ export function getDefaultCachePath() { return path.join('node_modules', '.stylex-cache'); } +// function findProjectRoot(startDir = process.cwd()) { +// let currentDir = startDir; + +// while (currentDir !== path.parse(currentDir).root) { +// if (fs.existsSync(path.join(currentDir, 'package.json'))) { +// return currentDir; +// } +// currentDir = path.dirname(currentDir); +// } + +// throw new Error('Project root not found'); +// } + +// export async function getCacheFilePath(cachePath, filePath) { +// const projectRoot = path.join(__dirname); +// //findProjectRoot(); // Dynamically find the true project root +// const relativePath = path.relative(projectRoot, filePath); +// const fileName = relativePath.replace(/[\\/]/g, '__'); +// return path.join(cachePath, `${fileName}.json`); +// } + export async function getCacheFilePath(cachePath, filePath) { - const projectRoot = path.join(__dirname, '../../../'); - const relativePath = path.relative(projectRoot, filePath); + const projectRoot = path.resolve(__dirname, '../../../'); + const absoluteFilePath = path.resolve(filePath); + const relativePath = path.relative(projectRoot, absoluteFilePath); const fileName = relativePath.replace(/[\\/]/g, '__'); return path.join(cachePath, `${fileName}.json`); } diff --git a/packages/cli/src/transform.js b/packages/cli/src/transform.js index 8c957e65f..b29ecf316 100644 --- a/packages/cli/src/transform.js +++ b/packages/cli/src/transform.js @@ -135,6 +135,8 @@ export async function compileFile( const newOutputHash = await computeHash(outputFilePath); + console.log('writefilepathh: ', filePath) + await writeCache(cachePath, filePath, { inputHash, outputHash: newOutputHash,