forked from Costava/solitaire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed.js
186 lines (144 loc) · 5.61 KB
/
embed.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Embed CSS and JS in an HTML file
* HTML file path must be first argument. Output file is second.
* Specifying CSS and JS is optional and either can be specified first
* Usage:
* node embed.js index.html output.html --css style.css style2.css --js script.js script2.js
*/
const fs = require('fs');
var fse = require('fs-extra');
//////////
var htmlReg = /.html$/;
var cssReg = /.css$/;
var jsReg = /.js$/;
//////////
// Cut off node and script file path
var args = process.argv.slice(2);
////////// Check if input file is .html and open it
var inputHTMLFilePath = args[0];
var inputIsHTMLFile = htmlReg.test(inputHTMLFilePath);
if (!inputIsHTMLFile) {
console.log(`Error: Input file is not HTML: ${htmlFilePath}`);
return 1;
}
var html = fs.readFileSync(inputHTMLFilePath, {encoding: 'utf-8'});
var outputLocation = args[1];
//////////
var cssOptionIndex = args.indexOf('--css');
if (cssOptionIndex != -1) {
var cssFilePaths = [];
// Get all CSS file paths
for (var i = cssOptionIndex + 1; i < args.length; i += 1) {
var current = args[i];
// Break if begin specifying something else
if (current.substr(0, 2) == '--') {
break;
}
if (!cssReg.test(current)) {
console.log(`WARNING: File may not be CSS: ${current}`);
}
cssFilePaths.push(current);
}
// console.log(`CSS files: ${cssFilePaths}`);
// Embed CSS files
for (var i = 0; i < cssFilePaths.length; i += 1) {
var current = cssFilePaths[i];
console.log(`Embedding ${current} in ${inputHTMLFilePath}`);
var css = fs.readFileSync(current, {encoding: 'utf-8'});
html = html.replace(
/^(\t*)<\/head>/m,
`$1\t<style>\n${css}\n$1\t</style>\n$1</head>`
);
// console.log(html);
}
}
//////////
var jsOptionIndex = args.indexOf('--js');
if (jsOptionIndex != -1) {
var jsFilePaths = [];
// Get all JS file paths
for (var i = jsOptionIndex + 1; i < args.length; i += 1) {
var current = args[i];
// Break if begin specifying something else
if (current.substr(0, 2) == '--') {
break;
}
if (!jsReg.test(current)) {
console.log(`WARNING: File may not be JS: ${current}`);
}
jsFilePaths.push(current);
}
// console.log(`JS files: ${jsFilePaths}`);
// Embed JS files
for (var i = 0; i < jsFilePaths.length; i += 1) {
var current = jsFilePaths[i];
console.log(`Embedding ${current} in ${inputHTMLFilePath}`);
var js = fs.readFileSync(current, {encoding: 'utf-8'});
html = html.replace(
/^(\t*)<\/body>/m,
`$1\t<script>\n${js}\n$1\t</script>\n$1</body>`
);
// console.log(html);
}
}
//////////
fse.outputFileSync(outputLocation, html);
console.log(`HTML file with embeds output to ${outputLocation}`);
//error in your code are mentioned here solving would be necessary
js: "main.js", line 37: illegal character
js: console.log(`Error: Input file is not HTML: ${htmlFilePath}`);
js: .............^
js: "main.js", line 37: missing ) after argument list
js: console.log(`Error: Input file is not HTML: ${htmlFilePath}`);
js: ...................^
js: "main.js", line 37: illegal character
js: console.log(`Error: Input file is not HTML: ${htmlFilePath}`);
js: ............................................................^
js: "main.js", line 37: syntax error
js: console.log(`Error: Input file is not HTML: ${htmlFilePath}`);
js: .............................................................^
js: "main.js", line 39: invalid return
js: return 1;
js: .......^
js: "main.js", line 63: illegal character
js: console.log(`WARNING: File may not be CSS: ${current}`);
js: ...............^
js: "main.js", line 63: missing ) after argument list
js: console.log(`WARNING: File may not be CSS: ${current}`);
js: .......................^
js: "main.js", line 63: illegal character
js: console.log(`WARNING: File may not be CSS: ${current}`);
js: ........................................................^
js: "main.js", line 63: syntax error
js: console.log(`WARNING: File may not be CSS: ${current}`);
js: .........................................................^
js: "main.js", line 75: illegal character
js: console.log(`Embedding ${current} in ${inputHTMLFilePath}`);
js: ..............^
js: "main.js", line 75: missing ) after argument list
js: console.log(`Embedding ${current} in ${inputHTMLFilePath}`);
js: ........................^
js: "main.js", line 75: illegal character
js: console.log(`Embedding ${current} in ${inputHTMLFilePath}`);
js: ...........................................................^
js: "main.js", line 75: syntax error
js: console.log(`Embedding ${current} in ${inputHTMLFilePath}`);
js: ............................................................^
js: "main.js", line 81: illegal character
js: `$1\t<style>\n${css}\n$1\t</style>\n$1</head>`
js: ...^
js: "main.js", line 81: illegal character
js: `$1\t<style>\n${css}\n$1\t</style>\n$1</head>`
js: .......^
js: "main.js", line 81: missing ) after argument list
js: `$1\t<style>\n${css}\n$1\t</style>\n$1</head>`
js: .......^
js: "main.js", line 81: illegal character
js: `$1\t<style>\n${css}\n$1\t</style>\n$1</head>`
js: ......................................^
js: "main.js", line 81: missing } in compound statement
js: `$1\t<style>\n${css}\n$1\t</style>\n$1</head>`
js: ......................................^
js: "main.js", line 81: unterminated regular expression literal
js: `$1\t<style>\n${css}\n$1\t</style>\n$1</head>`
js: ................................................^