Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit f042e78

Browse files
committed
Update project conventions
1 parent 68982fe commit f042e78

File tree

11 files changed

+94
-89
lines changed

11 files changed

+94
-89
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
dist
22
node_modules
33
package-lock.json
4+
yarn.lock
45
.*
56
!.editorconfig
67
!.gitignore

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PostCSS Tape [<img src="http://postcss.github.io/postcss/logo.svg" alt="PostCSS" width="90" height="90" align="right">][PostCSS]
22

33
[<img alt="NPM Version" src="https://img.shields.io/npm/v/postcss-tape.svg" height="20">][npm-url]
4-
[<img alt="Build Status" src="https://img.shields.io/travis/csstools/postcss-tape/master.svg" height="20">][cli-url]
4+
[<img alt="Build Status" src="https://img.shields.io/travis/csstools/postcss-tape/main.svg" height="20">][cli-url]
55
[<img alt="Support Chat" src="https://img.shields.io/badge/support-chat-blue.svg" height="20">][git-url]
66

77
[PostCSS Tape] lets you quickly test [PostCSS] plugins.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"test:tape:ci": "npm run test:tape:7 -- --ci true && npm run test:tape:8 -- --ci true"
2828
},
2929
"engines": {
30-
"node": "^10 || ^12 || ^14"
30+
"node": ">=10.0.0"
3131
},
3232
"peerDependencies": {
3333
"postcss": "^7 || ^8"

src/index.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { exitFail, exitPass } from './lib/exit'
2-
import { readOrWriteFile, safelyReadFile, writeFile } from './lib/utils'
3-
import * as log from './lib/log'
4-
import getErrorMessage from './lib/get-error-message'
5-
import getOptions from './lib/get-options'
1+
import * as exit from './lib/exit.js'
2+
import { readOrWriteFile, safelyReadFile, writeFile } from './lib/utils.js'
3+
import { getErrorMessage } from './lib/get-error-message.js'
4+
import { getOptions } from './lib/get-options.js'
5+
import * as log from './lib/log.js'
66
import path from 'path'
77

8-
async function postcss8(plugins) {
8+
const postcss8 = async (plugins) => {
99
const pkg = await import('postcss/package.json')
10+
1011
if (pkg.version[0] === '8') {
1112
const m = await import('postcss')
1213
return m.default(plugins)
@@ -15,9 +16,7 @@ async function postcss8(plugins) {
1516
}
1617
}
1718

18-
function isPostcss8Plugin(plugin) {
19-
return typeof plugin === 'function' && Object(plugin).postcss === true
20-
}
19+
const isPostcss8Plugin = (plugin) => typeof plugin === 'function' && Object(plugin).postcss === true
2120

2221
getOptions().then(
2322
async options => {
@@ -39,9 +38,11 @@ getOptions().then(
3938
const pluginOptions = test.options
4039

4140
let rawPlugin = test.plugin || options.plugin
41+
4242
if (rawPlugin.default) {
4343
rawPlugin = rawPlugin.default
4444
}
45+
4546
const plugin = isPostcss8Plugin(rawPlugin)
4647
? rawPlugin(pluginOptions)
4748
: typeof Object(rawPlugin).process === 'function'
@@ -63,12 +64,15 @@ getOptions().then(
6364
const sourceCSS = await readOrWriteFile(sourcePath, expectCSS)
6465

6566
let result
67+
6668
if (isPostcss8Plugin(rawPlugin)) {
6769
const postcss = await postcss8([ plugin ])
70+
6871
result = await postcss.process(sourceCSS, processOptions)
6972
} else {
7073
result = await plugin.process(sourceCSS, processOptions, pluginOptions)
7174
}
75+
7276
const resultCSS = result.css
7377

7478
if (options.fix) {
@@ -168,6 +172,5 @@ getOptions().then(
168172
throw hadError
169173
}
170174
}
171-
).then(exitPass, exitFail)
172-
175+
).then(exit.pass, exit.fail)
173176

src/lib/color.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** Color keys. */
12
const colors = {
23
reset: '\x1b[0m',
34
bold: '\x1b[1m',
@@ -24,6 +25,9 @@ const colors = {
2425
bgWhite: '\x1b[47m'
2526
}
2627

27-
export default function color (name, string) {
28-
return colors[name] + string.replace(colors.reset, colors.reset + colors[name]) + colors.reset
29-
}
28+
/** Return a string wrapped in a CLI color. */
29+
export const color = (/** @type {keyof colors} */ name, /** @type {string} */ string) => (
30+
colors[name] +
31+
string.replace(colors.reset, colors.reset + colors[name]) +
32+
colors.reset
33+
)

src/lib/exit.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
export function exitFail (error) {
1+
/** Exit the process logging an error and with a failing exit code. */
2+
export const fail = (error) => {
23
console.log(error)
34

45
process.exit(1)
56
}
67

7-
export function exitPass () {
8+
/** Exit the process with a passing exit code. */
9+
export const pass = () => {
810
process.exit(0)
911
}

src/lib/get-error-message.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export default function getErrorMessage (error) {
2-
return Object(error).message || error
3-
}
1+
/** Return the error message. */
2+
export const getErrorMessage = (/** @type {Error | string} */ error) => String(
3+
Object(error).message || error
4+
)

src/lib/get-options-from-arguments.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,30 @@ const primativeRegExp = /^(false|null|true|undefined|(\d+\.)?\d+|\{.*\}|\[.*\])$
33
const relaxedJsonPropRegExp = /(['"])?([a-z0-9A-Z_]+)\1:/g
44
const relaxedJsonValueRegExp = /("[a-z0-9A-Z_]+":\s*)(?!true|false|null|\d+)'?([A-z0-9]+)'?([,}])/g
55

6-
export default function getOptionsFromArguments (defaultOptions) {
7-
return process.argv.slice(2).reduce(
8-
(args, arg, index, argv) => {
9-
const nextIndex = index + 1
10-
const nextArg = argv[nextIndex]
11-
const argMatch = arg.match(argRegExp)
6+
/** Return an object of options from a CLI array of arguments. */
7+
export const getOptionsFromArguments = (/** @type {object} */ defaultOptions) => process.argv.slice(2).reduce(
8+
(/** @type {object} */ args, /** @type {string} */ arg, /** @type {number} */ index, /** @type {object} */ argv) => {
9+
const nextIndex = index + 1
10+
const nextArg = argv[nextIndex]
11+
const argMatch = arg.match(argRegExp)
1212

13-
if (argMatch) {
14-
const [, name] = argMatch
13+
if (argMatch) {
14+
const [, name] = argMatch
1515

16-
if (!nextArg || argRegExp.test(nextArg)) {
17-
args[name] = true
18-
} else {
19-
args[name] = primativeRegExp.test(nextArg)
20-
? JSON.parse(
21-
nextArg
22-
.replace(relaxedJsonPropRegExp, '"$2": ')
23-
.replace(relaxedJsonValueRegExp, '$1"$2"$3')
24-
)
25-
: nextArg
26-
}
16+
if (!nextArg || argRegExp.test(nextArg)) {
17+
args[name] = true
18+
} else {
19+
args[name] = primativeRegExp.test(nextArg)
20+
? JSON.parse(
21+
nextArg
22+
.replace(relaxedJsonPropRegExp, '"$2": ')
23+
.replace(relaxedJsonValueRegExp, '$1"$2"$3')
24+
)
25+
: nextArg
2726
}
27+
}
2828

29-
return args
30-
},
31-
Object.assign({}, defaultOptions)
32-
)
33-
}
29+
return args
30+
},
31+
Object.assign({}, defaultOptions)
32+
)

src/lib/get-options.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import getOptionsFromArguments from './get-options-from-arguments'
2-
import { readJSON } from './utils'
1+
import { getOptionsFromArguments } from './get-options-from-arguments.js'
2+
import { readJSON } from './utils.js'
33
import path from 'path'
44

5-
export default async function getOptions() {
5+
/** Asynchronously return the options from the project. */
6+
export const getOptions = async () => {
67
const cwd = process.cwd()
78

89
// default options
@@ -34,6 +35,7 @@ export default async function getOptions() {
3435
'.tape.mjs',
3536
'.tape.cjs'
3637
]
38+
3739
let returnError
3840

3941
while (postcssTapeConfigFiles.length) {

src/lib/log.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import color from './color'
1+
import { color } from './color.js'
22
import readline from 'readline'
33

44
// symbols
@@ -9,7 +9,8 @@ const stdout = process.stdout
99

1010
let interval
1111

12-
export function pass (name, message, ci) {
12+
/** Log as a passing state. */
13+
export const pass = (/** @type {string} */ name, /** @type {string} */ message, /** @type {boolean} */ ci) => {
1314
clearInterval(interval)
1415

1516
if (ci) {
@@ -23,7 +24,8 @@ export function pass (name, message, ci) {
2324
}
2425
}
2526

26-
export function fail (name, message, details, ci) {
27+
/** Log as a failing state. */
28+
export const fail = (/** @type {string} */ name, /** @type {string} */ message, /** @type {string} */ details, /** @type {boolean} */ ci) => {
2729
clearInterval(interval)
2830

2931
if (ci) {
@@ -37,8 +39,8 @@ export function fail (name, message, details, ci) {
3739
}
3840
}
3941

40-
// log with a waiting appearance
41-
export function wait (name, message, ci) {
42+
/** Log as a waiting state. */
43+
export const wait = (/** @type {string} */ name, /** @type {string} */ message, /** @type {boolean} */ ci) => {
4244
if (ci) {
4345
stdout.write(`${name} ${color('dim', message)}`)
4446
} else {

0 commit comments

Comments
 (0)