Skip to content

Commit

Permalink
feat: add --standalone option to 'init' command
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Feb 5, 2024
1 parent 4d06d86 commit a6e133b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 7 deletions.
4 changes: 4 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ async function main() {
"-t, --template <template:string>",
"Create a workspace from a template"
)
.option(
"-s, --standalone [standalone:boolean]",
"Create a standalone workspace from a template"
)
.action(function (options, name) {
init(options, name);
})
Expand Down
9 changes: 8 additions & 1 deletion src/cmd/down.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pkgx, Logger } from "../../deps.ts";
import * as workspaces from "../workspaces.ts";
import { existsSync } from "node:fs";

async function down({ ask }: { ask?: boolean }, workspace?: string) {
const logger = new Logger();
Expand All @@ -20,9 +21,15 @@ async function down({ ask }: { ask?: boolean }, workspace?: string) {
workdir = result.path;
}

if (existsSync(`${workdir}/.pocketenv`)) {
workdir = `${workdir}/.pocketenv`;
}

await pkgx.run(`terraform destroy ${args.join(" ")}`, "inherit", workdir);

const result = await workspaces.get(workspace || workdir);
const result = await workspaces.get(
workspace || workdir.replace(/\/.pocketenv$/, "")
);

if (!result) {
logger.warn("Workspace not found");
Expand Down
24 changes: 20 additions & 4 deletions src/cmd/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import { existsSync } from "node:fs";
import * as workspaces from "../workspaces.ts";
import { getDefaultGithubBranch } from "../lib.ts";

async function init({ template }: { template?: string }, name?: string) {
async function init(
{ template, standalone }: { template?: string; standalone?: boolean },
name?: string
) {
if (!name) {
console.log(`${cyan("?")} Workspace name: `);

Expand All @@ -35,7 +38,7 @@ async function init({ template }: { template?: string }, name?: string) {
template = template.trim();
}

await downloadFromGithub(template);
await downloadFromGithub(template, standalone);

await workspaces.save(Deno.cwd(), {
containerId: null,
Expand All @@ -50,7 +53,17 @@ async function init({ template }: { template?: string }, name?: string) {
console.log(`Run ${brightGreen("`pocketenv up`")} to start the workspace.`);
}

async function downloadFromGithub(template: string) {
async function downloadFromGithub(template: string, standalone?: boolean) {
if (!standalone) {
if (existsSync(".pocketenv")) {
console.log(
`🚨 ${brightGreen(
".pocketenv"
)} directory already exists. Please remove it and try again.`
);
Deno.exit(1);
}
}
const terminalSpinner = new TerminalSpinner({
text: `Downloading ${green(template)} template ...`,
spinner: SpinnerTypes.dots,
Expand Down Expand Up @@ -98,7 +111,10 @@ async function downloadFromGithub(template: string) {
);
}

await copyDir(`${cacheDir}/${template.split("/").pop()}-${branch}`, `.`);
await copyDir(
`${cacheDir}/${template.split("/").pop()}-${branch}`,
standalone ? "." : ".pocketenv"
);
}

async function copyDir(src: string, dest: string) {
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/logs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pkgx } from "../../deps.ts";
import { spawn } from "../lib.ts";
import { existsSync } from "node:fs";
import * as workspaces from "../workspaces.ts";

async function logs({ follow }: { follow?: boolean }, workspace?: string) {
Expand All @@ -8,12 +9,16 @@ async function logs({ follow }: { follow?: boolean }, workspace?: string) {
if (workspace) {
const result = await workspaces.get(workspace);
if (!result) {
console.error(`Workspace ${workspace} not found.`);
console.error(`🚨 Workspace ${workspace} not found.`);
Deno.exit(1);
}
workdir = result.path;
}

if (existsSync(`${workdir}/.pocketenv`)) {
workdir = `${workdir}/.pocketenv`;
}

const containerId = await spawn(
"sh",
["-c", 'pkgx terraform output -json | pkgx jq -r ".container_id.value"'],
Expand Down
7 changes: 6 additions & 1 deletion src/cmd/shell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { pkgx } from "../../deps.ts";
import { spawn } from "../lib.ts";
import { existsSync } from "node:fs";
import * as workspaces from "../workspaces.ts";

async function shell(workspace?: string) {
Expand All @@ -8,12 +9,16 @@ async function shell(workspace?: string) {
if (workspace) {
const result = await workspaces.get(workspace);
if (!result) {
console.error(`Workspace ${workspace} not found.`);
console.error(`🚨 Workspace ${workspace} not found.`);
Deno.exit(1);
}
workdir = result.path;
}

if (existsSync(`${workdir}/.pocketenv`)) {
workdir = `${workdir}/.pocketenv`;
}

const containerId = await spawn(
"sh",
["-c", 'pkgx terraform output -json | pkgx jq -r ".container_id.value"'],
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ async function up(
workdir = result.path;
}

if (existsSync(`${workdir}/.pocketenv`)) {
workdir = `${workdir}/.pocketenv`;
}

if (!existsSync(`${workdir}/.terraform`)) {
await pkgx.run(`terraform init`, "inherit", workdir);
}
Expand Down

0 comments on commit a6e133b

Please sign in to comment.