tsrun/ts/index.ts

29 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-03-12 13:43:54 +00:00
import * as plugins from './plugins.js';
2022-03-12 20:59:14 +00:00
const __dirname = plugins.path.dirname(plugins.url.fileURLToPath(import.meta.url));
2018-06-04 21:25:19 +00:00
2022-03-12 20:59:14 +00:00
export const runPath = async (pathArg: string, fromFileUrl?: string) => {
pathArg = fromFileUrl
? plugins.path.join(plugins.path.dirname(plugins.url.fileURLToPath(fromFileUrl)), pathArg)
: pathArg;
2022-03-12 18:13:56 +00:00
await runCli(pathArg);
2022-03-12 20:52:14 +00:00
};
2022-03-12 18:13:56 +00:00
export const runCli = async (pathArg?: string) => {
2018-06-25 08:23:15 +00:00
// contents of argv array
// process.argv[0] -> node Executable
// process.argv[1] -> tsrun executable
2022-03-12 18:13:56 +00:00
const relativePathToTsFile = pathArg ? pathArg : process.argv[2];
2022-03-12 20:52:14 +00:00
const absolutePathToTsFile = plugins.path.isAbsolute(relativePathToTsFile)
? relativePathToTsFile
: plugins.path.join(process.cwd(), relativePathToTsFile);
2022-03-12 23:01:40 +00:00
// we want to have command line arguments available in the child process.
// when we have a path sepcified through a function there is one argeument less to pay respect to.
// thus when pathArg is specifed -> we only splice 2
2022-03-12 23:02:20 +00:00
pathArg ? process.argv.splice(0, 2) : process.argv.splice(0, 3); // this ensures transparent arguments for the child process
2022-03-12 18:13:56 +00:00
const tsx = await import('tsx/esm/api');
const unregister = tsx.register();
await import(absolutePathToTsFile);
2020-06-01 19:38:19 +00:00
};