Code style updates
This commit is contained in:
parent
1b1ae8abc9
commit
eb4da80ede
9 changed files with 84 additions and 77 deletions
|
|
@ -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', () => {
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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 <file> [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)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 + ' <command> [options]'))
|
||||
log()
|
||||
log('Commands:')
|
||||
utils.log()
|
||||
utils.log('Usage:')
|
||||
utils.log(' ', chalk.bold(constants.cli + ' <command> [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()
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue