Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@d4rekanguok
Created January 14, 2019 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d4rekanguok/294724b2ded30cb1373fa65c7c3a66c8 to your computer and use it in GitHub Desktop.
Save d4rekanguok/294724b2ded30cb1373fa65c7c3a66c8 to your computer and use it in GitHub Desktop.
Modify json with input from gulp-hashsum
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