Skip to content

Commit eb4da80

Browse files
committed
Code style updates
1 parent 1b1ae8a commit eb4da80

File tree

9 files changed

+84
-77
lines changed

9 files changed

+84
-77
lines changed

__tests__/cli.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'path'
22

33
import cli from '../src/cli/main'
4-
import constants from '../src/cli/constants'
4+
import * as constants from '../src/cli/constants'
55
import * as utils from '../src/cli/utils'
66

77
describe('cli', () => {

src/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

33
import main from './cli/main'
4-
import { die } from './cli/utils'
4+
import * as utils from './cli/utils'
55

6-
main(process.argv.slice(2)).catch(error => die(error.stack))
6+
main(process.argv.slice(2)).catch(error => utils.die(error.stack))

src/cli/commands/build.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import chalk from 'chalk'
44
import postcss from 'postcss'
55
import prettyHrtime from 'pretty-hrtime'
66

7-
import commands from '.'
8-
import emoji from '../emoji'
97
import tailwind from '../..'
10-
import { die, error, exists, footer, header, log, readFile, writeFile } from '../utils'
8+
9+
import commands from '.'
10+
import * as emoji from '../emoji'
11+
import * as utils from '../utils'
1112

1213
export const usage = 'build <file> [options]'
1314
export const description = 'Compiles Tailwind CSS file.'
@@ -34,9 +35,9 @@ export const optionMap = {
3435
* @param {...string} [msgs]
3536
*/
3637
function stop(...msgs) {
37-
header()
38-
error(...msgs)
39-
die()
38+
utils.header()
39+
utils.error(...msgs)
40+
utils.die()
4041
}
4142

4243
/**
@@ -45,10 +46,10 @@ function stop(...msgs) {
4546
* @param {...string} [msgs]
4647
*/
4748
function stopWithHelp(...msgs) {
48-
header()
49-
error(...msgs)
49+
utils.header()
50+
utils.error(...msgs)
5051
commands.help.forCommand(commands.build)
51-
die()
52+
utils.die()
5253
}
5354

5455
/**
@@ -60,7 +61,7 @@ function stopWithHelp(...msgs) {
6061
* @return {Promise}
6162
*/
6263
function build(inputFile, configFile, outputFile) {
63-
const css = readFile(inputFile)
64+
const css = utils.readFile(inputFile)
6465

6566
return new Promise((resolve, reject) => {
6667
postcss([tailwind(configFile), autoprefixer])
@@ -95,20 +96,20 @@ function buildToStdout(inputFile, configFile, outputFile) {
9596
* @return {Promise}
9697
*/
9798
function buildToFile(inputFile, configFile, outputFile, startTime) {
98-
header()
99-
log()
100-
log(emoji.go, 'Building...', chalk.bold.cyan(inputFile))
99+
utils.header()
100+
utils.log()
101+
utils.log(emoji.go, 'Building...', chalk.bold.cyan(inputFile))
101102

102103
return build(inputFile, configFile, outputFile).then(result => {
103-
writeFile(outputFile, result.css)
104+
utils.writeFile(outputFile, result.css)
104105

105106
const prettyTime = prettyHrtime(process.hrtime(startTime))
106107

107-
log()
108-
log(emoji.yes, 'Finished in', chalk.bold.magenta(prettyTime))
109-
log(emoji.pack, 'Size:', chalk.bold.magenta(bytes(result.css.length)))
110-
log(emoji.disk, 'Saved to', chalk.bold.cyan(outputFile))
111-
footer()
108+
utils.log()
109+
utils.log(emoji.yes, 'Finished in', chalk.bold.magenta(prettyTime))
110+
utils.log(emoji.pack, 'Size:', chalk.bold.magenta(bytes(result.css.length)))
111+
utils.log(emoji.disk, 'Saved to', chalk.bold.cyan(outputFile))
112+
utils.footer()
112113
})
113114
}
114115

@@ -127,13 +128,16 @@ export function run(cliParams, cliOptions) {
127128
const outputFile = cliOptions.output && cliOptions.output[0]
128129

129130
!inputFile && stopWithHelp('CSS file is required.')
130-
!exists(inputFile) && stop(chalk.bold.magenta(inputFile), 'does not exist.')
131-
configFile && !exists(configFile) && stop(chalk.bold.magenta(configFile), 'does not exist.')
131+
!utils.exists(inputFile) && stop(chalk.bold.magenta(inputFile), 'does not exist.')
132+
133+
configFile &&
134+
!utils.exists(configFile) &&
135+
stop(chalk.bold.magenta(configFile), 'does not exist.')
132136

133-
const promise = outputFile
137+
const buildPromise = outputFile
134138
? buildToFile(inputFile, configFile, outputFile, startTime)
135139
: buildToStdout(inputFile, configFile, outputFile)
136140

137-
promise.then(resolve).catch(reject)
141+
buildPromise.then(resolve).catch(reject)
138142
})
139143
}

src/cli/commands/help.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@ import chalk from 'chalk'
22
import { forEach, map, padEnd } from 'lodash'
33

44
import commands from '.'
5-
import constants from '../constants'
6-
import { die, error, footer, header, log } from '../utils'
5+
import * as constants from '../constants'
6+
import * as utils from '../utils'
77

88
export const usage = 'help [command]'
99
export const description = 'More information about the command.'
1010

11+
const PADDING_SIZE = 3
12+
1113
/**
1214
* Prints general help.
1315
*/
1416
export function forApp() {
15-
const pad = Math.max(...map(commands, 'usage.length')) + 3
17+
const pad = Math.max(...map(commands, 'usage.length')) + PADDING_SIZE
1618

17-
log()
18-
log('Usage:')
19-
log(' ', chalk.bold(constants.cli + ' <command> [options]'))
20-
log()
21-
log('Commands:')
19+
utils.log()
20+
utils.log('Usage:')
21+
utils.log(' ', chalk.bold(constants.cli + ' <command> [options]'))
22+
utils.log()
23+
utils.log('Commands:')
2224
forEach(commands, command => {
23-
log(' ', chalk.bold(padEnd(command.usage, pad)), command.description)
25+
utils.log(' ', chalk.bold(padEnd(command.usage, pad)), command.description)
2426
})
2527
}
2628

@@ -30,20 +32,20 @@ export function forApp() {
3032
* @param {object} command
3133
*/
3234
export function forCommand(command) {
33-
log()
34-
log('Usage:')
35-
log(' ', chalk.bold(constants.cli, command.usage))
36-
log()
37-
log('Description:')
38-
log(' ', chalk.bold(command.description))
35+
utils.log()
36+
utils.log('Usage:')
37+
utils.log(' ', chalk.bold(constants.cli, command.usage))
38+
utils.log()
39+
utils.log('Description:')
40+
utils.log(' ', chalk.bold(command.description))
3941

4042
if (command.options) {
41-
const pad = Math.max(...map(command.options, 'usage.length')) + 3
43+
const pad = Math.max(...map(command.options, 'usage.length')) + PADDING_SIZE
4244

43-
log()
44-
log('Options:')
45+
utils.log()
46+
utils.log('Options:')
4547
forEach(command.options, option => {
46-
log(' ', chalk.bold(padEnd(option.usage, pad)), option.description)
48+
utils.log(' ', chalk.bold(padEnd(option.usage, pad)), option.description)
4749
})
4850
}
4951
}
@@ -54,9 +56,9 @@ export function forCommand(command) {
5456
* @param {string} commandName
5557
*/
5658
export function invalidCommand(commandName) {
57-
error('Invalid command:', chalk.bold.magenta(commandName))
59+
utils.error('Invalid command:', chalk.bold.magenta(commandName))
5860
forApp()
59-
die()
61+
utils.die()
6062
}
6163

6264
/**
@@ -67,7 +69,7 @@ export function invalidCommand(commandName) {
6769
*/
6870
export function run(cliParams) {
6971
return new Promise(resolve => {
70-
header()
72+
utils.header()
7173

7274
const commandName = cliParams[0]
7375
const command = commands[commandName]
@@ -76,7 +78,8 @@ export function run(cliParams) {
7678
commandName && command && forCommand(command)
7779
commandName && !command && invalidCommand(commandName)
7880

79-
footer()
81+
utils.footer()
82+
8083
resolve()
8184
})
8285
}

src/cli/commands/init.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import chalk from 'chalk'
22

3-
import constants from '../constants'
4-
import emoji from '../emoji'
5-
import { die, exists, footer, header, log, readFile, writeFile } from '../utils'
3+
import * as constants from '../constants'
4+
import * as emoji from '../emoji'
5+
import * as utils from '../utils'
66

77
export const usage = 'init [file]'
88
export const description =
@@ -16,22 +16,24 @@ export const description =
1616
*/
1717
export function run(cliParams) {
1818
return new Promise(resolve => {
19-
header()
19+
utils.header()
2020

2121
const file = cliParams[0] || constants.defaultConfigFile
2222

23-
exists(file) && die(chalk.bold.magenta(file), 'already exists.')
23+
utils.exists(file) && utils.die(chalk.bold.magenta(file), 'already exists.')
2424

25-
const stub = readFile(constants.configStubFile)
25+
const stub = utils
26+
.readFile(constants.configStubFile)
2627
.replace('// let defaultConfig', 'let defaultConfig')
2728
.replace("require('./plugins/container')", "require('tailwindcss/plugins/container')")
2829

29-
writeFile(file, stub)
30+
utils.writeFile(file, stub)
3031

31-
log()
32-
log(emoji.yes, 'Created Tailwind config file:', chalk.bold.magenta(file))
32+
utils.log()
33+
utils.log(emoji.yes, 'Created Tailwind config file:', chalk.bold.magenta(file))
34+
35+
utils.footer()
3336

34-
footer()
3537
resolve()
3638
})
3739
}

src/cli/constants.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import path from 'path'
22

3-
export default {
4-
cli: 'tailwind',
5-
defaultConfigFile: 'tailwind.js',
6-
configStubFile: path.resolve(__dirname, '../../defaultConfig.stub.js'),
7-
}
3+
export const cli = 'tailwind'
4+
export const defaultConfigFile = 'tailwind.js'
5+
export const configStubFile = path.resolve(__dirname, '../../defaultConfig.stub.js')

src/cli/emoji.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { get } from 'node-emoji'
22

3-
export default {
4-
yes: get('white_check_mark'),
5-
no: get('no_entry_sign'),
6-
go: get('rocket'),
7-
pack: get('package'),
8-
disk: get('floppy_disk'),
9-
}
3+
export const yes = get('white_check_mark')
4+
export const no = get('no_entry_sign')
5+
export const go = get('rocket')
6+
export const pack = get('package')
7+
export const disk = get('floppy_disk')

src/cli/main.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import commands from './commands'
2-
import { parseCliOptions, parseCliParams } from './utils'
2+
import * as utils from './utils'
33

44
/**
55
* CLI application entrypoint.
@@ -9,12 +9,14 @@ import { parseCliOptions, parseCliParams } from './utils'
99
*/
1010
export default function run(cliArgs) {
1111
return new Promise((resolve, reject) => {
12-
const params = parseCliParams(cliArgs)
12+
const params = utils.parseCliParams(cliArgs)
1313
const command = commands[params[0]]
14-
const options = command ? parseCliOptions(cliArgs, command.optionMap) : {}
14+
const options = command ? utils.parseCliOptions(cliArgs, command.optionMap) : {}
1515

16-
const promise = command ? command.run(params.slice(1), options) : commands.help.run(params)
16+
const commandPromise = command
17+
? command.run(params.slice(1), options)
18+
: commands.help.run(params)
1719

18-
promise.then(resolve).catch(reject)
20+
commandPromise.then(resolve).catch(reject)
1921
})
2022
}

src/cli/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import chalk from 'chalk'
22
import { ensureFileSync, existsSync, outputFileSync, readFileSync } from 'fs-extra'
33
import { findKey, mapValues, trimStart } from 'lodash'
44

5-
import emoji from './emoji'
5+
import * as emoji from './emoji'
66
import packageJson from '../../package.json'
77

88
/**

0 commit comments

Comments
 (0)