From 8c5c8c8e540f3a635b7aece803021607f98a68c1 Mon Sep 17 00:00:00 2001 From: Christian Zoppi Date: Thu, 6 Jun 2024 11:27:53 +0200 Subject: [PATCH] feat: add option to prefix presets filenames --- README.md | 4 +++- src/cli.ts | 5 +++-- src/tasks/pull-components.js | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fef788b0..11a747d2 100644 --- a/README.md +++ b/README.md @@ -96,12 +96,14 @@ $ storyblok pull-languages --space 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 # Will save files like components-1234.json ``` ```sh -$ storyblok pull-components --space --separate-files --file-name production # Will save files like feature-production.json grid-production.json +$ storyblok pull-components --space --separate-files --prefix-presets-names --file-name production # Will save files like feature-production.json grid-production.json ``` #### Options diff --git a/src/cli.ts b/src/cli.ts index aebde669..e26dc9ba 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -142,11 +142,12 @@ program .option("--sf, --separate-files [value]", "Argument to create a single file for each component") .option("-p, --path ", "Path to save the component files") .option("-f, --file-name ", "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); @@ -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); } diff --git a/src/tasks/pull-components.js b/src/tasks/pull-components.js index d0b97445..8d31cd33 100644 --- a/src/tasks/pull-components.js +++ b/src/tasks/pull-components.js @@ -24,7 +24,7 @@ const getNameFromComponentGroups = (groups, uuid) => { * @return {Promise} */ const pullComponents = async (api, options) => { - const { fileName, separateFiles, path } = options + const { fileName, separateFiles, path, prefixPresetsNames } = options try { const componentGroups = await api.getComponentGroups() @@ -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) }