UNPKG

5.73 kBJavaScriptView Raw
1// There the heroku app should be created.
2// There the oauth app should be created.
3// There the remote url for the app have to be added to git.
4
5var exec = require('child_process').exec;
6var inquirer = require('inquirer')
7var Heroku = require('heroku-client');
8var heroku;
9var path = require('path')
10var fs = require('fs-extra');
11var configFile = require(path.join(process.cwd(),'.config.book.json'));
12var book_name = configFile.name;
13var author = configFile.authors[0];
14var heroku_app_name = book_name + "-" + author + '-gs';
15var heroku_url = "https://" + heroku_app_name + ".herokuapp.com";
16var launcher = require( 'launch-browser' );
17var Tacks = require('tacks');
18var copy = require('copy-to-clipboard')
19const { URL } = require('url');
20var File = Tacks.File;
21var Dir = Tacks.Dir;
22
23function 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
35function 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
52function 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 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 callback(null,"Created heroku app " + heroku_app_name);
90 });
91 }).catch(function(e) {
92 console.log("El error")
93 console.log(e.message);
94 });
95 }
96 });
97 }
98 });
99}
100
101module.exports.install = (callback) => {
102 if (configFile['private'] == "yes") {
103 var base_url = 'https://github.com/settings/applications/new?';
104 var name_param_url = 'oauth_application[name]=' + heroku_app_name;
105 var url_param_url = '&oauth_application[url]=' + heroku_url;
106 var desc_param_url = '&oauth_application[description]=' + configFile['description'];
107 var callback_param_url = '&oauth_application[callback_url]=' + heroku_url + '/github/auth/return';
108 var oauth_register_url = base_url + name_param_url + url_param_url + desc_param_url + callback_param_url;
109
110 var canOpenBrowser = false;
111
112 const myURL = new URL(oauth_register_url);
113
114 inquirer.prompt([
115 {
116 type:'input',
117 name: 'nothing',
118 message: 'Now browser will be opened to create oauth app. YOU HAVE TO COPY the app clientID and clientSecret (Press Enter)',
119 filter: function (val) {
120 launcher(myURL.href, { browser: ['chromium','chrome', 'firefox', 'safari'] }, function (e, browser) {
121 if(e) {
122 copy(myURL.href);
123 console.log(e + " Now just CTRL+V on your browser and press Enter. ");
124 //return console.log(e);
125 }
126 else {
127 canOpenBrowser = true;
128 }
129 //browser.on('stop', function(code){
130 //});
131 });
132 return val;
133 }
134 },
135 {
136 type: 'input',
137 name: 'justClick',
138 message: 'Press Enter to push client ID:',
139 validate: function (val) {
140 canOpenBrowser = true;
141 return true;
142 },
143 when: function() {
144 return !canOpenBrowser;
145 }
146 },
147 {
148 type: 'input',
149 name: 'clientID',
150 message: 'Put the client ID of the oauth App: ',
151 when: function() {
152 return canOpenBrowser;
153 },
154 validate: function (value) {
155 if (!value) return false;
156 return true;
157 }
158 },
159 {
160 type: 'input',
161 name: 'clientSecret',
162 message: 'Put the client Secret of the oauth App: ',
163 validate: function (value) {
164 if (!value) return false;
165 return true;
166 }
167 }]).then((answers) => {
168 var oauth_file = new Tacks(Dir({
169 '.oauth.github.json' : File(JSON.stringify({
170 clientID: answers.clientID,
171 clientSecret: answers.clientSecret,
172 callbackURL: heroku_url + '/github/auth/return'
173 }, null, "\t"))
174 }));
175 oauth_file.create(process.cwd());
176 setup((err, msg) => {
177 callback(err, msg);
178 });
179 });
180 }
181 else {
182 setup();
183 }
184}