Skip to content

Commit

Permalink
Update meow dependency (#41)
Browse files Browse the repository at this point in the history
* refactor: update source to ES modules

Update the source code from CommonJS to ES Modules style. This consists
of changing `require`s to `import from` statements, `module.exports` to
`export` statements, and in some cases the structure of imports and
exports as well. Notably when the old `require` statement also contained
some additional logic, which is not possible with import statements.

* chore(deps): update meow from ^3.7.0 to ^10.1.1

Update the meow dependency to ^10.1.1 - which is the latest version -
and update how it is used in accordance with breaking changes.

* refactor: update tests to ES modules

* chore: update package manifest

Update the exports field for exporting ESM and CJS and set package
type to "module".

* docs: update JS API example

* docs: update config example to use ESM syntax

* ci: run tests on Node v12, v14, v16, and v17

* chore: make svglint config file an ESM

* fix: loading config files and built-in rules on Windows

Use the `"file://"` prefix when loading CLI config and rules to ensure
importing always works on Windows.

* ci: run CLI on various node versions and operating systems

* feat: add CJS version of Node API

Add a CommonJS version of the Node API of SVGLint by building it using
Rollup. Update the package manifest to allow for importing/requiring ESM
and CJS (resp.) and `.gitignore` to ignore the generated CJS file.

One "custom" Rollup plugin (provided by Rollup but not included by
default) is used, namely `plugin-dynamic-import-vars`. This plugin
ensures that dynamic imports are properly handled as the CJS output
generated by Rollup cannot dynamically import ESM rules. To make this
work the rule-loader had to be updated as well, unfortunately the
requirements of this plugin are quite strict and it is required that the
`"../rules"` path is hardcoded into the `import` statement. Furthermore
it has trouble detecting things that should be ignored, so errors are
disabled to prevent errors due to dynamic imports of external plugins.

* chore: add .npmrc to enforce `engine-strict` option

BREAKING CHANGE: Minimum node version increased to `^12.20.0` or
`^14.13.1` or `>=16.0.0`.
BREAKING CHANGE: The Node API is now fully `async`.
  • Loading branch information
ericcornelissen authored Dec 18, 2021
1 parent 8e77613 commit 2d242fe
Show file tree
Hide file tree
Showing 37 changed files with 1,165 additions and 835 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module.exports = {
"es6": true,
"jasmine": true
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2020,
},
"extends": "eslint:recommended",
"rules": {
"indent": ["error", 4, { "SwitchCase": 1 }],
Expand Down
33 changes: 32 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ jobs:
test:
name: Run tests
runs-on: ubuntu-18.04
strategy:
matrix:
node-version:
- 12.20.0
- 14.13.1
- 16.0.0
- 12
- 14
- 16
- 17
steps:
- name: Checkout
uses: actions/checkout@v2
Expand All @@ -13,10 +23,31 @@ jobs:
- name: Node.js setup
uses: actions/setup-node@v1
with:
node-version: 12
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
- name: Lint
run: npm run lint
- name: Test
run: npm test
- name: Run CLI
run: node ./bin/cli.js test/svgs/attr.test.svg
test-cli:
name: Run CLI
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Node.js setup
uses: actions/setup-node@v1
with:
node-version: 16
- name: Install dependencies
run: npm ci
- name: Run CLI
run: node ./bin/cli.js test/svgs/attr.test.svg
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.vscode
src/svglint.cjs
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
2 changes: 1 addition & 1 deletion .svglintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
rules: {
attr: {
"rule::selector": "path",
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ $ svglint --help
The tool can also be used through the JS API.

```javascript
const SVGLint = require("svglint");
const linting = SVGLint.lintSource("<svg>...</svg>", {
import SVGLint from "svglint";

const linting = await SVGLint.lintSource("<svg>...</svg>", {
// ... config goes here
});
linting.on("done", () => {
Expand All @@ -50,7 +51,7 @@ If you are using the CLI, this configuration object is read from the file specif
This configuration file should export a single object, of the format:

```javascript
module.exports = {
export default {
// additional configuration may go here in the future
// for now, "rules" is the only useful key
rules: {
Expand All @@ -66,7 +67,7 @@ module.exports = {
function() { // config 1 for the "custom" rule }
]
}
};
}
```
For specifics on how the config for each rule should be formatted, see [their specific rule files](https://github.com/birjolaxew/svglint/tree/master/src/rules).
Expand Down
31 changes: 16 additions & 15 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
* @fileoverview The CLI that is executed from a terminal.
* Acts as an interface to the JS API
*/
const path = require("path");
const GUI = new (require("../src/cli/gui"));
const Logger = require("../src/lib/logger");
const SVGLint = require("../src/svglint");
import path from "path";
import gui from "../src/cli/gui.js";
import Logger from "../src/lib/logger.js";
import SVGLint from "../src/svglint.js";
// @ts-ignore
const meta = require("../package.json");
const { getConfigurationFile } = require("../src/cli/config");
const meow = require("meow");
const chalk = require("chalk");
const glob = require("glob");
import config from "../src/cli/config.js";
import meow from "meow";
import chalk from "chalk";
import glob from "glob";

const GUI = new gui();
const { getConfigurationFile } = config;

const logger = Logger("");
// Pretty logs all errors, then exits
Expand All @@ -23,10 +25,7 @@ process.on("uncaughtException", err => {
});

// Generates the CLI binding using meow
const cli = meow({
description: meta.description,
version: meta.version,
help: `
const cli = meow(`
${chalk.yellow("Usage:")}
${chalk.bold("svglint")} [--config config.js] [--ci] [--debug] ${chalk.bold("file1.svg file2.svg")}
Expand All @@ -35,7 +34,8 @@ const cli = meow({
${chalk.bold("--version")} Show the current SVGLint version
${chalk.bold("--config, -c")} Specify the config file. Defaults to '.svglintrc.js'
${chalk.bold("--debug, -d")} Show debug logs
${chalk.bold("--ci, -C")} Only output to stdout once, when linting is finished`,
${chalk.bold("--ci, -C")} Only output to stdout once, when linting is finished`, {
importMeta: import.meta,
flags: {
config: { type: "string", alias: "c", },
debug: { type: "boolean", alias: "d" },
Expand All @@ -62,7 +62,8 @@ process.on("exit", () => {
try {
const configFile = await getConfigurationFile(cli.flags.config);
if (configFile) {
configObj = require(configFile);
const module = await import(`file://${configFile}`);
configObj = module.default;
} else {
logger.debug("No configuration file found")
if (cli.flags.config) {
Expand Down
Loading

0 comments on commit 2d242fe

Please sign in to comment.