-
Notifications
You must be signed in to change notification settings - Fork 1
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
Support input url option for cli #203
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏻
function isValidUrl(url: string): boolean { | ||
try { | ||
new URL(url) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 I thought URL.canParse() could be used, but it looks like this was only supported in Node.js in v19.
We have not considered the minimum version of Node.js to be supported by the CLI, but since the LTS is v18, it would be better to support that.
So it looks good as it is 👍🏻
ref: https://developer.mozilla.org/en-US/docs/Web/API/URL/canParse_static
return isGitHubFileUrl(inputPath) | ||
? await downloadGitHubRawContent(inputPath) | ||
: await downloadFile(inputPath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only GitHub gets special handling, I see.
function isGitHubFileUrl(url: string): boolean { | ||
const parsedUrl = new URL(url) | ||
return parsedUrl.hostname === 'github.com' && url.includes('/blob/') | ||
} | ||
|
||
function readLocalFile(filePath: string): string { | ||
if (!fs.existsSync(filePath)) { | ||
throw new Error('Invalid input path. Please provide a valid file.') | ||
} | ||
return fs.readFileSync(filePath, 'utf8') | ||
} | ||
|
||
async function downloadGitHubRawContent(githubUrl: string): Promise<string> { | ||
const rawFileUrl = githubUrl | ||
.replace('github.com', 'raw.githubusercontent.com') | ||
.replace('/blob', '') | ||
return await downloadFile(rawFileUrl) | ||
} | ||
|
||
async function downloadFile(url: string): Promise<string> { | ||
const response = await fetch(url) | ||
if (!response.ok) { | ||
throw new Error(`Failed to download file: ${response.statusText}`) | ||
} | ||
const data = await response.text() | ||
return data | ||
} | ||
|
||
export async function getInputContent(inputPath: string): Promise<string> { | ||
if (!isValidUrl(inputPath)) { | ||
return readLocalFile(inputPath) | ||
} | ||
|
||
return isGitHubFileUrl(inputPath) | ||
? await downloadGitHubRawContent(inputPath) | ||
: await downloadFile(inputPath) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems that the CLI is handling quite a lot under the --input
argument, which might be somewhat unexpected for users.
Perhaps generating the raw URL could be left as the user's responsibility. I’d like to hear others’ opinions on this as well.
Example:
@liam-hq/cli erd build --input https://raw.githubusercontent.com/mastodon/mastodon/refs/heads/main/db/schema.rb --format schemarb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps generating the raw URL should be left to the user.
For private repositories, GitHub's raw
URL appears to include a pre-signed token as a ?token=
parameter.
Therefore, this URL conversion feature would only support public repositories. But it is ok, I think now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly, I was wondering if i should convert url.
If it is not useful and understandable for user, let's change implementation👍
Yeah, only public repo is available for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO: I thought slightly extensive too, but it was a minor point of contention, so once merged, it looked good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. This is fine. While the functionality feels slightly extensive, it’s still perfectly reasonable.
Summary
Support input url option for cli.
In most cases, we will receive a GitHub URL.
In that case, convert it to a RawContent URL such as:
Related Issue
Changes
Testing
Other Information