-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
1688276
commit e115f3a
Showing
8 changed files
with
220 additions
and
51 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,6 @@ | ||
exports.command = 'forms <command>'; | ||
exports.desc = 'Helpers for managing forms'; | ||
exports.builder = function(yargs) { | ||
return yargs.commandDir('forms_cmds'); | ||
}; | ||
exports.handler = function(argv) {}; |
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,26 @@ | ||
const chalk = require('chalk'); | ||
const utils = require('../../utils'); | ||
|
||
exports.command = 'add <key> <name>'; | ||
exports.desc = 'Adds a new form to config'; | ||
|
||
exports.builder = yargs => { | ||
yargs.option('file', { | ||
describe: 'Path to the local `statickit.json` file', | ||
default: 'statickit.json' | ||
}); | ||
}; | ||
|
||
exports.handler = function(args) { | ||
const config = utils.readConfig(args.file); | ||
const forms = config.forms || (config.forms = {}); | ||
const coloredKey = chalk.cyan(`\`${args.key}\``); | ||
|
||
if (forms[args.key]) { | ||
utils.logError(`${coloredKey} already exists`); | ||
} else { | ||
forms[args.key] = { name: args.name }; | ||
utils.writeConfig(args.file, config); | ||
utils.logSuccess(`${coloredKey} added`); | ||
} | ||
}; |
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 |
---|---|---|
@@ -1,20 +1,16 @@ | ||
const { formatLine } = require('../output'); | ||
const chalk = require('chalk'); | ||
const fs = require('fs'); | ||
|
||
const logSuccess = msg => { | ||
console.log(formatLine(chalk.green(msg))); | ||
}; | ||
const utils = require('../utils'); | ||
|
||
exports.command = 'init'; | ||
exports.desc = 'Create a statickit.json file in the current directory'; | ||
exports.desc = 'Creates a statickit.json file'; | ||
exports.builder = {}; | ||
exports.handler = function(args) { | ||
fs.writeFile('statickit.json', '{}', { flag: 'wx' }, function(err) { | ||
if (err) { | ||
logSuccess('statickit.json already exists'); | ||
utils.logSuccess('statickit.json already exists'); | ||
} else { | ||
logSuccess('statickit.json created'); | ||
utils.logSuccess('statickit.json created'); | ||
} | ||
}); | ||
}; |
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,66 @@ | ||
const chalk = require('chalk'); | ||
const fs = require('fs'); | ||
|
||
const withCaret = str => { | ||
return `${chalk.gray('>')} ${str}`; | ||
}; | ||
|
||
/** | ||
* Logs a green success message to stdout. | ||
* | ||
* @param {string} msg | ||
*/ | ||
const logSuccess = msg => { | ||
return console.log(withCaret(chalk.green(msg))); | ||
}; | ||
|
||
/** | ||
* Logs a subtle gray message to stdout. | ||
* | ||
* @param {string} msg | ||
*/ | ||
const logMeta = msg => { | ||
return console.log(withCaret(chalk.gray(msg))); | ||
}; | ||
|
||
/** | ||
* Logs a red error message to stderr. | ||
* | ||
* @param {string} msg | ||
*/ | ||
const logError = msg => { | ||
return console.error(withCaret(chalk.red(msg))); | ||
}; | ||
|
||
/** | ||
* Reads a config file and parses it's contents as JSON. | ||
* If the file does not exist, returns an empty object. | ||
* Throws an error if a parsing error occurs. | ||
* | ||
* @param {string} file | ||
*/ | ||
const readConfig = file => { | ||
try { | ||
return JSON.parse(fs.readFileSync(file, 'utf8')); | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return {}; | ||
} else { | ||
logError('statickit.json could not be parsed'); | ||
throw err; | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* Stringifys the given config object to pretty JSON and | ||
* writes it to the given file path. | ||
* | ||
* @param {string} file | ||
* @param {object} config | ||
*/ | ||
const writeConfig = (file, config) => { | ||
fs.writeFileSync(file, JSON.stringify(config, null, 2)); | ||
}; | ||
|
||
module.exports = { logSuccess, logMeta, logError, readConfig, writeConfig }; |
Oops, something went wrong.