Skip to content

Commit

Permalink
feat: implement JSON converter (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
dobromir-hristov authored Dec 12, 2021
1 parent eea9fb3 commit 554213e
Show file tree
Hide file tree
Showing 6 changed files with 2,372 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const argv = yargs // eslint-disable-line
alias: 'f',
describe: 'Format to generate - sass,less,stylus',
type: 'string',
choices: ['sass', 'scss', 'less', 'styl'],
choices: ['sass', 'scss', 'less', 'styl', 'json'],
nargs: 1,
demand: true
})
Expand Down Expand Up @@ -57,7 +57,7 @@ const argv = yargs // eslint-disable-line
describe: 'Keys to preserve',
type: 'array', /* array | boolean | string */
nargs: 1,
coerce: array => {
coerce: (array = []) => {
return array.flatMap(v => v.split(','))
}
})
Expand Down
15 changes: 15 additions & 0 deletions src/converters/JSON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Converter from './Converter'

class JSONConverter extends Converter {
format = 'json'

convert () {
const filtered = Object.entries(this.theme).filter(([key]) => {
return this._isSettingEnabled(key)
})

return JSON.stringify(Object.fromEntries(filtered), null, 2)
}
}

export default JSONConverter
3 changes: 2 additions & 1 deletion src/converters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import Less from './Less'
import Stylus from './Stylus'
import Sass from './Sass'
import Scss from './Scss'
import JSON from './JSON'

export default {
Less, Sass, Scss, Stylus
Less, Sass, Scss, Stylus, JSON
}
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const allowedFormatsMap = {
styl: converters.Stylus,
sass: converters.Sass,
scss: converters.Scss,
less: converters.Less
less: converters.Less,
json: converters.JSON
}

/**
Expand Down Expand Up @@ -42,7 +43,10 @@ class ConvertTo {
* @returns {string}
*/
convert () {
let buffer = `/* Converted Tailwind Config to ${this.options.format} */`
let buffer = ''
if (this.options.format !== 'json') {
buffer = `/* Converted Tailwind Config to ${this.options.format} */`
}
buffer += this.converterInstance.convert()
return buffer
}
Expand Down
14 changes: 14 additions & 0 deletions tests/specs/converters/JSON.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import JSONConverter from '../../../src/converters/JSON'
import { resolveConfig } from '../../../src/converters/utils'
import testConfigDefault from '../../tailwind-default.config'

describe('JSON converter', () => {
describe('full config', () => {
it('converts all props, to a JSON', () => {
const converter = new JSONConverter({
config: resolveConfig(testConfigDefault)
})
expect(converter.convert()).toMatchSnapshot()
})
})
})
Loading

0 comments on commit 554213e

Please sign in to comment.