Skip to content

Commit

Permalink
migrate to ecma modules
Browse files Browse the repository at this point in the history
  • Loading branch information
fpetrakov committed Oct 7, 2023
1 parent 792a01b commit 0517206
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 61 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module.exports = {
env: {
node: true,
commonjs: true,
commonjs: false,
module: true,
es2021: true,
jest: true,
},
Expand Down
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.yml
node_modules
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"useTabs": true,
"tabWidth": 4,
"arrowParens": "avoid",
"trailingComma": "all"
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Colors that contain custom properties inside are ignored:

```css
a {
color: rgb(102, 173, 221, var(--opacity));
color: rgb(102, 173, 221, var(--opacity));
}
```

Expand Down
7 changes: 3 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
verbose: true,
export default {
verbose: true,
transform: {},
};

module.exports = config;
28 changes: 25 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "convert-to-oklch",
"version": "1.2.3",
"version": "1.2.4",
"description": "CLI tool that converts rgb(), rgba(), hex, hsl() and hsla() colors to oklch() in specified CSS files.",
"main": "./src/index.js",
"bin": {
Expand All @@ -9,7 +9,7 @@
"engines": {
"node": ">=16"
},
"type": "commonjs",
"type": "module",
"files": [
"src/**/*.js",
"!**/**/*.test.js"
Expand All @@ -34,7 +34,8 @@
},
"license": "MIT",
"scripts": {
"test": "jest"
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"format": "prettier --write ."
},
"dependencies": {
"colorjs.io": "^0.4.0",
Expand All @@ -44,6 +45,7 @@
},
"devDependencies": {
"eslint": "^8.29.0",
"jest": "^28.1.3"
"jest": "^28.1.3",
"prettier": "^3.0.3"
}
}
67 changes: 35 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
#!/usr/bin/env node
const { program, Option } = require('commander')
const packageVersion = require('../package.json').version
const postcss = require('postcss')
const fs = require('fs')
const pc = require('picocolors')
const plugin = require('./plugin')
const { logError } = require('./utils')
import { Command, Option } from "commander";
import postcss from "postcss";
import fs from "fs";
import pc from "picocolors";

const PRECISION_VALUES = Array.from(Array(21), (_, index) => String(index + 1))
import plugin from "./plugin.js";
import { logError } from "./utils.js";


const program = new Command();

const PRECISION_VALUES = Array.from(Array(21), (_, index) => String(index + 1));

const precisionOption = new Option(
'-p, --precision <number>',
'precision of color conversion',
'5',
).choices(PRECISION_VALUES)
"-p, --precision <number>",
"precision of color conversion",
"5",
).choices(PRECISION_VALUES);

program
.name('convert-to-oklch')
.name("convert-to-oklch")
.description(
'CLI tool that converts rgb(), rgba(), hex, hsl() and hsla() colors to oklch() in specified CSS files.',
"CLI tool that converts rgb(), rgba(), hex, hsl() and hsla() colors to oklch() in specified CSS files.",
)
.version(packageVersion)
.argument('<path>', 'path to css files')
.version("1.2.4")
.argument("<path>", "path to css files")
.addOption(precisionOption)
.parse()
.parse();

const { args: cssFilePaths } = program
const { precision } = program.opts()
const { args: cssFilePaths } = program;
const { precision } = program.opts();

const processCssFile = async path => {
if (!fs.existsSync(path)) {
logError("File doesn't exist: " + path)
return
logError("File doesn't exist: " + path);
return;
}

const css = fs.readFileSync(path, 'utf-8')
const css = fs.readFileSync(path, "utf-8");

const convertedCss = await postcss([plugin(precision)])
.process(css, { from: path })
.toString()
.toString();

fs.writeFile(path, convertedCss, err => {
if (err) {
logError(err)
fs.writeFileSync(path, css)
logError(err);
fs.writeFileSync(path, css);
}
})
}
});
};

const processFiles = async () => {
cssFilePaths.map(file => processCssFile(file))
await Promise.allSettled(cssFilePaths)
console.log(pc.bgGreen(pc.black('Done!')))
}
cssFilePaths.map(file => processCssFile(file));
await Promise.allSettled(cssFilePaths);
console.log(pc.bgGreen(pc.black("Done!")));
};

processFiles()
processFiles();
12 changes: 6 additions & 6 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
const Color = require("colorjs.io").default;
const { logError } = require("./utils");
import Color from "colorjs.io";
import { logError } from "./utils.js";

const colorsRegex = new RegExp(/(#[0-9a-f]{3,8}|(hsla?|rgba?)\([^)]+\))/gi);

module.exports = (precision) => ({
export default precision => ({
postcssPlugin: "postcss-convert-to-oklch",
Declaration(decl) {
processDecl(decl, precision);
},
});

export const postcss = true;

function processDecl(decl, precision) {
const originalColors = decl.value.match(colorsRegex);
if (!originalColors) return;

originalColors
.filter(doesNotIncludeVar)
.map((original) => {
.map(original => {
try {
return {
original,
Expand Down Expand Up @@ -47,5 +49,3 @@ function doesNotIncludeVar(color) {
function getConvertedColor(color, precision) {
return new Color(color).to("oklch").toString({ precision });
}

module.exports.postcss = true;
11 changes: 7 additions & 4 deletions src/tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const postcss = require("postcss");
const fs = require("fs");
const path = require("path");
import postcss from "postcss";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";

const plugin = require("../plugin");
import plugin from "../plugin.js";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

it("works with default precision", async () => {
const input = readFixture("input.css");
Expand Down
8 changes: 2 additions & 6 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const pc = require("picocolors");
import pc from "picocolors";

function logError(errorMessage) {
export function logError(errorMessage) {
console.error(pc.bgRed(errorMessage));
}

module.exports = {
logError,
};

0 comments on commit 0517206

Please sign in to comment.