|
| 1 | +// eslint-disable-next-line n/no-unsupported-features/node-builtins |
| 2 | +const fs = require("fs").promises; |
| 3 | +const path = require("path"); |
| 4 | + |
| 5 | +/* eslint-disable no-console */ |
| 6 | + |
| 7 | +const randomBytesFallback = ` |
| 8 | +var g = typeof globalThis !== 'undefined' ? globalThis : global; |
| 9 | +var crypto = g.crypto || {}; |
| 10 | +
|
| 11 | +if (typeof crypto.getRandomValues !== 'function') { |
| 12 | + var nodeCrypto = require('crypto'); |
| 13 | +
|
| 14 | + crypto.getRandomValues = function(typedArray) { |
| 15 | + var bytes = nodeCrypto.randomBytes(typedArray.byteLength); |
| 16 | +
|
| 17 | + new Uint8Array( |
| 18 | + typedArray.buffer, |
| 19 | + typedArray.byteOffset, |
| 20 | + typedArray.byteLength |
| 21 | + ).set(bytes); |
| 22 | +
|
| 23 | + return typedArray; |
| 24 | + }; |
| 25 | +} |
| 26 | +`; |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {string} src source path |
| 30 | + * @param {string} dest destination path |
| 31 | + * @returns {Promise<void>} |
| 32 | + */ |
| 33 | +async function copyIfChanged(src, dest) { |
| 34 | + let srcContent; |
| 35 | + |
| 36 | + try { |
| 37 | + srcContent = await fs.readFile(src, "utf8"); |
| 38 | + srcContent = `// @ts-nocheck\n${randomBytesFallback}${srcContent}`; |
| 39 | + } catch (_err) { |
| 40 | + srcContent = null; |
| 41 | + } |
| 42 | + |
| 43 | + let destContent; |
| 44 | + try { |
| 45 | + destContent = await fs.readFile(dest, "utf8"); |
| 46 | + } catch (_err) { |
| 47 | + destContent = null; |
| 48 | + } |
| 49 | + |
| 50 | + if ( |
| 51 | + srcContent === null || |
| 52 | + destContent === null || |
| 53 | + srcContent !== destContent |
| 54 | + ) { |
| 55 | + if (process.argv.includes("--check")) { |
| 56 | + throw new Error(`Content mismatch between ${src} and ${dest}`); |
| 57 | + } |
| 58 | + |
| 59 | + await fs.writeFile(dest, srcContent); |
| 60 | + console.log("File copied: content changed."); |
| 61 | + } else { |
| 62 | + console.log("No copying required: the content is identical."); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +const src = path.resolve( |
| 67 | + __dirname, |
| 68 | + "../node_modules/serialize-javascript/index.js", |
| 69 | +); |
| 70 | +const dest = path.resolve(__dirname, "../src/serialize-javascript.js"); |
| 71 | + |
| 72 | +copyIfChanged(src, dest); |
0 commit comments