-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
28 lines (26 loc) · 943 Bytes
/
utils.js
File metadata and controls
28 lines (26 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { fileURLToPath } from 'url';
import { dirname, parse, join } from 'path';
/**
* Returns { __filename, __dirname } object defining a scripts directory and file name.
* @param {*} meta A script's meta object defining es module meta information
*/
export function getScriptMeta(meta) {
const __filename = fileURLToPath(meta.url);
const __dirname = dirname(__filename);
return { __filename, __dirname };
}
/**
* Returns true if the provided meta comes from the script that started this process.
* @param {*} meta The main script meta object usually provided by import.meta
*/
export function isMain(meta) {
const { __filename, __dirname } = getScriptMeta(meta);
const loadedScript = join(dirname(process.argv[1]), parse(process.argv[1]).name);
const match = [ join(__dirname, parse(__filename).name), __dirname ];
for(const scriptPath of match) {
if ( scriptPath == loadedScript ) {
return true;
}
}
return false;
}