This repository has been archived by the owner on Jul 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
181 lines (148 loc) · 5.69 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
171
172
173
174
175
176
177
178
179
180
181
var tsify = require('tsify')
var browserify = require('browserify')
var source = require('vinyl-source-stream')
var streamify = require('gulp-streamify')
var uglify = require('gulp-uglify')
var rename = require('gulp-rename')
var gulp = require('gulp')
//requiring path and fs modules
const path = require('path')
const fs = require('fs')
const bundleTrackers = async function () {
//joining path of directory
const directoryPath = path.join(__dirname, 'src')
const destDir = './bundles'
// If the bundles directory does not exist, create it here
if (!fs.existsSync(destDir)) {
fs.mkdirSync(destDir)
}
var promises = []
var bundleThis = function (trackerArray) {
for (let file of trackerArray) {
let filePath = file
// If its a directory
if (
!fs.statSync(path.join(directoryPath, filePath)).isDirectory()
) {
console.log('Not a directory, skipping ' + filePath)
continue
}
let finalPath = path.join(directoryPath, filePath, `/${file}.ts`)
if (!fs.existsSync(finalPath)) {
console.log("The file doesn't exist, skipping. " + filePath)
continue
}
promises.push(
new Promise((res, rej) => {
browserify([finalPath], { standalone: 'Trackers' })
.plugin(tsify, { noImplicitAny: true })
.bundle()
.pipe(source('tracker.js'))
.pipe(gulp.dest(path.join(destDir, file)))
.on('end', () => {
copyFolderRecursive(
path.join(directoryPath, filePath, 'includes'),
path.join(destDir, file)
)
res()
})
})
)
}
}
const bundlesPath = path.join(__dirname, 'bundles')
deleteFolderRecursive(bundlesPath)
bundleThis(fs.readdirSync(directoryPath))
await Promise.all(promises) //.then(function () { done() })
}
const generateVersioningFile = async function () {
let jsonObject = {
buildTime: new Date(),
trackers: []
}
//joining path of directory
const directoryPath = path.join(__dirname, 'bundles')
var promises = []
var generateTrackerList = function (trackerArray) {
for (let trackerId of trackerArray) {
// If its a directory
if (!fs.statSync(path.join(directoryPath, trackerId)).isDirectory()) {
console.log('not a Directory, skipping ' + trackerId)
return
}
let finalPath = `./bundles/${trackerId}/tracker.js`
promises.push(
new Promise((res, rej) => {
let req = require(finalPath)
let tracker = req[trackerId]
let classInstance = new tracker(null)
// make sure the icon is present in the includes folder.
if (!fs.existsSync(path.join(directoryPath, trackerId, 'includes', classInstance.icon))) {
console.log('[ERROR] [' + trackerId + '] Icon must be inside the includes folder')
rej()
return
}
jsonObject.trackers.push({
id: trackerId,
name: classInstance.name,
author: classInstance.author,
desc: classInstance.description,
website: classInstance.authorWebsite,
version: classInstance.version,
icon: classInstance.icon
})
res()
})
)
}
}
generateTrackerList(fs.readdirSync(directoryPath))
await Promise.all(promises)
// Write the JSON payload to file
fs.writeFileSync(
path.join(directoryPath, 'versioning.json'),
JSON.stringify(jsonObject)
)
}
const deleteFolderRecursive = function (folderPath) {
folderPath = folderPath.trim()
if (folderPath.length == 0 || folderPath === '/') return
if (fs.existsSync(folderPath)) {
fs.readdirSync(folderPath).forEach((file, index) => {
const curPath = path.join(folderPath, file);
if (fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(folderPath);
}
};
const copyFolderRecursive = function (tracker, target) {
tracker = tracker.trim()
if (tracker.length == 0 || tracker === '/') return
target = target.trim()
if (target.length == 0 || target === '/') return
if (!fs.existsSync(tracker)) return
var files = [];
//check if folder needs to be created or integrated
var targetFolder = path.join(target, path.basename(tracker));
if (!fs.existsSync(targetFolder)) {
fs.mkdirSync(targetFolder);
}
//copy
if (fs.lstatSync(tracker).isDirectory()) {
files = fs.readdirSync(tracker);
files.forEach(function (file) {
var curSource = path.join(tracker, file);
if (fs.lstatSync(curSource).isDirectory()) {
copyFolderRecursive(curSource, targetFolder);
} else {
fs.copyFileSync(curSource, path.join(targetFolder, file));
}
});
}
}
// exports.bundle = bundleTrackers
exports.bundle = gulp.series(bundleTrackers, generateVersioningFile)