@projectwallace/format-css 2.2.6 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -5
- package/dist/cli.mjs +96 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -65,7 +65,9 @@ Need more examples?
|
|
|
65
65
|
1. Multiline tokens like **Selectors, Values, etc.** are rendered on a single line
|
|
66
66
|
1. Unknown syntax is rendered as-is, with multi-line formatting kept intact
|
|
67
67
|
|
|
68
|
-
##
|
|
68
|
+
## Options
|
|
69
|
+
|
|
70
|
+
### Minify CSS
|
|
69
71
|
|
|
70
72
|
This package also exposes a minifier function since minifying CSS follows many of the same rules as formatting.
|
|
71
73
|
|
|
@@ -79,7 +81,7 @@ let minified = minify('a {}')
|
|
|
79
81
|
let formatted_mini = format('a {}', { minify: true })
|
|
80
82
|
```
|
|
81
83
|
|
|
82
|
-
|
|
84
|
+
### Tab size
|
|
83
85
|
|
|
84
86
|
For cases where you cannot control the tab size with CSS there is an option to override the default tabbed indentation with N spaces.
|
|
85
87
|
|
|
@@ -91,8 +93,36 @@ let formatted = format('a { color: red; }', {
|
|
|
91
93
|
})
|
|
92
94
|
```
|
|
93
95
|
|
|
96
|
+
## CLI
|
|
97
|
+
|
|
98
|
+
This library also ships a CLI tools that's a small wrapper around the library.
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
USAGE
|
|
102
|
+
format-css [options] [file...]
|
|
103
|
+
cat styles.css | format-css [options]
|
|
104
|
+
|
|
105
|
+
OPTIONS
|
|
106
|
+
--minify Minify the CSS output
|
|
107
|
+
--tab-size=<n> Use N spaces for indentation instead of tabs
|
|
108
|
+
--help, -h Show this help
|
|
109
|
+
|
|
110
|
+
EXAMPLES
|
|
111
|
+
# Format a file
|
|
112
|
+
format-css styles.css
|
|
113
|
+
|
|
114
|
+
# Format with 2-space indentation
|
|
115
|
+
format-css styles.css --tab-size=2
|
|
116
|
+
|
|
117
|
+
# Minify
|
|
118
|
+
format-css styles.css --minify
|
|
119
|
+
|
|
120
|
+
# Via pipe
|
|
121
|
+
cat styles.css | format-css
|
|
122
|
+
```
|
|
123
|
+
|
|
94
124
|
## Related projects
|
|
95
125
|
|
|
96
|
-
- [Format CSS online](https://www.projectwallace.com/prettify-css
|
|
97
|
-
- [Minify CSS online](https://www.projectwallace.com/minify-css
|
|
98
|
-
- [CSS Analyzer](https://github.com/projectwallace/css-analyzer) - The best CSS analyzer that powers all analysis on [projectwallace.com](https://www.projectwallace.com
|
|
126
|
+
- [Format CSS online](https://www.projectwallace.com/prettify-css) - See this formatter in action online!
|
|
127
|
+
- [Minify CSS online](https://www.projectwallace.com/minify-css) - See this minifier in action online!
|
|
128
|
+
- [CSS Analyzer](https://github.com/projectwallace/css-analyzer) - The best CSS analyzer that powers all analysis on [projectwallace.com](https://www.projectwallace.com)
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { parseArgs, styleText } from "node:util";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { resolve, sep } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { format } from "@projectwallace/format-css";
|
|
7
|
+
//#region src/cli/cli.ts
|
|
8
|
+
function help() {
|
|
9
|
+
return `
|
|
10
|
+
${styleText("bold", "USAGE")}
|
|
11
|
+
format-css [options] [file...]
|
|
12
|
+
cat styles.css | format-css [options]
|
|
13
|
+
|
|
14
|
+
${styleText("bold", "OPTIONS")}
|
|
15
|
+
--minify Minify the CSS output
|
|
16
|
+
--tab-size=<n> Use N spaces for indentation instead of tabs
|
|
17
|
+
--help, -h Show this help
|
|
18
|
+
|
|
19
|
+
${styleText("bold", "EXAMPLES")}
|
|
20
|
+
${styleText("dim", "# Format a file")}
|
|
21
|
+
format-css styles.css
|
|
22
|
+
|
|
23
|
+
${styleText("dim", "# Format with 2-space indentation")}
|
|
24
|
+
format-css styles.css --tab-size=2
|
|
25
|
+
|
|
26
|
+
${styleText("dim", "# Minify")}
|
|
27
|
+
format-css styles.css --minify
|
|
28
|
+
|
|
29
|
+
${styleText("dim", "# Via pipe")}
|
|
30
|
+
cat styles.css | format-css
|
|
31
|
+
`.trim();
|
|
32
|
+
}
|
|
33
|
+
function parse_arguments(args) {
|
|
34
|
+
const { values, positionals } = parseArgs({
|
|
35
|
+
args,
|
|
36
|
+
allowPositionals: true,
|
|
37
|
+
options: {
|
|
38
|
+
minify: {
|
|
39
|
+
type: "boolean",
|
|
40
|
+
default: false
|
|
41
|
+
},
|
|
42
|
+
"tab-size": { type: "string" }
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
const issues = [];
|
|
46
|
+
let tab_size;
|
|
47
|
+
if (values["tab-size"] !== void 0) {
|
|
48
|
+
tab_size = Number(values["tab-size"]);
|
|
49
|
+
if (isNaN(tab_size) || tab_size < 1) issues.push("--tab-size must be a positive integer");
|
|
50
|
+
}
|
|
51
|
+
const cwd = process.cwd();
|
|
52
|
+
const files = [];
|
|
53
|
+
for (const file of positionals) {
|
|
54
|
+
const resolved = resolve(file);
|
|
55
|
+
if (resolved !== cwd && !resolved.startsWith(cwd + sep)) issues.push(`Invalid path: ${file}`);
|
|
56
|
+
else files.push(resolved);
|
|
57
|
+
}
|
|
58
|
+
if (issues.length > 0) throw new Error(issues.join("\n"));
|
|
59
|
+
return {
|
|
60
|
+
files,
|
|
61
|
+
minify: values.minify ?? false,
|
|
62
|
+
tab_size
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function run(args, io) {
|
|
66
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
67
|
+
io.write(help() + "\n");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const { files, minify, tab_size } = parse_arguments(args);
|
|
71
|
+
const options = {
|
|
72
|
+
minify,
|
|
73
|
+
tab_size
|
|
74
|
+
};
|
|
75
|
+
if (files.length > 0) for (const file of files) io.write(format(io.readFile(file), options));
|
|
76
|
+
else if (!io.isTTY) io.write(format(await io.readStdin(), options));
|
|
77
|
+
else io.write(help() + "\n");
|
|
78
|
+
}
|
|
79
|
+
async function read_stdin() {
|
|
80
|
+
const chunks = [];
|
|
81
|
+
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
82
|
+
return Buffer.concat(chunks).toString("utf-8");
|
|
83
|
+
}
|
|
84
|
+
if (process.argv[1] === fileURLToPath(import.meta.url)) try {
|
|
85
|
+
await run(process.argv.slice(2), {
|
|
86
|
+
readFile: (path) => readFileSync(path, "utf-8"),
|
|
87
|
+
readStdin: read_stdin,
|
|
88
|
+
write: (output) => process.stdout.write(output),
|
|
89
|
+
isTTY: process.stdin.isTTY === true
|
|
90
|
+
});
|
|
91
|
+
} catch (error) {
|
|
92
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
93
|
+
process.exit(1);
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { parse_arguments, run };
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@projectwallace/format-css",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Fast, small, zero-config library to format or minify CSS with basic rules.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"css",
|
|
@@ -20,6 +20,9 @@
|
|
|
20
20
|
"type": "git",
|
|
21
21
|
"url": "git+https://github.com/projectwallace/format-css.git"
|
|
22
22
|
},
|
|
23
|
+
"bin": {
|
|
24
|
+
"format-css": "./dist/cli.mjs"
|
|
25
|
+
},
|
|
23
26
|
"files": [
|
|
24
27
|
"dist"
|
|
25
28
|
],
|
|
@@ -42,6 +45,7 @@
|
|
|
42
45
|
},
|
|
43
46
|
"devDependencies": {
|
|
44
47
|
"@codecov/rollup-plugin": "^1.9.1",
|
|
48
|
+
"@types/node": "^25.5.0",
|
|
45
49
|
"@vitest/coverage-v8": "^4.0.3",
|
|
46
50
|
"oxfmt": "^0.36.0",
|
|
47
51
|
"oxlint": "^1.24.0",
|
|
@@ -51,7 +55,7 @@
|
|
|
51
55
|
"vitest": "^4.0.3"
|
|
52
56
|
},
|
|
53
57
|
"engines": {
|
|
54
|
-
"node": ">=
|
|
58
|
+
"node": ">=20.12.0"
|
|
55
59
|
},
|
|
56
60
|
"issues": "https://github.com/projectwallace/format-css/issues"
|
|
57
61
|
}
|