@hyperse/exec-program
v1.0.10
Published
Executes a command using `file ...arguments`, supports `.ts` file for esm type module.
Downloads
177
Readme
@hyperse/exec-program
Runs commands in your script, application or library. Unlike shells, it is optimized for programmatic usage.
README
Getting started
npm i --save @hyperse/exec-program
Usage
runTsScript
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { runTsScript } from '@hyperse/exec-program';
const getDirname = (url: string, ...paths: string[]) => {
return join(dirname(fileURLToPath(url)), ...paths);
};
const cliPath = getDirname(import.meta.url, './cli-test.ts');
const { stderr, stdout } = await runTsScript(cliPath);
console.log(stderr, stdout);
execute command
import { execute } from '@hyperse/exec-program';
const { stdout, stderr } = await execute(
'npm',
['i', '--no-save', '--no-package-lock', ...toInstall],
{
cwd: target.directory,
maxBuffer: TEN_MEGA_BYTE,
env: this.options.npmEnv,
}
);
await execute('npm', ['pack', directory], {
cwd: this.uniqueDir,
maxBuffer: TEN_MEGA_BYTE,
});
run ts file for unit testing
config tsconfig.json
{
"extends": "@hyperse/eslint-config-hyperse/tsconfig.base.json",
"compilerOptions": {
...
"baseUrl": "./src",
"rootDir": "./",
"outDir": "dist",
"types": [
"vitest/globals"
],
"paths": {
"@hyperse/exec-program": [
"../src/index.js"
],
}
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
]
create cli-test.ts
// cause of `tsconfig.json` we can directly import source .ts file from '@hyperse/exec-program';
import { runTsScript } from '@hyperse/exec-program';
console.log(typeof runTsScript);
console.log('cli...');
create test file main.spec.ts
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { runTsScript } from '@hyperse/exec-program';
const getDirname = (url: string, ...paths: string[]) => {
return join(dirname(fileURLToPath(url)), ...paths);
};
const cliPath = getDirname(import.meta.url, './cli-test.ts');
describe('test suites of exec program', () => {
it('should correct invoke cli.ts', async () => {
const { stderr, stdout } = await runTsScript(cliPath);
console.log(stderr, stdout);
expect(stderr).toBe('');
expect(stdout).toMatch(/cli.../);
});
});