From 10f2603c8ffd24b19c62037e8f27ab97e46afcd2 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman <17342435+RyanZim@users.noreply.github.com> Date: Fri, 24 Sep 2021 08:27:47 -0400 Subject: [PATCH] Port to ESM (#401) * Port to ESM * Port tests to ESM * Remove broken coveralls * Remove old nyc cleanup --- .eslintrc.yaml | 2 + .github/workflows/ci.yml | 2 - bin/postcss | 3 -- index.js | 90 ++++++++++++++++++++---------------- lib/DependencyGraph.js | 7 ++- lib/DependencyGraph.test.js | 7 ++- lib/args.js | 8 ++-- lib/getMapfile.js | 5 +- lib/getMapfile.test.js | 27 +++++++++++ package.json | 10 ++-- test/base.js | 11 ++--- test/cli.js | 9 ++-- test/config.js | 11 ++--- test/dir.js | 11 ++--- test/error.js | 13 +++--- test/ext.js | 11 ++--- test/fixtures/_bad-plugin.js | 1 - test/glob.js | 11 ++--- test/helpers/clean.js | 4 +- test/helpers/cli.js | 9 ++-- test/helpers/env.js | 13 +++--- test/helpers/read.js | 7 ++- test/helpers/tmp.js | 7 ++- test/map.js | 38 ++------------- test/misc.js | 5 +- test/parser.js | 9 ++-- test/replace.js | 13 +++--- test/stdin.js | 15 +++--- test/stdout.js | 13 +++--- test/stringifier.js | 9 ++-- test/syntax.js | 9 ++-- test/use.js | 9 ++-- test/watch.js | 44 ++++++++---------- 33 files changed, 210 insertions(+), 233 deletions(-) delete mode 100755 bin/postcss mode change 100644 => 100755 index.js create mode 100644 lib/getMapfile.test.js diff --git a/.eslintrc.yaml b/.eslintrc.yaml index 0473b0f..d838ba7 100644 --- a/.eslintrc.yaml +++ b/.eslintrc.yaml @@ -1,5 +1,7 @@ env: node: true +parserOptions: + sourceType: module extends: problems rules: no-console: off diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e0cb97d..04cd7e1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,5 +20,3 @@ jobs: node-version: ${{ matrix.node }} - run: npm install - run: npm run ci - - run: nyc report --reporter=text-lcov | coveralls - continue-on-error: true diff --git a/bin/postcss b/bin/postcss deleted file mode 100755 index d40abef..0000000 --- a/bin/postcss +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -require('../') diff --git a/index.js b/index.js old mode 100644 new mode 100755 index 21704bb..da83943 --- a/index.js +++ b/index.js @@ -1,23 +1,25 @@ -'use strict' - -const fs = require('fs-extra') -const path = require('path') - -const prettyHrtime = require('pretty-hrtime') -const stdin = require('get-stdin') -const read = require('read-cache') -const { bold, dim, red, cyan, green } = require('colorette') -const globber = require('globby') -const slash = require('slash') -const chokidar = require('chokidar') - -const postcss = require('postcss') -const postcssrc = require('postcss-load-config') -const reporter = require('postcss-reporter/lib/formatter')() - -const argv = require('./lib/args') -const createDependencyGraph = require('./lib/DependencyGraph') -const getMapfile = require('./lib/getMapfile') +#!/usr/bin/env node + +import fs from 'fs-extra' +import path from 'path' + +import prettyHrtime from 'pretty-hrtime' +import stdin from 'get-stdin' +import read from 'read-cache' +import { bold, dim, red, cyan, green } from 'colorette' +import globber from 'globby' +import slash from 'slash' +import chokidar from 'chokidar' + +import postcss from 'postcss' +import postcssrc from 'postcss-load-config' +import postcssReporter from 'postcss-reporter/lib/formatter.js' + +import argv from './lib/args.js' +import createDependencyGraph from './lib/DependencyGraph.js' +import getMapfile from './lib/getMapfile.js' + +const reporter = postcssReporter() const depGraph = createDependencyGraph() let input = argv._ @@ -25,25 +27,33 @@ const { dir, output } = argv if (argv.map) argv.map = { inline: false } -const cliConfig = { - options: { - map: argv.map !== undefined ? argv.map : { inline: true }, - parser: argv.parser ? require(argv.parser) : undefined, - syntax: argv.syntax ? require(argv.syntax) : undefined, - stringifier: argv.stringifier ? require(argv.stringifier) : undefined, - }, - plugins: argv.use - ? argv.use.map((plugin) => { - try { - return require(plugin)() - } catch (e) { - const msg = e.message || `Cannot find module '${plugin}'` - let prefix = msg.includes(plugin) ? '' : ` (${plugin})` - if (e.name && e.name !== 'Error') prefix += `: ${e.name}` - return error(`Plugin Error${prefix}: ${msg}'`) - } - }) - : [], +let cliConfig + +async function buildCliConfig() { + cliConfig = { + options: { + map: argv.map !== undefined ? argv.map : { inline: true }, + parser: argv.parser ? await import(argv.parser) : undefined, + syntax: argv.syntax ? await import(argv.syntax) : undefined, + stringifier: argv.stringifier + ? await import(argv.stringifier) + : undefined, + }, + plugins: argv.use + ? await Promise.all( + argv.use.map(async (plugin) => { + try { + return (await import(plugin)).default() + } catch (e) { + const msg = e.message || `Cannot find module '${plugin}'` + let prefix = msg.includes(plugin) ? '' : ` (${plugin})` + if (e.name && e.name !== 'Error') prefix += `: ${e.name}` + return error(`Plugin Error${prefix}: ${msg}'`) + } + }) + ) + : [], + } } let configFile @@ -61,7 +71,7 @@ if (parseInt(postcss().version) < 8) { error('Please install PostCSS 8 or above') } -Promise.resolve() +buildCliConfig() .then(() => { if (argv.watch && !(argv.output || argv.replace || argv.dir)) { error('Cannot write to stdout in watch mode') diff --git a/lib/DependencyGraph.js b/lib/DependencyGraph.js index c293cda..efb7ac3 100644 --- a/lib/DependencyGraph.js +++ b/lib/DependencyGraph.js @@ -1,8 +1,7 @@ -'use strict' -const path = require('path') -const { DepGraph } = require('dependency-graph') +import path from 'path' +import { DepGraph } from 'dependency-graph' -module.exports = function () { +export default function createDependencyGraph() { const graph = new DepGraph() return { add(message) { diff --git a/lib/DependencyGraph.test.js b/lib/DependencyGraph.test.js index a0235ba..d54393c 100644 --- a/lib/DependencyGraph.test.js +++ b/lib/DependencyGraph.test.js @@ -1,7 +1,6 @@ -'use strict' -const test = require('ava') -const path = require('path') -const createDependencyGraph = require('./DependencyGraph.js') +import test from 'ava' +import path from 'path' +import createDependencyGraph from './DependencyGraph.js' function resolveArray(arr) { return arr.map((p) => path.resolve(p)) diff --git a/lib/args.js b/lib/args.js index e25dd0d..fb38937 100644 --- a/lib/args.js +++ b/lib/args.js @@ -1,5 +1,5 @@ -'use strict' -const { bold, red } = require('colorette') +import yargs from 'yargs' +import { bold, red } from 'colorette' const logo = ` /|\\ @@ -13,7 +13,7 @@ const logo = ` //_____||___*_________*___||_____// ` -const { argv } = require('yargs') +const { argv } = yargs(process.argv.slice(2)) .usage( `${bold(red(logo))} Usage: @@ -126,4 +126,4 @@ For more details, please see https://github.com/postcss/postcss-cli` if (argv.ext && argv.ext.indexOf('.') !== 0) argv.ext = `.${argv.ext}` -module.exports = argv +export default argv diff --git a/lib/getMapfile.js b/lib/getMapfile.js index 346fea7..e4d0c93 100644 --- a/lib/getMapfile.js +++ b/lib/getMapfile.js @@ -1,6 +1,5 @@ -'use strict' -const path = require('path') -module.exports = function getMapfile(options) { +import path from 'path' +export default function getMapfile(options) { if (options.map && typeof options.map.annotation === 'string') { return `${path.dirname(options.to)}/${options.map.annotation}` } diff --git a/lib/getMapfile.test.js b/lib/getMapfile.test.js new file mode 100644 index 0000000..becc460 --- /dev/null +++ b/lib/getMapfile.test.js @@ -0,0 +1,27 @@ +import test from 'ava' +import getMapfile from './getMapfile.js' + +test('mapFile path is properly resolved', async (t) => { + const paths = [ + { + input: { to: '/foo/bar.css/baz/index.css' }, + want: '/foo/bar.css/baz/index.css.map', + }, + { + input: { to: '/foo/bar.sss/baz/index.sss' }, + want: '/foo/bar.sss/baz/index.sss.map', + }, + { + input: { to: '/foo/bar.css/baz/bar.css' }, + want: '/foo/bar.css/baz/bar.css.map', + }, + { + input: { map: { annotation: 'foo.map' }, to: '/foo/bar.css/baz/bar.css' }, + want: '/foo/bar.css/baz/foo.map', + }, + ] + + for (const p of paths) { + t.is(getMapfile(p.input), p.want) + } +}) diff --git a/package.json b/package.json index a235e25..d06f1f3 100644 --- a/package.json +++ b/package.json @@ -2,20 +2,20 @@ "name": "postcss-cli", "version": "8.3.1", "description": "CLI for PostCSS", - "main": "index.js", + "type": "module", "engines": { "node": ">=12" }, "bin": { - "postcss": "./bin/postcss" + "postcss": "./index.js" }, "scripts": { - "ci": "eslint . && nyc ava -v && npm run prettier -- --list-different", + "ci": "eslint . && c8 ava -v && npm run prettier -- --list-different", "clean": "node test/helpers/clean.js", "prettier": "prettier --single-quote --no-semi \"**/*.{js,md}\"", "format": "npm run prettier -- --write && eslint . --fix", "pretest": "npm run clean && npm run format", - "test": "nyc ava -v" + "test": "c8 ava -v" }, "dependencies": { "chokidar": "^3.3.0", @@ -33,10 +33,10 @@ }, "devDependencies": { "ava": "^3.1.0", + "c8": "^7.9.0", "coveralls": "^3.0.0", "eslint": "^7.8.0", "eslint-config-problems": "5.0.0", - "nyc": "^15.0.0", "postcss": "^8.0.4", "postcss-import": "^14.0.0", "prettier": "~2.4.0", diff --git a/test/base.js b/test/base.js index 32eff27..496ca14 100644 --- a/test/base.js +++ b/test/base.js @@ -1,10 +1,9 @@ -'use strict' -const test = require('ava') -const path = require('path') +import test from 'ava' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--base --dir works', async (t) => { const dir = tmp() diff --git a/test/cli.js b/test/cli.js index 0cdc7dc..3adab9b 100644 --- a/test/cli.js +++ b/test/cli.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('works with defaults', async (t) => { const output = tmp('output.css') diff --git a/test/config.js b/test/config.js index b8b3de5..a4eb8c6 100644 --- a/test/config.js +++ b/test/config.js @@ -1,11 +1,10 @@ -'use strict' -const test = require('ava') -const path = require('path') +import test from 'ava' +import path from 'path' -const ENV = require('./helpers/env.js') +import ENV from './helpers/env.js' -const cli = require('./helpers/cli.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import read from './helpers/read.js' test('supports common config', async (t) => { const env = `module.exports = { diff --git a/test/dir.js b/test/dir.js index ec4e60e..695f198 100644 --- a/test/dir.js +++ b/test/dir.js @@ -1,10 +1,9 @@ -'use strict' -const test = require('ava') -const path = require('path') +import test from 'ava' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--dir works', async (t) => { const dir = tmp() diff --git a/test/error.js b/test/error.js index 38b972b..9901f47 100644 --- a/test/error.js +++ b/test/error.js @@ -1,8 +1,7 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const tmp = require('./helpers/tmp.js') -const cli = require('./helpers/cli.js') +import tmp from './helpers/tmp.js' +import cli from './helpers/cli.js' test('multiple input files && --output', (t) => { return cli(['test/fixtures/*.css', '-o', tmp()]).then(({ error, code }) => { @@ -34,7 +33,7 @@ test('plugin not found', (t) => { t.is(code, 1, 'expected non-zero error code') t.regex( error.toString(), - /Plugin Error: Cannot find module 'postcss-plugin'/ + /Plugin Error: Cannot find package 'postcss-plugin'/ ) } ) @@ -44,12 +43,12 @@ test('plugin throws on require', (t) => { return cli([ 'test/fixtures/a.css', '-u', - './test/fixtures/_bad-plugin', + './test/fixtures/_bad-plugin.js', '-o', tmp(), ]).then(({ error, code }) => { t.is(code, 1, 'expected non-zero error code') - t.regex(error.toString(), /Plugin Error \(.*bad-plugin\): This fails/) + t.regex(error.toString(), /Plugin Error \(.*bad-plugin.js\): This fails/) }) }) diff --git a/test/ext.js b/test/ext.js index c646630..329f2c4 100644 --- a/test/ext.js +++ b/test/ext.js @@ -1,11 +1,10 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') +import fs from 'fs-extra' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' test('--ext works', async (t) => { const dir = tmp() diff --git a/test/fixtures/_bad-plugin.js b/test/fixtures/_bad-plugin.js index 7a4ffeb..2911e95 100644 --- a/test/fixtures/_bad-plugin.js +++ b/test/fixtures/_bad-plugin.js @@ -1,2 +1 @@ -'use strict' throw new Error('This fails') diff --git a/test/glob.js b/test/glob.js index e60b75a..33917ad 100644 --- a/test/glob.js +++ b/test/glob.js @@ -1,10 +1,9 @@ -'use strict' -const test = require('ava') -const path = require('path') +import test from 'ava' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('works with glob patterns', async (t) => { const output = tmp() diff --git a/test/helpers/clean.js b/test/helpers/clean.js index cabfc72..73bb191 100644 --- a/test/helpers/clean.js +++ b/test/helpers/clean.js @@ -1,10 +1,8 @@ -'use strict' -const fs = require('fs-extra') +import fs from 'fs-extra' Promise.all([ fs.emptyDir('./test/fixtures/.tmp/'), fs.remove('./coverage'), - fs.remove('./.nyc_output'), ]).catch((err) => { console.error(err) process.exit(1) diff --git a/test/helpers/cli.js b/test/helpers/cli.js index 64b9154..565d3ff 100644 --- a/test/helpers/cli.js +++ b/test/helpers/cli.js @@ -1,11 +1,10 @@ -'use strict' -const path = require('path') -const { exec } = require('child_process') +import path from 'path' +import { exec } from 'child_process' -module.exports = function (args, cwd) { +export default function (args, cwd) { return new Promise((resolve) => { exec( - `node ${path.resolve('bin/postcss')} ${args.join(' ')}`, + `node ${path.resolve('index.js')} ${args.join(' ')}`, { cwd }, (error, stdout, stderr) => { resolve({ diff --git a/test/helpers/env.js b/test/helpers/env.js index b389370..e6f48c2 100644 --- a/test/helpers/env.js +++ b/test/helpers/env.js @@ -1,11 +1,10 @@ -'use strict' -const fs = require('fs-extra') -const path = require('path') -const globby = require('globby') +import fs from 'fs-extra' +import path from 'path' +import globby from 'globby' -const tmp = require('./tmp.js') +import tmp from './tmp.js' -module.exports = function (config, fixtures) { +export default function (config, fixtures) { fixtures = fixtures || '**/*' const dir = tmp() @@ -15,6 +14,6 @@ module.exports = function (config, fixtures) { return fs.copy(path.join('test/fixtures', item), path.join(dir, item)) }) }), - fs.outputFile(path.join(dir, 'postcss.config.js'), config), + fs.outputFile(path.join(dir, 'postcss.config.cjs'), config), ]).then(() => dir) } diff --git a/test/helpers/read.js b/test/helpers/read.js index 88b6421..1dbe451 100644 --- a/test/helpers/read.js +++ b/test/helpers/read.js @@ -1,8 +1,7 @@ -'use strict' -const { readFile } = require('fs-extra') +import fs from 'fs-extra' -module.exports = function (path) { - return readFile(path, 'utf8').then( +export default function (path) { + return fs.readFile(path, 'utf8').then( (content) => content.replace(/\r\n/g, '\n') // normalize line endings on Windows ) } diff --git a/test/helpers/tmp.js b/test/helpers/tmp.js index a23fa35..bfa1369 100644 --- a/test/helpers/tmp.js +++ b/test/helpers/tmp.js @@ -1,8 +1,7 @@ -'use strict' -const path = require('path') -const { v4: uuid } = require('uuid') +import path from 'path' +import { v4 as uuid } from 'uuid' -module.exports = function (ext) { +export default function (ext) { ext = ext || '' return path.join('test/fixtures/.tmp', uuid(), ext) diff --git a/test/map.js b/test/map.js index 04f90fd..4783010 100644 --- a/test/map.js +++ b/test/map.js @@ -1,12 +1,9 @@ -'use strict' -const test = require('ava') -const fs = require('fs-extra') +import test from 'ava' +import fs from 'fs-extra' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') - -const getMapfile = require('../lib/getMapfile') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('inline maps are generated by default', async (t) => { const output = tmp('output.css') @@ -56,28 +53,3 @@ test('--no-map disables internal sourcemaps', async (t) => { t.notRegex(await read(output), /\/*# sourceMappingURL=/) }) - -test('mapFile path is property resolved', async (t) => { - const paths = [ - { - input: { to: '/foo/bar.css/baz/index.css' }, - want: '/foo/bar.css/baz/index.css.map', - }, - { - input: { to: '/foo/bar.sss/baz/index.sss' }, - want: '/foo/bar.sss/baz/index.sss.map', - }, - { - input: { to: '/foo/bar.css/baz/bar.css' }, - want: '/foo/bar.css/baz/bar.css.map', - }, - { - input: { map: { annotation: 'foo.map' }, to: '/foo/bar.css/baz/bar.css' }, - want: '/foo/bar.css/baz/foo.map', - }, - ] - - for (const p of paths) { - t.is(getMapfile(p.input), p.want) - } -}) diff --git a/test/misc.js b/test/misc.js index 9240e32..812b6d0 100644 --- a/test/misc.js +++ b/test/misc.js @@ -1,7 +1,6 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') +import cli from './helpers/cli.js' test('--help', async (t) => { const help = await cli(['--help']) diff --git a/test/parser.js b/test/parser.js index 1ac9805..689d9b7 100644 --- a/test/parser.js +++ b/test/parser.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--parser works', async (t) => { const output = tmp('output.css') diff --git a/test/replace.js b/test/replace.js index f2712a2..dd24b98 100644 --- a/test/replace.js +++ b/test/replace.js @@ -1,12 +1,11 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') +import fs from 'fs-extra' +import path from 'path' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--replace works', async (t) => { const dir = tmp() diff --git a/test/stdin.js b/test/stdin.js index 8709a94..9fb0679 100644 --- a/test/stdin.js +++ b/test/stdin.js @@ -1,18 +1,17 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') -const { exec } = require('child_process') +import fs from 'fs-extra' +import path from 'path' +import { exec } from 'child_process' -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test.cb('reads from stdin', (t) => { const output = tmp('output.css') const cp = exec( - `node ${path.resolve('bin/postcss')} -o ${output} --no-map`, + `node ${path.resolve('index.js')} -o ${output} --no-map`, (error, stdout, stderr) => { if (error) t.end(error, stderr) diff --git a/test/stdout.js b/test/stdout.js index 8bd5d7b..68b6b4b 100644 --- a/test/stdout.js +++ b/test/stdout.js @@ -1,16 +1,15 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') -const { exec } = require('child_process') +import fs from 'fs-extra' +import path from 'path' +import { exec } from 'child_process' -const read = require('./helpers/read.js') +import read from './helpers/read.js' test.cb('writes to stdout', (t) => { const cp = exec( `node ${path.resolve( - 'bin/postcss' + 'index.js' )} --parser sugarss -u postcss-import --no-map`, (error, stdout, stderr) => { if (error) t.end(error, stderr) diff --git a/test/stringifier.js b/test/stringifier.js index db3ef28..69adaff 100644 --- a/test/stringifier.js +++ b/test/stringifier.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--stringifier works', async (t) => { const output = tmp('output.sss') diff --git a/test/syntax.js b/test/syntax.js index c4295f9..b17e1fd 100644 --- a/test/syntax.js +++ b/test/syntax.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--syntax works', async (t) => { const output = tmp('output.sss') diff --git a/test/use.js b/test/use.js index 25400f6..c35d6cd 100644 --- a/test/use.js +++ b/test/use.js @@ -1,9 +1,8 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const cli = require('./helpers/cli.js') -const tmp = require('./helpers/tmp.js') -const read = require('./helpers/read.js') +import cli from './helpers/cli.js' +import tmp from './helpers/tmp.js' +import read from './helpers/read.js' test('--use works', async (t) => { const output = tmp('i.css') diff --git a/test/watch.js b/test/watch.js index 3bc8368..b38b81d 100644 --- a/test/watch.js +++ b/test/watch.js @@ -1,14 +1,13 @@ -'use strict' -const test = require('ava') +import test from 'ava' -const fs = require('fs-extra') -const path = require('path') -const { exec, spawn } = require('child_process') -const chokidar = require('chokidar') +import fs from 'fs-extra' +import path from 'path' +import { exec, spawn } from 'child_process' +import chokidar from 'chokidar' -const ENV = require('./helpers/env.js') -const read = require('./helpers/read.js') -const tmp = require('./helpers/tmp.js') +import ENV from './helpers/env.js' +import read from './helpers/read.js' +import tmp from './helpers/tmp.js' // XXX: All the tests in this file are skipped on the CI; too flacky there const testCb = process.env.CI ? test.cb.skip : test.cb @@ -51,9 +50,7 @@ testCb('--watch works', (t) => { watcher.on('ready', () => { // Using exec() and quoting "*.css" to test watch's glob handling: cp = exec( - `node ${path.resolve( - 'bin/postcss' - )} "*.css" -o output.css --no-map -w`, + `node ${path.resolve('index.js')} "*.css" -o output.css --no-map -w`, { cwd: dir } ) cp.on('error', t.end) @@ -121,7 +118,7 @@ testCb('--watch dependencies', (t) => { watcher.on('ready', () => { cp = exec( `node ${path.resolve( - 'bin/postcss' + 'index.js' )} import.css -o output.css -u postcss-import -w --no-map`, { cwd: dir } ) @@ -172,7 +169,7 @@ testCb("--watch doesn't exit on CssSyntaxError", (t) => { let killed = false const cp = exec( - `node ${path.resolve('bin/postcss')} a.css -o output.css -w --no-map`, + `node ${path.resolve('index.js')} a.css -o output.css -w --no-map`, { cwd: dir } ) cp.on('error', t.end) @@ -207,10 +204,9 @@ testCb("--watch doesn't exit on CssSyntaxError", (t) => { testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => { t.plan(1) - const cp = spawn( - `./bin/postcss test/fixtures/a.css -o ${tmp()} -w --no-map`, - { shell: true } - ) + const cp = spawn(`./index.js test/fixtures/a.css -o ${tmp()} -w --no-map`, { + shell: true, + }) cp.on('error', t.end) cp.on('exit', (code) => { @@ -227,7 +223,7 @@ testCb('--watch watches dependencies', (t) => { ENV('', ['s.css', 'a.css', 'b.css']).then((dir) => { fs.writeFile( - path.join(dir, 'postcss.config.js'), + path.join(dir, 'postcss.config.cjs'), ` const fs = require('fs') module.exports = { @@ -281,7 +277,7 @@ testCb('--watch watches dependencies', (t) => { // Using exec() and quoting "*.css" to test watch's glob handling: cp = exec( `node ${path.resolve( - 'bin/postcss' + 'index.js' )} "s.css" -o output.css --no-map -w`, { cwd: dir } ) @@ -321,7 +317,7 @@ testCb('--watch watches directory dependencies', (t) => { ENV('', ['s.css', 'base/level-1/b.css', 'base/level-1/level-2/a.css']).then( (dir) => { fs.writeFile( - path.join(dir, 'postcss.config.js'), + path.join(dir, 'postcss.config.cjs'), ` const fs = require('fs') module.exports = { @@ -382,7 +378,7 @@ testCb('--watch watches directory dependencies', (t) => { // Using exec() and quoting "*.css" to test watch's glob handling: cp = exec( `node ${path.resolve( - 'bin/postcss' + 'index.js' )} "s.css" -o output.css --no-map -w`, { cwd: dir } ) @@ -430,7 +426,7 @@ testCb( 'base/level-1/level-2/unrelated.md', ]).then((dir) => { fs.writeFile( - path.join(dir, 'postcss.config.js'), + path.join(dir, 'postcss.config.cjs'), ` const fs = require('fs') module.exports = { @@ -490,7 +486,7 @@ testCb( watcher.on('ready', () => { cp = exec( `node ${path.resolve( - 'bin/postcss' + 'index.js' )} "s.css" -o output.css --no-map -w`, { cwd: dir } )