1
1
mirror of https://github.com/primer/css.git synced 2024-11-28 13:12:16 +03:00

nix set-json utility

This commit is contained in:
Shawn Allen 2017-09-08 16:45:50 -07:00
parent 6c8b02ec24
commit b58ab1019a
2 changed files with 0 additions and 91 deletions

View File

@ -17,7 +17,6 @@
"devDependencies": {
"ava": "^0.21.0",
"commit-status": "^4.1.0",
"dot-component": "^0.1.1",
"glob": "^7.1.2",
"lerna": "^2.0.0",
"npm-run-all": "^4.0.2",

View File

@ -1,90 +0,0 @@
#!/usr/bin/env node
const dot = require("dot-component")
const fse = require("fs-extra")
const yargs = require("yargs")
.option("set", {
describe: "Set key 'x' in the form --set.x=foo",
alias: "s",
})
.option("copy", {
describe: "Copy key 'x' from file.json in the form: --copy=file.json:x",
alias: "c",
})
.option("del", {
describe: "Delete the provided key",
alias: "d",
})
const opts = yargs.argv
const args = opts._
const ops = []
if (opts.set && typeof opts.set === "object") {
Object.entries(opts.set).forEach(([key, val]) => {
ops.push(json => {
dot.set(json, key, val)
return json
})
})
}
if (opts.copy) {
const [file, key] = opts.copy.split(":")
if (file && key) {
console.warn("copying '%s' from %s...", key, file)
ops.push(json => {
return fse.readFile(file, UTF)
.then(JSON.parse)
.then(data => dot.get(data, key))
.then(data => dot.set(json, key, data))
.then(() => json)
})
} else {
console.error(
"malformed --copy directive; expected 'file:key', got '%s'",
opts.copy
)
process.exit(1)
}
}
if (opts.del) {
const dels = Array.isArray(opts.del) ? opts.del : [opts.del]
dels.forEach(key => {
ops.push(json => {
dot.set(json, key, undefined)
return json
})
})
}
const UTF = "utf8"
const tasks = args.map(filename => {
return fse.readFile(filename, UTF)
.then(JSON.parse)
.then(json => {
return Promise.all(
ops.map(op => op(json) || json)
).then(() => json)
})
.then(json => JSON.stringify(json, null, " "))
.then(str => fse.writeFile(filename, `${str}\n`, UTF))
.then(() => filename)
})
Promise.all(tasks)
.catch(error => {
console.log(error)
process.exit(1)
})
.then(files => {
if (files.length > 1) {
console.warn("wrote %d files", files.length)
} else {
console.warn("wrote %s:", files[0])
return fse.readFile(files[0], UTF)
.then(str => console.warn(str))
}
})
.then(() => process.exit(0))