New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@sparkbox/cachebust

Package Overview
Dependencies
Maintainers
9
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sparkbox/cachebust - npm Package Compare versions

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 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc