0

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"
  }
}

1 Answer 1

2

Don't know if that can help, but you could try and use packages like enquirer and inquirer:

Example:

const { prompt } = require('enquirer');
 
const response = await prompt({
  type: 'input',
  name: 'username',
  message: 'What is your username?'
});
 
console.log(response); // { username: 'jonschlinkert' }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.