-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
299 lines (278 loc) · 6.8 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"use strict";
const gulp = require("gulp");
// const { task, src, dest } = require("gulp");
const babel = require("gulp-babel");
const uglify = require("gulp-uglify");
const rename = require("gulp-rename");
const concat = require("gulp-concat");
const replace = require("gulp-replace");
const del = require("del");
const banner = require("gulp-header");
const pkg = require("./package.json");
const { argv } = require("node:process");
const { exec } = require("node:child_process");
const env = process.env;
const COPY_RIGHT =
"(C) 2015 - 2023 Open Source Matters,Inc. All right reserved.";
const LINK_DOCBLOCK_LONG = `http://git.akhi.ir/js/SHDate | ${pkg.homepage}`;
const INFO_DOCBLOCK_LONG = [
`/**
* In the name of Allah, the Beneficent, the Merciful.
* @package ${pkg.name} - ${pkg.description}
* @author ${pkg.author}
* @link ${LINK_DOCBLOCK_LONG}
* @copyright ${COPY_RIGHT}
* @license ${pkg.license} License
* @version Release: ${pkg.version}
*/
`
].join("\n");
const INFO_DOCBLOCK_SHORT = [
`/** In the name of Allah. | ${pkg.name}@${pkg.version} | ${COPY_RIGHT} | ${pkg.license} License | ${LINK_DOCBLOCK_LONG} */`
].join("\n");
function replaceDocBlockInfo() {
/**
* In the name of Allah, the Beneficent, the Merciful. | \* In the name of Allah, the Beneficent, the Merciful\./im;
*/
return gulp
.src(["src/**/*.ts"])
.pipe(
replace(
/\* @(package( [\w\/\-\,\{\}@]+)+)/g,
`* @package ${pkg.name} - ${pkg.description}`
)
)
.pipe(
replace(
/\* @(author( [\w\/\-\,\{\}@<>\.\(\):]+)+)/g,
`* @author ${pkg.author}`
)
)
.pipe(
replace(/\* @(link( [\w:\/\.\|#-]+)+)/g, `* @link ${LINK_DOCBLOCK_LONG}`)
)
.pipe(
replace(
/\* @(copyright( [\w\d.\(\)\-\,]+)+)/g,
`* @copyright ${COPY_RIGHT}`
)
)
.pipe(
replace(
/\* @(license( [\w\d:\/.\(\)\-]+)+)/g,
`* @license ${pkg.license} License`
)
)
.pipe(
replace(
/\* @(version( [\w\d:\/\.\-]+)+)/g,
`* @version Release: ${pkg.version}`
)
)
.pipe(
replace(
/VERSION: string = "([\w\d:\/\.\-]+)"/g,
`VERSION: string = "${pkg.version}"`
)
)
.pipe(gulp.dest("src/"));
}
function bumpVersion() {
return gulp
.src(["src/**/*.ts"])
.pipe(
replace(
/\* @(version( [\w\d:\/\.\-]+)+)/gm,
`* @version Release: ${pkg.version}`
)
)
.pipe(
replace(
/VERSION: string = "([\w\d:\/\.\-]+)"/gm,
`VERSION: string = "${pkg.version}"`
)
)
.pipe(gulp.dest("src/"));
}
function cleanCompile(cb) {
return del(["dist", "src/browser"], cb);
}
function compileCjs(cb) {
//"tsc:cjs"
return exec(
'tsc --project tsconfig.json && echo {"type":"commonjs"} > dist/cjs/package.json',
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}
function makeTypes() {
return gulp.src("dist/cjs/**/*.d.ts").pipe(gulp.dest("dist/types"));
}
function cleanTSCJS(cb) {
return del(["dist/cjs/**/*.d.ts"], cb);
}
function compileMjs(cb) {
//"tsc:mjs"
return exec(
'tsc --project tsconfig.mjs.json && echo {"type":"module"} > dist/mjs/package.json',
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}
/**
* combine all .ts files into one
*/
function concatBrowserTS() {
return gulp
.src(["./src/languages/**/*.ts", "./src/parser/**/*.ts", "src/*.ts"])
.pipe(concat("shdate.ts"))
.pipe(replace(/export (default)?/g, ""))
.pipe(replace(/import [a-zA-Z_,{ }]* from [0-9a-zA-Z_\/\.\"]*;/g, ""))
.pipe(replace(/(Languages?|Word)(\.)?/g, "SHDate$1$2"))
.pipe(replace(/ ([a-z]{2,3}_[A-Z]{2})(\.)?/g, " SHDateLanguage_$1$2"))
.pipe(replace(/SH(Parser|Lexer|Token)/g, "SHDate$1"))
.pipe(gulp.dest("src/browser"));
}
function compileBrowser(cb) {
//"tsc:browser"
return exec(
"tsc --project tsconfig.browser.json",
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}
function makeBrowserTypes() {
return gulp.src(["src/browser/*.ts"]).pipe(gulp.dest("dist/browser"));
}
function cleanBrowserTS(cb) {
return del(
[
"src/browser"
// , "dist/browser/*.d.ts"
],
cb
);
}
function compressBrowserJS() {
return gulp
.src("dist/browser/shdate.js", { sourcemaps: true })
.pipe(babel({ presets: ["@babel/env"] }))
.pipe(replace(/_a/g, "_SHDate"))
.pipe(banner(INFO_DOCBLOCK_LONG))
.pipe(gulp.dest("dist/browser"))
.pipe(babel({ presets: ["@babel/env"] }))
.pipe(replace(/_a/g, "_SHDate"))
.pipe(uglify())
.pipe(rename({ extname: ".min.js" }))
.pipe(banner(INFO_DOCBLOCK_SHORT))
.pipe(gulp.dest("./dist/browser", { sourcemaps: "." }));
}
/**
* Run default.
*/
exports.default = gulp.task(
"build",
gulp.series(
cleanCompile,
gulp.parallel(gulp.series(compileCjs, makeTypes, cleanTSCJS), compileMjs),
concatBrowserTS,
compileBrowser,
compressBrowserJS,
makeBrowserTypes,
cleanBrowserTS,
replaceDocBlockInfo,
bumpVersion
)
);
function cleanTest(cb) {
return del(["dist/src", "dist/test"], cb);
}
function compileTest(cb) {
return exec(
"tsc --project tsconfig.test.json",
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}
gulp.task(compileTest);
function copyTestBrowser(cb) {
return gulp.src(["test/browser/*"]).pipe(gulp.dest("dist/test/browser"));
}
function installTestBrowser(cb) {
return exec(
"(cd dist/test/browser && npm i && npm test)",
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}
function copyTestCJS(cb) {
return gulp.src(["test/cjs/*"]).pipe(gulp.dest("dist/test/cjs"));
}
function installTestCJS(cb) {
return exec(
"(cd dist/test/cjs && npm i && npm test)",
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}
function copyTestMJS(cb) {
return gulp.src(["test/mjs/*"]).pipe(gulp.dest("dist/test/mjs"));
}
function installTestMJS(cb) {
return exec(
"(cd dist/test/mjs && npm i && npm test)",
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}
function copyTestPKG(cb) {
return gulp.src(["test/pkgs/*"]).pipe(gulp.dest("dist/test/pkgs"));
}
function installTestPKGS(cb) {
return exec(
"(cd dist/test/pkgs && npm i && npm test) ",
function (err, stdout, stderr) {
// console.log(stdout);
// console.log(stderr);
cb(err);
}
);
}
gulp.task(
"test",
gulp.series(
cleanTest,
compileTest,
gulp.parallel(
gulp.series(copyTestBrowser, installTestBrowser),
gulp.series(copyTestCJS, installTestCJS),
gulp.series(copyTestMJS, installTestMJS),
gulp.series(copyTestPKG, installTestPKGS)
)
)
);
gulp.task("testRelease", gulp.series(cleanTest, compileTest));
// buildUntilTests.displayName = 'clean:all';
// buildUntilTests.description = 'Build the project';
// buildUntilTests.flags = { '-e': 'An example flag' };