Skip to content

Commit c4c36b9

Browse files
authored
Merge pull request tailwindlabs#558 from MattStypa/init-no-comments
Adds --no-comments option to CLI init command
2 parents 4fc519f + 34ad504 commit c4c36b9

File tree

7 files changed

+182
-3
lines changed

7 files changed

+182
-3
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/lib
22
/docs
3+
/__tests__/fixtures/cli-utils.js
34
defaultConfig.stub.js

__tests__/cli.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ describe('cli', () => {
2828
expect(utils.writeFile.mock.calls[0][1]).toContain('defaultConfig')
2929
})
3030
})
31+
32+
it('creates a Tailwind config file without comments', () => {
33+
cli(['init', '--no-comments']).then(() => {
34+
expect(utils.writeFile.mock.calls[0][1]).not.toContain('/**')
35+
expect(utils.writeFile.mock.calls[0][1]).toContain('//')
36+
})
37+
})
3138
})
3239

3340
describe('build', () => {

__tests__/cli.utils.test.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import path from 'path'
2+
import * as utils from '../src/cli/utils'
3+
4+
describe('cli utils', () => {
5+
const fixture = utils.readFile(path.resolve(__dirname, 'fixtures/cli-utils.js'))
6+
7+
describe('parseCliParams', () => {
8+
it('parses CLI parameters', () => {
9+
const result = utils.parseCliParams(['a', 'b', '-c', 'd'])
10+
11+
expect(result).toEqual(['a', 'b'])
12+
})
13+
})
14+
15+
describe('parseCliOptions', () => {
16+
it('parses CLI options', () => {
17+
const result = utils.parseCliOptions(['a', '-b', 'c'], { test: ['b'] })
18+
19+
expect(result).toEqual({ test: ['c'] })
20+
})
21+
22+
it('parses multiple types of options', () => {
23+
const result = utils.parseCliOptions(['a', '-b', 'c', '--test', 'd', '-test', 'e'], {
24+
test: ['test', 'b'],
25+
})
26+
27+
expect(result).toEqual({ test: ['c', 'd', 'e'] })
28+
})
29+
30+
it('ignores unknown options', () => {
31+
const result = utils.parseCliOptions(['a', '-b', 'c'], {})
32+
33+
expect(result).toEqual({})
34+
})
35+
36+
it('maps options', () => {
37+
const result = utils.parseCliOptions(['a', '-b', 'c', '-d', 'e'], { test: ['b', 'd'] })
38+
39+
expect(result).toEqual({ test: ['c', 'e'] })
40+
})
41+
42+
it('parses undefined options', () => {
43+
const result = utils.parseCliOptions(['a'], { test: ['b'] })
44+
45+
expect(result).toEqual({ test: undefined })
46+
})
47+
48+
it('parses flags', () => {
49+
const result = utils.parseCliOptions(['a', '-b'], { test: ['b'] })
50+
51+
expect(result).toEqual({ test: [] })
52+
})
53+
54+
it('accepts multiple values per option', () => {
55+
const result = utils.parseCliOptions(['a', '-b', 'c', 'd', '-e', 'f', '-g', 'h'], {
56+
test: ['b', 'g'],
57+
})
58+
59+
expect(result).toEqual({ test: ['c', 'd', 'h'] })
60+
})
61+
})
62+
63+
describe('stripBlockComments', () => {
64+
it('does not strip code', () => {
65+
const result = utils.stripBlockComments(fixture)
66+
67+
expect(result).toEqual(expect.stringContaining('__code_no_comment__'))
68+
expect(result).toEqual(expect.stringContaining('__code_comment_line__'))
69+
expect(result).toEqual(expect.stringContaining('__code_comment_block__'))
70+
expect(result).toEqual(expect.stringContaining('__code_comment_line_important__'))
71+
expect(result).toEqual(expect.stringContaining('__code_comment_block_important__'))
72+
})
73+
74+
it('strips block comments', () => {
75+
const result = utils.stripBlockComments(fixture)
76+
77+
expect(result).not.toEqual(expect.stringContaining('__comment_block__'))
78+
expect(result).not.toEqual(expect.stringContaining('__comment_block_multiline__'))
79+
expect(result).not.toEqual(expect.stringContaining('__comment_block_code__'))
80+
})
81+
82+
it('strips docblock comments', () => {
83+
const result = utils.stripBlockComments(fixture)
84+
85+
expect(result).not.toEqual(expect.stringContaining('__comment_docblock__'))
86+
})
87+
88+
it('does not strip line comments', () => {
89+
const result = utils.stripBlockComments(fixture)
90+
91+
expect(result).toEqual(expect.stringContaining('__comment_line__'))
92+
expect(result).toEqual(expect.stringContaining('__comment_line_important__'))
93+
expect(result).toEqual(expect.stringContaining('__comment_line_code__'))
94+
expect(result).toEqual(expect.stringContaining('__comment_line_important_code__'))
95+
})
96+
97+
it('does not strip important block comments', () => {
98+
const result = utils.stripBlockComments(fixture)
99+
100+
expect(result).toEqual(expect.stringContaining('__comment_block_important__'))
101+
expect(result).toEqual(expect.stringContaining('__comment_block_multiline_important__'))
102+
expect(result).toEqual(expect.stringContaining('__comment_block_important_code__'))
103+
})
104+
105+
it('does not strip important docblock comments', () => {
106+
const result = utils.stripBlockComments(fixture)
107+
108+
expect(result).toEqual(expect.stringContaining('__comment_docblock_important__'))
109+
})
110+
})
111+
})

__tests__/fixtures/cli-utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// __comment_line__
2+
3+
//! __comment_line_important__
4+
5+
/* __comment_block__ */
6+
7+
/*! __comment_block_important__ */
8+
9+
/*
10+
__comment_block_multiline__
11+
*/
12+
13+
/*!
14+
__comment_block_multiline_important__
15+
*/
16+
17+
/**
18+
__comment_docblock__
19+
*/
20+
21+
/**!
22+
__comment_docblock_important__
23+
*/
24+
25+
const __code_no_comment__ = 'test'
26+
const __code_comment_line__ = 'test' // __comment_line_code__
27+
const __code_comment_block__ = 'test' /* __comment_block_code__ */
28+
const __code_comment_line_important__ = 'test' //! __comment_line_important_code__
29+
const __code_comment_block_important__ = 'test' /*! __comment_block_important_code__ */

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
"postcss-js": "^1.0.1",
5656
"postcss-nested": "^3.0.0",
5757
"postcss-selector-parser": "^3.1.1",
58-
"pretty-hrtime": "^1.0.3"
58+
"pretty-hrtime": "^1.0.3",
59+
"strip-comments": "^1.0.2"
5960
},
6061
"browserslist": [
6162
"> 1%"

src/cli/commands/init.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,40 @@ export const usage = 'init [file]'
88
export const description =
99
'Creates Tailwind config file. Default: ' + chalk.bold.magenta(constants.defaultConfigFile)
1010

11+
export const options = [
12+
{
13+
usage: '--no-comments',
14+
description: 'Omit comments from the config file.',
15+
},
16+
]
17+
18+
export const optionMap = {
19+
noComments: ['no-comments'],
20+
}
21+
1122
/**
1223
* Runs the command.
1324
*
1425
* @param {string[]} cliParams
26+
* @param {object} cliOptions
1527
* @return {Promise}
1628
*/
17-
export function run(cliParams) {
29+
export function run(cliParams, cliOptions) {
1830
return new Promise(resolve => {
1931
utils.header()
2032

33+
const noComments = cliOptions.noComments
2134
const file = cliParams[0] || constants.defaultConfigFile
2235

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

25-
const stub = utils
38+
let stub = utils
2639
.readFile(constants.configStubFile)
2740
.replace('// let defaultConfig', 'let defaultConfig')
2841
.replace("require('./plugins/container')", "require('tailwindcss/plugins/container')")
2942

43+
noComments && (stub = utils.stripBlockComments(stub))
44+
3045
utils.writeFile(file, stub)
3146

3247
utils.log()

src/cli/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import chalk from 'chalk'
22
import { ensureFileSync, existsSync, outputFileSync, readFileSync } from 'fs-extra'
33
import { findKey, mapValues, trimStart } from 'lodash'
4+
import stripComments from 'strip-comments'
45

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

122123
return outputFileSync(path, content)
123124
}
125+
126+
/**
127+
* Strips block comments from input string. Consolidates multiple line breaks.
128+
*
129+
* @param {string} input
130+
* @return {string}
131+
*/
132+
export function stripBlockComments(input) {
133+
return stripComments
134+
.block(input, { keepProtected: true })
135+
.replace(/\n\s*\n\s*\n/g, '\n\n') // Strip unnecessary line breaks
136+
.trim()
137+
.concat('\n')
138+
}

0 commit comments

Comments
 (0)