Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
derrickreimer committed Nov 13, 2019
1 parent f55c1f4 commit ce89d5f
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 124 deletions.
24 changes: 12 additions & 12 deletions src/cmds/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const chalk = require('chalk');
const deploy = require('@statickit/deploy');
const ora = require('ora');
const version = require('../../package.json').version;
const utils = require('../utils');
const log = require('../log');
const messages = require('../messages');

exports.command = 'deploy';
Expand Down Expand Up @@ -36,10 +36,10 @@ exports.handler = async args => {
const userAgent = `@statickit/cli@${version}`;
const spinner = ora(chalk.gray('Deploying...'));

utils.preamble();
log.preamble();

if (!rawConfig) {
utils.logError('Configuration not provided');
log.error('Configuration not provided');
process.exitCode = 1;
return;
}
Expand All @@ -49,15 +49,15 @@ exports.handler = async args => {
try {
config = JSON.parse(rawConfig);
} catch (err) {
utils.logError('Configuration could not be parsed');
log.error('Configuration could not be parsed');
process.exitCode = 1;
return;
}

const key = deploy.getDeployKey(args);

if (!key) {
messages.logAuthRequired();
messages.authRequired();
process.exitCode = 1;
return;
}
Expand All @@ -76,32 +76,32 @@ exports.handler = async args => {

switch (response.status) {
case 200:
utils.logSuccess('Deployment succeeded');
utils.logMeta(`id: ${response.data.id}`);
log.success('Deployment succeeded');
log.meta(`id: ${response.data.id}`);
return;

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

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

default:
utils.logError('Deployment failed');
log.error('Deployment failed');
process.exitCode = 1;
return;
}
} catch (error) {
spinner.stop();
utils.logError('Deployment failed unexpectedly');
log.error('Deployment failed unexpectedly');
process.exitCode = 1;
throw error;
}
Expand Down
7 changes: 3 additions & 4 deletions src/cmds/forms_cmds/add.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const chalk = require('chalk');
const utils = require('../../utils');
const log = require('../../log');

exports.command = 'add <key> <name>';
exports.desc = 'Adds a new form to config';
Expand All @@ -14,13 +14,12 @@ exports.builder = yargs => {
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`);
log.error(`${log.variable(args.key)} already exists`);
} else {
forms[args.key] = { name: args.name };
utils.writeConfig(args.file, config);
utils.logSuccess(`${coloredKey} added`);
log.success(`${log.variable(args.key)} added`);
}
};
6 changes: 3 additions & 3 deletions src/cmds/init.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const fs = require('fs');
const utils = require('../utils');
const log = require('../log');

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');
log.success('statickit.json already exists');
} else {
utils.logSuccess('statickit.json created');
log.success('statickit.json created');
}
});
};
20 changes: 9 additions & 11 deletions src/cmds/secrets_cmds/add.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const deploy = require('@statickit/deploy');
const axios = require('axios');
const utils = require('../../utils');
const log = require('../../log');
const messages = require('../../messages');
const version = require('../../../package.json').version;

Expand Down Expand Up @@ -35,17 +35,15 @@ exports.handler = async args => {
const endpoint = args.endpoint || 'https://api.statickit.com';
const userAgent = `@statickit/cli@${version}`;

utils.preamble();
log.preamble();

if (!deployKey) {
messages.logAuthRequired();
messages.authRequired();
process.exitCode = 1;
return;
}

utils.logProgress(
`Adding ${utils.colorVariable(args.name)} to your secrets...`
);
log.progress(`Adding ${log.variable(args.name)} to your secrets...`);

try {
const response = await axios({
Expand All @@ -64,11 +62,11 @@ exports.handler = async args => {

switch (response.status) {
case 200:
utils.logSuccess(`${coloredName} added to secrets`);
log.success(`${coloredName} added to secrets`);
return;

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

Expand All @@ -78,19 +76,19 @@ exports.handler = async args => {
error.field == 'key' &&
error.message == 'has already been taken'
) {
utils.logError(
log.error(
'This secret already exists. Use `statickit secrets update` to update it.'
);
} else {
utils.logError(`${humanizeField(error.field)} ${error.message}`);
log.error(`${humanizeField(error.field)} ${error.message}`);
}
});

process.exitCode = 1;
return;
}
} catch (error) {
utils.logError('Request failed unexpectedly');
log.error('Request failed unexpectedly');
process.exitCode = 1;
throw error;
}
Expand Down
64 changes: 64 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const chalk = require('chalk');
const version = require('../package.json').version;

const withCaret = str => {
return `${chalk.gray('>')} ${str}`;
};

const withX = str => {
return `${chalk.red.bold('✕')} ${str}`;
};

/**
* Logs a green success message to stdout.
*
* @param {string} msg
*/
const success = msg => {
console.log(withCaret(chalk.green(msg)));
};

/**
* Logs a progress message to stdout.
*
* @param {string} msg
*/
const progress = msg => {
console.log(withCaret(chalk.whiteBright(msg)));
};

/**
* Logs a subtle gray message to stdout.
*
* @param {string} msg
*/
const meta = msg => {
console.log(withCaret(chalk.gray(msg)));
};

/**
* Logs a red error message to stderr.
*
* @param {string} msg
*/
const error = msg => {
console.error(withX(chalk.red.bold(msg)));
};

/**
* Logs the CLI preamble message.
*/
const preamble = () => {
meta(`StaticKit CLI v${version}`);
};

/**
* Colorizes a variable for display.
*
* @param {string} val
*/
const variable = val => {
return chalk.cyan.bold(`\`${val}\``);
};

module.exports = { success, progress, meta, error, preamble, variable };
50 changes: 25 additions & 25 deletions src/messages.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
const chalk = require('chalk');
const utils = require('./utils');
const log = require('./log');
const { stripIndent } = require('common-tags');

// prettier-ignore
const authRequired = stripIndent`
Your deploy key can be found under "Settings" in the UI.
There are couple ways to use your key:
const authRequired = () => {
log.error('Deploy key is required');
console.error('');

- Use the ${utils.colorVariable('-k')} flag, or
- Set the ${utils.colorVariable('STATICKIT_DEPLOY_KEY')} env variable
// prettier-ignore
console.error(stripIndent`
Your deploy key can be found under "Settings" in the UI.
There are couple ways to use your key:
${chalk.yellow.bold('-- Examples -----------------------------------------------')}
- Use the ${log.variable('-k')} flag, or
- Set the ${log.variable('STATICKIT_DEPLOY_KEY')} env variable
The inline method looks like this:
${chalk.yellow.bold('-- Examples -----------------------------------------------')}
${chalk.gray('$')} statickit deploy ${chalk.cyan('-k')} c4cf8a3b6cc15b9ea0817e4fa00cb036
For convenience, you can add it to a ${utils.colorVariable('.env')} file.
That way, you don't have to copy/paste it every time
you run a command:
The inline method looks like this:
${chalk.gray('$')} echo "${chalk.gray('STATICKIT_DEPLOY_KEY=c4cf8a3b6cc...')}" >> .env
${chalk.gray('$')} statickit deploy
Just be sure to add ${utils.colorVariable('.env')} to your ${utils.colorVariable('.gitignore')} file,
so your deploy key does not end up in version control.
`;
${chalk.gray('$')} statickit deploy ${chalk.cyan('-k')} c4cf8a3b6cc15b9ea0817e4fa00cb036
For convenience, you can add it to a ${log.variable('.env')} file.
That way, you don't have to copy/paste it every time
you run a command:
${chalk.gray('$')} echo "${chalk.gray('STATICKIT_DEPLOY_KEY=c4cf8a3b6cc...')}" >> .env
${chalk.gray('$')} statickit deploy
Just be sure to add ${log.variable('.env')} to your ${log.variable('.gitignore')} file,
so your deploy key does not end up in version control.
`);

