Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0ea5263
Showing
7 changed files
with
504 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
printWidth = 80 | ||
trailingComma = "es5" | ||
semi = false | ||
singleQuote = false | ||
useTabs = true | ||
bracketSpacing = false | ||
jsxBracketSameLine = true | ||
arrowParens = "avoid" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
require = require("esm")(module) | ||
module.exports = require("./main.js") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"lib": "es2019" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
})() |
Oops, something went wrong.