I want to create my own cli using typescript and node.
After building the code using tsc, I installed as a global cli using npm install -g .
When running test-cli on cmd, it is expected to print "test" on console, but instead it opens a javascript file build/index.js on the editor. It seems it opens a file instead of running the file.
While checking the test-cli.cmd file in C:\Users\MyUser\AppData\Roaming\npm, I found that it is a bit different from other successful cli files.
test-cli.cmd content (not working):
...
"%dp0%\node_modules\test-cli\build\index.js" %*
...
truffle.cmd content (working correctly):
...
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
"%_prog%" "%dp0%\node_modules\truffle\build\cli.bundled.js" %*
...
Definitely having a difference for specifiying node.exe to run that javascript file.
How can I fix this problem?
Here's my code overview:
Source tree:
|__build
| |__index.js
| |__index.map.js
|__src
| |__index.ts
|__package.json
|__tsconfig.json
index.ts just having a simple code.
// index.ts
console.log("my test cli");
Also have a simple data in package.json
// package.json
{
"name": "test-cli",
"version": "1.0.0",
"main": "src/index.ts",
"bin": {
"test-cli": "build/index.js"
},
"license": "MIT",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"@types/node": "^17.0.22",
"ts-node": "^10.7.0",
"typescript": "^4.6.2"
}
}