UNPKG

2.54 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";
3var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11Object.defineProperty(exports, "__esModule", { value: true });
12const csvParser = require("csv-parser");
13const execPromise = require("exec-promise");
14const through2 = require("through2");
15const xo_lib_1 = require("xo-lib");
16const parseBoolean = (value, defaultValue) => {
17 if (value === undefined || value === '') {
18 return defaultValue;
19 }
20 const lcValue = value.toLocaleLowerCase();
21 if (value === '0' || lcValue === 'false') {
22 return false;
23 }
24 if (value === '1' || lcValue === 'true') {
25 return true;
26 }
27 throw new Error(`invalid boolean value: ${value}`);
28};
29const requiredParam = (name) => {
30 throw `missing param: ${name}
31
32Usage: xo-import-servers-csv $url $username $password < $csvFile`;
33};
34execPromise(([url = requiredParam('url'), username = requiredParam('username'), password = requiredParam('password')]) => __awaiter(this, void 0, void 0, function* () {
35 const xo = new xo_lib_1.default({ url });
36 yield xo.open();
37 yield xo.signIn({ username, password });
38 console.log('connected as', xo.user.email);
39 const errors = [];
40 const stream = process.stdin
41 .pipe(csvParser())
42 .pipe(through2.obj(({ allowUnauthorized, autoConnect, host, label, password, username, }, _, next) => {
43 console.log('server', host);
44 xo.call('server.add', {
45 allowUnauthorized: parseBoolean(allowUnauthorized),
46 autoConnect: parseBoolean(autoConnect, false),
47 host,
48 label,
49 password,
50 username
51 }).then(() => next(), (error) => {
52 errors.push({ host, error });
53 return next();
54 });
55 }));
56 yield new Promise((resolve, reject) => {
57 stream.on('error', reject);
58 stream.on('finish', resolve);
59 });
60 if (errors.length) {
61 console.error(errors);
62 }
63}));
64//# sourceMappingURL=index.js.map
\No newline at end of file