1
|
|
|
import path from 'path'; |
2
|
|
|
import { promisify, resolve } from 'bluebird'; |
3
|
|
|
import { curry, isNil } from 'ramda'; |
4
|
|
|
import superagent from 'superagent'; |
5
|
|
|
import { isURL } from 'validator'; |
6
|
|
|
import rimraf from 'rimraf'; |
7
|
|
|
import inquirer from 'inquirer'; |
8
|
|
|
import { emitError, emitWarning } from './input'; |
9
|
|
|
import build from './build'; |
10
|
|
|
import { validator } from './types'; |
11
|
|
|
|
12
|
|
|
const request = superagent.agent(); |
13
|
|
|
const rm = promisify(rimraf); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Returns the Rung API URL. Emits a warning when the URL is possibly invalid |
17
|
|
|
* |
18
|
|
|
* @return {String} |
19
|
|
|
*/ |
20
|
|
|
function fetchRungApi() { |
21
|
|
|
const envApi = process.env.RUNG_API; |
22
|
|
|
|
23
|
|
|
if (isNil(envApi)) { |
24
|
|
|
return 'https://app.rung.com.br/api'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (!isURL(envApi)) { |
28
|
|
|
emitWarning(`invalid API for Rung: ${JSON.stringify(envApi)}`); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return envApi; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Publishes a Rung package from a file |
36
|
|
|
* |
37
|
|
|
* @param {String} api |
38
|
|
|
* @param {String} filename |
39
|
|
|
* @return {Promise} |
40
|
|
|
*/ |
41
|
|
|
const publishFile = curry((api, filename) => |
42
|
|
|
request.post(`${api}/metaExtensions/drafts`) |
43
|
|
|
.attach('metaExtension', filename) |
44
|
|
|
.then(~rm(filename))); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Builds or uses the passed file to publication |
48
|
|
|
* |
49
|
|
|
* @param {Object} args |
50
|
|
|
* @return {Promise} |
51
|
|
|
*/ |
52
|
|
|
function resolveInputFile(args) { |
53
|
|
|
const { file } = args; |
54
|
|
|
return file ? resolve(path.resolve(file)) : build(args); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Authenticates and publishes the app |
59
|
|
|
* |
60
|
|
|
* @param {Object} args |
61
|
|
|
* @return {Promise} |
62
|
|
|
*/ |
63
|
|
|
export default function publish(args) { |
64
|
|
|
const api = fetchRungApi(); |
65
|
|
|
|
66
|
|
|
return inquirer.prompt([ |
67
|
|
|
{ name: 'email', message: 'Rung email', validate: validator.Email }, |
68
|
|
|
{ name: 'password', type: 'password', message: 'Rung password' }]) |
69
|
|
|
.then(payload => payload | request.post(`${api}/login`).send) |
70
|
|
|
.then(~resolveInputFile(args)) |
71
|
|
|
.then(publishFile(api)) |
72
|
|
|
.catch(err => { |
73
|
|
|
emitError(err.message); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
|