UNPKG

2.15 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const program = require('commander');
4const keys = require('lodash/keys');
5
6const { runner, cliLogger, cliUtils, buildConfiguration } = require('../dist/dsm-storybook-node');
7
8program
9 .version('0.1.0')
10 .option('--dsm-host [host]', 'The DSM server')
11 .option('--auth-token [token]', 'Key provided to you by DSM to authenticate uploads')
12 .option('--story-path [glob]', 'Path to folder containing storybook stories')
13 .option('-o, --output-dir [path]', 'Path to folder to store the DSM build artifacts');
14
15program
16 .command('publish')
17 .allowUnknownOption()
18 .action(async function() {
19 const { customArgs, configuration } = getConfigurations();
20 runner.clearPreviousBuild(configuration);
21 try {
22 await runner.publish(configuration, customArgs);
23 } catch (e) {
24 cliLogger.error(e);
25 }
26 });
27
28program
29 .command('preview')
30 .allowUnknownOption()
31 .action(async function() {
32 const { customArgs, configuration } = getConfigurations();
33 try {
34 await runner.preview(configuration, customArgs);
35 } catch (e) {
36 cliLogger.error(e);
37 }
38 });
39
40program.command('list [limit]').action(async function(limit = 10) {
41 const { configuration } = getConfigurations();
42
43 try {
44 await runner.listStorybooks(configuration, parseInt(limit));
45 } catch (e) {
46 cliLogger.error(e);
47 }
48});
49
50program.command('delete <buildId>').action(async function(buildId) {
51 const { configuration } = getConfigurations();
52
53 try {
54 await runner.deleteStorybook(configuration, buildId);
55 } catch (e) {
56 cliLogger.error(e);
57 }
58});
59
60program.parse(process.argv);
61
62function getConfigurations() {
63 const options = optionsFromArgs();
64 const customArgs = cliUtils.getCustomArgs(program).join(' ');
65 const configuration = buildConfiguration.create(options);
66
67 return {
68 customArgs,
69 configuration
70 };
71}
72
73/**
74 * normalize cli args
75 * */
76function optionsFromArgs() {
77 const configurationKeys = buildConfiguration.configurationKeys;
78 return keys(configurationKeys).reduce((acc, key) => {
79 if (program[key]) {
80 acc[configurationKeys[key]] = program[key];
81 }
82 return acc;
83 }, {});
84}