Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds --no-comments option to CLI init command #558

Merged
merged 4 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/lib
/docs
/__tests__/fixtures/cli-utils.js
defaultConfig.stub.js
7 changes: 7 additions & 0 deletions __tests__/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ describe('cli', () => {
expect(utils.writeFile.mock.calls[0][1]).toContain('defaultConfig')
})
})

it('creates a Tailwind config file without comments', () => {
cli(['init', '--no-comments']).then(() => {
expect(utils.writeFile.mock.calls[0][1]).not.toContain('/**')
expect(utils.writeFile.mock.calls[0][1]).toContain('//')
})
})
})

describe('build', () => {
Expand Down
111 changes: 111 additions & 0 deletions __tests__/cli.utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import path from 'path'
import * as utils from '../src/cli/utils'

describe('cli utils', () => {
const fixture = utils.readFile(path.resolve(__dirname, 'fixtures/cli-utils.js'))

describe('parseCliParams', () => {
it('parses CLI parameters', () => {
const result = utils.parseCliParams(['a', 'b', '-c', 'd'])

expect(result).toEqual(['a', 'b'])
})
})

describe('parseCliOptions', () => {
it('parses CLI options', () => {
const result = utils.parseCliOptions(['a', '-b', 'c'], { test: ['b'] })

expect(result).toEqual({ test: ['c'] })
})

it('parses multiple types of options', () => {
const result = utils.parseCliOptions(['a', '-b', 'c', '--test', 'd', '-test', 'e'], {
test: ['test', 'b'],
})

expect(result).toEqual({ test: ['c', 'd', 'e'] })
})

it('ignores unknown options', () => {
const result = utils.parseCliOptions(['a', '-b', 'c'], {})

expect(result).toEqual({})
})

it('maps options', () => {
const result = utils.parseCliOptions(['a', '-b', 'c', '-d', 'e'], { test: ['b', 'd'] })

expect(result).toEqual({ test: ['c', 'e'] })
})

it('parses undefined options', () => {
const result = utils.parseCliOptions(['a'], { test: ['b'] })

expect(result).toEqual({ test: undefined })
})

it('parses flags', () => {
const result = utils.parseCliOptions(['a', '-b'], { test: ['b'] })

expect(result).toEqual({ test: [] })
})

it('accepts multiple values per option', () => {
const result = utils.parseCliOptions(['a', '-b', 'c', 'd', '-e', 'f', '-g', 'h'], {
test: ['b', 'g'],
})

expect(result).toEqual({ test: ['c', 'd', 'h'] })
})
})

describe('stripBlockComments', () => {
it('does not strip code', () => {
const result = utils.stripBlockComments(fixture)

expect(result).toEqual(expect.stringContaining('__code_no_comment__'))
expect(result).toEqual(expect.stringContaining('__code_comment_line__'))
expect(result).toEqual(expect.stringContaining('__code_comment_block__'))
expect(result).toEqual(expect.stringContaining('__code_comment_line_important__'))
expect(result).toEqual(expect.stringContaining('__code_comment_block_important__'))
})

it('strips block comments', () => {
const result = utils.stripBlockComments(fixture)

expect(result).not.toEqual(expect.stringContaining('__comment_block__'))
expect(result).not.toEqual(expect.stringContaining('__comment_block_multiline__'))
expect(result).not.toEqual(expect.stringContaining('__comment_block_code__'))
})

it('strips docblock comments', () => {
const result = utils.stripBlockComments(fixture)

expect(result).not.toEqual(expect.stringContaining('__comment_docblock__'))
})

it('does not strip line comments', () => {
const result = utils.stripBlockComments(fixture)

expect(result).toEqual(expect.stringContaining('__comment_line__'))
expect(result).toEqual(expect.stringContaining('__comment_line_important__'))
expect(result).toEqual(expect.stringContaining('__comment_line_code__'))
expect(result).toEqual(expect.stringContaining('__comment_line_important_code__'))
})

it('does not strip important block comments', () => {
const result = utils.stripBlockComments(fixture)

expect(result).toEqual(expect.stringContaining('__comment_block_important__'))
expect(result).toEqual(expect.stringContaining('__comment_block_multiline_important__'))
expect(result).toEqual(expect.stringContaining('__comment_block_important_code__'))
})

it('does not strip important docblock comments', () => {
const result = utils.stripBlockComments(fixture)

expect(result).toEqual(expect.stringContaining('__comment_docblock_important__'))
})
})
})
29 changes: 29 additions & 0 deletions __tests__/fixtures/cli-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// __comment_line__

//! __comment_line_important__

/* __comment_block__ */

/*! __comment_block_important__ */

/*
__comment_block_multiline__
*/

/*!
__comment_block_multiline_important__
*/

/**
__comment_docblock__
*/

/**!
__comment_docblock_important__
*/

const __code_no_comment__ = 'test'
const __code_comment_line__ = 'test' // __comment_line_code__
const __code_comment_block__ = 'test' /* __comment_block_code__ */
const __code_comment_line_important__ = 'test' //! __comment_line_important_code__
const __code_comment_block_important__ = 'test' /*! __comment_block_important_code__ */
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"postcss-js": "^1.0.1",
"postcss-nested": "^3.0.0",
"postcss-selector-parser": "^3.1.1",
"pretty-hrtime": "^1.0.3"
"pretty-hrtime": "^1.0.3",
"strip-comments": "^1.0.2"
},
"browserslist": [
"> 1%"
Expand Down
19 changes: 17 additions & 2 deletions src/cli/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,40 @@ export const usage = 'init [file]'
export const description =
'Creates Tailwind config file. Default: ' + chalk.bold.magenta(constants.defaultConfigFile)

export const options = [
{
usage: '--no-comments',
description: 'Omit comments from the config file.',
},
]

export const optionMap = {
noComments: ['no-comments'],
}

/**
* Runs the command.
*
* @param {string[]} cliParams
* @param {object} cliOptions
* @return {Promise}
*/
export function run(cliParams) {
export function run(cliParams, cliOptions) {
return new Promise(resolve => {
utils.header()

const noComments = cliOptions.noComments
const file = cliParams[0] || constants.defaultConfigFile

utils.exists(file) && utils.die(chalk.bold.magenta(file), 'already exists.')

const stub = utils
let stub = utils
.readFile(constants.configStubFile)
.replace('// let defaultConfig', 'let defaultConfig')
.replace("require('./plugins/container')", "require('tailwindcss/plugins/container')")

noComments && (stub = utils.stripBlockComments(stub))

utils.writeFile(file, stub)

utils.log()
Expand Down
15 changes: 15 additions & 0 deletions src/cli/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from 'chalk'
import { ensureFileSync, existsSync, outputFileSync, readFileSync } from 'fs-extra'
import { findKey, mapValues, trimStart } from 'lodash'
import stripComments from 'strip-comments'

import * as emoji from './emoji'
import packageJson from '../../package.json'
Expand Down Expand Up @@ -121,3 +122,17 @@ export function writeFile(path, content) {

return outputFileSync(path, content)
}

/**
* Strips block comments from input string. Consolidates multiple line breaks.
*
* @param {string} input
* @return {string}
*/
export function stripBlockComments(input) {
return stripComments
.block(input, { keepProtected: true })
.replace(/\n\s*\n\s*\n/g, '\n\n') // Strip unnecessary line breaks
.trim()
.concat('\n')
}