Last active
January 17, 2018 17:43
-
-
Save jaredallard/44e04c20f4b35d6ba8868de38e255132 to your computer and use it in GitHub Desktop.
Generate Forge Inputs easily: https://asciinema.org/a/157554
This file contains hidden or 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
| #!/usr/bin/env node | |
| /** | |
| * Generates a input/output list without writing JSON | |
| * | |
| * INSTALL: yarn add colors csv lodash | |
| * | |
| * @author Jared Allard <[email protected]> | |
| * @license MIT | |
| */ | |
| 'use strict' | |
| const readline = require('readline') | |
| const colors = require('colors') | |
| const csv = require('csv') | |
| const _ = require('lodash') | |
| // quick intro header | |
| console.log(colors.cyan('Format is:'), 'Name,type') | |
| console.log(colors.cyan('Navigate stack: enter .GROUP,[Collection: boolean] | exit ..')) | |
| console.log(colors.red('^D to end.')) | |
| const inputs = [] | |
| const header_map = {} | |
| let location = null; | |
| let ui_loc = null; | |
| const rl = readline.createInterface({ | |
| input: process.stdin, | |
| output: process.stdout | |
| }) | |
| /** | |
| * Quick promise wrapper for csv | |
| * @param {String} line CSV line | |
| * @return {Array} Parsed csv | |
| */ | |
| const parse = line => { | |
| return new Promise((resolv, reject) => { | |
| csv.parse(line, (err, data) => { | |
| if(err) return reject(err) | |
| return resolv(data) | |
| }) | |
| }) | |
| } | |
| /** | |
| * Quick promise wrapper for readline | |
| * @param {String} prompt Prompt | |
| * @return {String} Response to the prompt | |
| */ | |
| const question = prompt => { | |
| return new Promise(resolv => { | |
| rl.question(prompt, data => { | |
| return resolv(data) | |
| }) | |
| }) | |
| } | |
| const expand = string => { | |
| return _.get(inputs, string) | |
| } | |
| const feedback = async () => { | |
| const line = await question(`${ui_loc|| ""}> `) | |
| const firstChar = line.charAt(0) | |
| // Move back | |
| if(line === '..') { | |
| const split_loc = location.split('.') | |
| split_loc.pop() | |
| split_loc.pop() | |
| location = split_loc.join('.') | |
| const split_ui_loc = ui_loc.split('.') | |
| split_ui_loc.pop() | |
| ui_loc = split_ui_loc.join('.') | |
| console.log(location, ui_loc) | |
| return | |
| } | |
| // JSON Traversel | |
| if(firstChar === '.') { | |
| const data = await parse(line.substr(1)) | |
| const header = data[0][0] | |
| const collection = data[0][1] | |
| let pos = expand(location) || inputs | |
| const headerObject = { | |
| name: header, | |
| attributes: [] | |
| } | |
| // support generating collection objects | |
| if(collection) headerObject.collection = true | |
| ui_loc = `${ui_loc ? `${ui_loc}.` : ''}${header}` | |
| // track "user-friendly" mapping | |
| header_map[ui_loc] = pos.push(headerObject) | |
| // lower by one since .push returns the length, prevents off by one | |
| header_map[ui_loc]-- | |
| const newPos = header_map[ui_loc] | |
| location = `${location ? `${location}.` : ''}${newPos}.attributes` | |
| console.log(`'${location}' '${ui_loc}'`) | |
| return | |
| } | |
| // parse the csv | |
| const data = await parse(line) | |
| const name = data[0][0] | |
| const type = data[0][1] || 'string' | |
| let pos = _.get(inputs, location) || inputs | |
| pos.push({ | |
| name: name, | |
| type: type | |
| }) | |
| } | |
| // Generate the JSON | |
| rl.on('close', () => { | |
| console.log() | |
| console.log(colors.green('Output')) | |
| console.log(JSON.stringify(inputs, 0, 2)) | |
| }) | |
| const init = async () => { | |
| await feedback() | |
| return init() | |
| } | |
| init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment