css-prefers-color-scheme 3.1.0 → 3.1.1

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.
Files changed (2) hide show
  1. package/cli.js +116 -0
  2. package/package.json +3 -2
package/cli.js ADDED
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const prefersColorScheme = require('./postcss');
5
+
6
+ if (process.argv.length < 3) {
7
+ console.log([
8
+ 'Prefers Color Scheme\n',
9
+ ' Transforms CSS with @media (prefers-color-scheme) {}\n',
10
+ 'Usage:\n',
11
+ ' css-prefers-color-scheme source.css transformed.css',
12
+ ' css-prefers-color-scheme --in=source.css --out=transformed.css --opts={}',
13
+ ' echo "@media (prefers-color-scheme: dark) {}" | css-prefers-color-scheme\n'
14
+ ].join('\n'));
15
+ process.exit(0);
16
+ }
17
+
18
+ // get process and plugin options from the command line
19
+ const fileRegExp = /^[\w\/.]+$/;
20
+ const argRegExp = /^--(\w+)=("|')?(.+)\2$/;
21
+ const relaxedJsonRegExp = /(['"])?([a-z0-9A-Z_]+)(['"])?:/g;
22
+ const argo = process.argv.slice(2).reduce(
23
+ (object, arg) => {
24
+ const argMatch = arg.match(argRegExp);
25
+ const fileMatch = arg.match(fileRegExp);
26
+
27
+ if (argMatch) {
28
+ object[argMatch[1]] = argMatch[3];
29
+ } else if (fileMatch) {
30
+ if (object.from === '<stdin>') {
31
+ object.from = arg;
32
+ } else if (object.to === '<stdout>') {
33
+ object.to = arg;
34
+ }
35
+ }
36
+
37
+ return object;
38
+ },
39
+ { from: '<stdin>', to: '<stdout>', opts: 'null' }
40
+ );
41
+
42
+ // get css from command line arguments or stdin
43
+ (argo.from === '<stdin>' ? getStdin() : readFile(argo.from))
44
+ .then(css => {
45
+ const pluginOpts = JSON.parse(argo.opts.replace(relaxedJsonRegExp, '"$2": '));
46
+ const processOptions = Object.assign({ from: argo.from, to: argo.to || argo.from }, argo.map ? { map: JSON.parse(argo.map) } : {});
47
+
48
+ const result = prefersColorScheme.process(css, processOptions, pluginOpts);
49
+
50
+ if (argo.to === '<stdout>') {
51
+ return result.css;
52
+ } else {
53
+ return writeFile(argo.to, result.css).then(
54
+ () => `CSS was written to "${argo.to}"`
55
+ )
56
+ }
57
+ }).then(
58
+ result => {
59
+ console.log(result);
60
+
61
+ process.exit(0);
62
+ },
63
+ error => {
64
+ console.error(error);
65
+
66
+ process.exit(1);
67
+ }
68
+ );
69
+
70
+ function readFile(pathname) {
71
+ return new Promise((resolve, reject) => {
72
+ fs.readFile(pathname, 'utf8', (error, data) => {
73
+ if (error) {
74
+ reject(error);
75
+ } else {
76
+ resolve(data);
77
+ }
78
+ });
79
+ });
80
+ }
81
+
82
+ function writeFile(pathname, data) {
83
+ return new Promise((resolve, reject) => {
84
+ fs.writeFile(pathname, data, (error, content) => {
85
+ if (error) {
86
+ reject(error);
87
+ } else {
88
+ resolve(content);
89
+ }
90
+ });
91
+ });
92
+ }
93
+
94
+ function getStdin() {
95
+ return new Promise(resolve => {
96
+ let data = '';
97
+
98
+ if (process.stdin.isTTY) {
99
+ resolve(data);
100
+ } else {
101
+ process.stdin.setEncoding('utf8');
102
+
103
+ process.stdin.on('readable', () => {
104
+ let chunk;
105
+
106
+ while (chunk = process.stdin.read()) {
107
+ data += chunk;
108
+ }
109
+ });
110
+
111
+ process.stdin.on('end', () => {
112
+ resolve(data);
113
+ });
114
+ }
115
+ });
116
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-prefers-color-scheme",
3
- "version": "3.1.0",
3
+ "version": "3.1.1",
4
4
  "description": "Use light and dark color schemes in all browsers",
5
5
  "author": "Jonathan Neal <jonathantneal@hotmail.com>",
6
6
  "license": "CC0-1.0",
@@ -10,12 +10,13 @@
10
10
  "main": "index.js",
11
11
  "module": "index.mjs",
12
12
  "bin": {
13
- "css-prefers-color-scheme": "src/postcss-cli.js"
13
+ "css-prefers-color-scheme": "cli.js"
14
14
  },
15
15
  "files": [
16
16
  "browser.js",
17
17
  "browser.js.map",
18
18
  "browser.min.js",
19
+ "cli.js",
19
20
  "index.mjs",
20
21
  "index.mjs.map",
21
22
  "index.js",