Skip to content

Commit

Permalink
Merge pull request #1 from unstacked/form-helpers
Browse files Browse the repository at this point in the history
Form helpers
  • Loading branch information
derrickreimer committed Oct 25, 2019
2 parents a7f5645 + 74f5ff6 commit 1c8a967
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 186 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
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.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ The command line interface for [StaticKit](https://statickit.com).

## Installation

Run the following to install in your project:
Run the following to install on your machine:

```bash
npm install --save-dev @statickit/cli
```

Then, run the `statickit` command:

```bash
npx statickit deploy
npm i -g @statickit/cli
```

## Releasing
Expand Down
2 changes: 1 addition & 1 deletion bin/statickit
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');
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
},
"license": "MIT",
"author": "Derrick Reimer",
"main": "index.js",
"bin": {
"statickit": "bin/statickit"
},
Expand Down Expand Up @@ -47,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
6 changes: 6 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require('dotenv').config();

require('yargs')
.commandDir('cmds')
.demandCommand()
.help().argv;
46 changes: 20 additions & 26 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ const chalk = require('chalk');
const deploy = require('@statickit/deploy');
const ora = require('ora');
const version = require('../../package.json').version;
const utils = require('../utils');

const setErrorExit = () => {
process.exitCode = 1;
};

exports.command = ['deploy', '$0'];

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

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

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

Expand All @@ -50,16 +46,16 @@ exports.handler = async args => {
try {
config = JSON.parse(rawConfig);
} catch (err) {
console.error(chalk.bold.red('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(chalk.bold.red('Deploy key not found'));
setErrorExit();
utils.logError('Deploy key not found');
process.exitCode = 1;
return;
}

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

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

case 401:
console.error(`--> ${chalk.red('Deploy key is not valid')}`);
setErrorExit();
utils.logError('Deploy key is not valid');
process.exitCode = 1;
return;

case 422:
console.error(
`--> ${chalk.red('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(`${chalk.gray('id: ' + response.data.id)}`);
setErrorExit();
utils.logMeta(`id: ${response.data.id}`);
process.exitCode = 1;
return;

default:
console.error(`--> ${chalk.red('Deployment failed')}`);
setErrorExit();
utils.logError('Deployment failed');
process.exitCode = 1;
return;
}
} catch (error) {
spinner.stop();
console.error(`--> ${chalk.red('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 = yargs => {
return yargs.commandDir('forms_cmds');
};
exports.handler = _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 = 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`);
}
};
15 changes: 15 additions & 0 deletions src/cmds/init.js
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');
}
});
};
3 changes: 0 additions & 3 deletions src/index.js

This file was deleted.

7 changes: 0 additions & 7 deletions src/parser.js

This file was deleted.

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 1c8a967

Please sign in to comment.