forked from SparkDevNetwork/Rock
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbuild.ts
More file actions
231 lines (193 loc) · 6.89 KB
/
build.ts
File metadata and controls
231 lines (193 loc) · 6.89 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
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
import { dirname, relative, resolve } from "path";
import { argv } from "process";
import { glob } from "glob";
import * as ts from "typescript";
import { copyFileSync, mkdirSync, Stats, statSync } from "fs";
const formatHost: ts.FormatDiagnosticsHost = {
getCanonicalFileName: path => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine
};
/**
* Perform a one-time build of the project.
*
* @param project The path to the project to be built.
*/
function buildProject(project: string): void {
const host = ts.createSolutionBuilderHost(ts.sys, undefined, reportDiagnostic);
const builder = ts.createSolutionBuilder(host, [project], {});
const startTime = Date.now();
const result = builder.build();
const duration = Date.now() - startTime;
logMessage(`Build finished in ${duration / 1000}s.`);
if (result === ts.ExitStatus.Success || result === ts.ExitStatus.DiagnosticsPresent_OutputsGenerated) {
copyFiles();
}
ts.sys.exit(result);
}
/**
* Perform a one-time build and then watch for changes and continue
* to rebuild the project.
*
* @param project The project to be built and watched.
*/
function watchProject(project: string): void {
const createProgram = ts.createSemanticDiagnosticsBuilderProgram;
let startTime = 0;
// Create the compiler host that will let us customize the process.
const host = ts.createWatchCompilerHost(
project,
{
skipLibCheck: true
},
ts.sys,
createProgram,
reportDiagnostic,
reportWatchStatusChanged
);
// You can technically override any given hook on the host, though you probably
// don't need to.
// Note that we're assuming `origCreateProgram` and `origPostProgramCreate`
// doesn't use `this` at all.
const origCreateProgram = host.createProgram;
host.createProgram = (rootNames: ReadonlyArray<string> | undefined, options, host, oldProgram) => {
startTime = Date.now();
const result = origCreateProgram(rootNames, options, host, oldProgram);
return result;
};
const origPostProgramCreate = host.afterProgramCreate;
host.afterProgramCreate = program => {
if (origPostProgramCreate) {
origPostProgramCreate(program);
}
const duration = Date.now() - startTime;
logMessage(`Build finished in ${duration / 1000}s.`);
};
// `createWatchProgram` creates an initial program, watches files, and updates
// the program over time.
ts.createWatchProgram(host);
}
/**
* Log a message to the console along with the current time.
*
* @param message The message to be logged.
*/
function logMessage(message: string): void {
const date = new Date();
const formatter = new Intl.DateTimeFormat(undefined, {
hour12: true,
hour: "numeric",
minute: "2-digit",
second: "2-digit"
});
const formattedDate = formatter.format(date);
console.log(`[${formattedDate}] ${message}`);
}
/**
* Report a diagnostic message.
*
* @param diagnostic The diagnostic message to be reported.
*/
function reportDiagnostic(diagnostic: ts.Diagnostic): void {
if (diagnostic.file && diagnostic.start) {
const { line, character } = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine());
logMessage(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
}
else {
logMessage(ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()));
}
}
/**
* Prints a diagnostic every time the watch status changes.
* This is mainly for messages like "Starting compilation" or "Compilation completed".
*/
function reportWatchStatusChanged(diagnostic: ts.Diagnostic): void {
if (diagnostic.code === 6032 || diagnostic.code === 6031) {
console.clear();
}
const messageText = ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine());
logMessage(`TS${diagnostic.code}: ${messageText}`);
if (diagnostic.code === 6194 && diagnostic.messageText === "Found 0 errors. Watching for file changes.") {
copyFiles();
}
}
type FileCopyPath = {
path: string;
relativePath: string;
};
/**
* Copy the files from the build artifact directory to the RockWeb folder so
* they can be used.
*/
function copyFiles(): void {
const projectPath = resolve(__dirname, "..");
const solutionPath = resolve(projectPath, "..");
let srcPath = resolve(projectPath, "Framework");
let distPath = resolve(projectPath, "dist", "Framework");
// Glob doesn't work with windows \ in path names.
if (process.platform === "win32") {
srcPath = srcPath.replace(/\\/g, "/");
distPath = distPath.replace(/\\/g, "/");
}
logMessage("Copying files...");
let copiedCount = 0;
let files: FileCopyPath[] = [];
// Include all compiled JavaScript files.
files = files.concat(glob.sync(`${distPath}/**/*.js`).map(f => {
return {
path: f,
relativePath: relative(distPath, f)
};
}));
// Include all source type definition files.
// NOTE: Not yet, they aren't needed until we open up to third party plugins.
//files = files.concat(glob.sync(`${srcPath}/**/*.d.ts`).map(f => {
// return {
// path: f,
// relativePath: relative(srcPath, f)
// };
//}));
// If we are not in release mode, then copy the map files too.
if (process.env.CONFIGURATION !== "Release") {
files = files.concat(glob.sync(`${distPath}/**/*.js.map`).map(f => {
return {
path: f,
relativePath: relative(distPath, f)
};
}));
}
// Loop over each file to be copied.
files.forEach(source => {
const relativeName = source.relativePath;
const destination = resolve(solutionPath, "RockWeb", "Obsidian", relativeName);
mkdirSync(dirname(destination), { recursive: true });
const sourceStat = statSync(source.path);
let destinationStat: Stats | null;
try {
destinationStat = statSync(destination);
}
catch {
destinationStat = null;
}
// Check if the file has changed, if so copy it.
if (destinationStat === null || sourceStat.size !== destinationStat.size || sourceStat.mtimeMs > destinationStat.mtimeMs) {
copyFileSync(source.path, destination);
copiedCount++;
}
});
logMessage(`Copied ${copiedCount} files.`);
}
/**
* The main logic.
*/
function main(): void {
const configPath = resolve(__dirname, "..", "Framework", "tsconfig.json");
if (argv.includes("--watch")) {
watchProject(configPath);
}
else {
buildProject(configPath);
}
}
main();