tsrun/ts/index.ts

41 lines
1.3 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);
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 13:52:42 +00:00
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;
})
: ''
}`
);
2020-06-01 19:38:19 +00:00
};