diff --git a/.eslintignore b/.eslintignore index e023f1b8c..f7288b4ad 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,3 +1,4 @@ /lib /docs +/__tests__/fixtures/cli-utils.js defaultConfig.stub.js diff --git a/__tests__/cli.utils.test.js b/__tests__/cli.utils.test.js new file mode 100644 index 000000000..e16eef84a --- /dev/null +++ b/__tests__/cli.utils.test.js @@ -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__')) + }) + }) +}) diff --git a/__tests__/fixtures/cli-utils.js b/__tests__/fixtures/cli-utils.js new file mode 100644 index 000000000..2b6500897 --- /dev/null +++ b/__tests__/fixtures/cli-utils.js @@ -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__ */ diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index 481506cec..9885c379d 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,5 +1,4 @@ import chalk from 'chalk' -import stripComments from 'strip-comments' import * as constants from '../constants' import * as emoji from '../emoji' @@ -20,20 +19,6 @@ export const optionMap = { noComments: ['no-comments'], } -/** - * Strips block comments from input string. Consolidates multiple line breaks. - * - * @param {string} input - * @return {string} - */ -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') -} - /** * Runs the command. * @@ -55,7 +40,7 @@ export function run(cliParams, cliOptions) { .replace('// let defaultConfig', 'let defaultConfig') .replace("require('./plugins/container')", "require('tailwindcss/plugins/container')") - noComments && (stub = stripBlockComments(stub)) + noComments && (stub = utils.stripBlockComments(stub)) utils.writeFile(file, stub) diff --git a/src/cli/utils.js b/src/cli/utils.js index ac138b854..ff9eec491 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -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' @@ -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') +}