storybook/scripts/utils/esmain.ts
2024-08-08 11:23:52 +02:00

41 lines
946 B
TypeScript

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: string) {
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.
*/
export function esMain(url: string) {
if (!url || !process.argv[1]) {
return false;
}
const require = createRequire(url);
const scriptPath = require.resolve(process.argv[1]);
const modulePath = fileURLToPath(url);
const extension = path.extname(scriptPath);
if (extension) {
return modulePath === scriptPath;
}
return stripExt(modulePath) === scriptPath;
}