2020-11-14 17:42:36 -05:00
|
|
|
import fs from "fs";
|
|
|
|
import { dirname, join } from "path";
|
|
|
|
|
|
|
|
import { getOrdered } from "../depgraph";
|
|
|
|
import { resolve } from "../path";
|
|
|
|
import { loadJson, mkdir } from "../utils";
|
|
|
|
import { colorify } from "../log";
|
|
|
|
|
2021-01-19 05:22:54 -05:00
|
|
|
//function diff(a: string, b: string): boolean {
|
|
|
|
// return (Buffer.compare(fs.readFileSync(a), fs.readFileSync(b)) !== 0);
|
|
|
|
//}
|
2020-11-14 17:42:36 -05:00
|
|
|
|
|
|
|
async function alias(name: string): Promise<void> {
|
|
|
|
console.log(` Aliasing: ${ name }`);
|
|
|
|
|
|
|
|
const baseDir = resolve("packages", name);
|
|
|
|
|
|
|
|
const info = loadJson(resolve(baseDir, "package.json"));
|
|
|
|
const replacements: Record<string, string> = info["_ethers.alias"] || { };
|
|
|
|
const skip = Object.keys(replacements).reduce((accum, key) => {
|
|
|
|
const replace = replacements[key];
|
|
|
|
accum[replace] = true;
|
|
|
|
accum[replace + ".map"] = true;
|
|
|
|
accum[replace.replace(/\.js$/, ".d.ts")] = true;
|
2021-01-19 05:22:54 -05:00
|
|
|
accum[replace.replace(/\.js$/, ".d.ts.map")] = true;
|
2020-11-14 17:42:36 -05:00
|
|
|
return accum;
|
|
|
|
}, <Record<string, boolean>>({ }));
|
|
|
|
|
|
|
|
const transforms: Array<{ input: string, output: string, transform?: (content: string) => string }> = [ ];
|
|
|
|
const recurse = function(input: string, output: string) {
|
|
|
|
fs.readdirSync(join(baseDir, input)).forEach((filename) => {
|
|
|
|
const stat = fs.statSync(join(baseDir, input, filename));
|
|
|
|
if (stat.isDirectory()) {
|
|
|
|
recurse(join(input, filename), join(output, filename));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (skip[filename]) { return; }
|
|
|
|
|
|
|
|
let inputFilename = filename;
|
|
|
|
let transform: (content: string) => string = null;
|
|
|
|
|
|
|
|
if (filename.match(/\.js(on)?$/)) {
|
|
|
|
// JavaScript; swap in any replacement
|
|
|
|
// e.g. (filename = geturl.js) => (inputFilename = browser-geturl.js)
|
|
|
|
// + transform (//# sourceMappingURL=browser-geturl.js.map) to
|
|
|
|
// (//# sourceMappingURL=geturl.js.map)
|
|
|
|
const replace = replacements[filename];
|
|
|
|
|
|
|
|
// Skip!
|
|
|
|
if (replace === "") { return; }
|
|
|
|
|
|
|
|
if (replace) {
|
|
|
|
inputFilename = replace;
|
|
|
|
transform = function(content: string) {
|
2021-10-16 01:53:12 -04:00
|
|
|
content = content.replace(/^(\/\/# sourceMappingURL=)(.*)$/mg, (all, prefix, mapFilename) => {
|
2020-11-14 17:42:36 -05:00
|
|
|
return prefix + filename + ".map";
|
|
|
|
});
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (filename.match(/\.d\.ts$/)) {
|
|
|
|
// TypeScript definietion file
|
|
|
|
// e.g. (filename = geturl.d.ts) => diff(geturl.d.ts, browser-geturl.d.ts)
|
|
|
|
// We do not need to swap anything out, but we need to ensure
|
|
|
|
// the definition of the node and browser are identical
|
|
|
|
const replace = replacements[filename.replace(/\.d\.ts$/i, ".js")];
|
|
|
|
|
|
|
|
// Skip!
|
|
|
|
if (replace === "") { return; }
|
|
|
|
|
|
|
|
if (replace) {
|
|
|
|
inputFilename = replace.replace(/\.js$/i, ".d.ts");
|
2021-01-19 05:22:54 -05:00
|
|
|
transform = function(content: string) {
|
|
|
|
content = content.replace(/(\/\/# sourceMappingURL=)(.*)$/g, (all, prefix, mapFilename) => {
|
|
|
|
return prefix + filename + ".map";
|
|
|
|
});
|
|
|
|
return content;
|
2020-11-14 17:42:36 -05:00
|
|
|
}
|
2021-01-19 05:22:54 -05:00
|
|
|
//if (diff(join(baseDir, input, filename), join(baseDir, input, inputFilename))) {
|
|
|
|
// console.log(`Warning: TypeScript Definition files differ: ${ filename } != ${ inputFilename }`);
|
|
|
|
//}
|
2020-11-14 17:42:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (filename.match(/\.map$/)) {
|
|
|
|
// Map files; swap in the replacement
|
|
|
|
// e.g. (filename = geturl.js.map) => (inputFilename = browser-geturl.js.map)
|
|
|
|
// + transform the map JSON to reference "geturl.js"
|
|
|
|
// We need to swap in the replacement and update its data
|
2021-01-19 05:22:54 -05:00
|
|
|
const replace = replacements[filename.replace(/\.d.ts\.map$|\.js\.map$/i, ".js")];
|
2020-11-14 17:42:36 -05:00
|
|
|
|
|
|
|
// Skip!
|
|
|
|
if (replace === "") { return; }
|
|
|
|
|
|
|
|
if (replace) {
|
2021-01-19 05:22:54 -05:00
|
|
|
if (filename.match(/\.js\.map$/)) {
|
|
|
|
inputFilename = replace + ".map";
|
|
|
|
} else if (filename.match(/\.d\.ts\.map$/)) {
|
|
|
|
inputFilename = replace.replace(/\.js$/, ".d.ts.map");
|
|
|
|
} else {
|
|
|
|
throw new Error(`unhandled map extension: ${ filename }`);
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:42:36 -05:00
|
|
|
transform = function(content: string) {
|
|
|
|
const data = JSON.parse(content);
|
2021-01-19 05:22:54 -05:00
|
|
|
data["file"] = filename.replace(/\.map$/, "");
|
2020-11-14 17:42:36 -05:00
|
|
|
return JSON.stringify(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
transforms.push({ input: join(input, inputFilename), output: join(output, filename), transform });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
recurse("lib._esm", "lib.esm");
|
|
|
|
|
|
|
|
transforms.forEach(({ input, output, transform }) => {
|
|
|
|
const sourceFile = join(baseDir, input);
|
|
|
|
let content = fs.readFileSync(sourceFile).toString();
|
|
|
|
|
|
|
|
if (transform) { content = transform(content); }
|
|
|
|
|
|
|
|
const targetFile = join(baseDir, output);
|
|
|
|
const targetDir = dirname(targetFile);
|
|
|
|
mkdir(targetDir);
|
|
|
|
|
|
|
|
fs.writeFileSync(targetFile, content);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
(async function() {
|
|
|
|
console.log(colorify.bold(`Aliasing Node ESM to Browser ESM...`));
|
|
|
|
const dirnames = getOrdered(true);
|
|
|
|
for (let i = 0; i < dirnames.length; i++) {
|
2021-10-16 01:53:12 -04:00
|
|
|
//if (dirnames[i] !== "signing-key") { continue; }
|
2020-11-14 17:42:36 -05:00
|
|
|
await alias(dirnames[i]);
|
|
|
|
}
|
|
|
|
})();
|