source: trip-planner-front/node_modules/css-blank-pseudo/cli.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.5 KB
Line 
1#!/usr/bin/env node
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5var fs = _interopDefault(require('fs'));
6var postcss = _interopDefault(require('postcss'));
7
8const selectorRegExp = /:blank([^\w-]|$)/gi;
9var plugin = postcss.plugin('css-blank-pseudo', opts => {
10 const replaceWith = String(Object(opts).replaceWith || '[blank]');
11 const preserve = Boolean('preserve' in Object(opts) ? opts.preserve : true);
12 return root => {
13 root.walkRules(selectorRegExp, rule => {
14 const selector = rule.selector.replace(selectorRegExp, ($0, $1) => {
15 return `${replaceWith}${$1}`;
16 });
17 const clone = rule.clone({
18 selector
19 });
20
21 if (preserve) {
22 rule.before(clone);
23 } else {
24 rule.replaceWith(clone);
25 }
26 });
27 };
28});
29
30if (process.argv.length < 3) {
31 console.log(['CSS Blank Pseudo\n', ' Transforms CSS with :blank {}\n', 'Usage:\n', ' css-blank-pseudo source.css transformed.css', ' css-blank-pseudo --in=source.css --out=transformed.css --opts={}', ' echo "@media (prefers-color-scheme: dark) {}" | css-blank-pseudo\n'].join('\n'));
32 process.exit(0);
33} // get process and plugin options from the command line
34
35
36const fileRegExp = /^[\w\/.]+$/;
37const argRegExp = /^--(\w+)=("|')?(.+)\2$/;
38const relaxedJsonPropRegExp = /(['"])?([a-z0-9A-Z_]+)(['"])?:/g;
39const relaxedJsonValueRegExp = /("[a-z0-9A-Z_]+":\s*)'?([A-z0-9]+)'?([,}])/g;
40const argo = process.argv.slice(2).reduce((object, arg) => {
41 const argMatch = arg.match(argRegExp);
42 const fileMatch = arg.match(fileRegExp);
43
44 if (argMatch) {
45 object[argMatch[1]] = argMatch[3];
46 } else if (fileMatch) {
47 if (object.from === '<stdin>') {
48 object.from = arg;
49 } else if (object.to === '<stdout>') {
50 object.to = arg;
51 }
52 }
53
54 return object;
55}, {
56 from: '<stdin>',
57 to: '<stdout>',
58 opts: 'null'
59}); // get css from command line arguments or stdin
60
61(argo.from === '<stdin>' ? getStdin() : readFile(argo.from)).then(css => {
62 const pluginOpts = JSON.parse(argo.opts.replace(relaxedJsonPropRegExp, '"$2": ').replace(relaxedJsonValueRegExp, '$1"$2"$3'));
63 const processOptions = Object.assign({
64 from: argo.from,
65 to: argo.to || argo.from
66 }, argo.map ? {
67 map: JSON.parse(argo.map)
68 } : {});
69 const result = plugin.process(css, processOptions, pluginOpts);
70
71 if (argo.to === '<stdout>') {
72 return result.css;
73 } else {
74 return writeFile(argo.to, result.css).then(() => `CSS was written to "${argo.to}"`);
75 }
76}).then(result => {
77 console.log(result);
78 process.exit(0);
79}, error => {
80 console.error(error);
81 process.exit(1);
82});
83
84function readFile(pathname) {
85 return new Promise((resolve, reject) => {
86 fs.readFile(pathname, 'utf8', (error, data) => {
87 if (error) {
88 reject(error);
89 } else {
90 resolve(data);
91 }
92 });
93 });
94}
95
96function writeFile(pathname, data) {
97 return new Promise((resolve, reject) => {
98 fs.writeFile(pathname, data, (error, content) => {
99 if (error) {
100 reject(error);
101 } else {
102 resolve(content);
103 }
104 });
105 });
106}
107
108function getStdin() {
109 return new Promise(resolve => {
110 let data = '';
111
112 if (process.stdin.isTTY) {
113 resolve(data);
114 } else {
115 process.stdin.setEncoding('utf8');
116 process.stdin.on('readable', () => {
117 let chunk;
118
119 while (chunk = process.stdin.read()) {
120 data += chunk;
121 }
122 });
123 process.stdin.on('end', () => {
124 resolve(data);
125 });
126 }
127 });
128}
Note: See TracBrowser for help on using the repository browser.