marko-js / cli

command-line tools for Marko
MIT License
94 stars 36 forks source link

npm init behaves different than node src/bin #213

Closed leoj3n closed 1 year ago

leoj3n commented 1 year ago

Manually running from cloned repo in marko-js/cli/packages/create:

❯ node src/bin --template tags-api
✔ Type your project name · my-app
✔ Project created! To get started, run:

    cd my-app
    npm run dev

❯ cd my-app

❯ cat package.json
{
  "name": "my-app",
  "description": "The Marko starter app with the tags api preview enabled",
  "version": "1.0.0",
  "dependencies": {
    "@marko/tags-api-preview": "^0",
    "marko": "^5"
  },
  "devDependencies": {
    "@marko/build": "^4",
    "@marko/serve": "^4"
  },
  "private": true,
  "scripts": {
    "build": "marko-build ./src/pages",
    "dev": "marko-serve ./src/pages",
    "start": "NODE_ENV=production node ./build/index.js"
  }

Works as expected.

However, when using npm init:

❯ npm init marko --template tags-api
? Choose a template …  Use ↑ and ↓. Return ⏎ to submit.
❯ Default starter app
  Example from marko-js/examples

...

❯ npm init marko --template tags-api
✔ Choose a template · Default starter app
✔ Project created! To get started, run:

    cd tags-api
    npm run dev

❯ cd tags-api

❯ cat package.json
{
  "name": "tags-api",
  "description": "The default Marko starter app",
  "version": "1.0.0",
  "dependencies": {
    "marko": "^5"
  },
  "devDependencies": {
    "@marko/build": "^4",
    "@marko/serve": "^4"
  },
  "private": true,
  "scripts": {
    "build": "marko-build ./src/pages",
    "dev": "marko-serve ./src/pages",
    "start": "NODE_ENV=production node ./build/index.js"
  }

See above it looks as though tags-api is being used as name of app and not take as template option.

❯ npm init marko tagapiapp --template tags-api
✔ Choose a template · Default starter app
✖ The "path" argument must be of type string. Received an instance of Array

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received an instance of Array
    at new NodeError (node:internal/errors:371:5)
    at validateString (node:internal/validators:120:11)
    at Object.resolve (node:path:1098:7)
    at create (~/.npm/_npx/0a9fd74b321b4319/node_modules/@marko/create/dist/index.js:50:28)
    at createProject (~/.npm/_npx/0a9fd74b321b4319/node_modules/@marko/create/dist/index.js:37:18)
    at run (~/.npm/_npx/0a9fd74b321b4319/node_modules/@marko/create/dist/cli.js:141:20)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Trying to add name on cli command results in array error for path argument.

❯ cat ~/.npm/_npx/0a9fd74b321b4319/node_modules/@marko/create/package.json | grep version
  "version": "6.1.0",

Not sure if anyone can reproduce or if it is only me?

leoj3n commented 1 year ago

The following seems to fix the issue with CLI flags not being read correctly on my machine:

❯ npm init marko -- --template tags-api
✔ Type your project name · heyhey
✔ Project created! To get started, run:

    cd heyhey
    npm run dev

❯ cd heyhey

❯ cat package.json
{
  "name": "heyhey",
  "description": "The Marko starter app with the tags api preview enabled",
  "version": "1.0.0",
  "dependencies": {
    "@marko/tags-api-preview": "^0",
    "marko": "^5"
  },
  "devDependencies": {
    "@marko/build": "^4",
    "@marko/serve": "^4"
  },
  "private": true,
  "scripts": {
    "build": "marko-build ./src/pages",
    "dev": "marko-serve ./src/pages",
    "start": "NODE_ENV=production node ./build/index.js"
  }

Notice the -- separator in npm init marko -- --template tags-api

I have encountered the issue on a long running heavily customized macOS dev machine as well as a mostly stock and newer macOS machine so not sure if it's just me but it seems to be an issue everywhere I've tried.

Perhaps update the suggested command across the docs and twitter post to make it more robust regardless?

EDIT: reference https://docs.npmjs.com/cli/v8/commands/npm-init#forwarding-additional-options

leoj3n commented 1 year ago

See #214 for PR fix