-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgitbook-start-heroku-josue-nayra.js
264 lines (224 loc) · 7.27 KB
/
gitbook-start-heroku-josue-nayra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"use strict"
const basePath = process.cwd();
const fs = require('fs-extra');
const path = require('path');
var exec = require('child_process').exec;
const inquirer = require('inquirer');
const jsonfile = require('jsonfile');
var heroku = require(path.join(__dirname,'lib','config_heroku.js'));
var packagejson = require(path.join(basePath,'package.json'));
//-------------------------------------------------------------------------------------------------
var respuesta = ((error, stdout, stderr) =>
{
if (error)
console.error("Error:"+error);
console.log("Stderr:"+stderr);
console.log("Stdout:"+stdout);
});
//-------------------------------------------------------------------------------------------------
var destroy = (()=>
{
console.log("Eliminando despliegue en Heroku");
heroku.destroy_app((err)=>
{
if(err)
{
console.log(err);
throw err;
}
exec('git remote rm heroku',respuesta);
});
});
var deploy = (() => {
console.log("Deploy to Heroku");
exec('git add .; git commit -m "Deploy to Heroku"; git push heroku master', respuesta);
});
//-------------------------------------------------------------------------------------------------
var escribir_gulpfile = (() => {
return new Promise((resolve,reject) => {
console.log("Tareas gulp:");
var tareas_gulp = require(path.join(__dirname,'lib','tareas_gulpfile.js'));
var tareas_introducidas = [];
fs.readFile('gulpfile.js', "utf8", function(err, data) {
if (err) throw err;
// console.log(data);
var i=0;
while(tareas_introducidas.length <= tareas_gulp.tareas.length && i < tareas_gulp.tareas.length)
{
var tarea_actual = tareas_gulp.tareas[i];
if(data.search(tarea_actual.nombre) != -1)
{
console.log("Ya existe una tarea de "+tarea_actual.nombre);
i++;
}
else
{
console.log("Escribiendo tarea "+tarea_actual.nombre + " en el gulpfile");
tareas_introducidas.push(tarea_actual);
fs.appendFile(path.join(basePath,'gulpfile.js'), `${tarea_actual.tarea}`, (err) => {
if (err){
console.log("ERROR:"+err);
reject(err);
}
});
i++;
}
}
resolve(tareas_introducidas);
});
});
});
//-------------------------------------------------------------------------------------------------
var select_bd = (() =>
{
return new Promise((resolve,reject)=>
{
var schema = [
{
name: "bd",
message: "Tipo de base de datos:",
type: 'list',
choices: ['dropbox', 'sequelize']
}
];
inquirer.prompt(schema).then((respuestas)=>
{
resolve(respuestas);
});
});
});
var obtener_variables = (() =>
{
return new Promise((result,reject) =>
{
select_bd().then((resolve,reject)=>
{
var schema = [
{
name: "authentication",
message: "¿Quiere autenticacion?:",
type: 'checkbox',
choices: ['Google','Twitter','Facebook','Github', 'Sin autenticacion']
}
];
inquirer.prompt(schema).then((respuestas) =>
{
fs.copy(path.join(__dirname, 'template', 'auth.json'), path.join(basePath, 'auth.json'), (err)=>
{
if(err)
throw err;
fs.readFile(path.join(basePath,'auth.json'),(err,data)=>
{
if(err)
{
console.log("ERROR:"+err);
reject(err);
}
var config = JSON.parse(data);
set_autenticacion(config,respuestas).then((resolve1,reject1)=>
{
console.log("Modificando fichero auth.json");
jsonfile.spaces = 5;
jsonfile.writeFileSync(path.join(basePath,'auth.json'),resolve1,{spaces: 5});
if(fs.existsSync(path.join(basePath,'auth.json')))
{
result({ bd: resolve.bd, auth: fs.existsSync(path.join(basePath,'auth.json'))});
}
});
});
});
});
});
});
});
//-------------------------------------------------------------------------------------------------
// Funcion para cambiar el nombre del index.html y evitar ambiguedades.
var preparar_despliegue = (() => {
return new Promise((resolve, reject) => {
if(fs.existsSync(path.join(basePath,'gh-pages','index.html')))
{
fs.rename(path.join(basePath,'gh-pages','index.html'), path.join(basePath,'gh-pages','introduccion.html'), (err) => {
if (err) {
console.log(err);
reject(err);
}
resolve(fs.existsSync(path.join(basePath,'gh-pages','introduccion.html')));
});
}
else
{
if(fs.existsSync(path.join(basePath,'gh-pages','introduccion.html')))
{
resolve(fs.existsSync(path.join(basePath,'gh-pages','introduccion.html')));
}
else
{
console.log("No existe gh-pages... Debe ejecutar gulp build para construir el libro");
}
}
});
});
var set_autenticacion = ((datos, resp)=>
{
return new Promise((resolve,reject)=>
{
for(var i=0;i<(resp.authentication).length;i++)
{
switch(resp.authentication[i])
{
case 'Google':
datos.Google.authentication = "Si";
break;
case 'Twitter':
datos.Twitter.authentication = "Si";
break;
case 'Facebook':
datos.Facebook.authentication = "Si";
break;
case 'Github':
datos.Github.authentication = "Si";
break;
case "Sin autenticacion":
datos.Autenticacion = false;
//Escribir en el package.json
fs.readFile(path.join(basePath,'package.json'),(err,data) =>
{
if(err)
throw err;
var datos = JSON.parse(data);
datos.Heroku.authentication = "No";
jsonfile.spaces = 5;
jsonfile.writeFileSync(path.join(basePath,'package.json'),datos,{spaces: 5});
});
break;
default:
console.log("Opción no disponible");
}
}
resolve(datos);
});
});
//-------------------------------------------------------------------------------------------------
var initialize = (() => {
console.log("Método initialize del plugin deploy-heroku");
obtener_variables().then((resolve1,reject1) =>
{
console.log("--------------------------------------");
preparar_despliegue().then((resolve2, reject2) =>
{
console.log("--------------------------------------");
escribir_gulpfile().then((resolve3,reject3)=>
{
console.log("--------------------------------------");
heroku.crear_app(resolve1.bd).then(()=>
{
console.log("Su aplicación se ha creado correctamente");
});
});
});
});
});
//-------------------------------------------------------------------------------------------------
exports.initialize = initialize;
exports.deploy = deploy;
exports.destroy = destroy;