Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit 1d96b5a

Browse files
authored
Merge pull request #99 from storyblok/fix/PRO-173
Fix/pro 173
2 parents 505356a + 3f883dc commit 1d96b5a

5 files changed

Lines changed: 27 additions & 27 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ $ storyblok pull-languages --space <SPACE_ID>
9696

9797
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.
9898

99+
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.
100+
99101
```sh
100102
$ storyblok pull-components --space <SPACE_ID> # Will save files like components-1234.json
101103
```
102104

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

107109
#### Options

src/cli.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ program
142142
.option("--sf, --separate-files [value]", "Argument to create a single file for each component")
143143
.option("-p, --path <path>", "Path to save the component files")
144144
.option("-f, --file-name <fileName>", "custom name to be used in file(s) name instead of space id")
145+
.option("-ppn, --prefix-presets-names", "Prefixes the names of presets with the name of the components")
145146
.description("Download your space's components schema as json")
146147
.action(async (options) => {
147148
console.log(`${chalk.blue("-")} Executing pull-components task`);
148149
const space = program.space;
149-
const { separateFiles, path } = options;
150+
const { separateFiles, path, prefixPresetsNames } = options;
150151
if (!space) {
151152
console.log(chalk.red("X") + " Please provide the space as argument --space YOUR_SPACE_ID.");
152153
process.exit(0);
@@ -160,7 +161,7 @@ program
160161
}
161162

162163
api.setSpaceId(space);
163-
await tasks.pullComponents(api, { fileName, separateFiles, path });
164+
await tasks.pullComponents(api, { fileName, separateFiles, path, prefixPresetsNames });
164165
} catch (e) {
165166
errorHandler(e, COMMANDS.PULL_COMPONENTS);
166167
}

src/tasks/pull-components.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const getNameFromComponentGroups = (groups, uuid) => {
2424
* @return {Promise<Object>}
2525
*/
2626
const pullComponents = async (api, options) => {
27-
const { fileName, separateFiles, path } = options
27+
const { fileName, separateFiles, path, prefixPresetsNames } = options
2828

2929
try {
3030
const componentGroups = await api.getComponentGroups()
@@ -52,7 +52,7 @@ const pullComponents = async (api, options) => {
5252
if (presets.length === 0) return
5353

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

src/utils/save-file-factory.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
import fs from 'fs'
22

3-
const saveFileFactory = async (fileName, content, path = './') => {
4-
return new Promise((resolve, reject) => {
5-
fs.writeFile(`${path}${fileName}`, content, err => {
6-
if (err) {
7-
Promise.reject(err)
8-
return
9-
}
10-
11-
Promise.resolve(true)
12-
})
13-
})
3+
const saveFileFactory = (fileName, content, path = './') => {
4+
try {
5+
fs.writeFileSync(`${path}${fileName}`, content);
6+
return true;
7+
} catch(err) {
8+
console.log(err);
9+
return false;
10+
}
1411
}
1512

1613
export default saveFileFactory

tests/units/pull-components.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import pullComponents from '../../src/tasks/pull-components'
33
import { FAKE_PRESET, FAKE_COMPONENTS } from '../constants'
44
import { jest } from '@jest/globals'
55

6-
jest.spyOn(fs, 'writeFile').mockImplementation(jest.fn((key, data, _) => {
6+
jest.spyOn(fs, 'writeFileSync').mockImplementation(jest.fn((key, data, _) => {
77
[key] = data
88
}))
99

@@ -25,7 +25,7 @@ describe('testing pullComponents', () => {
2525
expect(api.getComponents.mock.calls.length).toBe(1)
2626
})
2727

28-
it('pull components should be call fs.writeFile correctly and generate component file', async () => {
28+
it('pull components should be call fs.writeFileSync correctly and generate component file', async () => {
2929
const SPACE = 12345
3030

3131
const api = {
@@ -47,14 +47,14 @@ describe('testing pullComponents', () => {
4747
const expectFileName = `components.${SPACE}.json`
4848

4949
await pullComponents(api, options)
50-
const [path, data] = fs.writeFile.mock.calls[0]
50+
const [path, data] = fs.writeFileSync.mock.calls[0]
5151

52-
expect(fs.writeFile.mock.calls.length).toBe(1)
52+
expect(fs.writeFileSync.mock.calls.length).toBe(1)
5353
expect(path).toBe(`./${expectFileName}`)
5454
expect(JSON.parse(data)).toEqual({ components: [FAKE_COMPONENTS()[0]] })
5555
})
5656

57-
it('pull components should be call fs.writeFile correctly and generate a component and preset files', async () => {
57+
it('pull components should be call fs.writeFileSync correctly and generate a component and preset files', async () => {
5858
const SPACE = 12345
5959

6060
const api = {
@@ -77,10 +77,10 @@ describe('testing pullComponents', () => {
7777
const expectPresetFileName = `presets.${SPACE}.json`
7878

7979
await pullComponents(api, options)
80-
const [compPath, compData] = fs.writeFile.mock.calls[0]
81-
const [presetPath, presetData] = fs.writeFile.mock.calls[1]
80+
const [compPath, compData] = fs.writeFileSync.mock.calls[0]
81+
const [presetPath, presetData] = fs.writeFileSync.mock.calls[1]
8282

83-
expect(fs.writeFile.mock.calls.length).toBe(2)
83+
expect(fs.writeFileSync.mock.calls.length).toBe(2)
8484

8585
expect(compPath).toBe(`./${expectComponentFileName}`)
8686
expect(JSON.parse(compData)).toEqual({ components: [FAKE_COMPONENTS()[0]] })
@@ -89,7 +89,7 @@ describe('testing pullComponents', () => {
8989
expect(JSON.parse(presetData)).toEqual({ presets: FAKE_PRESET() })
9090
})
9191

92-
it('pull components should be call fs.writeFile and generate a single file for each component', async () => {
92+
it('pull components should be call fs.writeFileSync and generate a single file for each component', async () => {
9393
const SPACE = 12345
9494

9595
const api = {
@@ -110,12 +110,12 @@ describe('testing pullComponents', () => {
110110
}
111111

112112
await pullComponents(api, options)
113-
expect(fs.writeFile.mock.calls.length).toBe(FAKE_COMPONENTS().length)
113+
expect(fs.writeFileSync.mock.calls.length).toBe(FAKE_COMPONENTS().length)
114114

115115
for (const comp in FAKE_COMPONENTS()) {
116116
const fileName = `${FAKE_COMPONENTS()[comp].name}-${SPACE}.json`
117117
let data = FAKE_COMPONENTS()[comp]
118-
const [compPath, compData] = fs.writeFile.mock.calls[comp]
118+
const [compPath, compData] = fs.writeFileSync.mock.calls[comp]
119119

120120
if (FAKE_COMPONENTS()[comp].name === 'logo') {
121121
data = { ...FAKE_COMPONENTS()[comp], component_group_name: '' }

0 commit comments

Comments
 (0)