forked from STS-Rosario/carpoolear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
movilizame.js
146 lines (139 loc) · 4.96 KB
/
movilizame.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
#!/usr/bin/env node
const argv = require('minimist')(process.argv.slice(2));
const shell = require('shelljs');
const fs = require('fs-extra');
const xmlParser = require('xml2js').parseString;
console.log('Movilizame builder -- Starting building');
const TARGET = process.env.TARGET_APP || argv.target || 'default';
const PROD = argv.prod || false;
const PLATFORM = argv.platform || 'android';
if (PLATFORM === 'ios' || PLATFORM === 'android' || PLATFORM === 'browser') {
process.env.CORDOVA = true;
}
const NODE_ENV = PROD ? 'production' : 'development';
const projectPath = `./dist/${TARGET}/${NODE_ENV}/`;
console.log('Enviroment: ' + NODE_ENV);
function showError (code, stderr, stdout) {
console.log('ERROR IN CORDOVA:');
console.log('CODE:', code);
console.log('STDERR', stderr)
console.log('STDOUT', stdout);
}
function preBuildAndCheckPlatform (callback) {
let folder = `dist/${TARGET}/${NODE_ENV}`;
let folderCordovaResFiles = `dist/${TARGET}/${NODE_ENV}/res`;
let cordovaFiles = `projects/${TARGET}/cordova`;
if (fs.existsSync(folderCordovaResFiles)) {
console.log('Deleting old files.')
fs.removeSync(folderCordovaResFiles);
}
fs.copy(cordovaFiles, folder, function (err) {
if (err) {
console.error(err);
} else {
console.log('Copyng cordova assets.')
buildAndCheckPlatform(callback);
}
});
}
function buildAndCheckPlatform (callback) {
let options = {
env: process.env
};
let buildEnv = PROD ? 'build.js' : 'build-dev.js';
console.log(`cross-env PLATFORM=${PLATFORM} node build/${buildEnv}`);
shell.exec(`cross-env PLATFORM=${PLATFORM} node build/${buildEnv}`, options)
;
if (!fs.existsSync(`./dist/${TARGET}/${NODE_ENV}/platforms/${PLATFORM}`)) {
console.log('Adding platform: ' + PLATFORM + ' - path: ' + projectPath);
shell.exec(`cross-env cordova platform add ${PLATFORM}`, {
cwd: projectPath,
silent: true
}, function (code, stderr, stdout) {
console.log('STDOUT: ', stdout);
if (stdout.search('already added')) {
console.log('Platform allready installed.');
} else {
showError(code, stderr, stdout);
}
callback();
});
} else {
console.log('Platform directory is present.');
callback();
}
}
function loadAppVersion () {
let path = `./projects/${TARGET}/cordova/config.xml`;
let xml = fs.readFileSync(path);
xmlParser(xml, function (err, result) {
if (err) {
console.error(error);
} else {
process.env.APP_VERSION = result.widget.$.version;
let i = 0;
for (i; i < result.widget.plugin.length; i++) {
if (result.widget.plugin[i].$.name === 'cordova-plugin-facebook4') {
let j = 0;
for (j; j < result.widget.plugin[i].variable.length; j++) {
if (result.widget.plugin[i].variable[j].$.name = 'APP_ID') {
process.env.FB_APP_ID = result.widget.plugin[i].variable[j].$.value;
break;
}
}
break;
}
}
}
});
}
if (argv._.length > 0) {
let action = argv._[0];
process.env.ACTION = action;
loadAppVersion();
switch (action) {
case 'serve':
process.env.SERVE = true;
process.env.CORDOVA = false;
process.env.NODE_ENV = NODE_ENV;
process.env.TARGET_APP = TARGET;
shell.exec('webpack-dev-server --inline --progress --config build/webpack.dev.conf.js',
{
env: process.env,
async: true
}
);
break;
case 'build':
preBuildAndCheckPlatform(() => {
shell.exec(`cordova build ${PLATFORM}`, {
env: process.env,
cwd: projectPath,
silent: true
}, showError);
});
break;
case 'prepare':
preBuildAndCheckPlatform(() => {
shell.exec(`cordova prepare ${PLATFORM}`, {
cwd: projectPath,
silent: true,
env: process.env
}, showError);
});
break;
case 'build-web':
let buildEnv = PROD ? 'build.js' : 'build-dev.js';
process.env.CORDOVA = false;
process.env.NODE_ENV = 'production';
process.env.TARGET_APP = TARGET;
let options = {
env: process.env
};
console.log(`cross-env PLATFORM=DESKTOP node build/${buildEnv}`);
shell.exec(`cross-env PLATFORM=DESKTOP node build/${buildEnv}`, options);
break;
default:
console.log(shell);
}
}