forked from microsoft/TypeSearch
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.ts
109 lines (89 loc) · 3.25 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
/// <reference path="./declarations.d.ts"/>
import {execSync} from "child_process";
import del = require("del");
import * as fse from "fs-extra";
import * as gulp from "gulp";
import {createServer} from "http-server";
import merge = require("merge-stream");
import * as path from "path";
import runSequence = require("run-sequence");
import * as tmp from "tmp";
import ts = require("gulp-typescript");
const out = "public";
function outDir(name: string): string { return path.join(out, name); }
gulp.task("clean", () => del(`${out}/*`));
gulp.task("script", () => {
const tsProject = ts.createProject("assets/script/tsconfig.json", {
typescript: require("typescript")
});
return tsProject.src().pipe(ts(tsProject)).js.pipe(gulp.dest(outDir("script")));
});
function copy(src: string, dest: string): NodeJS.ReadWriteStream {
return gulp.src(src).pipe(gulp.dest(dest));
}
gulp.task("static", () => copy("assets/static/**", out));
gulp.task("lib", () =>
merge(...["jquery/dist/jquery.min.js", "typeahead.js/dist/typeahead.bundle.min.js"].map(src =>
copy(`node_modules/${src}`, outDir("lib")))));
gulp.task("build", (cb: any) => {
runSequence("clean", ["script", "static", "lib"], cb);
});
gulp.task("serve", () => {
const server = createServer({ root: "public" });
console.log("\nServing to localhost\n");
server.listen(80);
});
gulp.task("watch", ["build", "serve"], () => {
gulp.watch("assets/script/**", ["script"]);
gulp.watch("assets/static/**", ["static"]);
gulp.watch("node_modules", ["lib"]);
gulp.watch("search-index-@(full|head|min).json", ["index"]);
});
gulp.task("default", ["watch"]);
gulp.task("publish", ["build"], async () => {
function exec(cmd: string): string {
return execSync(cmd, { encoding: "utf8" });
}
if (!(exec("git status").includes("nothing to commit"))) {
throw new Error("Commit all changes first!")
}
if (exec("git rev-parse --abbrev-ref HEAD").trim() !== "master") {
throw new Error("You are not on master branch.");
}
const tmpObj = tmp.dirSync();
console.log(`Temporaries are stored at ${tmpObj.name}`);
function tmpDir(dir: string): string {
return path.join(tmpObj.name, dir);
}
const toMove = ["node_modules", "public"];
// Move files away temporarily.
await Promise.all(toMove.map(dir => mvPromise(dir, tmpDir(dir))));
exec("git checkout gh-pages");
// Clean out the old
const oldFiles = fse.readdirSync(".").filter(f => f !== ".git");
oldFiles.forEach(fse.removeSync);
// Move in the new
fse.copySync(tmpDir("public"), ".");
// And commit it
exec("git add --all");
exec("git commit -m \"Update from master\"");
exec("git push");
exec("git checkout master");
// Move files back.
await Promise.all(toMove.map(dir => mvPromise(tmpDir(dir), dir)));
});
declare module "fs-extra" {
function move(src: string, dest: string, cb: (err: Error | undefined) => void): void;
}
function mvPromise(src: string, dest: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
fse.move(src, dest, err => {
if (err) {
reject(err);
}
else {
resolve();
}
})
});
}