Skip to content

Commit dc1a3ef

Browse files
committed
Add acron and remove trailing comma
1 parent 23e043d commit dc1a3ef

File tree

5 files changed

+99
-49
lines changed

5 files changed

+99
-49
lines changed

.prettierrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{}
1+
{
2+
"trailingComma": "es5"
3+
}

compile.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const UglifyJS = require("uglify-js");
33
const fs = require("fs");
44
const esprima = require("esprima");
55
const path = require("path");
6+
const acorn = require("acorn");
67

78
const GREEN = "\x1b[32m%s\x1b[0m";
89
const YELLOW = "\x1b[33m%s\x1b[0m";
@@ -30,21 +31,21 @@ const fillTemplate = (template, { overwriteOptions = null } = {}) => {
3031
return template
3132
.replace(
3233
/\{\{\s?nginxHost\s?\}\}/gi,
33-
'<!--# echo var="http_host" default="" -->',
34+
'<!--# echo var="http_host" default="" -->'
3435
)
3536
.replace(
3637
/\{\{\s?nginxProxyHost\s?\}\}/gi,
37-
'<!--# echo var="proxy_hostname" default="" --><!--# echo var="proxy_path" default="/simple" -->',
38+
'<!--# echo var="proxy_hostname" default="" --><!--# echo var="proxy_path" default="/simple" -->'
3839
)
3940
.replace(
4041
/\\?"\{\{\s?overwriteOptions\s?\}\}\\?"/gi,
4142
overwriteOptions
4243
? JSON.stringify(overwriteOptions).replace(/:"([^"]+)"/gi, ":$1")
43-
: "{}",
44+
: "{}"
4445
)
4546
.replace(
4647
/"\{\{\s?cloudFlareCustomDomain\s?\}\}"/gi,
47-
'INSTALL_OPTIONS.custom_domain || "queue.simpleanalyticscdn.com"',
48+
'INSTALL_OPTIONS.custom_domain || "queue.simpleanalyticscdn.com"'
4849
);
4950
};
5051

@@ -284,7 +285,7 @@ const files = templates.reduce((list, template) => {
284285
if (template.variables.sri) {
285286
list.push(
286287
{ ...template, variables: { ...template.variables, sri: true } },
287-
{ ...template, variables: { ...template.variables, sri: false } },
288+
{ ...template, variables: { ...template.variables, sri: false } }
288289
);
289290
} else {
290291
list.push(template);
@@ -320,7 +321,7 @@ for (const file of files) {
320321
.digest("hex")
321322
.slice(0, 4);
322323

323-
const prepend = `/* Simple Analytics - Privacy friendly analytics (docs.simpleanalytics.com/script; ${date}; ${hash}${
324+
const prepend = `/* Simple Analytics - Privacy-first analytics (docs.simpleanalytics.com/script; ${date}; ${hash}${
324325
variables.sri ? `; SRI-version` : ""
325326
}${variables.version ? `; v${variables.version}` : ""}) */\n`;
326327

@@ -346,7 +347,7 @@ for (const file of files) {
346347
filename: originalFileName,
347348
url: `${finalFileName}.map`,
348349
},
349-
},
350+
}
350351
)
351352
: {
352353
code: prepend + rawCode,
@@ -358,6 +359,31 @@ for (const file of files) {
358359
for (const warning of warnings || [])
359360
console.warn(YELLOW, `[${name}] ${warning}`);
360361

362+
try {
363+
acorn.parse(rawCode, { ecmaVersion: 5 });
364+
} catch (error) {
365+
// console.log("acorn", error);
366+
367+
// error object:
368+
// pos: 147,
369+
// loc: Position { line: 12, column: 0 },
370+
// raisedAt: 148
371+
372+
// Find part of the code that is causing the error
373+
const lines = rawCode.split("\n");
374+
const line = error.loc.line;
375+
const column = error.loc.column;
376+
const start = Math.max(0, line - 3);
377+
const end = Math.min(lines.length, line + 3);
378+
const codeSnippet = lines.slice(start, end).join("\n");
379+
const sentence = lines[error.loc.line - 1];
380+
381+
// console.log({ codeSnippet, line: lines[error.loc.line - 1] });
382+
383+
throw new Error(
384+
`${error.message} at line ${sentence} position ${column}: ${codeSnippet}`
385+
);
386+
}
361387
const code = fillTemplate(codeTemplate, variables);
362388

363389
const validate = template({
@@ -374,7 +400,7 @@ for (const file of files) {
374400
RED,
375401
`[${name}][ERROR] ${input
376402
.split("/")
377-
.pop()} ${description} at line ${lineNumber} position ${index}`,
403+
.pop()} ${description} at line ${lineNumber} position ${index}`
378404
);
379405
continue;
380406
}
@@ -402,15 +428,15 @@ for (const file of files) {
402428

403429
let write = code.replace(
404430
/sourceMappingURL=latest\.js\.map/gi,
405-
`sourceMappingURL=${cdnFileName}.map`,
431+
`sourceMappingURL=${cdnFileName}.map`
406432
);
407433

408434
fs.writeFileSync(versionFile, write);
409435

410436
if (compiledMap) {
411437
let writeCompiled = compiledMap.replace(
412438
/latest\.source\.js/gi,
413-
cdnFileName,
439+
cdnFileName
414440
);
415441

416442
fs.writeFileSync(`${versionFile}.map`, writeCompiled);
@@ -438,7 +464,7 @@ for (const file of files) {
438464
console.log(
439465
` ${name.toLowerCase()} ${fill1}Compiled ${sourceName} ${fill2} ${bytesZeroFilled} bytes ${
440466
variables.sri ? " (SRI)" : ""
441-
}`,
467+
}`
442468
);
443469
}
444470

package-lock.json

Lines changed: 27 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"pretest": "npm run build -- testing",
1111
"test": "node -r dotenv/config ./test/index.js",
1212
"posttest": "npm run build",
13-
"prettier": "prettier --write ."
13+
"prettier": "prettier --check ."
1414
},
1515
"repository": {
1616
"type": "git",
@@ -24,6 +24,7 @@
2424
},
2525
"homepage": "https://github.com/simpleanalytics/scripts#readme",
2626
"devDependencies": {
27+
"acorn": "^8.11.3",
2728
"browserstack-local": "^1.5.1",
2829
"chai": "^4.2.0",
2930
"dotenv": "^8.2.0",

0 commit comments

Comments
 (0)