UNPKG

3.66 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const program = require('commander'),
4 fs = require('fs'),
5 rl = require('readline'),
6 logger = require('./lib/logger'),
7 validate = require('./lib/validators'),
8 version = require('./package.json').version,
9 Portal = require('./lib/portal');
10
11const checkParams = params => {
12 validate.existence({ argumentValue: params.email, argumentName: 'email', fail: program.help.bind(program) });
13 validate.existence({ argumentValue: params.url, argumentName: 'URL', fail: program.help.bind(program) });
14 validate.email(params.email);
15
16 if (params.url.slice(-1) != '/') {
17 params.url = params.url + '/';
18 }
19
20 validate.url(params.url);
21};
22
23// turn to promise
24const getPassword = () => {
25 return new Promise((resolve, reject) => {
26 const reader = rl.createInterface({ input: process.stdin, output: process.stdout });
27 reader.stdoutMuted = true;
28 reader.question('Password: ', password => {
29 reader.close();
30 logger.Info('');
31 resolve(password);
32 });
33
34 reader._writeToOutput = stringToWrite => {
35 (reader.stdoutMuted && reader.output.write('*')) || reader.output.write(stringToWrite);
36 };
37 });
38};
39
40const storeEnvironment = settings => {
41 const environmentSettings = {
42 [settings.endpoint]: {
43 url: settings.url,
44 token: settings.token,
45 email: settings.email
46 }
47 };
48 saveFile(Object.assign({}, existingSettings(process.env.CONFIG_FILE_PATH), environmentSettings));
49};
50
51const saveFile = settings => {
52 fs.writeFileSync(process.env.CONFIG_FILE_PATH, JSON.stringify(settings, null, 2), err => {
53 if (err) throw err;
54 });
55};
56
57const existingSettings = configFilePath => {
58 let settings = {};
59
60 try {
61 settings = JSON.parse(fs.readFileSync(configFilePath));
62 } catch (e) {}
63
64 return settings;
65};
66
67PARTNER_PORTAL_HOST = process.env.PARTNER_PORTAL_HOST || 'https://portal.apps.near-me.com';
68
69program
70 .version(version)
71 .arguments('[environment]', 'name of environment. Example: staging')
72 .option('--email <email>', 'Partner Portal account email. Example: admin@example.com')
73 .option('--url <url>', 'marketplace url. Example: https://example.com')
74 .option('--token <token>', 'if you have a token you can add it directly to mpkit configution without connecting to portal')
75 .option('-c --config-file <config-file>', 'config file path', '.marketplace-kit')
76 .action((environment, params) => {
77 process.env.CONFIG_FILE_PATH = params.configFile;
78 checkParams(params);
79 const settings = { url: params.url, endpoint: environment, email: params.email };
80 if (params.token) {
81 storeEnvironment(Object.assign(settings, { token: params.token }));
82 logger.Success(`Environment ${params.url} as ${environment} has been added successfuly.`);
83 process.exit(0);
84 }
85
86 logger.Info(
87 `Please make sure that you have a permission to deploy. \n
88 You can verify it here: ${PARTNER_PORTAL_HOST}/me/permissions`,
89 { hideTimestamp: true }
90 );
91
92 getPassword().then(password => {
93 logger.Info(`Asking ${PARTNER_PORTAL_HOST} for access token...`);
94
95 Portal.login(params.email, password)
96 .then(response => {
97 const token = response[0].token;
98
99 if (token) {
100 storeEnvironment(Object.assign(settings, { token }));
101 logger.Success(`Environment ${params.url} as ${environment} has been added successfuly.`);
102 }
103 })
104 .catch(() => logger.Error('Response from server invalid, token is missing.'));
105 });
106 });
107
108program.parse(process.argv);
109
110validate.existence({ argumentValue: program.args[0], argumentName: 'environment', fail: program.help.bind(program) });