forked from p4535992/foundryvtt-automated-polymorpher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
667 lines (576 loc) · 20.5 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/* eslint-disable no-useless-escape */
/* eslint-disable @typescript-eslint/no-var-requires */
/**
* This is important for the bundle.js
*/
const mainFilePath = `src/automated-polymorpher.ts`; // MOD 4535992
const gulp = require(`gulp`);
const fs = require(`fs`);
const path = require(`path`);
const archiver = require(`archiver`);
const stringify = require(`json-stringify-pretty-compact`);
const sourcemaps = require(`gulp-sourcemaps`);
const buffer = require(`vinyl-buffer`);
const source = require(`vinyl-source-stream`);
const loadJson = (path) => {
console.log(path)
try {
const str = fs.readFileSync(path).toString();
return JSON.parse(str);
}
catch {
throw Error(`Unable to load module.json`)
}
};
const typescript = require(`typescript`);
// const createLiteral = typescript.createLiteral;
const createLiteral = typescript.factory.createStringLiteral;
const factory = typescript.factory;
const isExportDeclaration = typescript.isExportDeclaration;
const isImportDeclaration = typescript.isImportDeclaration;
const isStringLiteral = typescript.isStringLiteral;
const LiteralExpression = typescript.LiteralExpression;
const Node = typescript.Node;
const TransformationContext = typescript.TransformationContext;
const TSTransformer = typescript.Transformer;
const TransformerFactory = typescript.TransformerFactory;
const visitEachChild = typescript.visitEachChild;
const visitNode = typescript.visitNode;
const less = require(`gulp-less`);
const sass = require(`gulp-sass`)(require(`sass`));
// import type {ModuleData} from `@league-of-foundry-developers/foundry-vtt-types/src/foundry/common/packages.mjs`; // MOD 4535992
const browserify = require(`browserify`);
const tsify = require(`tsify`);
const ts = require(`gulp-typescript`);
const git = require(`gulp-git`);
const jest = require(`gulp-jest`).default;
const argv = require(`yargs`).argv;
function getConfig() {
const configPath = path.resolve(process.cwd(), `foundryconfig.json`);
let config;
if (fs.existsSync(configPath)) {
config = loadJson(configPath);
return config;
} else {
return;
}
}
/* MOD 4535992
interface Manifest {
root: string;
file: ModuleData;
name: string;
}
*/
const getManifest = () => {
const json = {
root: ``,
// @ts-ignore
file: {},
name: ``
};
if (fs.existsSync(`src`)) {
json.root = `src`;
} else {
json.root = `dist`;
}
const modulePath = path.join(json.root, `module.json`);
const systemPath = path.join(json.root, `system.json`);
if (fs.existsSync(modulePath)) {
json.file = loadJson(modulePath);
json.name = `module.json`;
} else if (fs.existsSync(systemPath)) {
json.file = loadJson(systemPath);
json.name = `system.json`;
} else {
return null;
}
// If we can pull our version from our package - saves us having to maintain the number in different places
if (process.env.npm_package_version) {
json.file[`version`] = process.env.npm_package_version;
}
return json;
}
const createTransformer = () => {
/**
* @param {typescript.Node} node
*/
const shouldMutateModuleSpecifier = (node) => {
if (!isImportDeclaration(node) && !isExportDeclaration(node))
return false;
if (node.moduleSpecifier === undefined)
return false;
if (!isStringLiteral(node.moduleSpecifier))
return false;
if (!node.moduleSpecifier.text.startsWith(`./`) && !node.moduleSpecifier.text.startsWith(`../`))
return false;
return path.extname(node.moduleSpecifier.text) === ``;
}
return (context) => {
return (node) => {
function visitor(node) {
if (shouldMutateModuleSpecifier(node)) {
if (isImportDeclaration(node)) {
const newModuleSpecifier = createLiteral(`${(node.moduleSpecifier).text}.js`);
return factory.updateImportDeclaration(node, node.decorators, node.modifiers, node.importClause, newModuleSpecifier,undefined);
} else if (isExportDeclaration(node)) {
const newModuleSpecifier = createLiteral(`${(node.moduleSpecifier).text}.js`);
return factory.updateExportDeclaration(node, node.decorators, node.modifiers, false, node.exportClause, newModuleSpecifier,undefined);
}
}
return visitEachChild(node, visitor, context);
}
return visitNode(node, visitor);
};
};
}
const tsConfig = ts.createProject(`tsconfig.json`, {
getCustomTransformers: (_program) => ({
after: [createTransformer()],
}),
});
/********************/
/* BUILD */
/********************/
/**
* Build TypeScript
*/
function buildTS() {
return (
gulp
.src(`src/**/*.ts`)
.pipe(tsConfig())
// // eslint() attaches the lint output to the `eslint` property
// // of the file object so it can be used by other modules.
// .pipe(eslint())
// // eslint.format() outputs the lint results to the console.
// // Alternatively use eslint.formatEach() (see Docs).
// .pipe(eslint.format())
// // To have the process exit with an error code (1) on
// // lint error, return the stream and pipe to failAfterError last.
// .pipe(eslint.failAfterError())
.pipe(gulp.dest(`dist`))
);
}
// function buildTS() {
// const debug = process.env.npm_lifecycle_event !== `package`;
// const res = tsConfig.src()
// .pipe(sourcemaps.init())
// .pipe(tsConfig());
// return res.js
// .pipe(sourcemaps.write(``, { debug: debug, includeContent: true, sourceRoot: `./ts/src` }))
// .pipe(gulp.dest(`dist`));
// }
/**
* Build JavaScript
*/
function buildJS() {
return (
gulp
.src(`src/**/*.js`)
// // eslint() attaches the lint output to the `eslint` property
// // of the file object so it can be used by other modules.
// .pipe(eslint())
// // eslint.format() outputs the lint results to the console.
// // Alternatively use eslint.formatEach() (see Docs).
// .pipe(eslint.format())
// // To have the process exit with an error code (1) on
// // lint error, return the stream and pipe to failAfterError last.
// .pipe(eslint.failAfterError())
.pipe(gulp.dest(`dist`))
);
}
/**
* Build Module JavaScript
*/
function buildMJS() {
return (
gulp
.src(`src/**/*.mjs`)
// // eslint() attaches the lint output to the `eslint` property
// // of the file object so it can be used by other modules.
// .pipe(eslint())
// // eslint.format() outputs the lint results to the console.
// // Alternatively use eslint.formatEach() (see Docs).
// .pipe(eslint.format())
// // To have the process exit with an error code (1) on
// // lint error, return the stream and pipe to failAfterError last.
// .pipe(eslint.failAfterError())
.pipe(gulp.dest(`dist`))
);
}
/**
* Build Css
*/
function buildCSS() {
return gulp.src(`src/**/*.css`).pipe(gulp.dest(`dist`));
}
/**
* Build Less
*/
function buildLess() {
return gulp.src(`src/**/*.less`).pipe(less()).pipe(gulp.dest(`dist`));
}
/**
* Build SASS
*/
function buildSASS() {
return gulp.src(`src/**/*.scss`).pipe(sass().on(`error`, sass.logError)).pipe(gulp.dest(`dist`));
}
const bundleModule = async () => {
const debug = argv.dbg || argv.debug;
const bsfy = browserify(path.join(__dirname, mainFilePath), { debug: debug });
return bsfy
.on(`error`, console.error)
.plugin(tsify)
.bundle()
.pipe(source(path.join(`dist`, `bundle.js`)))
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write(`./`))
.pipe(gulp.dest(`./`));
// await Promise.resolve(`bundleModule done`);
}
const copyFiles = async() => {
const statics = [`lang`, `languages`, `fonts`, `assets`, `icons`, `templates`, `packs`, `module.json`, `system.json`, `template.json`];
const recursiveFileSearch = (dir, callback) => {
const err = callback.err;
const res = callback.res;
let results = [];
fs.readdir(dir, (err, list) => {
if (err)
return callback(err, results);
let pending = list.length;
if (!pending)
return callback(null, results);
for (let file of list) {
file = path.resolve(dir, file);
fs.stat(file, (err, stat) => {
if (stat && stat.isDirectory()) {
recursiveFileSearch(file, (err, res) => {
results = results.concat(res);
if (!--pending)
callback(null, results);
});
}
else {
results.push(file);
if (!--pending)
callback(null, results);
}
});
}
});
};
console.log(`files:` + statics);
try {
for (const entity of statics) {
const p = path.join(`src`, entity);
/* MOD 4535992
let p:string|null = null;
if (entity.endsWith(`module.json`) || entity.endsWith(`templates`) || entity.endsWith(`lang`)) {
p = path.join(`src`, entity);
} else {
p = path.join(`assets`, entity);
}
*/
if (fs.existsSync(p)) {
if (fs.lstatSync(p).isDirectory())
recursiveFileSearch(p, (err, res) => {
if (err)
throw err;
for (const file of res) {
const newFile = path.join(`dist`, path.relative(process.cwd(), file.replace(/src[\/\\]/g, ``)));
console.log(`Copying file: ` + newFile);
const folder = path.parse(newFile).dir;
if (!fs.existsSync(folder)) {
fs.mkdirSync(folder, { recursive: true });
}
fs.copyFileSync(file, newFile);
}
})
else {
console.log(`Copying file: ` + p + ` to ` + path.join(`dist`, entity));
fs.copyFileSync(p, path.join(`dist`, entity));
}
}
}
return Promise.resolve();
} catch (err) {
await Promise.reject(err);
}
}
const cleanDist = async () => {
if (argv.dbg || argv.debug){
return;
}
console.log(`Cleaning dist file clutter`);
const files = [];
const getFiles = async (dir) => {
const arr = await fs.promises.readdir(dir);
for(const entry of arr)
{
const fullPath = path.join(dir, entry);
const stat = await fs.promises.stat(fullPath);
if (stat.isDirectory())
await getFiles(fullPath);
else
files.push(fullPath);
}
}
await getFiles(path.resolve(`./dist`));
for(const file of files) {
/* MOD 4535992
if (file.endsWith(`bundle.js`) ||
file.endsWith(`.css`) ||
file.endsWith(`module.json`) ||
file.endsWith(`templates`) ||
file.endsWith(`lang`)||
file.endsWith(`.json`) ||
file.endsWith(`.html`)){
continue;
}
*/
console.warn(`Cleaning ` + path.relative(process.cwd(), file));
await fs.promises.unlink(file);
}
}
/**
* Watch for changes for each build step
*/
const buildWatch = () => {
// gulp.watch(`src/**/*.ts`, { ignoreInitial: false }, gulp.series(buildTS, bundleModule));
gulp.watch(`src/**/*.ts`, { ignoreInitial: false }, gulp.series(buildTS));
gulp.watch(`src/**/*.less`, { ignoreInitial: false }, buildLess);
gulp.watch(`src/**/*.sass`, { ignoreInitial: false }, buildSASS);
gulp.watch([`src/fonts`, `src/lang`, `src/languages`, `src/templates`, `src/*.json`], { ignoreInitial: false }, copyFiles);
}
/********************/
/* CLEAN */
/********************/
/**
* Remove built files from `dist` folder
* while ignoring source files
*/
const clean = async () => {
if (!fs.existsSync(`dist`)){
fs.mkdirSync(`dist`);
}
const name = path.basename(path.resolve(`.`));
const files = [];
// If the project uses TypeScript
// if (fs.existsSync(path.join(`src`, mainFilePath))) { // MOD 4535992
files.push(
`lang`,
`languages`,
`fonts`,
`icons`,
`packs`,
`templates`,
`assets`,
`module`,
`index.js`,
`module.json`,
`system.json`,
`template.json`
);
// } // MOD 4535992
// If the project uses Less
/* MOD 4535992
// if (fs.existsSync(path.join(`src/styles/`, `${name}.less`))) {
// files.push(`fonts`, `${name}.css`);
// }
*/
// Attempt to remove the files
try {
for (const filePath of files) {
if (fs.existsSync(path.join(`dist`, filePath))){
// fs.unlinkSync(path.join(`dist`, filePath)); // MOD 4535992
fs.rmSync(path.join(`dist`, filePath), { recursive: true, force: true });
}
}
return Promise.resolve();
} catch (err) {
await Promise.reject(err);
}
}
const linkUserData = async () => {
const name = getManifest()?.file.name;
const config = loadJson(`foundryconfig.json`);
let destDir;
try {
if (fs.existsSync(path.resolve(`.`, `dist`, `module.json`)) || fs.existsSync(path.resolve(`.`, `src`, `module.json`))) {
destDir = `modules`;
} else {
throw Error(`Could not find module.json or system.json`);
}
let linkDir;
if (config.dataPath) {
if (!fs.existsSync(path.join(config.dataPath, `Data`)))
throw Error(`User Data path invalid, no Data directory found`);
linkDir = path.join(config.dataPath, `Data`, destDir, name);
} else {
throw Error(`No User Data path defined in foundryconfig.json`);
}
if (argv.clean || argv.c) {
console.warn(`Removing build in ${linkDir}`);
fs.unlinkSync(linkDir);
} else if (!fs.existsSync(linkDir)) {
console.log(`Copying build to ${linkDir}`);
fs.symlinkSync(path.resolve(`./dist`), linkDir);
}
return Promise.resolve();
} catch (err) {
await Promise.reject(err);
}
}
/*********************/
/* PACKAGE */
/*********************/
/**
* Package build
*/
async function packageBuild() {
const manifest = getManifest();
if (manifest === null)
{
console.error(`Manifest file could not be loaded.`);
throw Error();
}
return new Promise((resolve, reject) => {
try {
// Remove the package dir without doing anything else
if (argv.clean || argv.c) {
console.warn(`Removing all packaged files`);
fs.rmSync(`package`, { force: true, recursive: true });
return;
}
// Ensure there is a directory to hold all the packaged versions
// fs.existsSync(`package`);
if (!fs.existsSync(`package`)) {
fs.mkdirSync(`package`, { recursive: true });
}
// Initialize the zip file
const zipName = `module.zip`; // `${manifest.file.name}-v${manifest.file.version}.zip`; // MOD 4535992
const zipFile = fs.createWriteStream(path.join(`package`, zipName));
//@ts-ignore
const zip = archiver(`zip`, { zlib: { level: 9 } });
zipFile.on(`close`, () => {
console.log(zip.pointer() + ` total bytes`);
console.log(`Zip file ${zipName} has been written`);
return resolve(true);
});
zip.on(`error`, (err) => {
throw err;
});
zip.pipe(zipFile);
// Add the directory with the final code
// zip.directory(`dist/`, manifest.file.name);
const moduleJson = JSON.parse(fs.readFileSync('./src/module.json'));
zip.directory(`dist/`, moduleJson.id);
/* MOD 4535992
zip.file(`dist/module.json`, { name: `module.json` });
zip.file(`dist/bundle.js`, { name: `bundle.js` });
zip.glob(`dist/*.css`, {cwd:__dirname});
zip.directory(`dist/lang`, `lang`);
zip.directory(`dist/templates`, `templates`);
*/
console.log(`Zip files`);
zip.finalize();
return resolve(`done`);
} catch (err) {
return reject(err);
}
});
}
/*********************/
/* PACKAGE */
/*********************/
/**
* Update version and URLs in the manifest JSON
*/
const updateManifest = (cb) => {
const packageJson = loadJson(`package.json`);
const config = getConfig(),
manifest = getManifest(),
rawURL = config.rawURL,
repoURL = config.repository,
manifestRoot = manifest?.root;
if (!config) {
cb(Error(`foundryconfig.json not found`));
}
if (manifest === null) {
cb(Error(`Manifest JSON not found`));
return;
}
if (!rawURL || !repoURL) {
cb(Error(`Repository URLs not configured in foundryconfig.json`));
}
try {
const version = argv.update || argv.u;
/* Update version */
const versionMatch = /^(\d{1,}).(\d{1,}).(\d{1,})$/;
const currentVersion = manifest?.file.version;
let targetVersion = ``;
if (!version) {
cb(Error(`Missing version number`));
}
if (versionMatch.test(version)) {
targetVersion = version;
} else {
targetVersion = currentVersion.replace(versionMatch, (substring, major, minor, patch) => {
console.log(substring, Number(major) + 1, Number(minor) + 1, Number(patch) + 1);
if (version === `major`) {
return `${Number(major) + 1}.0.0`;
} else if (version === `minor`) {
return `${major}.${Number(minor) + 1}.0`;
} else if (version === `patch`) {
return `${major}.${minor}.${Number(patch) + 1}`;
} else {
return ``;
}
});
}
if (targetVersion === ``) {
return cb(Error(`Error: Incorrect version arguments.`));
}
if (targetVersion === currentVersion) {
return cb(Error(`Error: Target version is identical to current version.`));
}
console.log(`Updating version number to "${targetVersion}"`);
packageJson.version = targetVersion;
manifest.file.version = targetVersion;
/* Update URLs */
const result = `${repoURL}/releases/download/${manifest.file.version}/module.zip`;
manifest.file.url = repoURL;
manifest.file.manifest = `${rawURL}/${manifest.file.version}/${manifestRoot}/${manifest.name}`;
manifest.file.download = result;
const prettyProjectJson = stringify(manifest.file, {
maxLength: 35,
indent: `\t`,
});
fs.writeFileSync(`package.json`, JSON.stringify(packageJson, null, `\t`));
fs.writeFileSync(path.join(manifest.root, manifest.name), prettyProjectJson, `utf8`);
return cb();
} catch (err) {
cb(err);
}
}
const test = () => {
return gulp.src(`src/__tests__`).pipe(jest({
"preprocessorIgnorePatterns": [
`dist/`, `node_modules/`
],
"automock": false
}));
}
// const execBuild = gulp.parallel(buildTS, buildLess, copyFiles); // MOD 4535992
const execBuild = gulp.parallel(buildTS, buildJS, buildMJS, buildCSS, buildLess, buildSASS, copyFiles);
exports.build = gulp.series(clean, execBuild);
exports.bundle = gulp.series(clean, execBuild, bundleModule, cleanDist);
exports.watch = buildWatch;
exports.clean = clean;
exports.link = linkUserData;
exports.package = packageBuild;
exports.update = updateManifest;
exports.publish = gulp.series(clean, updateManifest, execBuild, bundleModule, cleanDist, packageBuild);