Created
January 14, 2019 07:28
-
-
Save d4rekanguok/294724b2ded30cb1373fa65c7c3a66c8 to your computer and use it in GitHub Desktop.
Modify json with input from gulp-hashsum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { src } = require('gulp') | |
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', | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment