Skip to content

Commit

Permalink
Resolved cross-format APP ROOT issue
Browse files Browse the repository at this point in the history
  • Loading branch information
o0101 committed Jan 15, 2023
1 parent 4deb359 commit a386ec5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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));

Expand Down
5 changes: 3 additions & 2 deletions src/root.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 22 additions & 0 deletions src/root.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit a386ec5

Please sign in to comment.