-
Notifications
You must be signed in to change notification settings - Fork 57
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
feat: Project and entrypoint inference #195
Conversation
src/config_inference.ts
Outdated
*/ | ||
async function inferEntrypoint() { | ||
const candidates = await Promise.all([ | ||
present("main.ts"), |
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.
also possible sets are *.tsx
& *.jsx
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
I would add the additional entrypoints with *.tsx
and *.jsx
too.
src/config_inference.ts
Outdated
async function getOriginUrlUsingGitCmd(): Promise<string | undefined> { | ||
const cmd = await new Deno.Command("git", { | ||
args: ["remote", "get-url", "origin"], | ||
}).output(); | ||
if (cmd.stdout) { | ||
return new TextDecoder().decode(cmd.stdout).trim(); | ||
} | ||
} |
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.
Maybe better to handle a situation where the environment doesn't have git
command?
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.
Sorry I don't understand what you mean, can you elaborate?
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.
Not all users' environments have git
command installed, right? (I believe git is available in 99.9% environments though)
If git command is not in $PATH then new Deno.Command("git").output()
throws an error, which we may want to handle.
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.
Ah I see. For some reason I expected it to just output the not found error in stderr. Will handle the error case.
src/config_inference.ts
Outdated
if (projectName) { | ||
wait("").start().warn( | ||
"No project name or ID provided with either the --project arg or a config file.", | ||
); | ||
for (;;) { | ||
let spinner; | ||
if (projectName) { | ||
spinner = wait( | ||
`Guessing project name '${projectName}': creating project...`, | ||
) | ||
.start(); | ||
} else { | ||
spinner = wait("Creating new project with a random name...").start(); | ||
} | ||
try { | ||
const project = await api.createProject(projectName); | ||
if (projectName) { | ||
spinner.succeed( | ||
`Guessed project name '${project.name}'.`, | ||
); | ||
} else { | ||
spinner.succeed(`Created new project '${project.name}'`); | ||
} | ||
wait({ text: "", indent: 3 }).start().info( | ||
`You can always change the project name in https://dash.deno.com/projects/${project.name}/settings`, | ||
); | ||
return project.name; | ||
} catch (e) { | ||
if (e instanceof APIError && e.code == "projectNameInUse") { | ||
spinner.stop(); | ||
spinner = wait( | ||
`Guessing project name '${projectName}': this project name is already used. Checking ownership...`, | ||
).start(); | ||
const hasAccess = projectName && | ||
(await api.getProject(projectName)) !== null; | ||
if (hasAccess) { | ||
spinner.stop(); | ||
const confirmation = confirm( | ||
`${ | ||
magenta("?") | ||
} Guessing project name '${projectName}': you already own this project. Should I deploy to it?`, | ||
); | ||
if (confirmation) { | ||
return projectName; | ||
} | ||
} | ||
projectName = `${projectName}-${Math.floor(Math.random() * 100)}`; | ||
spinner.stop(); | ||
} else if (e instanceof APIError && e.code == "slugInvalid") { | ||
// Fallback to random name given by the API | ||
projectName = undefined; | ||
spinner.stop(); | ||
} else { | ||
spinner.fail( | ||
`Guessing project name '${projectName}': Creating project...`, | ||
); | ||
error(e.code); | ||
} | ||
} | ||
} | ||
} |
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.
Can we do early-return here?
if (projectName) { | |
wait("").start().warn( | |
"No project name or ID provided with either the --project arg or a config file.", | |
); | |
for (;;) { | |
let spinner; | |
if (projectName) { | |
spinner = wait( | |
`Guessing project name '${projectName}': creating project...`, | |
) | |
.start(); | |
} else { | |
spinner = wait("Creating new project with a random name...").start(); | |
} | |
try { | |
const project = await api.createProject(projectName); | |
if (projectName) { | |
spinner.succeed( | |
`Guessed project name '${project.name}'.`, | |
); | |
} else { | |
spinner.succeed(`Created new project '${project.name}'`); | |
} | |
wait({ text: "", indent: 3 }).start().info( | |
`You can always change the project name in https://dash.deno.com/projects/${project.name}/settings`, | |
); | |
return project.name; | |
} catch (e) { | |
if (e instanceof APIError && e.code == "projectNameInUse") { | |
spinner.stop(); | |
spinner = wait( | |
`Guessing project name '${projectName}': this project name is already used. Checking ownership...`, | |
).start(); | |
const hasAccess = projectName && | |
(await api.getProject(projectName)) !== null; | |
if (hasAccess) { | |
spinner.stop(); | |
const confirmation = confirm( | |
`${ | |
magenta("?") | |
} Guessing project name '${projectName}': you already own this project. Should I deploy to it?`, | |
); | |
if (confirmation) { | |
return projectName; | |
} | |
} | |
projectName = `${projectName}-${Math.floor(Math.random() * 100)}`; | |
spinner.stop(); | |
} else if (e instanceof APIError && e.code == "slugInvalid") { | |
// Fallback to random name given by the API | |
projectName = undefined; | |
spinner.stop(); | |
} else { | |
spinner.fail( | |
`Guessing project name '${projectName}': Creating project...`, | |
); | |
error(e.code); | |
} | |
} | |
} | |
} | |
if (!projectName) { | |
return undefined; | |
} | |
wait("").start().warn( | |
"No project name or ID provided with either the --project arg or a config file.", | |
); | |
for (;;) { | |
let spinner; | |
if (projectName) { | |
spinner = wait( | |
`Guessing project name '${projectName}': creating project...`, | |
) | |
.start(); | |
} else { | |
spinner = wait("Creating new project with a random name...").start(); | |
} | |
try { | |
const project = await api.createProject(projectName); | |
if (projectName) { | |
spinner.succeed( | |
`Guessed project name '${project.name}'.`, | |
); | |
} else { | |
spinner.succeed(`Created new project '${project.name}'`); | |
} | |
wait({ text: "", indent: 3 }).start().info( | |
`You can always change the project name in https://dash.deno.com/projects/${project.name}/settings`, | |
); | |
return project.name; | |
} catch (e) { | |
if (e instanceof APIError && e.code == "projectNameInUse") { | |
spinner.stop(); | |
spinner = wait( | |
`Guessing project name '${projectName}': this project name is already used. Checking ownership...`, | |
).start(); | |
const hasAccess = projectName && | |
(await api.getProject(projectName)) !== null; | |
if (hasAccess) { | |
spinner.stop(); | |
const confirmation = confirm( | |
`${ | |
magenta("?") | |
} Guessing project name '${projectName}': you already own this project. Should I deploy to it?`, | |
); | |
if (confirmation) { | |
return projectName; | |
} | |
} | |
projectName = `${projectName}-${Math.floor(Math.random() * 100)}`; | |
spinner.stop(); | |
} else if (e instanceof APIError && e.code == "slugInvalid") { | |
// Fallback to random name given by the API | |
projectName = undefined; | |
spinner.stop(); | |
} else { | |
spinner.fail( | |
`Guessing project name '${projectName}': Creating project...`, | |
); | |
error(e.code); | |
} | |
} | |
} |
src/config_inference.ts
Outdated
const cmd = await new Deno.Command("git", { | ||
args: ["remote", "get-url", "origin"], | ||
}).output(); | ||
if (cmd.stdout) { | ||
return new TextDecoder().decode(cmd.stdout).trim(); | ||
} | ||
} |
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.
If there's no remote named origin
then the git command just fails with non-zero exit code. In this case stdout is empty. So it'd be better to check the exit code before looking at cmd.stdout
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.
Empty stdout is falsy
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.
To clarify, stdout
is an empty UInt8Array
which is not falsy, but it decodes to an empty string which is falsy. Maybe in this case it's overly confusing. I'll be more explicit
depends on #193 (for the wiring of the entrypoint through the
Args
)