Skip to content

Commit

Permalink
feat: add option to prefix presets filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
christianzoppi committed Jun 6, 2024
1 parent e3c0907 commit 8c5c8c8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,14 @@ $ storyblok pull-languages --space <SPACE_ID>

Download your space's components schema as json. By default this command will download 2 files: 1 for the components and 1 for the presets; But if you pass a flag `--separate-files or --sf` the command will create file for each component and presets. And also you could pass a path `--path or -p` to save your components and presets.

It's highly recommended to use also the `--prefix-presets-names` or `-ppn` parameter if you use `--separate-files` because it will prefix the names of the individual files with the name of the component. This feature solves the issue of multiple presets from different compoentns but with the same name, being written in the same file. In a future major version this will become the default behavior.

```sh
$ storyblok pull-components --space <SPACE_ID> # Will save files like components-1234.json
```

```sh
$ storyblok pull-components --space <SPACE_ID> --separate-files --file-name production # Will save files like feature-production.json grid-production.json
$ storyblok pull-components --space <SPACE_ID> --separate-files --prefix-presets-names --file-name production # Will save files like feature-production.json grid-production.json
```

#### Options
Expand Down
5 changes: 3 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,12 @@ program
.option("--sf, --separate-files [value]", "Argument to create a single file for each component")
.option("-p, --path <path>", "Path to save the component files")
.option("-f, --file-name <fileName>", "custom name to be used in file(s) name instead of space id")
.option("-ppn, --prefix-presets-names", "Prefixes the names of presets with the name of the components")
.description("Download your space's components schema as json")
.action(async (options) => {
console.log(`${chalk.blue("-")} Executing pull-components task`);
const space = program.space;
const { separateFiles, path } = options;
const { separateFiles, path, prefixPresetsNames } = options;
if (!space) {
console.log(chalk.red("X") + " Please provide the space as argument --space YOUR_SPACE_ID.");
process.exit(0);
Expand All @@ -160,7 +161,7 @@ program
}

api.setSpaceId(space);
await tasks.pullComponents(api, { fileName, separateFiles, path });
await tasks.pullComponents(api, { fileName, separateFiles, path, prefixPresetsNames });
} catch (e) {
errorHandler(e, COMMANDS.PULL_COMPONENTS);
}
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/pull-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const getNameFromComponentGroups = (groups, uuid) => {
* @return {Promise<Object>}
*/
const pullComponents = async (api, options) => {
const { fileName, separateFiles, path } = options
const { fileName, separateFiles, path, prefixPresetsNames } = options

try {
const componentGroups = await api.getComponentGroups()
Expand Down Expand Up @@ -52,7 +52,7 @@ const pullComponents = async (api, options) => {
if (presets.length === 0) return

for (const preset in presets) {
const presetFileName = `${presets[preset].name}-${fileName}.json`
const presetFileName = `${prefixPresetsNames ? `${presets[preset].preset.component}-` : ""}${presets[preset].name}-${fileName}.json`
const data = JSON.stringify(presets[preset], null, 2)
saveFileFactory(presetFileName, data, path)
}
Expand Down

0 comments on commit 8c5c8c8

Please sign in to comment.