populate lib dir entirely in postinstall script

This commit is contained in:
Bruno Windels 2020-08-27 19:51:04 +02:00
parent b14eed9309
commit 68a3e8867b
5 changed files with 29 additions and 8 deletions

View file

@ -1 +0,0 @@
../../node_modules/olm/olm.js

View file

@ -1 +0,0 @@
../../node_modules/olm/olm.wasm

View file

@ -1 +0,0 @@
../../node_modules/olm/olm_legacy.js

12
scripts/common.mjs Normal file
View file

@ -0,0 +1,12 @@
import fsRoot from "fs";
const fs = fsRoot.promises;
export async function removeDirIfExists(targetDir) {
try {
await fs.rmdir(targetDir, {recursive: true});
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
}
}

View file

@ -23,6 +23,7 @@ import { dirname } from 'path';
// needed to translate commonjs modules to esm // needed to translate commonjs modules to esm
import commonjs from '@rollup/plugin-commonjs'; import commonjs from '@rollup/plugin-commonjs';
// multi-entry plugin so we can add polyfill file to main // multi-entry plugin so we can add polyfill file to main
import {removeDirIfExists} from "./common.mjs";
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename); const __dirname = dirname(__filename);
@ -41,12 +42,23 @@ async function commonjsToESM(src, dst) {
await fs.writeFile(dst, code, "utf8"); await fs.writeFile(dst, code, "utf8");
} }
async function transpile() { async function populateLib() {
await fs.mkdir(path.join(projectDir, "lib/another-json/")); const libDir = path.join(projectDir, "lib/");
const modulesDir = path.join(projectDir, "node_modules/");
await removeDirIfExists(libDir);
await fs.mkdir(libDir);
const olmSrcDir = path.join(modulesDir, "olm/");
const olmDstDir = path.join(libDir, "olm/");
await fs.mkdir(olmDstDir);
for (const file of ["olm.js", "olm.wasm", "olm_legacy.js"]) {
await fs.symlink(path.join(olmSrcDir, file), path.join(olmDstDir, file));
}
// transpile another-json to esm
await fs.mkdir(path.join(libDir, "another-json/"));
await commonjsToESM( await commonjsToESM(
path.join(projectDir, 'node_modules/another-json/another-json.js'), path.join(modulesDir, 'another-json/another-json.js'),
path.join(projectDir, "lib/another-json/index.js") path.join(libDir, "another-json/index.js")
); );
} }
transpile(); populateLib();