-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
gulpfile.js
260 lines (219 loc) · 7.75 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
/* eslint-disable no-shadow */
import gulp from 'gulp';
import chalk from 'chalk';
import less from 'gulp-less';
import cleanCSS from 'gulp-clean-css';
import yaml from 'gulp-yaml';
import * as fs from 'fs-extra-plus';
import { execa } from 'execa';
import semver from 'semver';
import argv from './tools/args-parser.js';
import esBuild from './esbuild.config.js';
/* ------------------------------------------ */
/* Configuration */
/* ------------------------------------------ */
const production = process.env.NODE_ENV === 'production';
const sourceDirectory = './src';
const distDirectory = './dist';
const templateExt = 'hbs';
const staticFiles = ['system.json', 'template.json', 'assets', 'fonts'];
const rootFiles = ['LICENSE', 'README.md'];
const getDownloadURL = version =>
`https://github.com/fvtt-fria-ligan/twilight2000-foundry-vtt/releases/download/${version}/t2k4e-fvtt_v${version}.zip`;
const packageJson = JSON.parse(fs.readFileSync('package.json'));
const stdio = 'inherit';
/* ------------------------------------------ */
/* Build */
/* ------------------------------------------ */
/**
* Builds the distributable JavaScript code.
* @async
*/
async function buildSource({ watch } = {}) {
await esBuild({ production, watch });
}
/* ------------------------------------------ */
/**
* Copies all template files.
* @async
*/
async function pipeTemplates() {
const templateFiles = await fs.glob([`${sourceDirectory}/**/*.${templateExt}`]);
if (templateFiles && templateFiles.length > 0) {
for (const file of templateFiles) {
await fs.copy(
file,
`${distDirectory}/templates/${file.replace(`${sourceDirectory}/`, '').replace('templates/', '')}`,
);
}
}
}
/* ------------------------------------------ */
/**
* Creates CSS styles from LESS.
*/
async function pipeStyles() {
gulp
.src('src/less/t2k4e.less')
.pipe(less().on('error', err => {
console.error(chalk.red(err.toString()));
this.emit('end');
}))
.pipe(cleanCSS({ debug: true }, details => {
console.log(`${chalk.bold('Styles:')} ${chalk.blueBright(details.name)}`
+ ` in ${chalk.magenta(details.stats.timeSpent + ' ms')}`
+ `\n ├ Before: ${chalk.cyan(Math.round(details.stats.originalSize / 100) / 10 + 'kb')}`
+ `\n ├ After: ${chalk.cyan(Math.round(details.stats.minifiedSize / 100) / 10 + 'kb')}`
+ `\n └ Efficiency: ${chalk.greenBright(Math.round(details.stats.efficiency * 100) + '%')}`,
);
}))
.pipe(gulp.dest('./dist'));
}
/* ------------------------------------------ */
/**
* Creates the JSON translation files, from the Yaml ones.
*/
async function pipeTranslations() {
gulp
.src('src/lang/**/*.{yml,yaml}')
.pipe(yaml({ safe: true }))
.pipe(gulp.dest('./dist/lang'));
}
/* ------------------------------------------ */
/**
* Copies other source files.
* @async
*/
async function pipeStatics() {
for (const file of staticFiles) {
if (fs.existsSync(`static/${file}`)) await fs.copy(`static/${file}`, `${distDirectory}/${file}`);
}
for (const file of rootFiles) {
if (fs.existsSync(`./${file}`)) await fs.copy(`./${file}`, `${distDirectory}/${file}`);
}
}
/* ------------------------------------------ */
/**
* Watches for changes for each build step.
*/
function buildWatch() {
buildSource({ watch: true });
gulp.watch(`${sourceDirectory}/**/*.${templateExt}`, { ignoreInitial: false }, pipeTemplates);
gulp.watch(`${sourceDirectory}/less/**/*.less`, { ignoreInitial: false }, pipeStyles);
gulp.watch(`${sourceDirectory}/lang/**/*.yml`, { ignoreInitial: false }, pipeTranslations);
gulp.watch(
staticFiles.map(file => `static/${file}`),
{ ignoreInitial: false },
pipeStatics,
);
}
/* ------------------------------------------ */
/* Clean */
/* ------------------------------------------ */
/**
* Removes built files from `dist` folder while ignoring source files.
* @async
*/
async function cleanDist() {
if (fs.existsSync('./dist')) await fs.remove('./dist');
}
/* ------------------------------------------ */
/* Versioning */
/* ------------------------------------------ */
/**
* Gets the contents of the manifest file as object.
* @returns {object}
*/
function getManifest() {
const manifestPath = 'static/system.json';
if (fs.existsSync(manifestPath)) {
return {
file: JSON.parse(fs.readFileSync(manifestPath)),
name: 'system.json',
};
}
}
/* ------------------------------------------ */
/**
* Gets the target version based on on the current version and the argument passed as release.
* @param {string} currentVersion The current version
* @param {string} release The release type,
* any of `['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease']`
* @returns {string} The target version
*/
function getTargetVersion(currentVersion, release) {
if (['major', 'premajor', 'minor', 'preminor', 'patch', 'prepatch', 'prerelease'].includes(release)) {
return semver.inc(currentVersion, release);
}
else {
return semver.valid(release);
}
}
/* ------------------------------------------ */
/**
* Makes a changelog.
* @async
*/
async function changelog() {
// await execa('npx', ['standard-version', '--skip.bump', '--skip.tag', '--skip.commit'], { stdio });
}
/**
* Commits and pushes release to Github Upstream.
* @async
*/
async function commitTagPush() {
const { version } = packageJson;
const commitMsg = `chore(release): Release ${version}`;
await execa('git', ['add', '-A'], { stdio });
await execa('git', ['commit', '--message', commitMsg], { stdio });
await execa('git', ['tag', `${version}`], { stdio });
await execa('git', ['push', 'upstream'], { stdio });
await execa('git', ['push', 'upstream', '--tag'], { stdio });
}
/* ------------------------------------------ */
/**
* Updates version and download URL.
* @param {function} cb Callback function
* @throws {Error} When manifest JSON not found
* @throws {Error} When missing release type
* @throws {Error} When incorrect version arguments
* @throws {Error} When target version is identical to current version
* @async
*/
async function bumpVersion(cb) {
const manifest = getManifest();
if (!manifest) cb(Error(chalk.red('Manifest JSON not found')));
try {
const release = argv.release || argv.r;
const currentVersion = packageJson.version;
if (!release) {
return cb(Error('Missing release type'));
}
const targetVersion = getTargetVersion(currentVersion, release);
if (!targetVersion) {
return cb(new Error(chalk.red('Error: Incorrect version arguments')));
}
if (targetVersion === currentVersion) {
return cb(new Error(chalk.red('Error: Target version is identical to current version')));
}
console.log(`Updating version number to '${targetVersion}'`);
packageJson.version = targetVersion;
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, ' '));
manifest.file.version = targetVersion;
manifest.file.download = getDownloadURL(targetVersion);
fs.writeFileSync(`static/${manifest.name}`, JSON.stringify(manifest.file, null, ' '));
return cb();
}
catch (err) {
cb(err);
}
}
/* ------------------------------------------ */
/* Scripts */
/* ------------------------------------------ */
const execBuild = gulp.parallel(buildSource, pipeStyles, pipeTemplates, pipeTranslations, pipeStatics);
export const clean = cleanDist;
export const build = gulp.series(clean, execBuild);
export const watch = gulp.series(buildWatch);
export const bump = gulp.series(bumpVersion, changelog, clean, execBuild);
export const release = commitTagPush;