tsrun/ts/index.ts

43 lines
1.7 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
// lets setup things for execution
2022-03-12 13:43:54 +00:00
const smartshellInstance = new plugins.smartshell.Smartshell({
2022-03-12 20:52:14 +00:00
executor: 'bash',
2022-03-12 13:43:54 +00:00
});
2022-03-12 20:52:14 +00:00
const tsNodeLoaderPath = plugins.path.join(__dirname, 'loader.js');
2022-03-12 17:54:19 +00:00
// note: -> reduce on emtpy array does not work
// thus check needed before reducing the argv array
2022-03-12 20:52:14 +00:00
smartshellInstance.exec(
`node --loader ${tsNodeLoaderPath} ${absolutePathToTsFile} ${
process.argv.length > 0
? process.argv.reduce((prevArg, currentArg) => {
return prevArg + ' ' + currentArg;
})
: ''
}`,
2022-03-12 20:52:14 +00:00
);
2020-06-01 19:38:19 +00:00
};