Skip to content

Commit

Permalink
feat: Create project if not found (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauorriols authored Nov 16, 2023
1 parent 420376e commit 5ddf441
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/subcommands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,48 @@ async function deploy(opts: DeployOpts): Promise<void> {
if (opts.dryRun) {
wait("").start().info("Performing dry run of deployment");
}
const projectSpinner = wait("Fetching project information...").start();
const projectInfoSpinner = wait(
`Fetching project '${opts.project}' information...`,
).start();
const api = opts.token
? API.fromToken(opts.token)
: API.withTokenProvisioner(TokenProvisioner);

const project = await api.getProject(opts.project);
let projectIsEmpty = false;
let project = await api.getProject(opts.project);
if (project === null) {
projectSpinner.fail("Project not found.");
Deno.exit(1);
}
projectInfoSpinner.stop();
const projectCreationSpinner = wait(
`Project '${opts.project}' not found in any of the user's organizations. Creating...`,
).start();
try {
project = await api.createProject(opts.project);
} catch (e) {
error(e.message);
}
projectCreationSpinner.succeed(`Created new project '${opts.project}'.`);
wait({ text: "", indent: 3 }).start().info(
`You can configure the name, env vars, custom domains and more in https://dash.deno.com/projects/${project.name}/settings`,
);
projectIsEmpty = true;
} else {
const deploymentsListing = await api.getDeployments(project.id);
if (deploymentsListing === null) {
projectInfoSpinner.fail("Project deployments details not found.");
Deno.exit(1);
}
const [projectDeployments, _pagination] = deploymentsListing!;
projectInfoSpinner.succeed(`Project: ${project.name}`);

const deploymentsListing = await api.getDeployments(project!.id);
if (deploymentsListing === null) {
projectSpinner.fail("Project deployments details not found.");
Deno.exit(1);
if (projectDeployments.length === 0) {
projectIsEmpty = true;
}
}
const [projectDeployments, _pagination] = deploymentsListing!;
projectSpinner.succeed(`Project: ${project!.name}`);

if (projectDeployments.length === 0) {
wait("").start().info(
"Empty project detected, automatically pushing initial deployment to production (use --prod for further updates).",
);
if (projectIsEmpty) {
opts.prod = true;
wait({ text: "", indent: 3 }).start().info(
"The project does not have a deployment yet. Automatically pushing initial deployment to production (use --prod for further updates).",
);
}

let url = opts.entrypoint;
Expand Down
9 changes: 9 additions & 0 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ export class API {
}
}

async createProject(
name?: string,
organizationId?: string,
envs?: Record<string, string>,
): Promise<Project> {
const body = { name, organizationId, envs };
return await this.#requestJson(`/projects/`, { method: "POST", body });
}

async getDeployments(
projectId: string,
): Promise<[Deployment[], DeploymentsSummary] | null> {
Expand Down

0 comments on commit 5ddf441

Please sign in to comment.