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

feat: Project and entrypoint inference #195

Merged
merged 17 commits into from
Nov 27, 2023
Merged

feat: Project and entrypoint inference #195

merged 17 commits into from
Nov 27, 2023

Conversation

arnauorriols
Copy link
Member

depends on #193 (for the wiring of the entrypoint through the Args)

*/
async function inferEntrypoint() {
const candidates = await Promise.all([
present("main.ts"),
Copy link
Member

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

Copy link
Member

@satyarohith satyarohith left a 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.

Comment on lines 119 to 126
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();
}
}
Copy link
Member

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?

Copy link
Member Author

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?

Copy link
Member

@magurotuna magurotuna Nov 25, 2023

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.

Copy link
Member Author

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 Show resolved Hide resolved
Comment on lines 28 to 88
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);
}
}
}
}
Copy link
Member

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?

Suggested change
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);
}
}
}

Comment on lines 120 to 126
const cmd = await new Deno.Command("git", {
args: ["remote", "get-url", "origin"],
}).output();
if (cmd.stdout) {
return new TextDecoder().decode(cmd.stdout).trim();
}
}
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty stdout is falsy

Copy link
Member Author

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

Base automatically changed from config-file to main November 27, 2023 11:30
@arnauorriols arnauorriols enabled auto-merge (squash) November 27, 2023 13:51
@arnauorriols arnauorriols merged commit 48b6a35 into main Nov 27, 2023
11 of 12 checks passed
@arnauorriols arnauorriols deleted the config-inference branch November 27, 2023 13:53
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.

3 participants