From c4940c2764a479a26acc667aa3aa349a74134518 Mon Sep 17 00:00:00 2001 From: Christian Zoppi Date: Fri, 31 May 2024 11:02:27 +0200 Subject: [PATCH 1/5] fix: change saveFileFactory to sync --- src/utils/save-file-factory.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/utils/save-file-factory.js b/src/utils/save-file-factory.js index e4c6d7a..a16381b 100644 --- a/src/utils/save-file-factory.js +++ b/src/utils/save-file-factory.js @@ -1,16 +1,13 @@ import fs from 'fs' -const saveFileFactory = async (fileName, content, path = './') => { - return new Promise((resolve, reject) => { - fs.writeFile(`${path}${fileName}`, content, err => { - if (err) { - Promise.reject(err) - return - } - - Promise.resolve(true) - }) - }) +const saveFileFactory = (fileName, content, path = './temp/') => { + try { + fs.writeFileSync(`${path}${fileName}`, content); + return true; + } catch(err) { + console.log(err); + return false; + } } export default saveFileFactory From acecffef97ec753015a11f012c7f82b2ada77c07 Mon Sep 17 00:00:00 2001 From: Christian Zoppi Date: Fri, 31 May 2024 11:02:49 +0200 Subject: [PATCH 2/5] revert path param for saveFileFactory --- src/utils/save-file-factory.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/save-file-factory.js b/src/utils/save-file-factory.js index a16381b..8e085f0 100644 --- a/src/utils/save-file-factory.js +++ b/src/utils/save-file-factory.js @@ -1,6 +1,6 @@ import fs from 'fs' -const saveFileFactory = (fileName, content, path = './temp/') => { +const saveFileFactory = (fileName, content, path = './') => { try { fs.writeFileSync(`${path}${fileName}`, content); return true; From e3c0907860c1925093d2e029bf5acfbd83ae5c08 Mon Sep 17 00:00:00 2001 From: Christian Zoppi Date: Fri, 31 May 2024 14:56:05 +0200 Subject: [PATCH 3/5] fix unit tests for pull-components --- tests/units/pull-components.spec.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/units/pull-components.spec.js b/tests/units/pull-components.spec.js index 5ee3ea2..b3368c7 100644 --- a/tests/units/pull-components.spec.js +++ b/tests/units/pull-components.spec.js @@ -3,7 +3,7 @@ import pullComponents from '../../src/tasks/pull-components' import { FAKE_PRESET, FAKE_COMPONENTS } from '../constants' import { jest } from '@jest/globals' -jest.spyOn(fs, 'writeFile').mockImplementation(jest.fn((key, data, _) => { +jest.spyOn(fs, 'writeFileSync').mockImplementation(jest.fn((key, data, _) => { [key] = data })) @@ -25,7 +25,7 @@ describe('testing pullComponents', () => { expect(api.getComponents.mock.calls.length).toBe(1) }) - it('pull components should be call fs.writeFile correctly and generate component file', async () => { + it('pull components should be call fs.writeFileSync correctly and generate component file', async () => { const SPACE = 12345 const api = { @@ -47,14 +47,14 @@ describe('testing pullComponents', () => { const expectFileName = `components.${SPACE}.json` await pullComponents(api, options) - const [path, data] = fs.writeFile.mock.calls[0] + const [path, data] = fs.writeFileSync.mock.calls[0] - expect(fs.writeFile.mock.calls.length).toBe(1) + expect(fs.writeFileSync.mock.calls.length).toBe(1) expect(path).toBe(`./${expectFileName}`) expect(JSON.parse(data)).toEqual({ components: [FAKE_COMPONENTS()[0]] }) }) - it('pull components should be call fs.writeFile correctly and generate a component and preset files', async () => { + it('pull components should be call fs.writeFileSync correctly and generate a component and preset files', async () => { const SPACE = 12345 const api = { @@ -77,10 +77,10 @@ describe('testing pullComponents', () => { const expectPresetFileName = `presets.${SPACE}.json` await pullComponents(api, options) - const [compPath, compData] = fs.writeFile.mock.calls[0] - const [presetPath, presetData] = fs.writeFile.mock.calls[1] + const [compPath, compData] = fs.writeFileSync.mock.calls[0] + const [presetPath, presetData] = fs.writeFileSync.mock.calls[1] - expect(fs.writeFile.mock.calls.length).toBe(2) + expect(fs.writeFileSync.mock.calls.length).toBe(2) expect(compPath).toBe(`./${expectComponentFileName}`) expect(JSON.parse(compData)).toEqual({ components: [FAKE_COMPONENTS()[0]] }) @@ -89,7 +89,7 @@ describe('testing pullComponents', () => { expect(JSON.parse(presetData)).toEqual({ presets: FAKE_PRESET() }) }) - it('pull components should be call fs.writeFile and generate a single file for each component', async () => { + it('pull components should be call fs.writeFileSync and generate a single file for each component', async () => { const SPACE = 12345 const api = { @@ -110,12 +110,12 @@ describe('testing pullComponents', () => { } await pullComponents(api, options) - expect(fs.writeFile.mock.calls.length).toBe(FAKE_COMPONENTS().length) + expect(fs.writeFileSync.mock.calls.length).toBe(FAKE_COMPONENTS().length) for (const comp in FAKE_COMPONENTS()) { const fileName = `${FAKE_COMPONENTS()[comp].name}-${SPACE}.json` let data = FAKE_COMPONENTS()[comp] - const [compPath, compData] = fs.writeFile.mock.calls[comp] + const [compPath, compData] = fs.writeFileSync.mock.calls[comp] if (FAKE_COMPONENTS()[comp].name === 'logo') { data = { ...FAKE_COMPONENTS()[comp], component_group_name: '' } From 8c5c8c8e540f3a635b7aece803021607f98a68c1 Mon Sep 17 00:00:00 2001 From: Christian Zoppi Date: Thu, 6 Jun 2024 11:27:53 +0200 Subject: [PATCH 4/5] 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 fef788b..11a747d 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 aebde66..e26dc9b 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 d0b9744..8d31cd3 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) } From 3f883dc0aac6cf9caea0e65c7a8e1289801b70a1 Mon Sep 17 00:00:00 2001 From: Christian Zoppi Date: Thu, 6 Jun 2024 14:20:21 +0200 Subject: [PATCH 5/5] fix typo in the readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 11a747d..b0ca332 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ $ 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. +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 components 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