From 6159263d8f7229c032fc49de4086557ac4de99d3 Mon Sep 17 00:00:00 2001 From: Leo Lamprecht Date: Fri, 28 Oct 2016 13:34:40 +0200 Subject: [PATCH] Set package.json on the fly --- lib/hash.js | 37 +++++++++++++++++++++++++++++-------- lib/index.js | 2 +- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/hash.js b/lib/hash.js index 940d7db..2db21e8 100644 --- a/lib/hash.js +++ b/lib/hash.js @@ -1,20 +1,41 @@ // Native import {createHash} from 'crypto' +import path from 'path' // Packages import {readFile} from 'fs-promise' -/** - * Computes hashes for the contents of each file given. - * - * @param {Array} of {String} full paths - * @return {Map} - */ +const listPackage = { + name: 'static', + version: '0.0.0', + scripts: { + start: 'list ./content' + }, + dependencies: { + list: 'latest' + } +} + + /** + * Computes hashes for the contents of each file given. + * + * @param {Array} of {String} full paths + * @return {Map} + */ -export default async function hashes(files) { +export default async function hashes(files, isStatic) { const map = new Map() await Promise.all(files.map(async name => { - const data = await readFile(name) + const filename = path.basename(name) + let data + + if (isStatic && filename === 'package.json') { + const packageString = JSON.stringify(listPackage, null, 2) + data = Buffer.from(packageString) + } else { + data = await readFile(name) + } + const h = hash(data) const entry = map.get(h) if (entry) { diff --git a/lib/index.js b/lib/index.js index 4128fa7..c2f55c0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -102,7 +102,7 @@ export default class Now extends EventEmitter { console.time('> [debug] Computing hashes') } - const hashes = await hash(files) + const hashes = await hash(files, isStatic) if (this._debug) { console.timeEnd('> [debug] Computing hashes')