|
1 | 1 | // Reads the apglib.css file and outputs a JavaScript function
|
| 2 | + |
2 | 3 | // which will return the CSS as a string.
|
3 |
| -module.exports = function (inFile, outFile) { |
4 |
| - "use strict"; |
| 4 | +module.exports = function cssToJs(inFile, outFile) { |
| 5 | + // function capFirst(str) { |
| 6 | + // return str.charAt(0).toUpperCase() + str.slice(1); |
| 7 | + // } |
5 | 8 |
|
6 |
| - function capFirst(str) { |
7 |
| - return str.charAt(0).toUpperCase() + str.slice(1); |
8 |
| - } |
| 9 | + function getFileExtension(filename) { |
| 10 | + // eslint-disable-next-line no-bitwise |
| 11 | + return filename.slice(((filename.lastIndexOf('.') - 1) >>> 0) + 2); |
| 12 | + } |
9 | 13 |
|
10 |
| - function getFileExtension(filename) { |
11 |
| - return filename.slice(((filename.lastIndexOf(".") - 1) >>> 0) + 2); |
| 14 | + const fs = require('fs'); |
| 15 | + try { |
| 16 | + /* validate arguments */ |
| 17 | + if (!inFile) { |
| 18 | + throw new Error('missing input CSS file (name.css)'); |
12 | 19 | }
|
13 |
| - |
14 |
| - let fs = require("fs"); |
15 |
| - try { |
16 |
| - /* validate arguments */ |
17 |
| - if (!inFile) { |
18 |
| - throw new Error("missing input CSS file (name.css)"); |
19 |
| - } |
20 |
| - if (getFileExtension(inFile) !== "css") { |
21 |
| - throw new Error("input CSS file (" + inFile + ") must have .css extension"); |
22 |
| - } |
23 |
| - if (!outFile) { |
24 |
| - throw new Error("missing output JavaScript file (name.js)"); |
25 |
| - } |
26 |
| - if (getFileExtension(outFile) !== "js") { |
27 |
| - throw new Error("output JavaScript file (" + outFile + ") must have .js extension"); |
28 |
| - } |
29 |
| - let cssBuf = fs.readFileSync(inFile); |
30 |
| - let cssStr = ""; |
31 |
| - debugger; |
32 |
| - for (let i = 0; i < cssBuf.length; i += 1) { |
33 |
| - let char = cssBuf[i]; |
34 |
| - if (char >= 32 && char <= 38) { |
35 |
| - cssStr += String.fromCharCode(char); |
36 |
| - } else if (char >= 40 && char <= 91) { |
37 |
| - cssStr += String.fromCharCode(char); |
38 |
| - } else if (char >= 93 && char <= 126) { |
39 |
| - cssStr += String.fromCharCode(char); |
40 |
| - } else { |
41 |
| - switch (char) { |
42 |
| - case 9: |
43 |
| - cssStr += "\\t"; |
44 |
| - break; |
45 |
| - case 10: |
46 |
| - cssStr += "\\n"; |
47 |
| - break; |
48 |
| - case 13: |
49 |
| - cssStr += "\\r"; |
50 |
| - break; |
51 |
| - case 39: |
52 |
| - cssStr += "\\'"; |
53 |
| - break; |
54 |
| - case 92: |
55 |
| - cssStr += "\\\\"; |
56 |
| - break; |
57 |
| - default: |
58 |
| - /* ignore invalid string characters */ |
59 |
| - break; |
60 |
| - } |
61 |
| - } |
| 20 | + if (getFileExtension(inFile) !== 'css') { |
| 21 | + throw new Error(`input CSS file (${inFile}) must have .css extension`); |
| 22 | + } |
| 23 | + if (!outFile) { |
| 24 | + throw new Error('missing output JavaScript file (name.js)'); |
| 25 | + } |
| 26 | + if (getFileExtension(outFile) !== 'js') { |
| 27 | + throw new Error(`output JavaScript file (${outFile}) must have .js extension`); |
| 28 | + } |
| 29 | + const cssBuf = fs.readFileSync(inFile); |
| 30 | + let cssStr = ''; |
| 31 | + for (let i = 0; i < cssBuf.length; i += 1) { |
| 32 | + const char = cssBuf[i]; |
| 33 | + if (char >= 32 && char <= 38) { |
| 34 | + cssStr += String.fromCharCode(char); |
| 35 | + } else if (char >= 40 && char <= 91) { |
| 36 | + cssStr += String.fromCharCode(char); |
| 37 | + } else if (char >= 93 && char <= 126) { |
| 38 | + cssStr += String.fromCharCode(char); |
| 39 | + } else { |
| 40 | + switch (char) { |
| 41 | + case 9: |
| 42 | + cssStr += '\\t'; |
| 43 | + break; |
| 44 | + case 10: |
| 45 | + cssStr += '\\n'; |
| 46 | + break; |
| 47 | + case 13: |
| 48 | + cssStr += '\\r'; |
| 49 | + break; |
| 50 | + case 39: |
| 51 | + cssStr += "\\'"; |
| 52 | + break; |
| 53 | + case 92: |
| 54 | + cssStr += '\\\\'; |
| 55 | + break; |
| 56 | + default: |
| 57 | + /* ignore invalid string characters */ |
| 58 | + break; |
62 | 59 | }
|
63 |
| - |
64 |
| - let js = "// This module has been developed programmatically in the `apg-lib` build process.\n"; |
65 |
| - js += |
66 |
| - "// It is used to build web pages programatically on the fly without the need for <script> or <style> tags.\n"; |
67 |
| - js += "\n"; |
68 |
| - js += "module.exports = function(){\n"; |
69 |
| - js += "return '" + cssStr + "';"; |
70 |
| - js += "\n}\n"; |
71 |
| - fs.writeFileSync(outFile, js); |
72 |
| - } catch (e) { |
73 |
| - console.log("cssToJS.js: EXCEPTION: \n"); |
74 |
| - console.log(e.message); |
| 60 | + } |
75 | 61 | }
|
| 62 | + |
| 63 | + let js = '// This module has been developed programmatically in the `apg-lib` build process.\n'; |
| 64 | + js += |
| 65 | + '// It is used to build web pages programatically on the fly without the need for <script> or <style> tags.\n'; |
| 66 | + js += '\n'; |
| 67 | + js += 'module.exports = function emittcss(){\n'; |
| 68 | + js += `return '${cssStr}';`; |
| 69 | + js += '\n}\n'; |
| 70 | + fs.writeFileSync(outFile, js); |
| 71 | + } catch (e) { |
| 72 | + console.log('cssToJS.js: EXCEPTION: \n'); |
| 73 | + console.log(e.message); |
| 74 | + } |
76 | 75 | };
|
0 commit comments