-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgulpfile.mjs
255 lines (218 loc) · 8.84 KB
/
gulpfile.mjs
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
//
// Copyright 2024 Wultra s.r.o.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Dependencies
import gulp from "gulp" // gulp itself
import ts from "gulp-typescript" // to be able to compiles typescript
import replace from "gulp-replace"
import concat from "gulp-concat"
import filter from "gulp-filter"
import { build } from "esbuild"
import stripImportExport from "gulp-strip-import-export"
import { rimraf } from "rimraf" // folder cleaner
import fs from "fs"
import { exec } from "child_process"
import pkg from "./package.json" with { type: "json" }
// Out files
const buildDir = "build";
const tmpDir = ".build";
const libVersion = pkg.version
console.log(`\x1b[32m\n#########################\n## POWERAUTH MOBILE SDK JS BUILD\n## Library version: ${libVersion}\n#########################\n\x1b[0m`)
/***********************
* REACT-NATIVE SECTION *
************************/
{
const RN_packageJson = "package.json";
const RN_tsConfig = "tsconfig.json";
const RN_buildDir = `${buildDir}/react-native`;
const RN_sources = "src/**/**.ts";
const RN_libDir = "lib";
const clearRN = () => rimraf([ RN_buildDir ]);
const compileRNTask = () =>
gulp
.src(RN_sources)
.pipe(ts(RN_tsConfig))
.pipe(gulp.dest(`${RN_buildDir}/${RN_libDir}`));
const copyRNFiles = () =>
gulp
.src(JSON.parse(fs.readFileSync(RN_packageJson, 'utf8')).files.filter((file) => !file.startsWith(`${RN_libDir}/`)), { base: "." })
.pipe(gulp.dest(RN_buildDir));
const copyRNPackageJson = () =>
gulp
.src(RN_packageJson)
.pipe(gulp.dest(RN_buildDir));
const packRNPackage = () => exec(`pushd ${RN_buildDir} && npm pack`);
var RN_buildTask = gulp.series(clearRN, compileRNTask, copyRNFiles, copyRNPackageJson, packRNPackage);
}
/***********************
* CAPACITOR.JS SECTION *
************************/
{
const CAP_packageJson = "cordova-support/package-capacitor.json";
const CAP_tsConfig = "cordova-support/tsconfig-capacitor.json";
const CAP_buildDir = `${buildDir}/capjs`;
const CAP_sources = "src/**/**.ts";
const CAP_libDir = "lib";
const clearCAP = () => rimraf([ CAP_buildDir ]);
const compileCAPTask = () =>
gulp
.src(CAP_sources)
.pipe(ts(CAP_tsConfig))
.pipe(gulp.dest(`${CAP_buildDir}/${CAP_libDir}`));
const copyCAPFiles = () =>
gulp
.src(JSON.parse(fs.readFileSync(CAP_packageJson, 'utf8')).files.filter((file) => !file.startsWith(`/${CAP_libDir}`)), { base: "." })
.pipe(gulp.dest(CAP_buildDir));
const copyCAPPackageJson = () =>
gulp
.src(CAP_packageJson)
.pipe(gulp.dest(CAP_buildDir));
const packCAPPackage = () => exec(`pushd ${CAP_buildDir} && npm pack`);
var CAP_buildTask = gulp.series(clearCAP, compileCAPTask, copyCAPFiles, copyCAPPackageJson, packCAPPackage);
}
/***********************
* CORDOVA.JS SECTION *
************************/
{
const CDV_patchSourcesDir = "other-platforms-support/cordova"
const CDV_packageJson = `${CDV_patchSourcesDir}/package.json`
const CDV_pluginXml = `${CDV_patchSourcesDir}/plugin.xml`
const CDV_buildDir = `${buildDir}/cdv`
const CDV_tempDir = `${tmpDir}/cdv`
const CDV_libDir = "lib"
const CDV_typingsFile = "typings.d.ts"
const CDV_outFileDir = `${CDV_buildDir}/${CDV_libDir}`
const CDV_pluginName = "PowerAuthPlugin"
const CDV_outFile = `${CDV_outFileDir}/${CDV_pluginName}.js`
const clearCDVall = () => rimraf([ CDV_buildDir, CDV_tempDir ]);
const clearCDVtemp = () => rimraf([ CDV_tempDir ]);
const copyCDVSourceFiles = () =>
gulp
.src("src/**/**.ts", { base: ".", allowEmpty: true })
.pipe(replace("__DEV__", "false")) // replace reaact-native __DEV__ with false by default
.pipe(gulp.dest(CDV_tempDir));
const copyCDVPatchSourceFiles = () =>
gulp
.src([`${CDV_patchSourcesDir}/src/**/**.ts`], { base: CDV_patchSourcesDir })
.pipe(gulp.dest(CDV_tempDir));
const compileCDVTask = () =>
build({
entryPoints: [`${CDV_tempDir}/src/index.ts`],
outfile: CDV_outFile,
bundle: true,
format: "cjs",
target: "ios13",
minify: true
})
const createCDVDtsTask = () =>
gulp
.src([`${CDV_tempDir}/src/PowerAuth**.ts`, `${CDV_tempDir}/src/*/**.ts`])
.pipe(stripImportExport())
.pipe(ts({ declaration: true, emitDeclarationOnly: true }))
.pipe(filter(f => !f.path.includes(`${CDV_tempDir}/src/internal/`)))
.pipe(concat(CDV_typingsFile))
.pipe(gulp.dest(CDV_buildDir))
// Will be filled by `processCDVobjectsToExport` task
let objectsToExport = []
const processCDVobjectsToExport = () => {
return new Promise(function(resolve) {
const matches = fs.readFileSync(`${CDV_buildDir}/${CDV_typingsFile}`, 'utf8').matchAll(/declare(\sabstract)? [a-z]* (?<name>[a-zA-z0-9_]*)/g)
objectsToExport = [...matches].flatMap(r => r.groups).flatMap(r => r.name)
resolve()
});
}
const exportModules = () => {
return new Promise(function(resolve) {
objectsToExport.forEach((v) => {
fs.writeFileSync(
`${CDV_outFileDir}/${v}.js`,
// TODO: extract
`require("cordova-powerauth-mobile-sdk.${CDV_pluginName}");\nmodule.exports = ${CDV_pluginName}.${v};`
);
})
resolve()
});
}
// Copy sources based on package.json for cordova, but the source directory (the root project) doesn't contain all the mentioned files.
// It's necessary to filter files not present in the source directory. Otherwise it fails completely.
const cdvPackageRegex = /.*\/powerauth\/cdv\/.*/;
const copyCDVFiles = () =>
gulp
.src(
JSON.parse(fs.readFileSync(CDV_packageJson, 'utf8'))
.files.filter((file) => !file.startsWith(`${CDV_libDir}/`) && !file.match(cdvPackageRegex)),
{ base: ".", allowEmpty: true })
.pipe(gulp.dest(CDV_buildDir));
const copyCDVPatchIOSFiles = () =>
gulp
.src([`${CDV_patchSourcesDir}/ios/PowerAuth/**`], { base: CDV_patchSourcesDir })
.pipe(gulp.dest(CDV_buildDir));
const copyCDVPatchAndroidFiles = () =>
gulp
.src([`${CDV_patchSourcesDir}/android/**`], { base: CDV_patchSourcesDir })
.pipe(gulp.dest(CDV_buildDir));
const copyCDVStaticFiles = () =>
gulp
.src([CDV_packageJson, CDV_pluginXml])
.pipe(
replace(
"<!-- PLACEHOLDER_MODULES -->",
[CDV_pluginName, ...objectsToExport]
.map((v) => ` <js-module src="${CDV_libDir}/${v}.js" name="${v}"><clobbers target="${v}" /></js-module>`)
.join("\n")
)
)
.pipe(
replace("<!-- PLACEHOLDER_VERSION -->", libVersion)
)
.pipe(gulp.dest(CDV_buildDir));
const packCDVPackage = () => exec(`pushd ${CDV_buildDir} && npm pack`);
// join cordova compile and modify for export task
var CDV_buildTask = gulp.series(
clearCDVall,
copyCDVSourceFiles,
copyCDVPatchSourceFiles,
compileCDVTask,
createCDVDtsTask,
processCDVobjectsToExport,
exportModules,
copyCDVFiles,
copyCDVPatchIOSFiles,
copyCDVPatchAndroidFiles,
copyCDVStaticFiles,
packCDVPackage,
clearCDVtemp
);
}
let cleanBuild = () => rimraf([ buildDir ])
let cleanTemp = () => rimraf([ tmpDir ])
// first, delete output folders, then compile cordova and capacitor in parallel
const buildAllTask = gulp.series(
cleanBuild,
cleanTemp,
gulp.parallel(
RN_buildTask,
//CAP_buildTask,
CDV_buildTask
),
cleanTemp
);
// watch and default task
gulp.task("watch", () => { gulp.watch("src/ts/**/*.ts", buildAllTask) });
gulp.task("default", buildAllTask);
gulp.task("clean", gulp.parallel(cleanBuild, cleanTemp));
gulp.task("rn", RN_buildTask);
gulp.task("cap", CAP_buildTask);
gulp.task("cdv", CDV_buildTask);