Skip to content

Commit b367b1c

Browse files
author
Andy Hanson
committed
Add publish task
1 parent 7a1b2d9 commit b367b1c

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ TODO: finish README
22

33
Online [here](http://microsoft.github.io/TypeSearch).
44

5-
To update:
6-
* `gulp build`
7-
* Move `public` out
8-
* `git checkout gh-pages`
9-
* Remove old contents and move contents of `public` in
10-
* `git add --all; git commit -m "message"; git push`
11-
* `git checkout master`
5+
### Run locally
6+
7+
Run `gulp`, then see [localhost](http://localhost).
8+
9+
### Publish
10+
11+
`gulp publish`

declarations.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
interface String {
2+
includes(substring: string): boolean;
3+
}
14

25
declare module "http-server" {
36
function createServer(options: { root: string }): { listen: (port: number) => void };

gulpfile.ts

+63
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
/// <reference path="./declarations.d.ts"/>
2+
/// <reference types="es6-promise" />
23

4+
import {execSync} from "child_process";
35
import del = require("del");
6+
import * as fse from "fs-extra";
47
import * as gulp from "gulp";
58
import {createServer} from "http-server";
69
//const httpServer = require("http-server");
710
import * as path from "path";
11+
import * as tmp from "tmp";
812
import ts = require("gulp-typescript");
913

1014
const out = "public";
@@ -38,3 +42,62 @@ gulp.task("serve", () => {
3842
gulp.task("watch", ["build", "serve"], () => gulp.watch("assets/**", ["build"]));
3943

4044
gulp.task("default", ["watch"]);
45+
46+
gulp.task("publish", ["build"], () => {
47+
function exec(cmd: string): string {
48+
return execSync(cmd, { encoding: "utf8" });
49+
}
50+
51+
if (!(exec("git status").includes("nothing to commit"))) {
52+
throw new Error("Commit all changes first!")
53+
}
54+
55+
if (exec("git rev-parse --abbrev-ref HEAD").trim() !== "master") {
56+
throw new Error("You are not on master branch.");
57+
}
58+
59+
const tmpObj = tmp.dirSync();
60+
console.log(`Temporaries are stored at ${tmpObj.name}`);
61+
function tmpDir(dir: string): string {
62+
return path.join(tmpObj.name, dir);
63+
}
64+
65+
const toMove = ["node_modules", "public"];
66+
// Move files away temporarily.
67+
const moved = Promise.all(toMove.map(dir => mvPromise(dir, tmpDir(dir))));
68+
69+
moved.then(() => {
70+
exec("git checkout gh-pages");
71+
// Clean out the old
72+
const oldFiles = fse.readdirSync(".").filter(f => f !== ".git");
73+
oldFiles.forEach(fse.removeSync);
74+
// Move in the new
75+
fse.copySync(tmpDir("public"), ".");
76+
77+
// And commit it
78+
exec("git add --all");
79+
exec("git commit -m \"Update from master\"");
80+
exec("git push");
81+
82+
exec("git checkout master");
83+
// Move files back.
84+
return Promise.all(toMove.map(dir => mvPromise(tmpDir(dir), dir)));
85+
}).catch(console.error);
86+
});
87+
88+
declare module "fs-extra" {
89+
function move(src: string, dest: string, cb: (err: Error | undefined) => void): void;
90+
}
91+
92+
function mvPromise(src: string, dest: string): Promise<void> {
93+
return new Promise<void>((resolve, reject) => {
94+
fse.move(src, dest, err => {
95+
if (err) {
96+
reject(err);
97+
}
98+
else {
99+
resolve();
100+
}
101+
})
102+
});
103+
}

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13+
"@types/es6-promise": "0.0.28",
14+
"@types/fs-extra": "0.0.28",
15+
"@types/tmp": "0.0.27",
16+
"fs-extra": "^0.30.0",
1317
"jquery": "^2.2.2",
18+
"tmp": "0.0.28",
1419
"typeahead.js": "^0.11.1"
1520
},
1621
"devDependencies": {

0 commit comments

Comments
 (0)