Skip to content

Commit

Permalink
hash the docs json asset
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac committed Jan 12, 2020
1 parent 1df352a commit 64a9e98
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public/js/cheatsheet.min.js
app.js

public/css/main.min.css
public/docs.json
public/docs.HASHME.json
public/index.html

symbols.json
Expand Down
58 changes: 38 additions & 20 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (grunt) {
function arrToObj (arr) {
var o = {}
for (var i = 0; i < arr.length; i++) {
o[ arr[i] ] = null
o[arr[i]] = null
}
return o
}
Expand Down Expand Up @@ -86,7 +86,7 @@ module.exports = function (grunt) {
const cssClasses = extractSnowflakeClasses('public/css/main.min.css')
const jsServer = extractSnowflakeClasses('app.js')
const jsClient = extractSnowflakeClasses('public/js/cheatsheet.min.js')
const docs = extractSnowflakeClasses('public/docs.json')
const docs = extractSnowflakeClasses('public/docs.HASHME.json')
const jsClasses = jsServer.concat(jsClient, docs)

console.log(cssClasses.length + ' class names found in css/main.min.css')
Expand All @@ -97,12 +97,12 @@ module.exports = function (grunt) {
}

// ---------------------------------------------------------------------------
// Build docs.json from kidif files
// Build docs.HASHME.json from kidif files
// ---------------------------------------------------------------------------

function splitSection (str) {
const lines = str.split('\n')
let lines2 = []
const lines2 = []
for (var i = 0; i < lines.length; i++) {
var line = lines[i].trim()
if (line !== '') {
Expand All @@ -118,17 +118,17 @@ module.exports = function (grunt) {
for (var i = 0; i < docsArr.length; i++) {
var symbol = docsArr[i]

docs[ symbol.name ] = {}
docs[ symbol.name ]['full-name'] = symbol.name
docs[ symbol.name ]['signature'] = splitSection(symbol.signature)
docs[ symbol.name ]['description-html'] = marked(symbol.description)
docs[symbol.name] = {}
docs[symbol.name]['full-name'] = symbol.name
docs[symbol.name]['signature'] = splitSection(symbol.signature)
docs[symbol.name]['description-html'] = marked(symbol.description)

if (symbol.related) {
docs[ symbol.name ]['related'] = splitSection(symbol.related)
docs[symbol.name]['related'] = splitSection(symbol.related)
}

if (symbol.type) {
docs[ symbol.name ]['type'] = symbol.type
docs[symbol.name]['type'] = symbol.type
}
}

Expand Down Expand Up @@ -158,8 +158,8 @@ module.exports = function (grunt) {
docsWeNeed[fullName] = allDocsObj[fullName]
}

grunt.file.write('public/docs.json', JSON.stringify(docsWeNeed))
grunt.log.writeln(symbolsWeNeed.length + ' doc symbols written to public/docs.json')
grunt.file.write('public/docs.HASHME.json', JSON.stringify(docsWeNeed))
grunt.log.writeln(symbolsWeNeed.length + ' doc symbols written to public/docs.HASHME.json')
}

// ---------------------------------------------------------------------------
Expand All @@ -181,29 +181,47 @@ module.exports = function (grunt) {
grunt.log.writeln('Everything looks ok for a build.')
}

// FIXME: re-write this to be more generic please :)
function hashAssets () {
const cssFile = grunt.file.read('00_build/css/main.min.css')
const cssHash = md5(cssFile).substr(0, 10)
const jsFile = grunt.file.read('00_build/js/cheatsheet.min.js')
const jsHash = md5(jsFile).substr(0, 10)
const unhashedDocsFilename = '00_build/docs.HASHME.json'
const docsFileContents = grunt.file.read(unhashedDocsFilename)
const docsHash = md5(docsFileContents).substr(0, 10)

// update cheatsheet.min.js with docs hash
const buildJsFilename = '00_build/js/cheatsheet.min.js'
const jsFileContents1 = grunt.file.read(buildJsFilename)
const jsFileContents2 = jsFileContents1.replace('docs.HASHME.json', 'docs.' + docsHash + '.json')
grunt.file.write(buildJsFilename, jsFileContents2)

// hash css file
const cssFileContents = grunt.file.read('00_build/css/main.min.css')
const cssHash = md5(cssFileContents).substr(0, 10)

// hash JS file
const jsHash = md5(jsFileContents2).substr(0, 10)

const htmlFile = grunt.file.read('00_build/index.html')

// write the new files
grunt.file.write('00_build/css/main.min.' + cssHash + '.css', cssFile)
grunt.file.write('00_build/js/cheatsheet.min.' + jsHash + '.js', jsFile)
grunt.file.write('00_build/css/main.min.' + cssHash + '.css', cssFileContents)
grunt.file.write('00_build/docs.' + docsHash + '.json', docsFileContents)
grunt.file.write('00_build/js/cheatsheet.min.' + jsHash + '.js', jsFileContents2)

// delete the old files
grunt.file.delete('00_build/css/main.min.css')
grunt.file.delete('00_build/docs.HASHME.json')
grunt.file.delete('00_build/js/cheatsheet.min.js')

// update the HTML file
grunt.file.write('00_build/index.html',
htmlFile.replace('main.min.css', 'main.min.' + cssHash + '.css')
.replace('cheatsheet.min.js', 'cheatsheet.min.' + jsHash + '.js'))
.replace('cheatsheet.min.js', 'cheatsheet.min.' + jsHash + '.js'))

// show some output
grunt.log.writeln('00_build/css/main.min.css → ' +
'00_build/css/main.min.' + cssHash + '.css')
grunt.log.writeln('00_build/docs.HASHME.json → ' +
'00_build/docs.' + docsHash + '.json')
grunt.log.writeln('00_build/js/cheatsheet.min.js → ' +
'00_build/js/cheatsheet.min.' + jsHash + '.js')
}
Expand All @@ -229,7 +247,7 @@ module.exports = function (grunt) {
copy: {
cheatsheet: {
files: [
{expand: true, cwd: 'public/', src: ['**'], dest: '00_build/'}
{ expand: true, cwd: 'public/', src: ['**'], dest: '00_build/' }
]
}
},
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ lein clean && lein cljsbuild auto
# NOTE: app.js is generated from "lein cljsbuild auto" above
node app.js

# create a build into the 00-build directory
# create a build into the 00_build directory
npx grunt build
```

Expand Down
2 changes: 1 addition & 1 deletion cljs-client/cljs_cheatsheet_client/tooltips.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@

(def docs (atom {}))

(fetch-clj "docs.json" #(reset! docs %))
(fetch-clj "docs.HASHME.json" #(reset! docs %))

;;------------------------------------------------------------------------------
;; Events
Expand Down

0 comments on commit 64a9e98

Please sign in to comment.