-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.ts
247 lines (207 loc) · 6.43 KB
/
gulpfile.ts
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
'use strict';
import { FSWatcher, promises as fsp } from 'fs';
import path from 'path';
import gulp from 'gulp';
import del from 'del';
// import sourcemaps from 'gulp-sourcemaps';
import webpack from 'webpack';
import webpackStream from 'webpack-stream';
import named from 'vinyl-named';
import gulp_sass from 'gulp-sass';
import node_sass from 'node-sass';
import gulp_zip from 'gulp-zip';
import { prompt } from 'enquirer';
import webpackConfig from './webpack.config';
(gulp_sass as any).compiler = node_sass;
const THEME_FILE_NAME = "NewPointe2020";
// ============================== //
// Utils //
// ============================== //
/** Array of functions to run on quit. */
const exitHandlers = new Set<() => Promise<void> | void>();
/**
* Registers a function to be run when the proccess is quit.
* Usefull for stopping and cleaning up watch tasks/servers.
* @param handler
*/
function onExit(handler: () => Promise<void> | void) {
exitHandlers.add(handler);
}
// On Ctrl-C cleanup and exit
process.on('SIGINT', async () => {
// Give them 2 seconds before forcibly quiting
setTimeout(() => process.exit(130), 2000);
// Notify all handlers
for (const h of exitHandlers) await h();
});
/**
* Closes the given watcher when the proccess is quit
* @param watcher The watcher to close
*/
async function closeOnExit(watcher: FSWatcher) {
return new Promise((resolve) => onExit(() => resolve(watcher.close())));
}
async function waitOnStream(stream: NodeJS.ReadWriteStream) {
return new Promise((resolve, reject) => stream.on("end", resolve).on("error", reject));
}
async function searchForRock(): Promise<string | null> {
const response: { themedir: string } = await prompt({
type: "input",
name: "themedir",
message: "Enter the path to the RockWeb/Themes folder you want to link"
});
if (response.themedir != null && response.themedir.trim() != "") {
return response.themedir;
}
return null;
}
// ============================== //
// Tasks //
// ============================== //
/**
* Cleans the build folders
*/
export function clean() {
return del("./build/*");
}
clean.description = "Cleans the build folders";
/**
* Compiles TypeScript files using webpack
* @param watch If webpack should watch the files
*/
function compile_typescript(watch = false, env = "development") {
return gulp.src("./Scripts/main.ts")
.pipe(named())
.pipe(webpackStream({ watch, ...webpackConfig(env) }, webpack as any))
.pipe(gulp.dest('./Scripts/'))
.pipe(gulp.dest('./build/theme/Scripts/'));
}
/**
* Compiles TypeScript files
*/
export function typescript() {
return compile_typescript(false);
}
typescript.description = "Compiles TypeScript files";
/**
* Compiles TypeScript files
*/
export function typescriptProd() {
return compile_typescript(false, "production");
}
typescriptProd.description = "Compiles TypeScript files";
/**
* Compiles sass files
*/
export function copyStyles() {
return gulp.src("./Styles/**/*")
.pipe(gulp.dest("./build/theme/Styles"));
}
copyStyles.description = "Copies less styles";
/**
* Copies static assets
*/
export function copyAssets() {
return gulp.src("./Assets/**/*")
.pipe(gulp.dest("./build/theme/Assets"));
}
copyAssets.description = "Copies static assets";
/**
* Copies layout files
*/
export function copyLayouts() {
return gulp.src("./Layouts/**/*")
.pipe(gulp.dest("./build/theme/Layouts"));
}
copyLayouts.description = "Copies layout files";
/**
* Copies files
*/
export const copy = gulp.parallel(copyStyles, copyAssets, copyLayouts);
copy.description = "Copies files";
/**
* Builds all files
*/
export const build = gulp.parallel(copy, typescript);
build.description = "Builds all files";
/**
* Builds all files
*/
export const buildProd = gulp.parallel(copy, typescriptProd);
buildProd.description = "Builds all files";
/**
* Packages the theme into a Rock plugin
*/
export async function packageTheme() {
await waitOnStream(gulp.src("./build/theme/**/*").pipe(gulp.dest(`./build/package/content/Themes/${THEME_FILE_NAME}/`)));
await waitOnStream(gulp.src("./build/package/**/*").pipe(gulp_zip(`./build/${THEME_FILE_NAME}.plugin`)).pipe(gulp.dest(".")));
console.log(`Exported plugin to '${path.resolve(`./build/${THEME_FILE_NAME}.plugin`)}'`);
}
packageTheme.description = "Packages the theme into a Rock plugin";
/**
* Links the theme into a local Rock instance for development.
*/
export async function link() {
const folderToLink = await searchForRock();
if (folderToLink == null) {
console.warn("No folder selected, aborting link.");
}
else {
console.warn(`Linking folder '${folderToLink}'`);
try {
await fsp.symlink(path.resolve("."), path.join(folderToLink, THEME_FILE_NAME), 'junction');
}
catch (e) {
console.error(`Failed to create symlink: ${e.message}`);
console.error(e);
}
}
}
link.description = "Links the theme into a local Rock instance for development.";
// ============================== //
// Watchers //
// ============================== //
/**
* Watches TypeScript files
*/
export function watchTypescript() {
return compile_typescript(true);
}
watchTypescript.description = "Watches TypeScript files";
/**
* Compiles sass files
*/
export function watchStyles() {
return closeOnExit(gulp.watch("./Styles/**/*", copyStyles));
}
watchStyles.description = "Watches less styles";
/**
* Copies static assets
*/
export function watchAssets() {
return closeOnExit(gulp.watch("./Assets/**/*", copyAssets));
}
watchAssets.description = "Watches static assets";
/**
* Copies layout files
*/
export function watchLayouts() {
return closeOnExit(gulp.watch("./Layouts/**/*", copyLayouts));
}
watchLayouts.description = "Watches layout files";
/**
* Watches static files
*/
export const watchStatic = gulp.parallel(watchStyles, watchAssets, watchLayouts);
watchStatic.description = "Watches static files";
/**
* Watches all files
*/
export const watch = gulp.parallel(watchTypescript, watchStatic);
watch.description = "Watches all files";
// ============================== //
// Default //
// ============================== //
const defaultTask = gulp.series(clean, build);
defaultTask.description = "Builds and packages the app";
export default defaultTask;