Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parseable output #22

Open
joelday opened this issue Jan 24, 2023 · 5 comments
Open

Parseable output #22

joelday opened this issue Jan 24, 2023 · 5 comments
Labels
Status: PR Welcome Welcome to Pull Request Status: Proposal Request for comments

Comments

@joelday
Copy link

joelday commented Jan 24, 2023

As a developer, I would like to know the nature of the failure canonically so that I can decide what to do next. Currently, the only option is to parse the error result message from stderr with the --verbose flag or the Error that is thrown when calling canNpmPublish.

I imagine this could be made available via a --json flag and as an option provided to canNpmPublish, where the error message is returned with a predictable error code instead of being thrown.

Happy to open a PR with any implementation suggestions. Thanks!

@azu azu added Status: Proposal Request for comments Status: PR Welcome Welcome to Pull Request labels Jan 25, 2023
@azu
Copy link
Owner

azu commented Jan 25, 2023

We need to change canNpmPublish for implementing --json.

const canNpmPublish = (packagePath, options = { verbose: false }) => {
return Promise.all([
checkPkgName(packagePath, options),
checkAlreadyPublish(packagePath, options),
checkPrivateField(packagePath)
]);
};
module.exports.canNpmPublish = canNpmPublish;

Current --verbose includes both messsage and errors.
So, we need to separate these like following:

type CanNpmPublishResult = {
  "ok": boolean;
  "messages": string[];
  "errors": Error[];
};
type canNpmPublish = (path: string) => Promise<CanNpmPublishResult>;

If user pass --json, cli return JSON.strinigify(CanNpmPublishResult).

📝 TODO

  • Change canNpmPublish to return CanNpmPublishResult
    • Also, canNpmPublish do not require verbose option
  • Move verbose works to cli

@joelday
Copy link
Author

joelday commented Jan 25, 2023

Interested in a TypeScript conversion while we're at it?

@azu
Copy link
Owner

azu commented Jan 25, 2023

I'm ok that convert to TypeScript.

@joelday
Copy link
Author

joelday commented Feb 16, 2023

@azu When --json is specified, should this still exit non-zero if ok is false?

@azu
Copy link
Owner

azu commented Feb 16, 2023

I think that --json is a just formatter like --format json.
So, we should exit with non-zero code.

❯ bat test.js
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: test.js
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ console.log(JSON.stringify({ test: 1}));
   2   │ process.exit(1);
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

# just execute
❯ node test.js
{"test":1}

❯ echo $?
1

# pipe
❯ node test.js | jq .
{
  "test": 1
}

❯ echo $?
0

# redirect 
❯ node test.js > out.json

❯ echo $?
1

# when pipefail
❯ bat pipe.sh
───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: pipe.sh
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ #!/bin/bash
   2   │
   3   │ set -o pipefail
   4   │
   5   │ node test.js | jq .
   6   │ echo $?
───────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

❯ bash pipe.sh
{
  "test": 1
}
1

If you want to support output as .json file, we need to add --output flag too.
npx can-npm-publish --json --output out.json exit 0 if succuss to output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: PR Welcome Welcome to Pull Request Status: Proposal Request for comments
Projects
None yet
Development

No branches or pull requests

2 participants