Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(script): use process.cwd() instead of __dirname #142

Merged
merged 3 commits into from
Feb 18, 2022
Merged

Conversation

eunjae-lee
Copy link
Contributor

🧭 What and Why

Changes included:

The output of yarn workspace scripts build has changed since the latest commit. So the path of the transpiled setHostsOptions.js is different, which means __dirname is now different. __dirname is not reliable, so we should stick to process.cwd() which is the working directory of the current process.

@eunjae-lee eunjae-lee changed the title fix(scripts): use process.cwd() instead of __dirname fix(script): use process.cwd() instead of __dirname Feb 17, 2022
@bodinsamuel
Copy link
Contributor

nb: process.cwd() reflect the dir of the execution, so could behave differently if you exec in ./scripts or / (or any subfolder since yarn allows it)

@eunjae-lee
Copy link
Contributor Author

nb: process.cwd() reflect the dir of the execution, so could behave differently if you exec in ./scripts or / (or any subfolder since yarn allows it)

Yeah, so far, the way we've been executing scripts are not

cd xxx && ./something or ./xxx/something

but more like

yarn workspace scripts something

So the result of process.cwd() is consistently ./scripts

@eunjae-lee eunjae-lee requested a review from shortcuts February 18, 2022 10:04
@@ -43,7 +43,7 @@ async function setHostsOptions(): Promise<void> {
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}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add make sure to use workspaces to run this script or something similar that can give an hint

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this script is ran by the generator so it might not relevant but at least give hints on why/where it fails)

@@ -32,7 +32,7 @@ type AdditionalProperties = Partial<{
}>;

async function setHostsOptions(): Promise<void> {
const openapitoolsPath = path.join(__dirname, '../../openapitools.json');
const openapitoolsPath = path.join(process.cwd(), '../openapitools.json');
const openapitools = JSON.parse(await readFile(openapitoolsPath, 'utf-8'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the same check as L48, with a similar mention to make sure to use workspaces to run the script

@eunjae-lee
Copy link
Contributor Author

@shortcuts I added one comment instead of two. It was a bit weird to have the same comment within the same function.

@eunjae-lee eunjae-lee requested a review from shortcuts February 18, 2022 13:21
@shortcuts
Copy link
Member

shortcuts commented Feb 18, 2022

@shortcuts I added one comment instead of two. It was a bit weird to have the same comment within the same function.

Sorry I was speaking about throwing an error directly and adding the reason in it.

Same as this but more detailed

if (!(await stat(specPath))) {
throw new Error(`File not found ${specPath}`);
}

e.g. File not found ${thePath}, make sure your run scripts from the root directory using yarn workspaces.

Copy link
Member

@shortcuts shortcuts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

@eunjae-lee eunjae-lee merged commit 59be865 into main Feb 18, 2022
@eunjae-lee eunjae-lee deleted the fix/dirname branch February 18, 2022 14:50
shortcuts pushed a commit that referenced this pull request Apr 22, 2022
* fix(scripts): use process.cwd() instead of __dirname

* chore: add comment

* chore: throw error when file not found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants