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