@sparkbox/cachebust
Advanced tools
Comparing version 0.1.1 to 0.2.0
17
index.js
#!/usr/bin/env node | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const cachebust = require('./lib/cachebust'); | ||
const backup = require('./lib/backupCachebust'); | ||
const restore = require('./lib/restore'); | ||
const program = require('commander'); | ||
const glob = require('globby'); | ||
@@ -73,8 +72,14 @@ program | ||
if(!backup(targetFiles, program.restore)) { | ||
if (!program.restore) { | ||
cachebust(sourceFiles, targetFiles); | ||
if(program.restore) { | ||
backup.restore(); | ||
} else { | ||
if(!backup.check()) { | ||
cachebust(sourceFiles, glob.sync(targetFiles)); | ||
} else { | ||
console.log('No operations performed. No prior cachebust to restore.'); | ||
console.log('Previous cache detected!'); | ||
console.log('Restore the backup file(s) run: `cachebust --restore`'); | ||
process.exit(); | ||
} | ||
} |
const fs = require('fs'); | ||
const restore = require('./restore'); | ||
const replace = require('replace-in-file'); | ||
const backupCachebust = (targetFiles, shouldRestore) => { | ||
// Check for previous cache bust. This should only be run once | ||
// on a staging or prodution environment. | ||
let cached = false; | ||
let backup = '.cache-backup'; | ||
const backupFile = '.cachebust-manifest.json'; | ||
for (let file of targetFiles) { | ||
if (fs.existsSync(`${file}${backup}`)) { | ||
cached = true; | ||
if (shouldRestore) { | ||
restore(file, `${file}${backup}`); | ||
} else { | ||
console.log('Previous cache detected!'); | ||
console.log('Restore the backup file(s) run: `cachebust --restore`'); | ||
console.log('Backup files:', `${file}${backup}`); | ||
process.exit(); | ||
} | ||
} | ||
} | ||
const create = (manifest) => { | ||
fs.writeFileSync(backupFile, JSON.stringify(manifest)); | ||
} | ||
if (!cached && !shouldRestore) { | ||
for (let file of targetFiles) { | ||
if (!cached) { | ||
fs.copyFile(file, `${file}${backup}`, err => { | ||
if (err) return console.error(err); | ||
}); | ||
} | ||
} | ||
const restore = () => { | ||
if(check()) { | ||
const { targetFiles: files, to: from, from: to } = JSON.parse(fs.readFileSync(backupFile)); | ||
replace({ | ||
files, | ||
from, | ||
to | ||
}); | ||
console.log(`${files.length} files restored.`); | ||
fs.unlinkSync(backupFile); | ||
} else { | ||
console.log('Must create a cachebust before restoring...'); | ||
console.log('Run: `cachebust`.'); | ||
process.exit(); | ||
} | ||
} | ||
return cached; | ||
const check = () => { | ||
return fs.existsSync(backupFile); | ||
} | ||
module.exports = backupCachebust; | ||
module.exports = { create, check, restore }; |
@@ -5,2 +5,3 @@ const finger = require('fingerprinting'); | ||
const replace = require('replace-in-file'); | ||
const backup = require('./backupCachebust'); | ||
@@ -33,3 +34,4 @@ const cachebust = async (sourceFiles, targetFiles) => { | ||
for (let file of sourceFiles) { | ||
const print = finger(file, { format: '{hash}.{ext}' }); | ||
const fileName = path.parse(file).name; | ||
const print = finger(file, { format: `${fileName}-{hash}.{ext}` }); | ||
const target = `${path.dirname(file)}/${print.file}`; | ||
@@ -46,10 +48,20 @@ | ||
if (from.length) { | ||
const changes = replace({ | ||
replace({ | ||
files: targetFiles, | ||
from: from, | ||
to: to | ||
from, | ||
to | ||
}); | ||
const manifest = { | ||
targetFiles, | ||
sourceFiles, | ||
from, | ||
to | ||
}; | ||
backup.create(manifest); | ||
} else { | ||
console.log('Failed to write cache files'); | ||
fs.unlinkSync(backup); | ||
backup.restore(); | ||
process.exit(); | ||
@@ -56,0 +68,0 @@ } |
{ | ||
"name": "@sparkbox/cachebust", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "A simple cache buster", | ||
@@ -23,4 +23,5 @@ "bin": { | ||
"fingerprinting": "^1.0.1", | ||
"globby": "^9.0.0", | ||
"replace-in-file": "^3.1.1" | ||
} | ||
} |
@@ -48,2 +48,4 @@ # cachebust | ||
**Update**: [Globbing](https://github.com/sindresorhus/globby) is allowed for `target` files. So `target: ["path/to/target/**/*.html"] is allowed. | ||
Also you may create a config file in the root of the project directory called `.cachebust.config` | ||
@@ -60,3 +62,3 @@ | ||
"target": [ | ||
"path/to/target/template.html" | ||
"path/to/target/**/*.html" | ||
] | ||
@@ -63,0 +65,0 @@ } |
109
7815
4
5
145
+ Addedglobby@^9.0.0
+ Added@mrmlnc/readdir-enhanced@2.2.1(transitive)
+ Added@nodelib/fs.stat@1.1.3(transitive)
+ Added@types/glob@7.2.0(transitive)
+ Added@types/minimatch@5.1.2(transitive)
+ Added@types/node@22.12.0(transitive)
+ Addedarr-diff@4.0.0(transitive)
+ Addedarr-flatten@1.1.0(transitive)
+ Addedarr-union@3.1.0(transitive)
+ Addedarray-union@1.0.2(transitive)
+ Addedarray-uniq@1.0.3(transitive)
+ Addedarray-unique@0.3.2(transitive)
+ Addedassign-symbols@1.0.0(transitive)
+ Addedatob@2.1.2(transitive)
+ Addedbase@0.11.2(transitive)
+ Addedbraces@2.3.2(transitive)
+ Addedcache-base@1.0.1(transitive)
+ Addedcall-me-maybe@1.0.2(transitive)
+ Addedclass-utils@0.3.6(transitive)
+ Addedcollection-visit@1.0.0(transitive)
+ Addedcomponent-emitter@1.3.1(transitive)
+ Addedcopy-descriptor@0.1.1(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddecode-uri-component@0.2.2(transitive)
+ Addeddefine-property@0.2.51.0.02.0.2(transitive)
+ Addeddir-glob@2.2.2(transitive)
+ Addedexpand-brackets@2.1.4(transitive)
+ Addedextend-shallow@2.0.13.0.2(transitive)
+ Addedextglob@2.0.4(transitive)
+ Addedfast-glob@2.2.7(transitive)
+ Addedfill-range@4.0.0(transitive)
+ Addedfor-in@1.0.2(transitive)
+ Addedfragment-cache@0.2.1(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-value@2.0.6(transitive)
+ Addedglob-parent@3.1.0(transitive)
+ Addedglob-to-regexp@0.3.0(transitive)
+ Addedglobby@9.2.0(transitive)
+ Addedhas-value@0.3.11.0.0(transitive)
+ Addedhas-values@0.1.41.0.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedignore@4.0.6(transitive)
+ Addedis-accessor-descriptor@1.0.1(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-data-descriptor@1.0.1(transitive)
+ Addedis-descriptor@0.1.71.0.3(transitive)
+ Addedis-extendable@0.1.11.0.1(transitive)
+ Addedis-extglob@2.1.1(transitive)
+ Addedis-glob@3.1.04.0.3(transitive)
+ Addedis-number@3.0.0(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedis-windows@1.0.2(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@2.1.03.0.1(transitive)
+ Addedkind-of@3.2.24.0.06.0.3(transitive)
+ Addedmap-cache@0.2.2(transitive)
+ Addedmap-visit@1.0.0(transitive)
+ Addedmerge2@1.4.1(transitive)
+ Addedmicromatch@3.1.10(transitive)
+ Addedmixin-deep@1.3.2(transitive)
+ Addedms@2.0.0(transitive)
+ Addednanomatch@1.2.13(transitive)
+ Addedobject-copy@0.1.0(transitive)
+ Addedobject-visit@1.0.1(transitive)
+ Addedobject.pick@1.3.0(transitive)
+ Addedpascalcase@0.1.1(transitive)
+ Addedpath-dirname@1.0.2(transitive)
+ Addedpath-type@3.0.0(transitive)
+ Addedpify@3.0.04.0.1(transitive)
+ Addedposix-character-classes@0.1.1(transitive)
+ Addedregex-not@1.0.2(transitive)
+ Addedrepeat-element@1.1.4(transitive)
+ Addedrepeat-string@1.6.1(transitive)
+ Addedresolve-url@0.2.1(transitive)
+ Addedret@0.1.15(transitive)
+ Addedsafe-regex@1.1.0(transitive)
+ Addedset-value@2.0.1(transitive)
+ Addedslash@2.0.0(transitive)
+ Addedsnapdragon@0.8.2(transitive)
+ Addedsnapdragon-node@2.1.1(transitive)
+ Addedsnapdragon-util@3.0.1(transitive)
+ Addedsource-map@0.5.7(transitive)
+ Addedsource-map-resolve@0.5.3(transitive)
+ Addedsource-map-url@0.4.1(transitive)
+ Addedsplit-string@3.1.0(transitive)
+ Addedstatic-extend@0.1.2(transitive)
+ Addedto-object-path@0.3.0(transitive)
+ Addedto-regex@3.0.2(transitive)
+ Addedto-regex-range@2.1.1(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedunion-value@1.0.1(transitive)
+ Addedunset-value@1.0.0(transitive)
+ Addedurix@0.1.0(transitive)
+ Addeduse@3.1.1(transitive)