source: trip-planner-front/node_modules/css-has-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: 4.6 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 parser = _interopDefault(require('postcss-selector-parser'));
7var postcss = _interopDefault(require('postcss'));
8
9const selectorRegExp = /:has/;
10var plugin = postcss.plugin('css-has-pseudo', opts => {
11 const preserve = Boolean('preserve' in Object(opts) ? opts.preserve : true);
12 return root => {
13 root.walkRules(selectorRegExp, rule => {
14 const modifiedSelector = parser(selectors => {
15 selectors.walkPseudos(selector => {
16 if (selector.value === ':has' && selector.nodes) {
17 const isNotHas = checkIfParentIsNot(selector);
18 selector.value = isNotHas ? ':not-has' : ':has';
19 const attribute = parser.attribute({
20 attribute: encodeURIComponent(String(selector)).replace(/%3A/g, ':').replace(/%5B/g, '[').replace(/%5D/g, ']').replace(/%2C/g, ',').replace(/[():%\[\],]/g, '\\$&')
21 });
22
23 if (isNotHas) {
24 selector.parent.parent.replaceWith(attribute);
25 } else {
26 selector.replaceWith(attribute);
27 }
28 }
29 });
30 }).processSync(rule.selector);
31 const clone = rule.clone({
32 selector: modifiedSelector
33 });
34
35 if (preserve) {
36 rule.before(clone);
37 } else {
38 rule.replaceWith(clone);
39 }
40 });
41 };
42});
43
44function checkIfParentIsNot(selector) {
45 return Object(Object(selector.parent).parent).type === 'pseudo' && selector.parent.parent.value === ':not';
46}
47
48const fileRegExp = /^[\w\/.]+$/;
49const argRegExp = /^--(\w+)=("|')?(.+)\2$/;
50const relaxedJsonPropRegExp = /(['"])?([a-z0-9A-Z_]+)(['"])?:/g;
51const relaxedJsonValueRegExp = /("[a-z0-9A-Z_]+":\s*)(?!true|false|null|\d+)'?([A-z0-9]+)'?([,}])/g;
52const argo = process.argv.slice(2).reduce((object, arg) => {
53 const argMatch = arg.match(argRegExp);
54 const fileMatch = arg.match(fileRegExp);
55
56 if (argMatch) {
57 object[argMatch[1]] = argMatch[3];
58 } else if (fileMatch) {
59 if (object.from === '<stdin>') {
60 object.from = arg;
61 } else if (object.to === '<stdout>') {
62 object.to = arg;
63 }
64 }
65
66 return object;
67}, {
68 from: '<stdin>',
69 to: '<stdout>',
70 opts: 'null'
71}); // get css from command line arguments or stdin
72
73(argo.from === '<stdin>' ? getStdin() : readFile(argo.from)).then(css => {
74 if (argo.from === '<stdin>' && !css) {
75 console.log(['CSS Has Pseudo\n', ' Transforms CSS with :has {}\n', 'Usage:\n', ' css-has-pseudo source.css transformed.css', ' css-has-pseudo --from=source.css --to=transformed.css --opts={}', ' echo "body:has(:focus) {}" | css-has-pseudo\n'].join('\n'));
76 process.exit(0);
77 }
78
79 const pluginOpts = JSON.parse(argo.opts.replace(relaxedJsonPropRegExp, '"$2": ').replace(relaxedJsonValueRegExp, '$1"$2"$3'));
80 const processOptions = Object.assign({
81 from: argo.from,
82 to: argo.to || argo.from
83 }, argo.map ? {
84 map: JSON.parse(argo.map)
85 } : {});
86 const result = plugin.process(css, processOptions, pluginOpts);
87
88 if (argo.to === '<stdout>') {
89 return result.css;
90 } else {
91 return writeFile(argo.to, result.css).then(() => `CSS was written to "${argo.to}"`);
92 }
93}).catch(error => {
94 if (Object(error).name === 'CssSyntaxError') {
95 throw new Error(`PostCSS had trouble reading the file (${error.reason} on line ${error.line}, column ${error.column}).`);
96 }
97
98 if (Object(error).errno === -2) {
99 throw new Error(`Sorry, "${error.path}" could not be read.`);
100 }
101
102 throw error;
103}).then(result => {
104 console.log(result);
105 process.exit(0);
106}, error => {
107 console.error(Object(error).message || 'Something bad happened and we don’t even know what it was.');
108 process.exit(1);
109});
110
111function readFile(pathname) {
112 return new Promise((resolve, reject) => {
113 fs.readFile(pathname, 'utf8', (error, data) => {
114 if (error) {
115 reject(error);
116 } else {
117 resolve(data);
118 }
119 });
120 });
121}
122
123function writeFile(pathname, data) {
124 return new Promise((resolve, reject) => {
125 fs.writeFile(pathname, data, (error, content) => {
126 if (error) {
127 reject(error);
128 } else {
129 resolve(content);
130 }
131 });
132 });
133}
134
135function getStdin() {
136 return new Promise(resolve => {
137 let data = '';
138
139 if (process.stdin.isTTY) {
140 resolve(data);
141 } else {
142 process.stdin.setEncoding('utf8');
143 process.stdin.on('readable', () => {
144 let chunk;
145
146 while (chunk = process.stdin.read()) {
147 data += chunk;
148 }
149 });
150 process.stdin.on('end', () => {
151 resolve(data);
152 });
153 }
154 });
155}
Note: See TracBrowser for help on using the repository browser.