UNPKG

2.38 kBJavaScriptView Raw
1'use strict';
2const _ = require('lodash'),
3 pluralize = require('pluralize'),
4 chalk = require('chalk'),
5 options = require('./cli-options'),
6 reporter = require('../lib/reporters'),
7 importItems = require('../lib/cmd/import');
8
9function builder(yargs) {
10 return yargs
11 .usage('Usage: $0 import [url]')
12 .example('$0 import --key prod domain.com < db_dump.clay', 'Import dispatch from stdin')
13 .example('$0 import --key prod --publish domain.com < db_dump.clay', 'Import and publish page')
14 .example('$0 import --key prod --yaml domain.com < bootstrap.yml', 'Import bootstrap from stdin')
15 .option('k', options.key)
16 .option('c', options.concurrency)
17 .option('p', options.publish)
18 .option('y', options.yaml)
19 .option('r', options.reporter);
20}
21
22/**
23 * show progress as we import things
24 * @param {object} argv
25 * @return {function}
26 */
27function handler(argv) {
28 const log = reporter.log(argv.reporter, 'import');
29
30 log('Importing items...');
31 return importItems(process.stdin, argv.url, {
32 key: argv.key,
33 concurrency: argv.concurrency,
34 publish: argv.publish,
35 yaml: argv.yaml
36 })
37 .map(reporter.logAction(argv.reporter, 'import'))
38 .map((item) => {
39 // catch people trying to import dispatches from yaml files
40 if (item.type === 'error' && item.message === 'Cannot import dispatch from yaml') {
41 reporter.logSummary(argv.reporter, 'import', () => ({ success: false, message: 'Unable to import' }))([item]);
42 process.exit(1);
43 } else {
44 return item;
45 }
46 })
47 .toArray((results) => {
48 const pages = _.map(_.filter(results, (result) => result.type === 'success' && _.includes(result.message, 'pages')), (page) => `${page.message}.html`);
49
50 reporter.logSummary(argv.reporter, 'import', (successes) => {
51 if (successes && pages.length) {
52 return { success: true, message: `Imported ${pluralize('page', pages.length, true)}\n${chalk.gray(pages.join('\n'))}` };
53 } else if (successes) {
54 return { success: true, message: `Imported ${pluralize('uri', successes, true)}` };
55 } else {
56 return { success: false, message: 'Imported 0 uris (´°ω°`)' };
57 }
58 })(results);
59 });
60}
61
62module.exports = {
63 command: 'import [url]',
64 describe: 'Import data into clay',
65 aliases: ['importer', 'i'],
66 builder,
67 handler
68};