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 | }
|
---|