Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Refactor Remote Init Script

Gildascd2ac5b8af2c
diff --git a/package-lock.json b/package-lock.json
index 85c07bc8..61f2dd74 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -71,7 +71,7 @@
"prettier": "~3.2.5",
"rollup-plugin-visualizer": "^5.12.0",
"storybook": "^8.3.0",
- "supabase": "^1.136.3",
+ "supabase": "^1.226.3",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vite-plugin-dts": "^4.2.1",
@@ -6589,18 +6589,20 @@
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="
},
"node_modules/bin-links": {
- "version": "4.0.4",
- "resolved": "https://registry.npmjs.org/bin-links/-/bin-links-4.0.4.tgz",
- "integrity": "sha512-cMtq4W5ZsEwcutJrVId+a/tjt8GSbS+h0oNkdl6+6rBuEv8Ot33Bevj5KPm40t309zuhVic8NjpuL42QCiJWWA==",
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/bin-links/-/bin-links-5.0.0.tgz",
+ "integrity": "sha512-sdleLVfCjBtgO5cNjA2HVRvWBJAHs4zwenaCPMNJAJU0yNxpzj80IpjOIimkpkr+mhlA+how5poQtt53PygbHA==",
"dev": true,
+ "license": "ISC",
"dependencies": {
- "cmd-shim": "^6.0.0",
- "npm-normalize-package-bin": "^3.0.0",
- "read-cmd-shim": "^4.0.0",
- "write-file-atomic": "^5.0.0"
+ "cmd-shim": "^7.0.0",
+ "npm-normalize-package-bin": "^4.0.0",
+ "proc-log": "^5.0.0",
+ "read-cmd-shim": "^5.0.0",
+ "write-file-atomic": "^6.0.0"
},
"engines": {
- "node": "^14.17.0 || ^16.13.0 || >=18.0.0"
+ "node": "^18.17.0 || >=20.5.0"
}
},
"node_modules/body-parser": {
@@ -7268,12 +7270,13 @@
}
},
"node_modules/cmd-shim": {
- "version": "6.0.3",
- "resolved": "https://registry.npmjs.org/cmd-shim/-/cmd-shim-6.0.3.tgz",
- "integrity": "sha512-FMabTRlc5t5zjdenF6mS0MBeFZm0XqHqeOkcskKFb/LYCcRQ5fVgLOHVc4Lq9CqABd9zhjwPjMBCJvMCziSVtA==",
+ "version": "7.0.0",
+ "resolved": "https://registry.npmjs.org/cmd-shim/-/cmd-shim-7.0.0.tgz",
+ "integrity": "sha512-rtpaCbr164TPPh+zFdkWpCyZuKkjpAzODfaZCf/SVJZzJN+4bHQb/LP3Jzq5/+84um3XXY8r548XiWKSborwVw==",
"dev": true,
+ "license": "ISC",
"engines": {
- "node": "^14.17.0 || ^16.13.0 || >=18.0.0"
+ "node": "^18.17.0 || >=20.5.0"
}
},
"node_modules/code-excerpt": {
@@ -11975,12 +11978,13 @@
"dev": true
},
"node_modules/npm-normalize-package-bin": {
- "version": "3.0.1",
- "resolved": "https://registry.npmjs.org/npm-normalize-package-bin/-/npm-normalize-package-bin-3.0.1.tgz",
- "integrity": "sha512-dMxCf+zZ+3zeQZXKxmyuCKlIDPGuv8EF940xbkC4kQVDTtqoh6rJFO+JTKSA6/Rwi0getWmtuy4Itup0AMcaDQ==",
+ "version": "4.0.0",
+ "resolved": "https://registry.npmjs.org/npm-normalize-package-bin/-/npm-normalize-package-bin-4.0.0.tgz",
+ "integrity": "sha512-TZKxPvItzai9kN9H/TkmCtx/ZN/hvr3vUycjlfmH0ootY9yFBzNOpiXAdIn1Iteqsvk4lQn6B5PTrt+n6h8k/w==",
"dev": true,
+ "license": "ISC",
"engines": {
- "node": "^14.17.0 || ^16.13.0 || >=18.0.0"
+ "node": "^18.17.0 || >=20.5.0"
}
},
"node_modules/npm-run-path": {
@@ -12898,6 +12902,16 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
+ "node_modules/proc-log": {
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/proc-log/-/proc-log-5.0.0.tgz",
+ "integrity": "sha512-Azwzvl90HaF0aCz1JrDdXQykFakSSNPaPoiZ9fm5qJIMHioDZEi7OAdRwSm6rSoPtY3Qutnm3L7ogmg3dc+wbQ==",
+ "dev": true,
+ "license": "ISC",
+ "engines": {
+ "node": "^18.17.0 || >=20.5.0"
+ }
+ },
"node_modules/process": {
"version": "0.11.10",
"resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz",
@@ -13486,12 +13500,13 @@
}
},
"node_modules/read-cmd-shim": {
- "version": "4.0.0",
- "resolved": "https://registry.npmjs.org/read-cmd-shim/-/read-cmd-shim-4.0.0.tgz",
- "integrity": "sha512-yILWifhaSEEytfXI76kB9xEEiG1AiozaCJZ83A87ytjRiN+jVibXjedjCRNjoZviinhG+4UkalO3mWTd8u5O0Q==",
+ "version": "5.0.0",
+ "resolved": "https://registry.npmjs.org/read-cmd-shim/-/read-cmd-shim-5.0.0.tgz",
+ "integrity": "sha512-SEbJV7tohp3DAAILbEMPXavBjAnMN0tVnh4+9G8ihV4Pq3HYF9h8QNez9zkJ1ILkv9G2BjdzwctznGZXgu/HGw==",
"dev": true,
+ "license": "ISC",
"engines": {
- "node": "^14.17.0 || ^16.13.0 || >=18.0.0"
+ "node": "^18.17.0 || >=20.5.0"
}
},
"node_modules/recast": {
@@ -14704,13 +14719,14 @@
"integrity": "sha512-Orov6g6BB1sDfYgzWfTHDOxamtX1bE/zo104Dh9e6fqJ3PooipYyfJ0pUmrZO2wAvO8YbEyeFrkV91XTsGMSrw=="
},
"node_modules/supabase": {
- "version": "1.187.10",
- "resolved": "https://registry.npmjs.org/supabase/-/supabase-1.187.10.tgz",
- "integrity": "sha512-7Nc50xN0k/G5fX3bpa+ZaulBoK/i5jwdjXhiNzyZhUf8qEWQnSsT1LTidCvpzuGt+8Gi6G3aMAh/5GlRdBLIoQ==",
+ "version": "1.226.3",
+ "resolved": "https://registry.npmjs.org/supabase/-/supabase-1.226.3.tgz",
+ "integrity": "sha512-+KytrqHMQTg1J71E26Og1lMOsUG7MW+Fvcdlu9ZpUCz2dQG96gm0HEjUlL9/PV01DmhNIUZ1+EUaf8QMco4BLg==",
"dev": true,
"hasInstallScript": true,
+ "license": "MIT",
"dependencies": {
- "bin-links": "^4.0.3",
+ "bin-links": "^5.0.0",
"https-proxy-agent": "^7.0.2",
"node-fetch": "^3.3.2",
"tar": "7.4.3"
@@ -15904,16 +15920,17 @@
"dev": true
},
"node_modules/write-file-atomic": {
- "version": "5.0.1",
- "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-5.0.1.tgz",
- "integrity": "sha512-+QU2zd6OTD8XWIJCbffaiQeH9U73qIqafo1x6V1snCWYGJf6cVE0cDR4D8xRzcEnfI21IFrUPzPGtcPf8AC+Rw==",
+ "version": "6.0.0",
+ "resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-6.0.0.tgz",
+ "integrity": "sha512-GmqrO8WJ1NuzJ2DrziEI2o57jKAVIQNf8a18W3nCYU3H7PNWqCCVTeH6/NQE93CIllIgQS98rrmVkYgTX9fFJQ==",
"dev": true,
+ "license": "ISC",
"dependencies": {
"imurmurhash": "^0.1.4",
"signal-exit": "^4.0.1"
},
"engines": {
- "node": "^14.17.0 || ^16.13.0 || >=18.0.0"
+ "node": "^18.17.0 || >=20.5.0"
}
},
"node_modules/ws": {
diff --git a/package.json b/package.json
index 3aa75bb7..1ac3dcf6 100644
--- a/package.json
+++ b/package.json
@@ -74,7 +74,7 @@
"prettier": "~3.2.5",
"rollup-plugin-visualizer": "^5.12.0",
"storybook": "^8.3.0",
- "supabase": "^1.136.3",
+ "supabase": "^1.226.3",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vite-plugin-dts": "^4.2.1",
diff --git a/scripts/supabase-remote-init.mjs b/scripts/supabase-remote-init.mjs
index bc13176c..1f198df9 100644
--- a/scripts/supabase-remote-init.mjs
+++ b/scripts/supabase-remote-init.mjs
@@ -14,6 +14,7 @@ import fs from 'node:fs';
});
const projectRef = await createProject({ projectName, databasePassword });
+ await waitForProjectToBeReady({ projectRef });
// This also ensures the project is ready
const { anonKey } = await fetchApiKeys({
@@ -47,6 +48,8 @@ async function createProject({ projectName, databasePassword }) {
'projects',
'create',
'--interactive',
+ '--output',
+ 'json',
'--db-password',
databasePassword,
projectName,
@@ -57,11 +60,47 @@ async function createProject({ projectName, databasePassword }) {
}
);
- const projectRef = stdout.match(
- /https:\/\/supabase.com\/dashboard\/project\/([a-zA-Z0-9_-]*)/
- )[1];
+ try {
+ const matchJSON = stdout.match(new RegExp('{.*}', 's'));
+ if (!matchJSON) {
+ throw new Error('Invalid JSON output');
+ }
+ const jsonOuput = JSON.parse(matchJSON[0]);
+ return jsonOuput.id;
+ } catch (e) {
+ console.error('Failed to create project');
+ console.error(e);
+ throw e;
+ }
+}
- return projectRef;
+async function waitForProjectToBeReady({ projectRef }) {
+ console.log('Waiting for project to be ready...');
+ const { stdout } = await execa(
+ 'npx',
+ ['supabase', 'projects', 'list', '--output', 'json'],
+ {
+ stdout: 'pipe',
+ }
+ );
+
+ try {
+ // The response is an Array of objects
+ const matchJSON = stdout.match(new RegExp('\\[.*\\]', 's'));
+ if (!matchJSON) {
+ throw new Error('Invalid JSON output');
+ }
+ const jsonOuput = JSON.parse(matchJSON[0]);
+ const project = jsonOuput.find(project => project.id === projectRef);
+ if (project.status !== 'ACTIVE_HEALTHY') {
+ await sleep(1000);
+ return waitForProjectToBeReady({ projectRef });
+ }
+ } catch (e) {
+ console.error('Failed to create project');
+ console.error(e);
+ throw e;
+ }
}
let retry = 0;
@@ -77,7 +116,7 @@ async function linkProject({ projectRef, databasePassword }) {
databasePassword,
],
{
- stdout: 'inherit',
+ stdout: 'ignore',
stderr: 'ignore',
}
).catch(() => {
@@ -111,27 +150,38 @@ async function setupDatabase({ databasePassword }) {
async function fetchApiKeys({ projectRef }) {
let anonKey = '';
-
try {
- const { stdout } = await execa(
+ const { stdout, exitCode } = await execa(
'npx',
- ['supabase', 'projects', 'api-keys', '--project-ref', projectRef],
+ [
+ 'supabase',
+ 'projects',
+ 'api-keys',
+ '--output',
+ 'json',
+ '--project-ref',
+ projectRef,
+ ],
{
+ stdout: 'pipe',
stderr: 'ignore',
}
);
-
- const keys = stdout
- .trim()
- .match(/[ ]+([a-z_]*)[ ]+│[ ]+([a-zA-Z0-9._-]+)/gm);
-
- for (const key of keys) {
- const [name, value] = key.split('│').map(s => s.trim());
- if (name === 'anon') {
- anonKey = value;
+ // If the exitCode is not 0, the command failed most probably because the project is not ready
+ if (exitCode === 0) {
+ // The response is an Array of objects
+ const matchJSON = stdout.match(new RegExp('\\[.*\\]', 's'));
+ if (!matchJSON) {
+ throw new Error('Invalid JSON output');
}
+ const jsonOuput = JSON.parse(matchJSON[0]);
+ anonKey = jsonOuput.find(key => key.name === 'anon')?.api_key;
}
- } catch (e) {}
+ } catch (e) {
+ console.error('Failed to fetch API keys');
+ console.error(e);
+ throw e;
+ }
if (anonKey === '') {
await sleep(1000);
@@ -142,24 +192,13 @@ async function fetchApiKeys({ projectRef }) {
}
async function persistSupabaseEnv({ projectRef, anonKey }) {
- let envFileStream = fs.createWriteStream(
- `${process.cwd()}/.env.production.local`,
- { flags: 'a' }
- );
- await execa(
- 'echo',
- [`VITE_SUPABASE_URL=https://${projectRef}.supabase.co`],
- {
- stdout: [envFileStream, 'pipe'],
- }
- );
- envFileStream = fs.createWriteStream(
+ fs.writeFileSync(
`${process.cwd()}/.env.production.local`,
- { flags: 'a' }
+ `
+VITE_SUPABASE_URL=https://${projectRef}.supabase.co
+VITE_SUPABASE_ANON_KEY=${anonKey}`,
+ { flag: 'a' }
);
- await execa('echo', [`VITE_SUPABASE_ANON_KEY=${anonKey}`], {
- stdout: [envFileStream, 'pipe'],
- });
}
function generatePassword(length) {