-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.js
75 lines (65 loc) · 2.65 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
const gulp = require('gulp');
const { watch, series, parallel } = require('gulp');
const browser = require('browser-sync');
const clean = require('gulp-clean');
const fs = require('fs');
// Get dependencies tasks
const project = require('./gulp/DefaultSpecs');
const tsTranspile = require('./gulp/Tasks/TsTanspile');
const updatetVersion = require('./gulp/Tasks/UpdateVersion');
// Local configs
const distFolder = './dist';
const serverPort = 3000;
const watchTsFiles = 'src/**/*.ts';
// Clean Dist Folder
function cleanOldFiles(cb) {
if (fs.existsSync(distFolder)) {
gulp.src(distFolder + '/*', { read: false }).pipe(clean());
}
cb();
}
// Starts a Browser instance
function initServer() {
updateIndexTemplateFile();
setTimeout(() => {
browser.init({ server: distFolder, port: serverPort, cors: true });
}, 0);
}
// Method to update development template code
function updateIndexTemplateFile() {
// Get the index.html base file
let code = fs.readFileSync('./gulp/Template/index.html', 'utf8');
let jsLinks = '';
if (
process.env.npm_config_target !== undefined &&
project.globalConsts.platformTarget[process.env.npm_config_target] !== undefined
) {
const platformType = project.globalConsts.platformTarget[process.env.npm_config_target];
code = code.replace(' • --platform--', platformType !== '' ? ' • ' + platformType : '');
jsLinks = `<li><p><a target="blank" href="./dev.${platformType !== '' ? platformType + '.' : ''}${
project.globalConsts.fileName
}.js">${platformType !== '' ? platformType + '.' : ''}${project.globalConsts.fileName}.js</a></p></li>`;
} else {
code = code.replace(' • --platform--', '');
const pts = project.globalConsts.platformTarget;
for (const pt in pts) {
jsLinks += `<li><p><a target="blank" href="./dev.${pts[pt] !== '' ? pts[pt] + '.' : ''}${
project.globalConsts.fileName
}.js">${pts[pt] !== '' ? pts[pt] + '.' : ''}${project.globalConsts.fileName}.js</a></p></li>\n`;
}
}
code = code.replace('<li>jsListItemToBeReplaced</li>', jsLinks);
code = code.replaceAll('_asset-name_', project.globalConsts.fileName);
code = code.replace('_more-info-repo-link_', project.globalConsts.gitUrl);
// Create the new index.html at the dist folder!
fs.writeFileSync(`${distFolder}/index.html`, code, 'utf8');
}
// Watch files changed
function watchFiles() {
watch(watchTsFiles, series(tsTranspile.transpileDev));
}
// Gulp tasks
exports.startDevelopment = series(cleanOldFiles, tsTranspile.transpileDev, parallel(watchFiles, initServer));
exports.createProduction = series(cleanOldFiles, tsTranspile.transpileProd);
exports.updateVersion = updatetVersion.setVersion;
exports.gtaSetVersion = updatetVersion.gtaSetVersion;