This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
99 lines (85 loc) · 2.68 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
/* eslint-env node */
const gulp = require("gulp");
const sass = require("gulp-sass")(require("sass"));
const rename = require("gulp-rename");
const gulpRev = require("gulp-rev-all");
const collectFiles = require("./scripts/gulp/collect-files");
const replace = require("gulp-replace");
const SASS_FILES = {
"sass/hds/index.scss": { basename: "hds" },
"sass/shared/index.scss": { basename: "shared" },
"sass/login/index.scss": { basename: "hs-login" },
};
const STYLES_THEME_PROPERTIES_KEY = "styles";
const SCRIPTS_THEME_PROPERTIES_KEY = "scripts";
function trimFromStart(str, prefix) {
return str.startsWith(prefix) ? str.substring(prefix.length) : str;
}
function processStyleFiles() {
const sourceFiles = Object.keys(SASS_FILES);
return gulp
.src(sourceFiles)
.pipe(sass.sync().on("error", sass.logError))
.pipe(
gulpRev.revision({
transformFilename: (file) => {
return file.basename;
},
})
)
.pipe(
rename((path, file) => {
const originalFullPath = file.history[0];
const originalFileName = trimFromStart(
originalFullPath,
`${file.cwd}/`
);
path.dirname = "";
path.basename = SASS_FILES[originalFileName].basename;
})
)
.pipe(collectFiles.collector(STYLES_THEME_PROPERTIES_KEY))
.pipe(gulp.dest("./helsinki/login/resources/css"));
}
function processScriptFiles() {
return gulp
.src("helsinki/login/resources/js/*")
.pipe(
gulpRev.revision({
transformFilename: (file) => {
return file.basename;
},
})
)
.pipe(collectFiles.collector(SCRIPTS_THEME_PROPERTIES_KEY));
}
function updateThemeProperties(fileType, filePath, prependedFileNames = []) {
const versionedFileNames = collectFiles.get(fileType).map((file) => {
return `${file.basename}?v=${file.revHash.substring(0, 8)}`;
});
const allFileNames = prependedFileNames.concat(versionedFileNames);
const allFilePaths = allFileNames.map((f) => `${filePath}/${f}`).join(" ");
return gulp
.src("helsinki/login/theme.properties")
.pipe(
replace(
new RegExp(`^${fileType}=.+$`, "m"),
`${fileType}=${allFilePaths}`
)
)
.pipe(gulp.dest("helsinki/login"));
}
function updateStylesInThemeProperties() {
// Include login.css from the Keycloak base theme
return updateThemeProperties(STYLES_THEME_PROPERTIES_KEY, "css", [
"login.css",
]);
}
function updateScriptsInThemeProperties() {
return updateThemeProperties(SCRIPTS_THEME_PROPERTIES_KEY, "js");
}
exports.default = gulp.series(
gulp.parallel(processStyleFiles, processScriptFiles),
updateStylesInThemeProperties,
updateScriptsInThemeProperties
);