-
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.
Merge pull request #1 from unstacked/form-helpers
Form helpers
- Loading branch information
Showing
15 changed files
with
396 additions
and
186 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,9 @@ | ||
# Changelog | ||
|
||
## 0.2.0 | ||
|
||
- Add `init` and `forms add` commands. | ||
|
||
## 0.1.0 | ||
|
||
- Initial release. |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
#!/usr/bin/env node | ||
require('../src/index.js'); | ||
require('../src/cli.js'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require('dotenv').config(); | ||
|
||
require('yargs') | ||
.commandDir('cmds') | ||
.demandCommand() | ||
.help().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
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 = yargs => { | ||
return yargs.commandDir('forms_cmds'); | ||
}; | ||
exports.handler = _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 = 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const fs = require('fs'); | ||
const utils = require('../utils'); | ||
|
||
exports.command = 'init'; | ||
exports.desc = 'Creates a statickit.json file'; | ||
exports.builder = {}; | ||
exports.handler = _args => { | ||
fs.writeFile('statickit.json', '{}', { flag: 'wx' }, function(err) { | ||
if (err) { | ||
utils.logSuccess('statickit.json already exists'); | ||
} else { | ||
utils.logSuccess('statickit.json created'); | ||
} | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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.