1 |
|
2 |
|
3 |
|
4 |
|
5 | var exec = require('child_process').exec;
|
6 | var inquirer = require('inquirer')
|
7 | var Heroku = require('heroku-client');
|
8 | var heroku;
|
9 | var path = require('path')
|
10 | var fs = require('fs-extra');
|
11 | var configFile = require(path.join(process.cwd(),'.config.book.json'));
|
12 | var book_name = configFile.name;
|
13 | var author = configFile.authors[0];
|
14 | var heroku_app_name = book_name + "-" + author + '-gs';
|
15 | var heroku_url = "https://" + heroku_app_name + ".herokuapp.com";
|
16 | var launcher = require( 'launch-browser' );
|
17 | var Tacks = require('tacks');
|
18 | var copy = require('copy-to-clipboard')
|
19 | const { URL } = require('url');
|
20 | var File = Tacks.File;
|
21 | var Dir = Tacks.Dir;
|
22 |
|
23 | function existsHerokuApp (name,callback) {
|
24 | var existe = false;
|
25 | heroku.get('/apps').then(apps => {
|
26 | apps.forEach (function (app,i) {
|
27 | if (app.name == name) {
|
28 | existe = app;
|
29 | }
|
30 | if (i+1 == apps.length) callback(existe);
|
31 | });
|
32 | });
|
33 | }
|
34 |
|
35 | function setHerokuData (app, callback) {
|
36 | configFile['heroku_url'] = app.web_url;
|
37 | fs.unlink('.config.book.json', function(err) {
|
38 | fs.writeFileSync('.config.book.json', JSON.stringify(configFile, null, '\t'));
|
39 | exec('git remote', (err, out) => {
|
40 | if (out.includes('heroku')) callback();
|
41 | else {
|
42 | exec('git remote add heroku ' + app.git_url, function(err, out) {
|
43 | if (err) callback(err);
|
44 | else callback();
|
45 | });
|
46 | }
|
47 | })
|
48 | });
|
49 | }
|
50 |
|
51 |
|
52 | function setup (callback) {
|
53 | exec ('gulp -T', (err, out) => {
|
54 | if (err)
|
55 | exec ('npm install gulp', (err, out) => {
|
56 | var msg = "gulp installed locally";
|
57 | console.log(msg);
|
58 | if (callback) callback(msg);
|
59 | })
|
60 | })
|
61 | exec ('which heroku', function (err, out) {
|
62 | if (out.length == 0) {
|
63 | console.log("\x1b[31m","YOU HAVE TO INSTALL HEROKU. EXECUTE '$npm install -g heroku'");
|
64 | process.exit();
|
65 | }
|
66 | });
|
67 | if (!fs.existsSync('_book')) {
|
68 | exec('gitbook build', function (err, out) {
|
69 | if (err) console.log(err);
|
70 | });
|
71 | }
|
72 | exec('heroku auth:token', function(err, out) {
|
73 | if (!err) {
|
74 | heroku = new Heroku({
|
75 | token : out.replace(' ','')
|
76 | });
|
77 | existsHerokuApp(heroku_app_name, function (app) {
|
78 | if (app) {
|
79 | setHerokuData(app, (err) => {
|
80 | if (err) callback(err, null);
|
81 | else if (callback) callback(null,"App already exists. Seting heroku app data. " + heroku_app_name);
|
82 | else console.log("App already exists. Seting heroku app data. " + heroku_app_name);
|
83 | });
|
84 | }
|
85 | else {
|
86 | heroku.post('/apps', {body: {name: heroku_app_name}}).then(app => {
|
87 | setHerokuData(app, (err) => {
|
88 | if (err) callback(err, null);
|
89 | else if (callback) callback(null,"Created heroku app " + heroku_app_name);
|
90 | else console.log("Created heroku app " + heroku_app_name);
|
91 | });
|
92 | }).catch(function(e) {
|
93 | console.log("El error")
|
94 | console.log(e.message);
|
95 | });
|
96 | }
|
97 | });
|
98 | }
|
99 | });
|
100 | }
|
101 |
|
102 | module.exports.install = (callback) => {
|
103 | if (configFile['private'] == "yes") {
|
104 | var base_url = 'https://github.com/settings/applications/new?';
|
105 | var name_param_url = 'oauth_application[name]=' + heroku_app_name;
|
106 | var url_param_url = '&oauth_application[url]=' + heroku_url;
|
107 | var desc_param_url = '&oauth_application[description]=' + configFile['description'];
|
108 | var callback_param_url = '&oauth_application[callback_url]=' + heroku_url + '/github/auth/return';
|
109 | var oauth_register_url = base_url + name_param_url + url_param_url + desc_param_url + callback_param_url;
|
110 |
|
111 | var canOpenBrowser = false;
|
112 |
|
113 | const myURL = new URL(oauth_register_url);
|
114 |
|
115 | inquirer.prompt([
|
116 | {
|
117 | type:'input',
|
118 | name: 'nothing',
|
119 | message: 'Now browser will be opened to create oauth app. YOU HAVE TO COPY the app clientID and clientSecret (Press Enter)',
|
120 | filter: function (val) {
|
121 | launcher(myURL.href, { browser: ['chromium','chrome', 'firefox', 'safari'] }, function (e, browser) {
|
122 | if(e) {
|
123 | copy(myURL.href);
|
124 | console.log(e + " Now just CTRL+V on your browser and press Enter. ");
|
125 |
|
126 | }
|
127 | else {
|
128 | canOpenBrowser = true;
|
129 | }
|
130 |
|
131 |
|
132 | });
|
133 | return val;
|
134 | }
|
135 | },
|
136 | {
|
137 | type: 'input',
|
138 | name: 'justClick',
|
139 | message: 'Press Enter to push client ID:',
|
140 | validate: function (val) {
|
141 | canOpenBrowser = true;
|
142 | return true;
|
143 | },
|
144 | when: function() {
|
145 | return !canOpenBrowser;
|
146 | }
|
147 | },
|
148 | {
|
149 | type: 'input',
|
150 | name: 'clientID',
|
151 | message: 'Put the client ID of the oauth App: ',
|
152 | when: function() {
|
153 | return canOpenBrowser;
|
154 | },
|
155 | validate: function (value) {
|
156 | if (!value) return false;
|
157 | return true;
|
158 | }
|
159 | },
|
160 | {
|
161 | type: 'input',
|
162 | name: 'clientSecret',
|
163 | message: 'Put the client Secret of the oauth App: ',
|
164 | validate: function (value) {
|
165 | if (!value) return false;
|
166 | return true;
|
167 | }
|
168 | }]).then((answers) => {
|
169 | var oauth_file = new Tacks(Dir({
|
170 | '.oauth.github.json' : File(JSON.stringify({
|
171 | clientID: answers.clientID,
|
172 | clientSecret: answers.clientSecret,
|
173 | callbackURL: heroku_url + '/github/auth/return'
|
174 | }, null, "\t"))
|
175 | }));
|
176 | oauth_file.create(process.cwd());
|
177 | setup((err, msg) => {
|
178 | if (callback) callback(err, msg);
|
179 | else console.log(err, message);
|
180 | });
|
181 | });
|
182 | }
|
183 | else {
|
184 | setup();
|
185 | }
|
186 | }
|