Ind.ie is now Small Technology Foundation.
Commit 6d874823 authored by Aral Balkan's avatar Aral Balkan

Create separate cache files for content and theme

parent 91c77811
......@@ -30,7 +30,7 @@ exec = (require 'child_process').exec
touch = require 'touch'
cache = require './Cache2'
Cache = require './Cache2'
class Builder
instance = null
......@@ -83,6 +83,7 @@ class Builder
build: =>
@pullContentRepository()
.then @pullThemesRepository
.then @updateCache
.then @renderBlockdown
.then @updateRepositories
.then =>
......@@ -106,6 +107,11 @@ class Builder
console.trace()
process.exit 1
#
# Update the cache.
#
updateCache: =>
(new Cache).build()
#
# Render content in Blockdown format.
......
......@@ -18,52 +18,74 @@ class Cache
contentDirectory: null
themesDirectory: null
cacheCurrentChecksumsDirectory: null
lastModifiedTimes: null
constructor: ->
@homeDirectory = path.join path.homedir(), 'better.fyi'
@contentDirectory = path.join @homeDirectory, 'content'
@themesDirectory = path.join @homeDirectory, 'themes'
@cacheCurrentModifiedTimesFile = path.join @homeDirectory, '.private', 'cache', 'current-modified-times.json'
@cachePreviousModifiedTimesFile = path.join @homeDirectory, '.private', 'cache', 'previous-modified-times.json'
@lastModifiedTimes = {}
build: =>
startTime = new Date
# Back up the current modified times file
if fs.existsSync @cacheCurrentModifiedTimesFile
fs.copySync @cacheCurrentModifiedTimesFile, @cachePreviousModifiedTimesFile
@cacheContent @contentDirectory
.then =>
@cacheContent @themesDirectory
.then =>
fs.outputFileAsync @cacheCurrentModifiedTimesFile, JSON.stringify @lastModifiedTimes, null, " "
.then =>
duration = (new Date) - startTime
console.log "Took: #{duration} ms"
console.log "Generated cache. Took: #{duration} ms"
.catch (e) =>
console.log "Cache build error: #{e}"
#
# Enumerate the files in the passed directory and output a JSON
# file with a hashatable of partial file paths to last modified times.
#
cacheContent: (directory) =>
(globAsync "#{directory}/**/*", {nodir: true})
.series (file) =>
lastModifiedTimes = {}
cacheFileName = (path.parse directory).name
cacheDirectory = path.join @homeDirectory, '.private', 'cache'
cacheCurrentModifiedTimesDirectory = path.join cacheDirectory, 'current-modified-times'
cachePreviousModifiedTimesDirectory = path.join cacheDirectory, 'previous-modified-times'
fs.ensureDirAsync cacheCurrentModifiedTimesDirectory
.then =>
fs.ensureDirAsync cachePreviousModifiedTimesDirectory
.then =>
fullyQualifiedFileName = "#{cacheFileName}.json"
# ~/better.fyi/.private/cache/current-modified-times/content.json & …/themes.json
cacheCurrentModifiedTimesFile = path.join cacheCurrentModifiedTimesDirectory, fullyQualifiedFileName
# ~/better.fyi/.private/cache/previous-modified-times/content.json & …/themes.json
cachePreviousModifiedTimesFile = path.join cachePreviousModifiedTimesDirectory, fullyQualifiedFileName
# Copy existing current modified times file (if one exists) to the previous file folder
# so we can compare and use the cache or render the content accordingly.
if fs.existsSync cacheCurrentModifiedTimesFile
fs.copySync cacheCurrentModifiedTimesFile, cachePreviousModifiedTimesFile
# Go through all the files in the requested directory, recursively, and compile
# their last modified times.
(globAsync "#{directory}/**/*", {nodir: true})
.series (file) =>
fileComponents = file.split '/'
filePathFragment = fileComponents[4..].join '/'
fileComponents = file.split '/'
filePathFragment = fileComponents[5..].join '/'
# Using the sync version of stat is a little faster here
stats = fs.statSync file
@lastModifiedTimes[filePathFragment] = stats.mtime
# Using the sync version of stat is a little faster here
stats = fs.statSync file
lastModifiedTimes[filePathFragment] = stats.mtime
.then =>
fs.outputFileAsync cacheCurrentModifiedTimesFile, JSON.stringify lastModifiedTimes, null, " "
module.exports = Cache
# Test
cache = new Cache
cache.build()
# cache = new Cache
# cache.build()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment