我正在艰难地完成这项艰巨的任务。我有一个文件,我需要修改后构建,这是一个使用Angular Service Worker的PWA包的一部分。作为修改的结果,我需要重新计算散列并更新ngsw.json文件中的条目,该文件的内容如下所示。
"hashTable": {
"/1.a634d7bd57cc0b60b07a.js": "f67c68837f048be737dcb1359072fc40c1f93144",
"/10.b18571adf198206cc401.js": "c59d8f124964b73456f22ed3a51efec0493ce7c5",
"/100.625f7b4770945172db3e.js": "da62af393973a8eb002874406e8a1dd828faecaf",
"/main.5be263c044e031156b6b.js": "5bfa4ec8a86ee2ab4a5f2f999b0742ac4a5ddbc7"
}我知道需要更新散列的文件的文件名,我有这个函数
let hashsum = require("gulp-hashsum");
function getHash() {
gulp.src(["www/main*.js"]).pipe(hashsum({
stream: true,
json: true
}
)).pipe(gulp.dest(
// How to replace ngsw.json 'hashTable' entry with this response? ));
}我不是很熟悉吞咽,任何帮助都将不胜感激。
发布于 2019-01-14 15:33:20
您可以编写一个从hashsum获取结果的插件,然后简单地使用fs来修改您的json。
下面是一个简单的实现:
const fs = require('fs')
const path = require('path')
const through = require('through2')
const hashsum = require('gulp-hashsum')
const modifyJson = ({ fileName, src, property }) => through.obj((file, _, cb) => {
const { name } = path.parse(file.path)
if (name !== fileName) return cb(null, file)
const pathToJson = path.resolve(__dirname, src)
if (!fs.existsSync(pathToJson)) {
return cb(new Error(`${src} doesn't exist.`), file)
}
const json = JSON.parse(fs.readFileSync(pathToJson, 'utf8'))
const content = JSON.parse(file.contents)
if (typeof json[property] === 'undefined') console.warn(`${src} doesn't has any property named '${property}' A new one will be created.`)
json[property] = content
fs.writeFileSync(pathToJson, JSON.stringify(json))
return cb(null, file)
})用法:
exports.modifyJson = () =>
src(['app/**/*.js'])
.pipe(hashsum({
stream: true,
json: true,
}))
.pipe(modifyJson({
fileName: 'SHA1SUMS',
src: './test.json',
property: 'hashTable',
}))我把这段代码放在了gist上。
https://stackoverflow.com/questions/54154102
复制相似问题