Skip to content

Commit

Permalink
fix(examples): rename prepare script alias, add commits.json (#46)
Browse files Browse the repository at this point in the history
- rename `prepare` => `prep` to avoid autoexec during `yarn install`
- add getCommits() to retrieve commits for caching and
  write to `commits.json`
- commits.json stub file
  • Loading branch information
postspectacular committed Sep 24, 2018
1 parent cf05efb commit c0a8926
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/commit-table-ssr/commits.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
4 changes: 2 additions & 2 deletions examples/commit-table-ssr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"license": "Apache-2.0",
"scripts": {
"clean": "rm -rf .cache build out",
"prepare": "yarn clean && mkdir -p out && cp commits.json out",
"prep": "yarn clean && mkdir -p out && cp commits.json out",
"build-static": "tsc && node build/server/static.js",
"build": "yarn prepare && parcel build index.html -d out --no-source-maps --no-cache --detailed-report --public-url ./",
"build": "yarn prep && parcel build index.html -d out --no-source-maps --no-cache --detailed-report --public-url ./",
"start": "tsc && node build/server/index.js"
},
"devDependencies": {
Expand Down
12 changes: 9 additions & 3 deletions examples/commit-table-ssr/src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TLRUCache } from "@thi.ng/cache";
import * as express from "express";
import * as Bundler from "parcel-bundler";
import * as fs from "fs";

import { Commit } from "../common/api";
import { ctx } from "../common/config";
Expand All @@ -13,13 +14,18 @@ import { repoCommits } from "./git";
const rawCache = new TLRUCache<string, Commit[]>(null, { ttl: 60 * 60 * 1000 });
const htmlCache = new TLRUCache<string, string>(null, { ttl: 60 * 60 * 1000 });

// console.log("parcel:", Object.keys(parcel));
const bundler = new Bundler("index.html", {
outDir: "./out",
outFile: "index.html",
publicUrl: "/out",
});

const getCommits = async () => {
const commits = [...repoCommits(ctx.repo.path)];
fs.writeFileSync("commits.json", JSON.stringify(commits));
return commits;
};

const app = express();

// route for browser version
Expand All @@ -34,7 +40,7 @@ app.get("/commits", (_, res) => {
// (re)create if missing...
rawCache.getSet(
ctx.repo.path,
async () => [...repoCommits(ctx.repo.path)]
getCommits
).then(
(commits) => res.type("json").send(commits)
)
Expand All @@ -50,7 +56,7 @@ app.get("/ssr", (_, res) => {
async () => buildRepoTableHTML(
await rawCache.getSet(
ctx.repo.path,
async () => [...repoCommits(ctx.repo.path)]
getCommits
)
)
).then((doc) => res.send(doc))
Expand Down

0 comments on commit c0a8926

Please sign in to comment.