-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.js
42 lines (35 loc) · 1.04 KB
/
main.js
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import {createRequire} from 'module';
import path from 'path';
import process from 'process';
import {fileURLToPath} from 'url';
/**
* Strip the extension from a filename if it has one.
* @param {string} name A filename.
* @return {string} The filename without a path.
*/
export function stripExt(name) {
const extension = path.extname(name);
if (!extension) {
return name;
}
return name.slice(0, -extension.length);
}
/**
* Check if a module was run directly with node as opposed to being
* imported from another module.
* @param {ImportMeta} meta The `import.meta` object.
* @return {boolean} The module was run directly with node.
*/
export default function esMain(meta) {
if (!meta || !process.argv[1]) {
return false;
}
const require = createRequire(meta.url);
const scriptPath = require.resolve(process.argv[1]);
const modulePath = fileURLToPath(meta.url);
const extension = path.extname(scriptPath);
if (extension) {
return modulePath === scriptPath;
}
return stripExt(modulePath) === scriptPath;
}