This repository was archived by the owner on Jan 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·511 lines (441 loc) · 15.9 KB
/
Copy pathcli.js
File metadata and controls
executable file
·511 lines (441 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/usr/bin/env node
const commander = require('commander')
const program = new commander.Command()
const chalk = require('chalk')
const clear = require('clear')
const figlet = require('figlet')
const inquirer = require('inquirer')
const updateNotifier = require('update-notifier')
const pkg = require('../package.json')
const tasks = require('./tasks')
const { getQuestions, lastStep, api, creds } = require('./utils')
const { SYNC_TYPES, COMMANDS } = require('./constants')
clear()
console.log(chalk.cyan(figlet.textSync('storyblok')))
console.log()
console.log()
console.log('Hi, welcome to the Storyblok CLI')
console.log()
// non-intrusive notify users if an update available
const notifyOptions = {
isGlobal: true
}
updateNotifier({ pkg })
.notify(notifyOptions)
program.version(pkg.version)
program
.option('-s, --space [value]', 'space ID')
// login
program
.command(COMMANDS.LOGIN)
.description('Login to the Storyblok cli')
.option('-t, --token <token>', 'Token to login directly without questions, like for CI enviroments')
.option('-r, --region <region>', 'The region you would like to work in. Please keep in mind that the region must match the region of your space. You can use us, cn or eu, if left empty, default is eu. This region flag will be used for the other cli\'s commands')
.action(async (options) => {
const { token, region } = options
if (api.isAuthorized()) {
console.log(chalk.green('✓') + ' The user has been already logged. If you want to change the logged user, you must logout and login again')
return
}
try {
await api.processLogin(token, region)
process.exit(0)
} catch (e) {
console.log(chalk.red('X') + ' An error occurred when logging the user: ' + e.message)
process.exit(1)
}
})
// getUser
program
.command('user')
.description('Get the currently logged in user')
.action(async () => {
if (api.isAuthorized()) {
try {
const user = await api.getUser()
console.log(chalk.green('✓') + ` Hi ${user.friendly_name}, you current logged in with: ${creds.get().email}`)
} catch (e) {
console.log(chalk.red('X') + ` Please check if your current region matches your user's region: ${e.message}.`)
} finally {
process.exit(0)
}
}
console.log(chalk.red('X') + ' There is currently no user logged.')
})
// logout
program
.command(COMMANDS.LOGOUT)
.description('Logout from the Storyblok cli')
.action(async () => {
try {
await api.logout()
console.log('Logged out successfully! Token has been removed from .netrc file.')
process.exit(0)
} catch (e) {
console.log(chalk.red('X') + ' An error occurred when logging out the user: ' + e.message)
process.exit(1)
}
})
// pull-languages
program
.command('pull-languages')
.description("Download your space's languages schema as json")
.action(async () => {
console.log(`${chalk.blue('-')} Executing pull-languages task`)
const space = program.space
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(0)
}
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
api.setSpaceId(space)
await tasks.pullLanguages(api, { space })
} catch (e) {
console.log(chalk.red('X') + ' An error occurred when executing the pull-languages task: ' + e.message)
process.exit(1)
}
})
// pull-components
program
.command(COMMANDS.PULL_COMPONENTS)
.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')
.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
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(0)
}
const fileName = options.fileName ? options.fileName : space
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
api.setSpaceId(space)
await tasks.pullComponents(api, { fileName, separateFiles, path })
} catch (e) {
errorHandler(e, COMMANDS.PULL_COMPONENTS)
}
})
// push-components
program
.command(COMMANDS.PUSH_COMPONENTS + ' <source>')
.option('-p, --presets-source <presetsSource>', 'Path to presets file')
.description("Download your space's components schema as json. The source parameter can be a URL to your JSON file or a path to it")
.action(async (source, options) => {
console.log(`${chalk.blue('-')} Executing push-components task`)
const space = program.space
const presetsSource = options.presetsSource
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(0)
}
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
api.setSpaceId(space)
await tasks.pushComponents(api, { source, presetsSource })
} catch (e) {
errorHandler(e, COMMANDS.PUSH_COMPONENTS)
}
})
// delete-component
program
.command('delete-component <component>')
.description('Delete a single component on your space.')
.action(async (component) => {
console.log(`${chalk.blue('-')} Executing delete-component task`)
const space = program.space
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(0)
}
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
api.setSpaceId(space)
await tasks.deleteComponent(api, { comp: component })
} catch (e) {
console.log(chalk.red('X') + ' An error occurred when executing the delete-component task: ' + e.message)
process.exit(1)
}
})
// delete-components
program
.command('delete-components <source>')
.description('Delete all components in your space that occur in your source file.')
.option('-r, --reverse', 'Delete all components in your space that do not appear in your source.', false)
.option('--dryrun', 'Does not perform any delete changes on your space.')
.action(async (source, options) => {
console.log(`${chalk.blue('-')} Executing delete-components task`)
const space = program.space
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(0)
}
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
api.setSpaceId(space)
await tasks.deleteComponents(api, { source, dryRun: !!options.dryrun, reversed: !!options.reverse })
} catch (e) {
console.log(chalk.red('X') + ' An error occurred when executing the delete-component task: ' + e.message)
process.exit(1)
}
})
// scaffold
program
.command(COMMANDS.SCAFFOLD + ' <name>')
.description('Scaffold <name> component')
.action(async (name) => {
console.log(`${chalk.blue('-')} Scaffolding a component\n`)
if (api.isAuthorized()) {
api.accessToken = creds.get().token || null
}
try {
await tasks.scaffold(api, name, program.space)
console.log(chalk.green('✓') + ' Generated files: ')
console.log(chalk.green('✓') + ' - views/components/_' + name + '.liquid')
console.log(chalk.green('✓') + ' - source/scss/components/below/_' + name + '.scss')
process.exit(0)
} catch (e) {
console.log(chalk.red('X') + ' An error occurred when executing operations to create the component: ' + e.message)
process.exit(1)
}
})
// select
program
.command(COMMANDS.SELECT)
.description('Usage to kickstart a boilerplate, fieldtype or theme')
.action(async () => {
console.log(`${chalk.blue('-')} Select a boilerplate, fieldtype or theme to initialize\n`)
try {
const questions = getQuestions('select')
const answers = await inquirer.prompt(questions)
await lastStep(answers)
} catch (e) {
console.error(chalk.red('X') + ' An error ocurred when execute the select command: ' + e.message)
process.exit(1)
}
})
// sync
program
.command(COMMANDS.SYNC)
.description('Sync schemas, roles, folders and stories between spaces')
.requiredOption('--type <TYPE>', 'Define what will be sync. Can be components, folders, stories, datasources or roles')
.requiredOption('--source <SPACE_ID>', 'Source space id')
.requiredOption('--target <SPACE_ID>', 'Target space id')
.option('--components-groups <UUIDs>', 'Synchronize components based on their group UUIDs separated by commas')
.option('--datasource-disable-dimensions-value-sync', 'Enables syncing of datasources without their corresponding dimension values.')
.action(async (options) => {
console.log(`${chalk.blue('-')} Sync data between spaces\n`)
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
const {
type,
target,
source,
componentsGroups,
datasourceDisableDimensionsValueSync,
} = options
const _componentsGroups = componentsGroups ? componentsGroups.split(',') : null
const token = creds.get().token || null
const _types = type.split(',') || []
_types.forEach(_type => {
if (!SYNC_TYPES.includes(_type)) {
throw new Error(`The type ${_type} is not valid`)
}
})
await tasks.sync(_types, {
api,
token,
target,
source,
_componentsGroups,
datasourceDisableDimensionsValueSync
})
console.log('\n' + chalk.green('✓') + ' Sync data between spaces successfully completed')
} catch (e) {
errorHandler(e, COMMANDS.SYNC)
}
})
// quickstart
program
.command(COMMANDS.QUICKSTART)
.description('Start a project quickly')
.action(async () => {
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
const space = program.space
const questions = getQuestions('quickstart', { space }, api)
const answers = await inquirer.prompt(questions)
await tasks.quickstart(api, answers, space)
} catch (e) {
console.log(chalk.red('X') + ' An error ocurred when execute quickstart operations: ' + e.message)
process.exit(1)
}
})
program
.command(COMMANDS.GENERATE_MIGRATION)
.description('Generate a content migration file')
.requiredOption('-c, --component <COMPONENT_NAME>', 'Name of the component')
.requiredOption('-f, --field <FIELD_NAME>', 'Name of the component field')
.action(async (options) => {
const { field = '' } = options
const { component = '' } = options
const space = program.space
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(1)
}
console.log(`${chalk.blue('-')} Creating the migration file in ./migrations/change_${component}_${field}.js\n`)
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
api.setSpaceId(space)
await tasks.generateMigration(api, component, field)
} catch (e) {
console.log(chalk.red('X') + ' An error ocurred when generate the migration file: ' + e.message)
process.exit(1)
}
})
program
.command(COMMANDS.RUN_MIGRATION)
.description('Run a migration file')
.requiredOption('-c, --component <COMPONENT_NAME>', 'Name of the component')
.requiredOption('-f, --field <FIELD_NAME>', 'Name of the component field')
.option('--dryrun', 'Do not update the story content')
.option('--publish <PUBLISH_OPTION>', 'Publish the content. It can be: all, published or published-with-changes')
.option('--publish-languages <LANGUAGES>', 'Publish specific languages')
.action(async (options) => {
const field = options.field || ''
const component = options.component || ''
const isDryrun = !!options.dryrun
const publish = options.publish || null
const publishLanguages = options.publishLanguages || ''
const space = program.space
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(1)
}
const publishOptionsAvailable = [
'all',
'published',
'published-with-changes'
]
if (publish && !publishOptionsAvailable.includes(publish)) {
console.log(chalk.red('X') + ' Please provide a correct publish option: all, published, or published-with-changes')
process.exit(1)
}
console.log(`${chalk.blue('-')} Processing the migration ./migrations/change_${component}_${field}.js\n`)
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
api.setSpaceId(space)
await tasks.runMigration(
api,
component,
field,
{ isDryrun, publish, publishLanguages }
)
} catch (e) {
console.log(chalk.red('X') + ' An error ocurred when run the migration file: ' + e.message)
process.exit(1)
}
})
program
.command(COMMANDS.ROLLBACK_MIGRATION)
.description('Rollback-migration a migration file')
.requiredOption('-c, --component <COMPONENT_NAME>', 'Name of the component')
.requiredOption('-f, --field <FIELD_NAME>', 'Name of the component field')
.action(async (options) => {
const field = options.field || ''
const component = options.component || ''
const space = program.space
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space YOUR_SPACE_ID.')
process.exit(1)
}
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
api.setSpaceId(space)
await tasks.rollbackMigration(api, field, component)
} catch (e) {
console.log(chalk.red('X') + ' An error ocurred when run rollback-migration: ' + e.message)
process.exit(1)
}
})
// list spaces
program
.command(COMMANDS.SPACES)
.description('List all spaces of the logged account')
.action(async () => {
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
const { region } = creds.get()
await tasks.listSpaces(api, region)
} catch (e) {
console.log(chalk.red('X') + ' An error ocurred to listing spaces: ' + e.message)
process.exit(1)
}
})
// import data
program
.command(COMMANDS.IMPORT)
.description('Import data from other systems and relational databases.')
.requiredOption('-f, --file <FILE_NAME>', 'Name of the file')
.requiredOption('-t, --type <TYPE>', 'Type of the content')
.option('-fr, --folder <FOLDER_ID>', '(Optional) This is a Id of folder in storyblok')
.option('-d, --delimiter <DELIMITER>', 'If you are using a csv file, put the file delimiter, the default is ";"')
.action(async (options) => {
const space = program.space
try {
if (!api.isAuthorized()) {
await api.processLogin()
}
if (!space) {
console.log(chalk.red('X') + ' Please provide the space as argument --space <SPACE_ID>.')
return
}
api.setSpaceId(space)
await tasks.importFiles(api, options)
console.log(
`${chalk.green('✓')} The import process was executed with success!`
)
} catch (e) {
console.log(chalk.red('X') + ' An error ocurred to import data : ' + e.message)
process.exit(1)
}
})
program.parse(process.argv)
if (program.rawArgs.length <= 2) {
program.help()
}
function errorHandler (e, command) {
if (/404/.test(e.message)) {
console.log(chalk.yellow('/!\\') + ' If your space was created under US or CN region, you must provide the region us or cn upon login.')
} else {
console.log(chalk.red('X') + ' An error occurred when executing the ' + command + ' task: ' + e || e.message)
}
process.exit(1)
}