Skip to content

Commit

Permalink
remove ink, use enquirer
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemcbride committed Feb 18, 2020
1 parent 85098ec commit 7c61c6a
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 4,830 deletions.
8 changes: 2 additions & 6 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#!/usr/bin/env node
'use strict'
const React = require('react')
const importJsx = require('import-jsx')
const {render} = require('ink')
const meow = require('meow')

const ui = importJsx('./ui')
const convertColor = require('./convertColor')

const cli = meow(`
Examples
Expand Down Expand Up @@ -39,4 +35,4 @@ const cli = meow(`
`)

const initialColor = cli.input.length === 0 ? null : cli.input.join(' ')
module.exports = render(React.createElement(ui, { color: initialColor }))
module.exports = convertColor(initialColor)
14 changes: 0 additions & 14 deletions components/InputColor.js

This file was deleted.

15 changes: 0 additions & 15 deletions components/SelectFormat.js

This file was deleted.

57 changes: 57 additions & 0 deletions convertColor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const clipboardy = require('clipboardy')
const converter = require('./converter')
const { prompt } = require('enquirer')
const c = require('ansi-colors')

module.exports = async function convertColor(initialColor) {
let initialStage = initialColor === null ? 0 : 1

if (initialStage === 1) {
if (!converter.isValidColor(initialColor)) {
console.log(c.red('Invalid color. Run `convert-color --help` for usage.'))
return
}
}

if (initialStage === 0) {
// we need to prompt for initial color
let { color } = await prompt({
type: 'input',
name: 'color',
message: 'What is the color you want to convert?'
})

initialColor = color
}

if (!converter.isValidColor(initialColor)) {
console.log(c.red('Invalid color. Please enter a valid hex, rgb, or hsl string. Run `convert-color --help` for usage.'))
return
}

let conversionOptions = converter.getConversionOptionsFromColor(initialColor)
if (!conversionOptions) {
console.log(c.red('Unable to get conversion options for color.'))
return
}

let { outputType } = await prompt({
type: 'select',
name: 'outputType',
message: 'Convert to which format:',
choices: conversionOptions
})

const output = converter.convertColor(initialColor, outputType)

if (output === undefined) {
// convertColor returns undefined if it cannot convert. this indicates an error.
console.log(c.red(`Unable to convert ${initialColor} to ${outputType}`))
return
} else {
// copy the output to the clipboard
clipboardy.writeSync(output)
// now we can update state
console.log(c.green(`Converted value is ${output}. It has been copied to your clipboard.`))
}
}
30 changes: 6 additions & 24 deletions converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,35 +196,17 @@ module.exports = {
getConversionOptionsFromColor: function(color) {
let result
if (isHex(color)) {
result = [
{ label: 'RGB', value: 'rgb' },
{ label: 'HSL', value: 'hsl' }
]
result = ['rgb', 'hsl' ]
} else if (isHexa(color)) {
result = [
{ label: 'RGBA', value: 'rgba' },
{ label: 'HSLA', value: 'hsla' }
]
result = ['rgba', 'hsla' ]
} else if (isRgb(color)) {
result = [
{ label: 'HEX', value: 'hex' },
{ label: 'HSL', value: 'hsl' }
]
result = ['hex', 'hsl' ]
} else if (isRgba(color)) {
result = [
{ label: 'HEX', value: 'hexa' },
{ label: 'HSLA', value: 'hsla' }
]
result = ['hexa', 'hsla' ]
} else if (isHsl(color)) {
result = [
{ label: 'HEX', value: 'hex' },
{ label: 'RGB', value: 'rgb' }
]
result = ['hex', 'rgb' ]
} else if (isHsla(color)) {
result = [
{ label: 'HEX', value: 'hexa' },
{ label: 'RGBA', value: 'rgba' }
]
result = ['hexa', 'rgba' ]
}

return result
Expand Down
33 changes: 6 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
"node": ">=8"
},
"scripts": {
"test": "ava",
"build": "parcel build index.js"
"test": "ava"
},
"files": [
"cli.js",
"ui.js",
"converter.js",
"components/InputColor.js",
"components/SelectFormat.js"
"convertColor.js",
"converter.js"
],
"repository": {
"type": "git",
Expand All @@ -40,45 +37,27 @@
},
"homepage": "https://github.com/mikemcbride/convert-color-cli#readme",
"dependencies": {
"@babel/preset-env": "^7.8.3",
"@babel/preset-react": "^7.8.3",
"@babel/register": "^7.8.3",
"ansi-colors": "^4.1.1",
"clipboardy": "^2.1.0",
"enquirer": "^2.3.4",
"hex-rgb": "^4.1.0",
"hex-to-hsl": "^1.0.2",
"hsl-regex": "^1.0.0",
"hsl-to-rgb-for-reals": "^1.1.1",
"hsla-regex": "^1.0.0",
"import-jsx": "^3.1.0",
"ink": "^2.6.0",
"ink-select-input": "^3.1.2",
"ink-text-input": "^3.2.2",
"meow": "^6.0.0",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"rgb-hex": "^3.0.0",
"rgb-regex": "^1.0.1",
"rgb-to-hsl": "^0.0.3",
"rgba-regex": "^1.0.0"
},
"devDependencies": {
"@babel/core": "^7.8.3",
"ava": "^2.4.0",
"chalk": "^3.0.0",
"eslint-plugin-react": "^7.18.0",
"eslint-plugin-react-hooks": "^2.3.0",
"esm": "^3.2.25",
"ink-testing-library": "^1.0.3",
"parcel-bundler": "^1.12.4"
"esm": "^3.2.25"
},
"ava": {
"require": [
"esm"
]
},
"babel": {
"presets": [
"@babel/preset-react"
]
}
}
119 changes: 0 additions & 119 deletions ui.js

This file was deleted.

Loading

0 comments on commit 7c61c6a

Please sign in to comment.