Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Sep 18, 2019
0 parents commit 0ea5263
Show file tree
Hide file tree
Showing 7 changed files with 504 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
8 changes: 8 additions & 0 deletions .prettierrc.toml
@@ -0,0 +1,8 @@
printWidth = 80
trailingComma = "es5"
semi = false
singleQuote = false
useTabs = true
bracketSpacing = false
jsxBracketSameLine = true
arrowParens = "avoid"
3 changes: 3 additions & 0 deletions index.js
@@ -0,0 +1,3 @@
#!/usr/bin/env node
require = require("esm")(module)
module.exports = require("./main.js")
3 changes: 3 additions & 0 deletions jsconfig.json
@@ -0,0 +1,3 @@
{
"lib": "es2019"
}
73 changes: 73 additions & 0 deletions main.js
@@ -0,0 +1,73 @@
import DataClient from "@financial-times/origami-repo-data-client"

let prototypeKeys = Reflect.ownKeys(DataClient.prototype)
let commandMatcher = /^(create|delete|get|list)([A-Za-z]+)$/
let commandTree = prototypeKeys.reduce(
(commandTree, key) => {
let match = commandMatcher.exec(String(key))
if (!match) {
return commandTree
}
let [, type, target] = match
target = target.replace(/(.)/, first => first.toLowerCase())
target = target.replace(
/([a-z])([A-Z])/g,
(_, lower, upper) => lower + "-" + upper.toLowerCase()
)
commandTree[type][target] = DataClient.prototype[key]
return commandTree
},
{
create: {},
delete: {},
get: {},
list: {},
}
)

function printHelp(commandTree) {
process.stdout.write("commands:\n\n")
for (let type in commandTree) {
process.stdout.write(type)
process.stdout.write("\t")
let first = true
process.stdout.write("<")
for (let target in commandTree[type]) {
if (first) {
first = false
} else {
process.stdout.write("|")
}
process.stdout.write(target)
}
process.stdout.write(">")
process.stdout.write("\n")
}
}

let tryParse = arg => {
try {
return eval(`(${arg})`)
} catch {
return arg
}
}

void (async function() {
let [, , type, target] = process.argv
let targetTree = commandTree[type]
let command = targetTree && targetTree[target]
if (!command) {
printHelp(commandTree)
process.exit(1)
}
let client = new DataClient({
apiKey: process.env.ORIGAMI_REPO_DATA_KEY,
apiSecret: process.env.ORIGAMI_REPO_DATA_SECRET,
})
let result = await command
.apply(client, process.argv.slice(4).map(tryParse))
.catch(() => printHelp(commandTree))
let output = typeof result == "string" ? result : JSON.stringify(result)
process.stdout.write(output)
})()

0 comments on commit 0ea5263

Please sign in to comment.