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

Add CLI interface #184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add CLI interface #184

wants to merge 2 commits into from

Conversation

henhal
Copy link

@henhal henhal commented Jan 20, 2022

This adds a CLI interface so that joi-to-typescript can be invoked as a binary.
Example:

$ joi-to-typescript src/schemas gen --sort=name --debug

The binary requires that ts-node is installed globally.

Fixes #183.

This adds a CLI interface so that joi-to-typescript can be invoked as a binary.
Example:

    $ joi-to-typescript src/schemas gen --sort=name --debug

The binary requires that ts-node is installed globally.
@henhal
Copy link
Author

henhal commented Jan 20, 2022

This works fine when I'm testing locally using npm link, I hope there are no issues when published.
The binary requires ts-node, since if joi-to-typescript is invoked using standard node, it won't be able to parse TS files. It seems reasonable that someone using a tool to produce TS types would themselves have ts-node installed, but in theory of course TS types could be generated from JOI schemas written in plain JS. If so, a wrapper shell script is probably needed to determine whether to use node or ts-node, but I didn't feel that this was critical.

@codecov
Copy link

codecov bot commented Jan 20, 2022

Codecov Report

Merging #184 (df4abbb) into master (3ec7948) will not change coverage.
The diff coverage is n/a.

❗ Current head df4abbb differs from pull request most recent head 8f9c0e4. Consider uploading reports for the commit 8f9c0e4 to get more accurate results

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #184   +/-   ##
=======================================
  Coverage   98.65%   98.65%           
=======================================
  Files           8        8           
  Lines         447      447           
  Branches      162      162           
=======================================
  Hits          441      441           
  Misses          6        6           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3ec7948...8f9c0e4. Read the comment docs.

@mrjono1
Copy link
Owner

mrjono1 commented Feb 8, 2022

I've had a go but I'm not sure how to get this working, are you able to help me get it going maybe with some readme notes or notes here?

@henhal
Copy link
Author

henhal commented Feb 8, 2022

Of course, sorry, I should have added a section to README as well. I will push another commit.
What problems did you encounter?
The main difference is that a binary is added to package.json so that you can invoke joi-to-typescript from the command line by itself, either by installing it globally or using npx. This removes the need to create a custom script file in your project that imports and calls joi-to-typescript.

Let's say I have my-project with files

package.json
src
|- schemas
   |- my-schemas.ts

Previously I would create a file called typegen.ts like this:

import { convertFromDirectory } from 'joi-to-typescript';

convertFromDirectory({
  schemaDirectory: './src/schemas',
  typeOutputDirectory: './gen',
  debug: true,
  sortPropertiesByName: false
}).catch(console.error);

And then add to my package.json

"scripts": {
  ...
  "typegen": "ts-node typegen"
}

But with a binary, I can skip creating my wrapper script typegen.ts and simply do this from package.json

"scripts": {
  ...
  "typegen": "npx joi-to-typescript --debug --sort=name ./src/schemas ./gen 
}

@henhal
Copy link
Author

henhal commented Feb 8, 2022

@mrjono1
Copy link
Owner

mrjono1 commented Feb 8, 2022

Thanks dude, sorry I took ages to review this

@AndrewCreer
Copy link

Hi @mrjono1 and @henhal
I was just going to do this myself.
I might have used yargs instead of @pawelgalazka/cli but the end result is pretty much the same.

Well done.

@AndrewCreer
Copy link

This command can be used (with 2.4.0)

 yarn ts-node -e 'import { convertFromDirectory } from"joi-to-typescript";convertFromDirectory({  schemaDirectory: "./src/schemas",  typeOutputDirectory: "./src/interfaces",  debug: true,});'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CLI interface
3 participants