redocly-cli icon indicating copy to clipboard operation
redocly-cli copied to clipboard

Use `redocly split --separator '/'` to use subdirectories as separators

Open TylerRick opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe.

Given a path /thing/{id}, the split command currently fails if you try to use / as a separator:

⟫ npx redocly split --separator '/' --outDir split api.yaml

Error: ENOENT: no such file or directory, open 'split/paths/thing/{id}.yaml'
    at Object.openSync (node:fs:599:3)
    at Object.writeFileSync (node:fs:2221:35)
    at Object.writeYaml (node_modules/@redocly/cli/lib/utils.js:159:8)

Describe the solution you'd like

To make a nested directory structure for the path items that actually matches the directory structure implied by the path...

So instead of the separator character being a part of the filename (which of course is not allowed on Unix-like OS's), a / separator would be treated as part of the path — the same way a / inside a path is usually treated on a Unix-like OS — and be used to separate folders/subfolders/filenames in the path.

So for path /things/{id} it would output to paths/things/{id}.yaml, and so on.

Probably wouldn't take much to implement. Just add an extra mkdir -p when the path contains / (to avoid the ENOENT error above) and then treat it as a path as-is like it's already doing....

export function saveBundle(filename: string, output: string) {
  fs.mkdirSync(path.dirname(filename), { recursive: true });
  fs.writeFileSync(filename, output);
}

TylerRick avatar Sep 23 '22 00:09 TylerRick

Seems like a good idea.

A path item can contain refs to components (such as schemas and parameters). The path to those schemas and parameter files would be different depending on the path depth (paths/things would be different from paths/things/{id}). This is doable, but something we need to account for and test too.

In some cases it's easy to have all paths at the same depth due to copy-pasta compatibility of $refs across paths. With this change, copy-pasta needs to adjust based on path depth. However, it may result in a nicer organization of the paths files themselves.

adamaltman avatar Sep 26 '22 06:09 adamaltman

Fixed in #997

lornajane avatar Mar 06 '24 10:03 lornajane