|
1 | 1 | /// <reference path="./declarations.d.ts"/>
|
| 2 | +/// <reference types="es6-promise" /> |
2 | 3 |
|
| 4 | +import {execSync} from "child_process"; |
3 | 5 | import del = require("del");
|
| 6 | +import * as fse from "fs-extra"; |
4 | 7 | import * as gulp from "gulp";
|
5 | 8 | import {createServer} from "http-server";
|
6 | 9 | //const httpServer = require("http-server");
|
7 | 10 | import * as path from "path";
|
| 11 | +import * as tmp from "tmp"; |
8 | 12 | import ts = require("gulp-typescript");
|
9 | 13 |
|
10 | 14 | const out = "public";
|
@@ -38,3 +42,62 @@ gulp.task("serve", () => {
|
38 | 42 | gulp.task("watch", ["build", "serve"], () => gulp.watch("assets/**", ["build"]));
|
39 | 43 |
|
40 | 44 | 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 | +} |
0 commit comments