storyblok 3.23.0 → 3.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ name: 'Unit Tests Production'
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - 'master'
7
+
8
+ jobs:
9
+ unit-tests:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v3
13
+ - uses: actions/setup-node@v3
14
+ with:
15
+ node-version: 18
16
+ cache: 'yarn'
17
+ - name: Install dependencies
18
+ run: yarn install --frozen-lockfile
19
+ - name: Clear jest cache
20
+ run: yarn test:unit --clearCache
21
+ - name: Run unit tests
22
+ run: yarn test:unit --silent --ci
package/README.md CHANGED
@@ -192,7 +192,25 @@ storyblok delete-components <SOURCE> --space <SPACE_ID>
192
192
  ```
193
193
 
194
194
  #### Parameters
195
- * `source`: can be a URL or path to JSON file.
195
+ * `source`: can be a URL or path to JSON file, the path to a json file could be to a single or multiple files separated by comma, like `./pages-1234.json,../User/components/grid-1234.json`
196
+
197
+ Using an **URL**
198
+
199
+ ```sh
200
+ $ storyblok push-components https://raw.githubusercontent.com/storyblok/nuxtdoc/master/seed.components.json --space 67819
201
+ ```
202
+
203
+ Using a **path** to a single file
204
+
205
+ ```sh
206
+ $ storyblok push-components ./components.json --space 67819
207
+ ```
208
+
209
+ Using a **path** to a multiple files
210
+
211
+ ```sh
212
+ $ storyblok push-components ./page.json,../grid.json,./feature.json --space 67819
213
+ ```
196
214
 
197
215
  #### Options
198
216
  * `space_id`: the space where the command should be executed.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storyblok",
3
- "version": "3.23.0",
3
+ "version": "3.24.1",
4
4
  "description": "A simple CLI to start Storyblok from your command line.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,7 +19,7 @@
19
19
  "scripts": {
20
20
  "lint": "eslint src/",
21
21
  "lint:fix": "eslint src/ --fix",
22
- "test": "jest --silent",
22
+ "test:unit": "jest --silent",
23
23
  "test:coverage": "jest --coverage"
24
24
  },
25
25
  "author": "Dominik Angerer <dominikangerer1@gmail.com>, Alexander Feiglstorfer <delooks@gmail.com>",
@@ -7,26 +7,46 @@ const isUrl = source => source.indexOf('http') === 0
7
7
 
8
8
  /**
9
9
  * Get the data from a local or remote JSON file
10
- * @param {string} source the local path or remote url of the file
10
+ * @param {string} path the local path or remote url of the file
11
11
  * @returns {Promise<Object>} return the data from the source or an error
12
12
  */
13
- const getDataFromSource = async (source) => {
14
- if (!source) {
13
+ const getDataFromPath = async (path) => {
14
+ if (!path) {
15
15
  return {}
16
16
  }
17
+ const sources = path.split(',')
18
+ const isList = sources.length > 1
17
19
 
18
20
  try {
19
- if (isUrl(source)) {
20
- return (await axios.get(source)).data
21
+ if (isUrl(path)) {
22
+ return (await axios.get(path)).data
21
23
  } else {
22
- return JSON.parse(fs.readFileSync(source, 'utf8'))
24
+ if (!isList) return JSON.parse(fs.readFileSync(sources[0], 'utf8'))
25
+
26
+ const data = []
27
+ sources.forEach((source) => {
28
+ data.push(JSON.parse(fs.readFileSync(source, 'utf8')))
29
+ })
30
+ return data
23
31
  }
24
32
  } catch (err) {
25
- console.error(`${chalk.red('X')} Can not load json file from ${source}`)
33
+ console.error(`${chalk.red('X')} Can not load json file from ${path}`)
26
34
  return Promise.reject(err)
27
35
  }
28
36
  }
29
37
 
38
+ /**
39
+ * Creat an array based in the content parameter and the key provided
40
+ * @param {object} content the data to create a list
41
+ * @param {string} key key to serch in the content
42
+ * @returns {Array} return the data from the source or an error
43
+ */
44
+ const createContentList = (content, key) => {
45
+ if (content[key]) return content[key]
46
+ else if (Array.isArray(content)) return [...content]
47
+ else return [content]
48
+ }
49
+
30
50
  /**
31
51
  * Delete all components from your Space that occur in your Local JSON.
32
52
  * @param api {Object}
@@ -37,7 +57,8 @@ const getDataFromSource = async (source) => {
37
57
  */
38
58
  const deleteComponents = async (api, { source, reversed = false, dryRun = false }) => {
39
59
  try {
40
- const sourceComponents = (await getDataFromSource(source)).components || []
60
+ const rawComponents = (await getDataFromPath(source)) || []
61
+ const sourceComponents = createContentList(rawComponents, 'components')
41
62
  if (!reversed) {
42
63
  return deleteAllComponents(api, sourceComponents, dryRun)
43
64
  }
@@ -71,19 +92,17 @@ const deleteAllComponents = async (api, components, dryrun) => {
71
92
  * @returns {Promise<void>}
72
93
  */
73
94
  const deleteComponentsReversed = async (api, components, spaceComponents, dryrun) => {
74
- const unifiedComps = components.concat([...spaceComponents])
75
- const toDelete = unifiedComps
76
- .filter((value, index, self) =>
77
- self.findIndex((o, i) => o.id === value.id && i !== index) < 0)
95
+ const toDeleteSpaceComponents = spaceComponents
96
+ .filter(spaceComponent => components.findIndex(o => o.name === spaceComponent.name) < 0)
78
97
  console.log(chalk.blue('-') + ' Deleting all components which do not appear in the given source.')
79
- for (const c of toDelete) {
98
+ for (const c of toDeleteSpaceComponents) {
80
99
  await deleteComponentAndSkip(api, c, dryrun)
81
100
  }
82
101
  }
83
102
 
84
103
  const deleteComponentAndSkip = async (api, c, dryrun) => {
85
104
  try {
86
- return await deleteComponent(api, { comp: c.id, dryrun: dryrun })
105
+ return await deleteComponent(api, { comp: c.name, dryrun: dryrun })
87
106
  } catch (e) {
88
107
  console.log(chalk.red('-') + ' Error deleting component ' + chalk.blue(c.name) + '! Skipped...')
89
108
  }
@@ -19,6 +19,9 @@ describe('testing deleteComponents', () => {
19
19
  const id = path.split('/')[1]
20
20
  return Promise.resolve({ data: { component: components[id] } })
21
21
  }),
22
+ getComponents: jest.fn(() => {
23
+ return components
24
+ }),
22
25
  delete: jest.fn(() => Promise.resolve())
23
26
  }
24
27
  return deleteComponents(api, { source, reversed: false }).then(() => {
@@ -29,18 +32,16 @@ describe('testing deleteComponents', () => {
29
32
  it('api.deleteComponents reverse', () => {
30
33
  const source = 'components.js'
31
34
  const components = FAKE_COMPONENTS()
32
- const spy = jest.spyOn(fs, 'readFileSync').mockReturnValue(JSON.stringify({
33
- components
34
- }))
35
+ const copy = [...components]
36
+ copy.splice(2, 1)
37
+ const spy = jest.spyOn(fs, 'readFileSync').mockReturnValue(JSON.stringify([...copy]))
35
38
  const api = {
36
39
  get: jest.fn((path) => {
37
40
  const id = path.split('/')[1]
38
41
  return Promise.resolve({ data: { component: components[id] } })
39
42
  }),
40
43
  getComponents: jest.fn(() => {
41
- const copy = [...components]
42
- copy.splice(3, 1)
43
- return copy
44
+ return components
44
45
  }),
45
46
  delete: jest.fn(() => Promise.resolve())
46
47
  }
@@ -40,7 +40,7 @@ describe('testing pullComponents', () => {
40
40
  }
41
41
 
42
42
  const options = {
43
- compFileName: SPACE
43
+ fileName: SPACE
44
44
  }
45
45
 
46
46
  const expectFileName = `components.${SPACE}.json`
@@ -71,7 +71,7 @@ describe('testing pullComponents', () => {
71
71
  }
72
72
 
73
73
  const options = {
74
- space: SPACE
74
+ fileName: SPACE
75
75
  }
76
76
 
77
77
  const expectComponentFileName = `components.${SPACE}.json`
@@ -108,7 +108,7 @@ describe('testing pullComponents', () => {
108
108
  }
109
109
 
110
110
  const options = {
111
- space: SPACE,
111
+ fileName: SPACE,
112
112
  separateFiles: true
113
113
  }
114
114
 
@@ -117,7 +117,7 @@ describe('testing pullComponents', () => {
117
117
  expect(fs.writeFile.mock.calls.length).toBe(FAKE_COMPONENTS().length)
118
118
 
119
119
  for (const comp in FAKE_COMPONENTS()) {
120
- const compFileName = `${FAKE_COMPONENTS()[comp].name}-${SPACE}.json`
120
+ const fileName = `${FAKE_COMPONENTS()[comp].name}-${SPACE}.json`
121
121
  let data = FAKE_COMPONENTS()[comp]
122
122
  const [compPath, compData] = fs.writeFile.mock.calls[comp]
123
123
 
@@ -125,7 +125,7 @@ describe('testing pullComponents', () => {
125
125
  data = { ...FAKE_COMPONENTS()[comp], component_group_name: '' }
126
126
  }
127
127
 
128
- expect(compPath).toBe(`./${compFileName}`)
128
+ expect(compPath).toBe(`./${fileName}`)
129
129
  expect(JSON.parse(compData)).toEqual(data)
130
130
  }
131
131
  })