-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.js
170 lines (137 loc) · 4.26 KB
/
gulpfile.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
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const gulp = require('gulp');
const xmlTransformer = require('gulp-xml-transformer');
const git = require('gulp-git');
const inquirer = require('inquirer');
const rename = require('gulp-rename');
const del = require('delete');
const vinylPaths = require('vinyl-paths');
const keystore = require('./config/keystore.json');
const packageJson = require('./package.json');
const configFileName = './config.xml';
const xmlNamespace = { n: 'http://www.w3.org/ns/widgets' };
function clean(cb) {
return del(['./built/*.apk'], cb);
}
function updateVersionNumber() {
return gulp.src(configFileName).pipe(xmlTransformer([{
path: '//n:widget', attr: { version: packageJson.version },
}], xmlNamespace)).pipe(gulp.dest('./'));
}
function revertConfig() {
return gulp.src(configFileName).pipe(git.checkoutFiles());
}
async function execCommand(cmdline) {
const { stderr } = await exec(cmdline);
if (stderr) {
if (stderr.indexOf('Mapping new') > -1) {
console.log('Got mapping new ns message, ignoring');
} else {
throw new Error(stderr);
}
}
}
let passwords;
async function askForPasswords() {
passwords = await inquirer.prompt([{
type: 'text',
name: 'store',
message: 'Please enter store password',
}, {
type: 'text',
name: 'keystore',
message: 'Please enter alias password',
},]);
if (!passwords.keystore || !passwords.store) {
throw new Error('Invalid passwords');
}
}
async function buildAndSignAndroidPackage() {
if (!keystore || !keystore.alias || !keystore.location) {
throw new Error('Keystore configuration is not correct');
}
if (!passwords.keystore || !passwords.store) {
throw new Error('Invalid passwords');
}
const cmdline = `cordova build android --release -- --keystore="${keystore.location}" --alias=${keystore.alias} --storePassword="${passwords.store}" --password="${passwords.keystore}" --packageType=apk`;
// console.log(cmdline);
await execCommand(cmdline);
}
function buildDebugAndroidPackage() {
return buildPackage('android');
}
function buildIosPackage() {
return buildPackage('ios');
}
async function buildPackage(type) {
return execCommand(`cordova build ${type}`);
}
function runAndroidPackage() {
return runPackage('android');
}
function runPackage(type) {
return execCommand(`cordova run ${type}`);
}
function copyDebugAndroidBuild() {
const debugPackageLocation = './platforms/android/app/build/outputs/apk/debug/app-debug.apk';
return gulp.src(debugPackageLocation).pipe(rename(`android-staging-v${packageJson.version}.apk`)).pipe(gulp.dest('./built'));
}
function copyProductionAndroidBuild() {
const productionPackageLocation = './platforms/android/app/build/outputs/apk/release/app-release.apk';
return gulp.src(productionPackageLocation).pipe(rename(`android-production-v${packageJson.version}.apk`)).pipe(gulp.dest('./built'));
}
function replaceEnvFile() {
const envLocation = './.env';
gulp.src(envLocation).pipe(vinylPaths(del)).pipe(rename('env.env')).pipe(gulp.dest('./'));
return gulp.src('./.env.sample').pipe(rename('.env')).pipe(gulp.dest('./'));
}
function revertEnvFile() {
const envLocation = './env.env';
return gulp.src(envLocation).pipe(vinylPaths(del)).pipe(rename('.env')).pipe(gulp.dest('./'));
}
function buildCodeForProduction() {
return execCommand('npm run build:production');
}
function buildCodeForStaging() {
return execCommand('npm run build');
}
const buildAndRunAndroid = gulp.series(
runAndroidPackage,
);
const buildStagingAndroid = gulp.series(
replaceEnvFile,
revertConfig,
buildCodeForStaging,
buildDebugAndroidPackage,
copyDebugAndroidBuild,
revertConfig,
revertEnvFile,
);
const buildProductionAndroid = gulp.series(
revertConfig,
buildCodeForProduction,
buildAndSignAndroidPackage,
copyProductionAndroidBuild,
revertConfig
);
exports.buildAndroid = gulp.series(
askForPasswords,
clean,
buildStagingAndroid,
buildProductionAndroid,
);
exports.buildStagingAndroid = buildStagingAndroid;
exports.buildIos = gulp.series(
buildCodeForProduction,
buildIosPackage,
revertConfig,
);
exports.buildStagingIos = gulp.series(
buildCodeForStaging,
buildIosPackage,
revertConfig,
);
exports.updateVersionNumber = updateVersionNumber;
exports.clean = clean;
exports.buildAndRunAndroid = buildAndRunAndroid;