-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
150 lines (129 loc) · 4.44 KB
/
gulpfile.js
File metadata and controls
150 lines (129 loc) · 4.44 KB
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
const fs = require('fs');
const path = require('path');
const gulp = require('gulp');
const webpackStream = require('webpack-stream');
const webpackCore = require('webpack');
const env = require('gulp-env');
const rename = require('gulp-rename');
const through2 = require('through2');
const projects = require('./projects');
const processFirebaseRules = require('./tasks/processFirebaseRules');
require('ignore-styles');
require('@babel/polyfill');
const prettyPrintJson = () => through2.obj((file, enc, cb) => {
if (file.isNull()) {
return cb(null, file);
}
if (file.isBuffer()) {
try {
const jsonContent = JSON.parse(file.contents.toString());
const prettyJson = JSON.stringify(jsonContent, null, 2);
file.contents = Buffer.from(prettyJson);
return cb(null, file);
} catch (error) {
return cb(new Error(`Invalid JSON format in ${file.path}`));
}
}
cb(null, file);
});
async function clean() {
const config = require('./webpack.config.js');
const { deleteAsync } = await import('del');
return await deleteAsync([config.output.path]);
}
async function prodEnv() {
env({
vars: {
ENV: 'production',
},
});
}
function bundleJS() {
const config = require('./webpack.config.js');
return webpackStream(config, webpackCore)
.pipe(gulp.dest(config.output.path));
}
function copyResetCss() {
const config = require('./webpack.config.js');
return gulp.src('./reset.css', { base: './', allowEmpty: true })
.pipe(gulp.dest(config.output.path));
}
function copyFavicons(done) {
const config = require('./webpack.config.js');
const projectName = process.env.npm_config_project || 'lszt';
const projectConf = projects.load(projectName);
const faviconDir = path.join(__dirname, 'theme', projectConf.theme, 'favicons');
if (!fs.existsSync(faviconDir)) {
console.log(`⚠️ Skipping favicons: Directory not found at ${faviconDir}`);
return done();
}
return gulp.src([
path.join(faviconDir, '*'),
'!' + path.join(faviconDir, 'manifest.json'),
'!' + path.join(faviconDir, 'site.webmanifest'),
], {
base: path.join(__dirname, 'theme', projectConf.theme),
allowEmpty: true,
encoding: false
})
.pipe(gulp.dest(config.output.path));
}
function generateManifest(done) {
const config = require('./webpack.config.js');
const projectName = process.env.npm_config_project || 'lszt';
const projectConf = projects.load(projectName);
const themeColor = projectConf.themeColor || '#ffffff';
const shortName = projectConf.shortName || projectConf.title;
const themeName = projectConf.theme;
const faviconDir = path.join(__dirname, 'theme', themeName, 'favicons');
const manifestPath = path.join(faviconDir, 'manifest.json');
let icons;
if (fs.existsSync(manifestPath)) {
const existing = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
icons = existing.icons || [];
} else {
const pngFiles = fs.existsSync(faviconDir)
? fs.readdirSync(faviconDir).filter(f => f.startsWith('android-chrome') && f.endsWith('.png'))
: [];
icons = pngFiles.map(f => {
const match = f.match(/(\d+)x(\d+)/);
const size = match ? `${match[1]}x${match[2]}` : '192x192';
return { src: `/favicons/${f}`, sizes: size, type: 'image/png' };
});
}
const manifest = {
name: projectConf.title,
short_name: shortName,
icons,
theme_color: themeColor,
background_color: '#ffffff',
display: 'standalone',
start_url: '/',
scope: '/',
orientation: 'any',
};
const outDir = path.join(config.output.path, 'favicons');
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true });
}
fs.writeFileSync(
path.join(outDir, 'manifest.json'),
JSON.stringify(manifest, null, 2)
);
done();
}
function buildFirebaseRules() {
const config = require('./webpack.config.js');
const projectName = process.env.npm_config_project || 'lszt';
const projectConf = projects.load(projectName);
return gulp.src('./firebase-rules-template.json', { allowEmpty: true })
.pipe(processFirebaseRules(projectConf))
.pipe(prettyPrintJson())
.pipe(rename('firebase-rules.json'))
.pipe(gulp.dest(config.output.path));
}
const assets = gulp.parallel(copyResetCss, gulp.series(copyFavicons, generateManifest), buildFirebaseRules);
exports.clean = clean;
exports.build = gulp.series(clean, bundleJS, assets);
exports['build:prod'] = gulp.series(prodEnv, clean, bundleJS, assets);
exports.default = exports.build;