Skip to content

Commit

Permalink
Implement forms add command
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Oct 25, 2019
1 parent 1688276 commit e115f3a
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 51 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"yargs": "^14.0.0"
},
"devDependencies": {
"common-tags": "^1.8.0",
"execa": "^2.0.4",
"husky": "^3.0.9",
"jest": "^24.9.0",
Expand Down
44 changes: 19 additions & 25 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ const chalk = require('chalk');
const deploy = require('@statickit/deploy');
const ora = require('ora');
const version = require('../../package.json').version;
const { formatLine, formatError } = require('../output');

const setErrorExit = () => {
process.exitCode = 1;
};
const utils = require('../utils');

exports.command = 'deploy';

exports.describe = 'Performs a deployment';
exports.describe = 'Deploys statickit.json';

exports.builder = yargs => {
yargs.option('config', {
Expand Down Expand Up @@ -41,8 +37,8 @@ exports.handler = async args => {
const spinner = ora(chalk.gray('Deploying...'));

if (!rawConfig) {
console.error(formatError('Configuration not provided'));
setErrorExit();
utils.logError('Configuration not provided');
process.exitCode = 1;
return;
}

Expand All @@ -51,16 +47,16 @@ exports.handler = async args => {
try {
config = JSON.parse(rawConfig);
} catch (err) {
console.error(formatError('Configuration could not be parsed'));
setErrorExit();
utils.logError('Configuration could not be parsed');
process.exitCode = 1;
return;
}

const key = deploy.getDeployKey(args);

if (!key) {
console.error(formatError('Deploy key not found'));
setErrorExit();
utils.logError('Deploy key not found');
process.exitCode = 1;
return;
}

Expand All @@ -78,35 +74,33 @@ exports.handler = async args => {

switch (response.status) {
case 200:
console.log(formatLine(chalk.green('Deployment succeeded')));
console.log(formatLine(chalk.gray('id: ' + response.data.id)));
utils.logSuccess('Deployment succeeded');
utils.logMeta(`id: ${response.data.id}`);
return;

case 401:
console.error(formatError('Deploy key is not valid'));
setErrorExit();
utils.logError('Deploy key is not valid');
process.exitCode = 1;
return;

case 422:
console.error(
formatError('Deployment failed due to configuration errors')
);
utils.logError('Deployment failed due to configuration errors');
console.log('');
console.table(response.data.errors);
console.log('');
console.log(formatLine(chalk.gray('id: ' + response.data.id)));
setErrorExit();
utils.logMeta(`id: ${response.data.id}`);
process.exitCode = 1;
return;

default:
console.error(formatError('Deployment failed'));
setErrorExit();
utils.logError('Deployment failed');
process.exitCode = 1;
return;
}
} catch (error) {
spinner.stop();
console.error(formatError('Deployment failed unexpectedly'));
setErrorExit();
utils.logError('Deployment failed unexpectedly');
process.exitCode = 1;
throw error;
}
};
6 changes: 6 additions & 0 deletions src/cmds/forms.js
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) {};
26 changes: 26 additions & 0 deletions src/cmds/forms_cmds/add.js
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`);
}
};
12 changes: 4 additions & 8 deletions src/cmds/init.js
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');
}
});
};
66 changes: 66 additions & 0 deletions src/utils.js
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 };
Loading

0 comments on commit e115f3a

Please sign in to comment.