From e4711b6e310d2b506c88138b81cbbc67e02936b1 Mon Sep 17 00:00:00 2001 From: Nir Date: Mon, 29 Jan 2024 15:36:57 +0200 Subject: [PATCH] Throw error on multiple carmi instances only when trying to compile carmi --- bin/carmi | 10 +++++++++- index.js | 8 +++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/bin/carmi b/bin/carmi index b1f825c..1c55aed 100755 --- a/bin/carmi +++ b/bin/carmi @@ -6,7 +6,7 @@ const commandLineArgs = require('command-line-args'); const carmi = require('../index'); const path = require('path'); const fs = require('fs-extra'); -const { enableCurrentLine } = require('../src/currentLine'); +const {enableCurrentLine} = require('../src/currentLine'); const {isUpToDate, getDependenciesHashes, analyzeDependencies} = require('../src/analyze-dependencies'); const getCacheFilePath = require('../src/get-cache-file-path'); const wrapModule = require('../src/wrap-module'); @@ -14,6 +14,8 @@ const base64ArrayBuffer = require('../bytecode/base64-arraybuffer'); const {renameSync} = require('fs') const {v4: uuidv4} = require('uuid'); +const GLOBAL_TOKEN = '__$CARMI$__'; + const CACHE_SCENARIOS = { mtime: 'mtime', gitHash: 'git-hash' @@ -142,6 +144,12 @@ async function run() { throw e; } + if (global[GLOBAL_TOKEN] && global[GLOBAL_TOKEN].length > 1) { + throw new Error( + `require of multiple versions of Carmi is not supported previously loaded from:${global[GLOBAL_TOKEN]}` + ); + } + code = carmi.compile(model, options); } diff --git a/index.js b/index.js index 92ccf12..bb3bb25 100644 --- a/index.js +++ b/index.js @@ -15,12 +15,10 @@ const {getCurrentLine} = require('./src/currentLine'); const {destruct} = require('./src/utils/destruct'); const GLOBAL_TOKEN = '__$CARMI$__'; -if (global[GLOBAL_TOKEN]) { - throw new Error( - `require of multiple versions of Carmi is not supported previously loaded from:${global[GLOBAL_TOKEN]}` - ); +if (!global[GLOBAL_TOKEN]) { + global[GLOBAL_TOKEN] = [] } -global[GLOBAL_TOKEN] = getCurrentLine(); +global[GLOBAL_TOKEN].push(getCurrentLine()) const {initProxyHandler} = require('./src/proxyHandler'); const expressionBuilder = require('./src/expressionBuilder'); const unwrapableProxy = require('./src/unwrapable-proxy');