From eb4da80ede78e6e581c5363ae8fb89559801db1a Mon Sep 17 00:00:00 2001 From: Matt Stypa Date: Mon, 24 Sep 2018 07:55:24 -0500 Subject: [PATCH] Code style updates --- __tests__/cli.test.js | 2 +- src/cli.js | 4 ++-- src/cli/commands/build.js | 50 +++++++++++++++++++++------------------ src/cli/commands/help.js | 49 ++++++++++++++++++++------------------ src/cli/commands/init.js | 22 +++++++++-------- src/cli/constants.js | 8 +++---- src/cli/emoji.js | 12 ++++------ src/cli/main.js | 12 ++++++---- src/cli/utils.js | 2 +- 9 files changed, 84 insertions(+), 77 deletions(-) diff --git a/__tests__/cli.test.js b/__tests__/cli.test.js index c3aec78c7..5dcbcf69a 100644 --- a/__tests__/cli.test.js +++ b/__tests__/cli.test.js @@ -1,7 +1,7 @@ import path from 'path' import cli from '../src/cli/main' -import constants from '../src/cli/constants' +import * as constants from '../src/cli/constants' import * as utils from '../src/cli/utils' describe('cli', () => { diff --git a/src/cli.js b/src/cli.js index 24fb66824..312ff6c26 100755 --- a/src/cli.js +++ b/src/cli.js @@ -1,6 +1,6 @@ #!/usr/bin/env node import main from './cli/main' -import { die } from './cli/utils' +import * as utils from './cli/utils' -main(process.argv.slice(2)).catch(error => die(error.stack)) +main(process.argv.slice(2)).catch(error => utils.die(error.stack)) diff --git a/src/cli/commands/build.js b/src/cli/commands/build.js index 7b97d5ec4..ab3939854 100644 --- a/src/cli/commands/build.js +++ b/src/cli/commands/build.js @@ -4,10 +4,11 @@ import chalk from 'chalk' import postcss from 'postcss' import prettyHrtime from 'pretty-hrtime' -import commands from '.' -import emoji from '../emoji' import tailwind from '../..' -import { die, error, exists, footer, header, log, readFile, writeFile } from '../utils' + +import commands from '.' +import * as emoji from '../emoji' +import * as utils from '../utils' export const usage = 'build [options]' export const description = 'Compiles Tailwind CSS file.' @@ -34,9 +35,9 @@ export const optionMap = { * @param {...string} [msgs] */ function stop(...msgs) { - header() - error(...msgs) - die() + utils.header() + utils.error(...msgs) + utils.die() } /** @@ -45,10 +46,10 @@ function stop(...msgs) { * @param {...string} [msgs] */ function stopWithHelp(...msgs) { - header() - error(...msgs) + utils.header() + utils.error(...msgs) commands.help.forCommand(commands.build) - die() + utils.die() } /** @@ -60,7 +61,7 @@ function stopWithHelp(...msgs) { * @return {Promise} */ function build(inputFile, configFile, outputFile) { - const css = readFile(inputFile) + const css = utils.readFile(inputFile) return new Promise((resolve, reject) => { postcss([tailwind(configFile), autoprefixer]) @@ -95,20 +96,20 @@ function buildToStdout(inputFile, configFile, outputFile) { * @return {Promise} */ function buildToFile(inputFile, configFile, outputFile, startTime) { - header() - log() - log(emoji.go, 'Building...', chalk.bold.cyan(inputFile)) + utils.header() + utils.log() + utils.log(emoji.go, 'Building...', chalk.bold.cyan(inputFile)) return build(inputFile, configFile, outputFile).then(result => { - writeFile(outputFile, result.css) + utils.writeFile(outputFile, result.css) const prettyTime = prettyHrtime(process.hrtime(startTime)) - log() - log(emoji.yes, 'Finished in', chalk.bold.magenta(prettyTime)) - log(emoji.pack, 'Size:', chalk.bold.magenta(bytes(result.css.length))) - log(emoji.disk, 'Saved to', chalk.bold.cyan(outputFile)) - footer() + utils.log() + utils.log(emoji.yes, 'Finished in', chalk.bold.magenta(prettyTime)) + utils.log(emoji.pack, 'Size:', chalk.bold.magenta(bytes(result.css.length))) + utils.log(emoji.disk, 'Saved to', chalk.bold.cyan(outputFile)) + utils.footer() }) } @@ -127,13 +128,16 @@ export function run(cliParams, cliOptions) { const outputFile = cliOptions.output && cliOptions.output[0] !inputFile && stopWithHelp('CSS file is required.') - !exists(inputFile) && stop(chalk.bold.magenta(inputFile), 'does not exist.') - configFile && !exists(configFile) && stop(chalk.bold.magenta(configFile), 'does not exist.') + !utils.exists(inputFile) && stop(chalk.bold.magenta(inputFile), 'does not exist.') - const promise = outputFile + configFile && + !utils.exists(configFile) && + stop(chalk.bold.magenta(configFile), 'does not exist.') + + const buildPromise = outputFile ? buildToFile(inputFile, configFile, outputFile, startTime) : buildToStdout(inputFile, configFile, outputFile) - promise.then(resolve).catch(reject) + buildPromise.then(resolve).catch(reject) }) } diff --git a/src/cli/commands/help.js b/src/cli/commands/help.js index 35642756f..a08031456 100644 --- a/src/cli/commands/help.js +++ b/src/cli/commands/help.js @@ -2,25 +2,27 @@ import chalk from 'chalk' import { forEach, map, padEnd } from 'lodash' import commands from '.' -import constants from '../constants' -import { die, error, footer, header, log } from '../utils' +import * as constants from '../constants' +import * as utils from '../utils' export const usage = 'help [command]' export const description = 'More information about the command.' +const PADDING_SIZE = 3 + /** * Prints general help. */ export function forApp() { - const pad = Math.max(...map(commands, 'usage.length')) + 3 + const pad = Math.max(...map(commands, 'usage.length')) + PADDING_SIZE - log() - log('Usage:') - log(' ', chalk.bold(constants.cli + ' [options]')) - log() - log('Commands:') + utils.log() + utils.log('Usage:') + utils.log(' ', chalk.bold(constants.cli + ' [options]')) + utils.log() + utils.log('Commands:') forEach(commands, command => { - log(' ', chalk.bold(padEnd(command.usage, pad)), command.description) + utils.log(' ', chalk.bold(padEnd(command.usage, pad)), command.description) }) } @@ -30,20 +32,20 @@ export function forApp() { * @param {object} command */ export function forCommand(command) { - log() - log('Usage:') - log(' ', chalk.bold(constants.cli, command.usage)) - log() - log('Description:') - log(' ', chalk.bold(command.description)) + utils.log() + utils.log('Usage:') + utils.log(' ', chalk.bold(constants.cli, command.usage)) + utils.log() + utils.log('Description:') + utils.log(' ', chalk.bold(command.description)) if (command.options) { - const pad = Math.max(...map(command.options, 'usage.length')) + 3 + const pad = Math.max(...map(command.options, 'usage.length')) + PADDING_SIZE - log() - log('Options:') + utils.log() + utils.log('Options:') forEach(command.options, option => { - log(' ', chalk.bold(padEnd(option.usage, pad)), option.description) + utils.log(' ', chalk.bold(padEnd(option.usage, pad)), option.description) }) } } @@ -54,9 +56,9 @@ export function forCommand(command) { * @param {string} commandName */ export function invalidCommand(commandName) { - error('Invalid command:', chalk.bold.magenta(commandName)) + utils.error('Invalid command:', chalk.bold.magenta(commandName)) forApp() - die() + utils.die() } /** @@ -67,7 +69,7 @@ export function invalidCommand(commandName) { */ export function run(cliParams) { return new Promise(resolve => { - header() + utils.header() const commandName = cliParams[0] const command = commands[commandName] @@ -76,7 +78,8 @@ export function run(cliParams) { commandName && command && forCommand(command) commandName && !command && invalidCommand(commandName) - footer() + utils.footer() + resolve() }) } diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index 719198d6a..1d127240c 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,8 +1,8 @@ import chalk from 'chalk' -import constants from '../constants' -import emoji from '../emoji' -import { die, exists, footer, header, log, readFile, writeFile } from '../utils' +import * as constants from '../constants' +import * as emoji from '../emoji' +import * as utils from '../utils' export const usage = 'init [file]' export const description = @@ -16,22 +16,24 @@ export const description = */ export function run(cliParams) { return new Promise(resolve => { - header() + utils.header() const file = cliParams[0] || constants.defaultConfigFile - exists(file) && die(chalk.bold.magenta(file), 'already exists.') + utils.exists(file) && utils.die(chalk.bold.magenta(file), 'already exists.') - const stub = readFile(constants.configStubFile) + const stub = utils + .readFile(constants.configStubFile) .replace('// let defaultConfig', 'let defaultConfig') .replace("require('./plugins/container')", "require('tailwindcss/plugins/container')") - writeFile(file, stub) + utils.writeFile(file, stub) - log() - log(emoji.yes, 'Created Tailwind config file:', chalk.bold.magenta(file)) + utils.log() + utils.log(emoji.yes, 'Created Tailwind config file:', chalk.bold.magenta(file)) + + utils.footer() - footer() resolve() }) } diff --git a/src/cli/constants.js b/src/cli/constants.js index 3d15c9340..3e9fdb42d 100644 --- a/src/cli/constants.js +++ b/src/cli/constants.js @@ -1,7 +1,5 @@ import path from 'path' -export default { - cli: 'tailwind', - defaultConfigFile: 'tailwind.js', - configStubFile: path.resolve(__dirname, '../../defaultConfig.stub.js'), -} +export const cli = 'tailwind' +export const defaultConfigFile = 'tailwind.js' +export const configStubFile = path.resolve(__dirname, '../../defaultConfig.stub.js') diff --git a/src/cli/emoji.js b/src/cli/emoji.js index f6c8b028c..586557066 100644 --- a/src/cli/emoji.js +++ b/src/cli/emoji.js @@ -1,9 +1,7 @@ import { get } from 'node-emoji' -export default { - yes: get('white_check_mark'), - no: get('no_entry_sign'), - go: get('rocket'), - pack: get('package'), - disk: get('floppy_disk'), -} +export const yes = get('white_check_mark') +export const no = get('no_entry_sign') +export const go = get('rocket') +export const pack = get('package') +export const disk = get('floppy_disk') diff --git a/src/cli/main.js b/src/cli/main.js index 05a4f83a9..06d2947a4 100644 --- a/src/cli/main.js +++ b/src/cli/main.js @@ -1,5 +1,5 @@ import commands from './commands' -import { parseCliOptions, parseCliParams } from './utils' +import * as utils from './utils' /** * CLI application entrypoint. @@ -9,12 +9,14 @@ import { parseCliOptions, parseCliParams } from './utils' */ export default function run(cliArgs) { return new Promise((resolve, reject) => { - const params = parseCliParams(cliArgs) + const params = utils.parseCliParams(cliArgs) const command = commands[params[0]] - const options = command ? parseCliOptions(cliArgs, command.optionMap) : {} + const options = command ? utils.parseCliOptions(cliArgs, command.optionMap) : {} - const promise = command ? command.run(params.slice(1), options) : commands.help.run(params) + const commandPromise = command + ? command.run(params.slice(1), options) + : commands.help.run(params) - promise.then(resolve).catch(reject) + commandPromise.then(resolve).catch(reject) }) } diff --git a/src/cli/utils.js b/src/cli/utils.js index 096b0ac4b..ac138b854 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -2,7 +2,7 @@ import chalk from 'chalk' import { ensureFileSync, existsSync, outputFileSync, readFileSync } from 'fs-extra' import { findKey, mapValues, trimStart } from 'lodash' -import emoji from './emoji' +import * as emoji from './emoji' import packageJson from '../../package.json' /**