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

mirror directory structure if output is a directory #1345

Merged
merged 10 commits into from
Sep 22, 2023

Conversation

SchabaJo
Copy link
Contributor

@SchabaJo SchabaJo commented Sep 14, 2023

Changes

Fixes #1337

Mirror the directory structure of input files if output is a directory. This prevents overwriting the same file again and again if there are multiple input files.

How to Review

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@changeset-bot
Copy link

changeset-bot bot commented Sep 14, 2023

🦋 Changeset detected

Latest commit: ff00349

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
openapi-typescript Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

create directory structure if we are dealing with multiple files or output directory and file paths match (which indicates it's a directory)
@drwpow
Copy link
Owner

drwpow commented Sep 16, 2023

Thanks so much for this fix! Is there any chance you’d be interested in writing a test for this?

I think part of the reason why this regressed was no glob test in the test suite. If you have time, I’d love to see if we could:

  1. Add a test to cli.test.ts
  2. Have the test read from test/fixtures/[somedir]/[files].yaml
  3. Have the test glob files and output to test/fixtures/[somedir]/out/[files].yaml
  4. Assert that there’s a 1:1 naming match between input and output
  5. Add the output directory to .gitignore so it’s generated fresh every CI run

Bonus points if the test has nested directories.

Each schema could be basically a stub / as minimal as possible as output is tested in many other tests; this would be a purely file structure test.

Would adding that test be possible for this PR, to ensure a future regression doesn’t happen?

@SchabaJo
Copy link
Contributor Author

SchabaJo commented Sep 18, 2023

Added a bunch of tests:

  • single file to file (cli should succeed)
  • single file to directory (cli should succeed, output directories should mirror input)
  • single file (glob) to file (cli should succeed)
  • multiple files to file (cli should fail)
  • multiple files to directory (cli should succeed, output directories should mirror input)
  • multiple nested files to directory (cli should succeed, output directories should mirror input)

PS: sorry for the missing commit signature verification... gonna setup now.

@@ -76,4 +85,59 @@ describe("CLI", () => {
expect(stdout).toEqual(expect.stringMatching(/^v[\d.]+(-.*)?$/));
});
});

describe("outputs", () => {
Copy link
Owner

Choose a reason for hiding this comment

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

🎉

@drwpow
Copy link
Owner

drwpow commented Sep 22, 2023

The tests look great, thank you! And no worries about commit signature verification.

I think we might want to either roll back #1335 or take another look into if there’s a fix for that small regression. But either way once that’s resolved, this fix will go out in the next release.

@@ -114,13 +114,13 @@ async function generateSchema(pathToSpec) {
let outputFilePath = new URL(flags.output, CWD); // note: may be directory
const isDir = fs.existsSync(outputFilePath) && fs.lstatSync(outputFilePath).isDirectory();
if (isDir) {
if (typeof flags.output === 'string' && !flags.output.endsWith('/')) {
outputFilePath = new URL(`${flags.output}/`, CWD)
if (typeof flags.output === "string" && !flags.output.endsWith("/")) {
Copy link
Owner

Choose a reason for hiding this comment

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

It looks like we’re accidentally skipping this from linting/Prettier. Thanks for fixing this file.

@drwpow drwpow merged commit 6f078c1 into drwpow:main Sep 22, 2023
6 checks passed
@github-actions github-actions bot mentioned this pull request Sep 22, 2023
@SchabaJo SchabaJo deleted the globbing_fix_directory_structure branch September 25, 2023 06:09
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.

Globbing - path of output files is always the same
2 participants