tsrun/ts/index.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-03-12 13:43:54 +00:00
import * as plugins from './plugins.js';
2018-06-04 21:25:19 +00:00
2022-03-12 13:43:54 +00:00
import { dirname } from 'path';
import { fileURLToPath } from 'url';
2022-03-12 13:43:54 +00:00
const __dirname = dirname(fileURLToPath(import.meta.url));
2018-06-04 21:25:19 +00:00
2022-03-12 18:13:56 +00:00
export const runPath = async (pathArg: string) => {
await runCli(pathArg);
}
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];
const absolutePathToTsFile = plugins.path.join(process.cwd(), relativePathToTsFile);
2022-03-12 13:43:54 +00:00
2022-03-12 13:52:42 +00:00
process.argv.splice(0, 3); // this ensures transparent arguments for the child process
2022-03-12 13:43:54 +00:00
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({
executor: 'bash'
});
2022-03-12 18:13:56 +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 18:13:56 +00:00
smartshellInstance.exec(`node --loader ${tsNodeLoaderPath} ${absolutePathToTsFile} ${process.argv.length > 0 ? process.argv.reduce((prevArg, currentArg) => {
2022-03-12 13:43:54 +00:00
return prevArg + ' ' + currentArg;
2022-03-12 17:50:17 +00:00
}) : ''}`);
2022-03-12 13:43:54 +00:00
2020-06-01 19:38:19 +00:00
};