From a7d4c2e9f650b86f8e19833079c286a64f807d2a Mon Sep 17 00:00:00 2001 From: JYC Date: Tue, 13 Sep 2022 21:50:46 +0200 Subject: [PATCH] Improve monorepo support (#539) * :sparkles: NEW: mon repo support * Trigger * :bug: FIX: isDirectory * :zap: IMPROVE: making it private * :zap: IMPROVE: ready for mocks as well * :recycle: IMPROVE: naming to existsSync --- .changeset/olive-carrots-fry.md | 5 +++++ src/common/config.ts | 36 +++++++++++++++++++++++++++++---- src/common/fs.ts | 9 +++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 .changeset/olive-carrots-fry.md diff --git a/.changeset/olive-carrots-fry.md b/.changeset/olive-carrots-fry.md new file mode 100644 index 0000000000..3d96fa0891 --- /dev/null +++ b/.changeset/olive-carrots-fry.md @@ -0,0 +1,5 @@ +--- +'houdini': patch +--- + +fix - mono repo support diff --git a/src/common/config.ts b/src/common/config.ts index 0f07bd4616..b9f3655326 100644 --- a/src/common/config.ts +++ b/src/common/config.ts @@ -293,15 +293,43 @@ ${ return '$houdini.d.ts' } + private findNodeModulesHoudini(currentLocation: string) { + const pathEndingBy = ['node_modules', 'houdini'] + + // Build the first possible location + let locationFound = path.join(currentLocation, ...pathEndingBy) + + // previousLocation is nothing + let previousLocation = '' + const backFolder = [] + + // if previousLocation !== locationFound that mean that we can go upper + // if the directory doesn't exist, let's go upper. + while (previousLocation !== locationFound && !fs.existsSync(locationFound)) { + // save the previous path + previousLocation = locationFound + + // add a back folder + backFolder.push('../') + + // set the new location + locationFound = path.join(currentLocation, ...backFolder, ...pathEndingBy) + } + + if (previousLocation === locationFound) { + throw new Error('Could not find any node_modules/houdini folder') + } + + return locationFound + } + get runtimeSource() { // when running in the real world, scripts are nested in a sub directory of build, in tests they aren't nested // under /src so we need to figure out how far up to go to find the appropriately compiled runtime const relative = process.env.TEST ? path.join(currentDir, '../../') - : // TODO: it's very possible this breaks someones setup. the old version walked up from currentDir - // there isn't a consistent number of steps up anymore since the vite plugin and cmd live at different depths - // a better approach could be to start at current dir and walk up until we find a `houdini` dir - path.join(path.dirname(this.filepath), 'node_modules', 'houdini') + : // start here and go to parent until we find the node_modules/houdini folder + this.findNodeModulesHoudini(path.join(path.dirname(this.filepath))) // we want to copy the typescript source code for the templates and then compile the files according // to the requirements of the platform diff --git a/src/common/fs.ts b/src/common/fs.ts index 818bea95e8..d3fb02f848 100644 --- a/src/common/fs.ts +++ b/src/common/fs.ts @@ -113,6 +113,15 @@ export async function stat(filepath: string) { return memfs.statSync(filepath) } +export function existsSync(dirPath: string) { + // no mock in production + if (process.env.NODE_ENV !== 'test') { + return fsExtra.existsSync(dirPath) + } + + return memfs.existsSync(dirPath) +} + export async function readdir(filepath: string): Promise { // no mock in production if (process.env.NODE_ENV !== 'test') {