Skip to content

Commit

Permalink
Adding scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhectorsosa committed Feb 19, 2024
1 parent c31e316 commit 759db40
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
50 changes: 50 additions & 0 deletions scripts/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const util = require("node:util");
const asyncExec = util.promisify(require("node:child_process").exec);
const fs = require("node:fs/promises");
const { existsSync } = require("node:fs");

async function setup() {
const { stdout: pnpmVersion } = await asyncExec("pnpm --version");

if (!pnpmVersion) {
throw new Error("`pnpm` is required to run this script.");
}

console.info(
"Welcome! We're just settings things up, hold a minute... πŸ•°οΈ \n"
);

const packageJsonExists = existsSync("./package.json");

if (!packageJsonExists) {
await asyncExec("pnpm init").catch((err) => console.error(err));
}

console.info("Adding dependencies... πŸ“¦");
await asyncExec("pnpm i -D @clack/prompts picocolors degit").catch((err) =>
console.error(err)
);

console.info("Adding scripts... πŸ“ƒ");
const packageJsonString = await fs.readFile("./package.json", {
encoding: "utf8",
});

if (packageJsonString) {
try {
let packageJsonParsed = JSON.parse(packageJsonString);
packageJsonParsed.scripts["db:install-modules"] =
"node ./scripts/wizard.js";
const newPacakgeJson = JSON.stringify(packageJsonParsed, null, 2);
await fs.writeFile("./package.json", newPacakgeJson);
} catch (err) {
throw new Error(`Failed parsing \`package.json\` file: ${err}`);
}
}

console.info(
"βœ… Success: You can run the Supabase Modules wizard using `pnpm db:install-modules`"
);
}

setup().catch((err) => console.error(`Error: setup.ts failed with: ${err}`));
132 changes: 132 additions & 0 deletions scripts/wizard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
const {
intro,
outro,
confirm,
select,
multiselect,
spinner,
isCancel,
cancel,
text,
} = require("@clack/prompts");
const color = require("picocolors");
const { existsSync } = require("node:fs");

const util = require("node:util");
const asyncExec = util.promisify(require("node:child_process").exec);

function handleCancel(currentProcess, message = "Operation cancelled") {
if (isCancel(currentProcess)) {
cancel(message);
return process.exit(0);
}
}

async function wizard() {
intro(`Welcome to ${color.magenta("Supabase Modules")} πŸ₯‘`);

const projectStarter = await select({
message: `${color.italic("How")} would you like to begin?`,
default: "from-example",
options: [
{
label: "Start from an Example",
value: "from-example",
hint: "Jumpstart your development by cloning an Example project",
},
{
label: "Add to Existing Project",
value: "existing-project",
hint: "Enhance your current project by directly integrating Supabase Modules",
},
],
});

handleCancel(projectStarter);

if (projectStarter === "from-example") {
const dir = await text({
message: `Which is your ${color.italic("project's working directory")}?`,
initialValue: ".",
validate: (dirInput) => {
const dirExists = existsSync(dirInput);
if (!dirExists) {
return "Not found - please make sure the directory provided exists.";
}
},
});

handleCancel(dir);

const installAllModules = await confirm({
message: `Would you like to install ${color.italic("all modules")}?`,
default: true,
});

handleCancel(installAllModules);

if (installAllModules) {
const s = spinner();
s.start("Installing project files... πŸ‘·πŸ½");
await asyncExec(
`degit iamhectorsosa/iamhectorsosa/src/components/layout ${dir}/supabase --force`
);
await asyncExec(
`degit iamhectorsosa/iamhectorsosa/src/components/providers ${dir}/modules --force`
);

s.stop("Project files successfully installed πŸ‘·πŸ½");

outro(
`βœ… ${color.green(
"Success"
)}: All set! Make sure to check out our docs at: \n ${color.magenta(
"https://supabase-modules-docs.vercel.app"
)} (hold cmd or ctl to click on URL)`
);
return process.exit(0);
}

const modulesToInstall = await multiselect({
message: `${color.italic(
"Select which modules"
)} would you like to install.`,
options: [
{ value: "auth", label: "Authentication" },
{ value: "profile", label: "Profile" },
],
});

handleCancel(modulesToInstall);

const s = spinner();
s.start("Installing project files... πŸ‘·πŸ½");

for (module in modulesToInstall) {
await asyncExec(
`degit iamhectorsosa/iamhectorsosa/src/components/layout ${dir}/supabase --force`
);
}

s.stop("Project files successfully installed πŸ‘·πŸ½");

outro(
`βœ… ${color.green(
"Success"
)}: All set! Make sure to check out our docs at: \n ${color.magenta(
"https://supabase-modules-docs.vercel.app"
)} (hold cmd or ctl to click on URL)`
);
return process.exit(0);
}

outro(
`βœ… ${color.green`Nothing to install!`} Make sure to check out our docs at: \n ${color.magenta(
"https://supabase-modules-docs.vercel.app"
)} (hold cmd or ctl to click on URL)`
);
}

wizard().catch((err) =>
console.error(`${color.red`Error: wizard.js failed with`} ${err}`)
);

0 comments on commit 759db40

Please sign in to comment.