From a386ec5bc9f751b8de38643dc586c8b9d31aa05a Mon Sep 17 00:00:00 2001 From: Cris Stringfellow <22254235+crislin2046@users.noreply.github.com> Date: Sun, 15 Jan 2023 15:07:51 +0800 Subject: [PATCH] Resolved cross-format APP ROOT issue --- src/common.js | 10 ++++++---- src/root.cjs | 5 +++-- src/root.js | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 src/root.js diff --git a/src/common.js b/src/common.js index 45df292..fe45657 100644 --- a/src/common.js +++ b/src/common.js @@ -2,7 +2,7 @@ import path from 'path'; import {fileURLToPath} from 'url'; import fs from 'fs'; import os from 'os'; -import root from './root.cjs'; +import {APP_ROOT as __ROOT} from './root.js'; const DEEB = false; @@ -14,6 +14,10 @@ export const DEBUG = { } export const SHOW_FETCH = false; +if ( DEBUG.debug ) { + console.log({APP_ROOT}); +} + // server related export const PUBLIC_SERVER = true; @@ -75,9 +79,7 @@ export const SNIP_CONTEXT = 31; export const NO_SANDBOX = (process.env.DEBUG_22120 && process.env.SET_22120_NO_SANDBOX) || false; -//export const APP_ROOT = '.'; -export const APP_ROOT = root.APP_ROOT; -//export const APP_ROOT = path.dirname(fileURLToPath(import.meta.url)); +export const APP_ROOT = __ROOT export const sleep = ms => new Promise(res => setTimeout(res, ms)); diff --git a/src/root.cjs b/src/root.cjs index 44a03a9..9d3dda9 100644 --- a/src/root.cjs +++ b/src/root.cjs @@ -3,11 +3,12 @@ const url = require('url'); const file = __filename; const dir = path.dirname(file); +const APP_ROOT = dir; -console.log({file, dir}); +console.log({APP_ROOT}); module.exports = { - APP_ROOT: dir, + APP_ROOT, dir, file } diff --git a/src/root.js b/src/root.js new file mode 100644 index 0000000..a92afd4 --- /dev/null +++ b/src/root.js @@ -0,0 +1,22 @@ +import path from 'path'; +import url from 'url'; + +let root; +let esm = false; + +try { + console.log(__dirname, __filename); +} catch(e) { + esm = true; +} + +if ( ! esm ) { + root = require('./root.cjs').APP_ROOT; +} else { + root = path.dirname(url.fileURLToPath(import.meta.url)); +} + +console.log({root}); + +export const APP_ROOT = root; +