-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathcopy-serialize-javascript.js
More file actions
72 lines (59 loc) · 1.68 KB
/
copy-serialize-javascript.js
File metadata and controls
72 lines (59 loc) · 1.68 KB
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
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const fs = require("fs").promises;
const path = require("path");
/* eslint-disable no-console */
const randomBytesFallback = `
var g = typeof globalThis !== 'undefined' ? globalThis : global;
var crypto = g.crypto || {};
if (typeof crypto.getRandomValues !== 'function') {
var nodeCrypto = require('crypto');
crypto.getRandomValues = function(typedArray) {
var bytes = nodeCrypto.randomBytes(typedArray.byteLength);
new Uint8Array(
typedArray.buffer,
typedArray.byteOffset,
typedArray.byteLength
).set(bytes);
return typedArray;
};
}
`;
/**
* @param {string} src source path
* @param {string} dest destination path
* @returns {Promise<void>}
*/
async function copyIfChanged(src, dest) {
let srcContent;
try {
srcContent = await fs.readFile(src, "utf8");
srcContent = `// @ts-nocheck\n${randomBytesFallback}${srcContent}`;
} catch (_err) {
srcContent = null;
}
let destContent;
try {
destContent = await fs.readFile(dest, "utf8");
} catch (_err) {
destContent = null;
}
if (
srcContent === null ||
destContent === null ||
srcContent !== destContent
) {
if (process.argv.includes("--check")) {
throw new Error(`Content mismatch between ${src} and ${dest}`);
}
await fs.writeFile(dest, srcContent);
console.log("File copied: content changed.");
} else {
console.log("No copying required: the content is identical.");
}
}
const src = path.resolve(
__dirname,
"../node_modules/serialize-javascript/index.js",
);
const dest = path.resolve(__dirname, "../src/serialize-javascript.js");
copyIfChanged(src, dest);