shellscape / doiuse-email

Lint HTML and CSS for email support against the `Can I email?` database.
MIT License
14 stars 5 forks source link

[Bug] Add "#!/usr/bin/env node" to start of cli.js entrypoint to fix "import: command not found" errors #7

Closed kdipippo closed 1 week ago

kdipippo commented 5 months ago

Summary of Issue

Running through the documentation, I get an "import: command not found" error that goes away when a shebang line is added to the start of cli.js. I'm creating this issue as I'm not sure where cli.js gets formed in this github repo. This repo is on version v0.3.2 for its tagged releases while the npm package directory (and my walkthrough here) has 0.4.1 available.

But thank you for creating this tool! Now that I have it working, I'm excited to use this to lint my HTML email templates to shrink the amount of manual checking needed to see how they render.

I am not sure what other machines are affected by this issue, i.e. I'm not sure if I ran into this due to using a MacBook or because of Sonoma, etc.

Table of Contents

Project Setup

My package.json:

{
  "dependencies": {
    "doiuse-email": "^0.4.1"
  }
}

My myfile.html

<!doctype html>
<html>
  <body>
    <div style='background-color: orange'></div>
  </body>
</html>

Ran both commands for installing globally and locally:

➜  npm install
➜  npm install -g doiuse-email

My local versions of things:

➜  npm --version
10.7.0
➜  node --version
v22.2.0
➜  npm config get prefix
/opt/homebrew

I am using a MacBook Pro on Sonoma 14.5.

Current Behavior

Error I get when running the project as-is with local dependency:

➜ node_modules/doiuse-email/bin/cli.js --email-clients='gmail.*' myfile.html
node_modules/doiuse-email/bin/cli.js: line 1: import: command not found
node_modules/doiuse-email/bin/cli.js: line 2: import: command not found
node_modules/doiuse-email/bin/cli.js: line 3: import: command not found
node_modules/doiuse-email/bin/cli.js: line 4: import: command not found
node_modules/doiuse-email/bin/cli.js: line 5: program: command not found
node_modules/doiuse-email/bin/cli.js: line 6: syntax error near unexpected token `'doiuse-email''
node_modules/doiuse-email/bin/cli.js: line 6: `    .name('doiuse-email')'

Error I get when running the project as-is with global dependency:

➜ doiuse-email --email-clients='gmail.*' myfile.html
/opt/homebrew/bin/doiuse-email: line 1: import: command not found
/opt/homebrew/bin/doiuse-email: line 2: import: command not found
/opt/homebrew/bin/doiuse-email: line 3: import: command not found
/opt/homebrew/bin/doiuse-email: line 4: import: command not found
/opt/homebrew/bin/doiuse-email: line 5: program: command not found
/opt/homebrew/bin/doiuse-email: line 6: syntax error near unexpected token `'doiuse-email''
/opt/homebrew/bin/doiuse-email: line 6: `    .name('doiuse-email')'

Change Performed

I made the following update to node_modules/doiuse-email/bin/cli.js:

+ #!/usr/bin/env node
  import { program } from 'commander';
  import * as fs from 'node:fs';
  import process from 'node:process';
  import { DoIUseEmail, doIUseEmail } from '../utils/doiuse.js';
  program

I applied the same change to /opt/homebrew/lib/node_modules/doiuse-email/bin/cli.js where my global dependency is installed.

My guess for this was based on another npm command line I use for linting that starts its own entry-point script file with that line as well: https://github.com/Redocly/redocly-cli/blob/7a496684fa67ec0dd3bbf1a8e7e1dac3ca446626/packages/cli/bin/cli.js#L1

Fixed Behavior

Locally calling the command now returns the results expected, as documented in the README.

➜ node_modules/doiuse-email/bin/cli.js --email-clients='gmail.*' myfile.html
{"success":true,"notes":["Note about `<body> element` support for `gmail.desktop-webmail`: Partial. Replaced by a `<div>` with supported attributes.","Note about `<body> element` support for `gmail.ios`: Partial. Replaced by a `<div>` with supported attributes.","Note about `<body> element` support for `gmail.android`: Partial. Replaced by a `<div>` with supported attributes.","Note about `<body> element` support for `gmail.mobile-webmail`: Partial. Replaced by a `<div>` with supported attributes."],"warnings":["`<body> element` is only partially supported by `gmail.desktop-webmail`","`<body> element` is only partially supported by `gmail.ios`","`<body> element` is only partially supported by `gmail.android`","`<body> element` is only partially supported by `gmail.mobile-webmail`"]}%

Globally calling now also succeeds:

➜ doiuse-email --email-clients='gmail.*' myfile.html
{"success":true,"notes":["Note about `<body> element` support for `gmail.desktop-webmail`: Partial. Replaced by a `<div>` with supported attributes.","Note about `<body> element` support for `gmail.ios`: Partial. Replaced by a `<div>` with supported attributes.","Note about `<body> element` support for `gmail.android`: Partial. Replaced by a `<div>` with supported attributes.","Note about `<body> element` support for `gmail.mobile-webmail`: Partial. Replaced by a `<div>` with supported attributes."],"warnings":["`<body> element` is only partially supported by `gmail.desktop-webmail`","`<body> element` is only partially supported by `gmail.ios`","`<body> element` is only partially supported by `gmail.android`","`<body> element` is only partially supported by `gmail.mobile-webmail`"]}%
Thomasdezeeuw commented 4 months ago

I hit this issue as well and the proposed fix (adding #!/usr/bin/env node) work for me. Thanks @kdipippo

shellscape commented 1 week ago

We're going to be fixing this up and merging the project into https://github.com/avigoldman/caniemail after discussions with the owner of that package. The CLI was removed when the doiuseemail project was transferred to me, but we may reinstate it again in the future.