UNPKG

5.87 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 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
102module.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 //return console.log(e);
126 }
127 else {
128 canOpenBrowser = true;
129 }
130 //browser.on('stop', function(code){
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}