From 288d07a7c7297dabd575a1c06b18a78186e1f6dd Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Thu, 17 Feb 2022 18:05:08 +0100 Subject: [PATCH 1/3] fix(scripts): use process.cwd() instead of __dirname --- scripts/pre-gen/setHostsOptions.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/pre-gen/setHostsOptions.ts b/scripts/pre-gen/setHostsOptions.ts index feef141da3..8224a75a35 100644 --- a/scripts/pre-gen/setHostsOptions.ts +++ b/scripts/pre-gen/setHostsOptions.ts @@ -32,7 +32,7 @@ type AdditionalProperties = Partial<{ }>; async function setHostsOptions(): Promise { - const openapitoolsPath = path.join(__dirname, '../../openapitools.json'); + const openapitoolsPath = path.join(process.cwd(), '../openapitools.json'); const openapitools = JSON.parse(await readFile(openapitoolsPath, 'utf-8')); const [language, client] = process.argv.slice(2); @@ -43,7 +43,7 @@ async function setHostsOptions(): Promise { throw new Error(`Generator not found: ${generator}`); } - const specPath = path.join(__dirname, `../../specs/bundled/${client}.yml`); + const specPath = path.join(process.cwd(), `../specs/bundled/${client}.yml`); if (!(await stat(specPath))) { throw new Error(`File not found ${specPath}`); From 73a6360ad7fc0d0c38bfd1b83150a65bb2348239 Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Fri, 18 Feb 2022 14:21:19 +0100 Subject: [PATCH 2/3] chore: add comment --- scripts/pre-gen/setHostsOptions.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/pre-gen/setHostsOptions.ts b/scripts/pre-gen/setHostsOptions.ts index 8224a75a35..35d9e2ec53 100644 --- a/scripts/pre-gen/setHostsOptions.ts +++ b/scripts/pre-gen/setHostsOptions.ts @@ -32,6 +32,8 @@ type AdditionalProperties = Partial<{ }>; async function setHostsOptions(): Promise { + // Run this script with `yarn workspace scripts setHostsOptions`, instead of (cd ./scripts ....) + // Use `process.cwd()` instead of `__dirname` because this will be transpiled to `dist/`. const openapitoolsPath = path.join(process.cwd(), '../openapitools.json'); const openapitools = JSON.parse(await readFile(openapitoolsPath, 'utf-8')); From 3e63c9e81f31b03f4895772f1c14d39883ef6eee Mon Sep 17 00:00:00 2001 From: Eunjae Lee Date: Fri, 18 Feb 2022 15:05:26 +0100 Subject: [PATCH 3/3] chore: throw error when file not found --- scripts/pre-gen/setHostsOptions.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/scripts/pre-gen/setHostsOptions.ts b/scripts/pre-gen/setHostsOptions.ts index 35d9e2ec53..872a5837bf 100644 --- a/scripts/pre-gen/setHostsOptions.ts +++ b/scripts/pre-gen/setHostsOptions.ts @@ -32,9 +32,12 @@ type AdditionalProperties = Partial<{ }>; async function setHostsOptions(): Promise { - // Run this script with `yarn workspace scripts setHostsOptions`, instead of (cd ./scripts ....) - // Use `process.cwd()` instead of `__dirname` because this will be transpiled to `dist/`. const openapitoolsPath = path.join(process.cwd(), '../openapitools.json'); + if (!(await stat(openapitoolsPath))) { + throw new Error( + `File not found ${openapitoolsPath}.\nMake sure your run scripts from the root directory using yarn workspace.` + ); + } const openapitools = JSON.parse(await readFile(openapitoolsPath, 'utf-8')); const [language, client] = process.argv.slice(2); @@ -48,7 +51,9 @@ async function setHostsOptions(): Promise { const specPath = path.join(process.cwd(), `../specs/bundled/${client}.yml`); if (!(await stat(specPath))) { - throw new Error(`File not found ${specPath}`); + throw new Error( + `File not found ${specPath}.\nMake sure your run scripts from the root directory using yarn workspace.` + ); } try {