From 68a3e8867bc18d470d12510e550cfe210a0badb1 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 27 Aug 2020 19:51:04 +0200 Subject: [PATCH] populate lib dir entirely in postinstall script --- lib/olm/olm.js | 1 - lib/olm/olm.wasm | 1 - lib/olm/olm_legacy.js | 1 - scripts/common.mjs | 12 ++++++++++++ scripts/post-install.mjs | 22 +++++++++++++++++----- 5 files changed, 29 insertions(+), 8 deletions(-) delete mode 120000 lib/olm/olm.js delete mode 120000 lib/olm/olm.wasm delete mode 120000 lib/olm/olm_legacy.js create mode 100644 scripts/common.mjs diff --git a/lib/olm/olm.js b/lib/olm/olm.js deleted file mode 120000 index 8bedac52..00000000 --- a/lib/olm/olm.js +++ /dev/null @@ -1 +0,0 @@ -../../node_modules/olm/olm.js \ No newline at end of file diff --git a/lib/olm/olm.wasm b/lib/olm/olm.wasm deleted file mode 120000 index 39293356..00000000 --- a/lib/olm/olm.wasm +++ /dev/null @@ -1 +0,0 @@ -../../node_modules/olm/olm.wasm \ No newline at end of file diff --git a/lib/olm/olm_legacy.js b/lib/olm/olm_legacy.js deleted file mode 120000 index 140fffb8..00000000 --- a/lib/olm/olm_legacy.js +++ /dev/null @@ -1 +0,0 @@ -../../node_modules/olm/olm_legacy.js \ No newline at end of file diff --git a/scripts/common.mjs b/scripts/common.mjs new file mode 100644 index 00000000..e135022a --- /dev/null +++ b/scripts/common.mjs @@ -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; + } + } +} diff --git a/scripts/post-install.mjs b/scripts/post-install.mjs index 8c00d2a0..328f8c7c 100644 --- a/scripts/post-install.mjs +++ b/scripts/post-install.mjs @@ -23,6 +23,7 @@ import { dirname } from 'path'; // needed to translate commonjs modules to esm import commonjs from '@rollup/plugin-commonjs'; // multi-entry plugin so we can add polyfill file to main +import {removeDirIfExists} from "./common.mjs"; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); @@ -41,12 +42,23 @@ async function commonjsToESM(src, dst) { await fs.writeFile(dst, code, "utf8"); } -async function transpile() { - await fs.mkdir(path.join(projectDir, "lib/another-json/")); +async function populateLib() { + 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( - path.join(projectDir, 'node_modules/another-json/another-json.js'), - path.join(projectDir, "lib/another-json/index.js") + path.join(modulesDir, 'another-json/another-json.js'), + path.join(libDir, "another-json/index.js") ); } -transpile(); +populateLib();