UNPKG

1.01 kBJavaScriptView Raw
1const program = require("commander");
2const jsonfile = require("jsonfile");
3
4const helpers = require("../dist");
5const apis = require("../dist");
6
7program.parse(process.argv);
8
9const stdout = console.log.bind(console);
10const stderr = console.error.bind(console);
11
12/**
13 * import collection file
14 * @param {string} file collection file path
15 */
16async function importing(file) {
17 await helpers.checkApiKey();
18
19 if (typeof file === "undefined") {
20 stderr("collection file not given!");
21 process.exit(1);
22 }
23
24 const collection = jsonfile.readFileSync(file);
25 const collections = await apis.listCollections();
26 const { info = {} } = collection;
27 const found = collections.find(c => c.name === info.name);
28
29 if (found) {
30 await apis.updateCollection(found.id, collection);
31 stdout("updated collection", info.name);
32 } else {
33 await apis.createCollection(collection);
34 stdout("created collection", info.name);
35 }
36}
37
38importing(program.file).catch(err => {
39 stderr(err);
40 process.exit(1);
41});