const logAuthRequired = () => {
utils.logError('Deploy key is required');
console.error('');
console.error(authRequired);
console.error('');
};

module.exports = { authRequired, logAuthRequired };
module.exports = { authRequired };
68 changes: 0 additions & 68 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,4 @@
const chalk = require('chalk');
const fs = require('fs');
const version = require('../package.json').version;

const withCaret = str => {
return `${chalk.gray('>')} ${str}`;
};

const withX = str => {
return `${chalk.red.bold('✕')} ${str}`;
};

/**
* Logs a green success message to stdout.
*
* @param {string} msg
*/
const logSuccess = msg => {
return console.log(withCaret(chalk.green(msg)));
};

/**
* Logs a progress message to stdout.
*
* @param {string} msg
*/
const logProgress = msg => {
return console.log(withCaret(chalk.whiteBright(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(withX(chalk.red.bold(msg)));
};

/**
* Logs the CLI preamble message.
*/
const preamble = () => {
return logMeta(`StaticKit CLI v${version}`);
};

/**
* Colorizes a variable for display.
*
* @param {string} val
*/
const colorVariable = val => {
return chalk.cyan.bold(`\`${val}\``);
};

/**
* Reads a config file and parses it's contents as JSON.
Expand Down Expand Up @@ -94,12 +32,6 @@ const writeConfig = (file, config) => {
};

module.exports = {
logSuccess,
logProgress,
logMeta,
logError,
preamble,
colorVariable,
readConfig,
writeConfig
};
Loading

0 comments on commit ce89d5f

Please sign in to comment.