Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit c6618a7

Browse files
authored
improvements: fetch accountId instead of asking the user for the value
improvements: fetch accountId instead of asking the user for the value
2 parents aef5752 + 1a355a4 commit c6618a7

File tree

12 files changed

+158
-95
lines changed

12 files changed

+158
-95
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,31 @@ This plugin gives you a new and isolated database for your preview deployments i
3131

3232
[[plugins]]
3333
package = "@snaplet/netlify-preview-database-plugin"
34-
35-
[plugins.inputs]
36-
databaseEnvVar = "DATABASE_URL"
37-
databaseCreateCommand = "snaplet db create --git --latest"
38-
databaseUrlCommand = "snaplet db url --git"
39-
reset = false
4034
```
4135
**Note:** We check the deploy context associated with the build. You can configure your settings by deploy context.
4236

43-
#### Inputs (All inputs are optional)
37+
#### Inputs
4438

4539
```yaml
4640
- name: databaseEnvVar
41+
required: false
4742
description: Database environment variable name
43+
default: "DATABASE_URL"
4844

4945
- name: databaseCreateCommand
50-
description: Command used to generate the preview database
46+
required: false
47+
description: Command used to generate the instant database
48+
default: "snaplet db create --git --latest"
5149

5250
- name: databaseUrlCommand
53-
description: Command used to get the preview database url
51+
required: false
52+
description: Command used to get the instant database url
53+
default: "snaplet db url --git"
5454

5555
- name: reset
56+
required: false
5657
description: Reset the database state on each commit
58+
default: false
5759
```
5860
5961
### 3. Set environment variables
@@ -69,7 +71,7 @@ This plugin gives you a new and isolated database for your preview deployments i
6971
7072
[[plugins]]
7173
# ...
72-
74+
7375
[context.deploy-preview.environment]
7476
SNAPLET_ACCESS_TOKEN="<YOUR_SNAPLET_ACCESS_TOKEN>"
7577
```
@@ -78,7 +80,6 @@ This plugin gives you a new and isolated database for your preview deployments i
7880

7981
```
8082
NETLIFY_ACCESS_TOKEN=// API Access token found in Netlify user settings.
81-
NETLIFY_ACCOUNT_ID=// Account ID found in team settings
8283
SNAPLET_ACCESS_TOKEN=// CLI Access token found in Snaplet UI
8384
SNAPLET_PROJECT_ID=// Project ID found in Snaplet project settings.
8485
```

manifest.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@ name: snaplet-netlify-plugin-preview-db
22

33
inputs:
44
- name: databaseEnvVar
5+
required: false
56
description: Database environment variable name
7+
default: "DATABASE_URL"
68

79
- name: databaseCreateCommand
10+
required: false
811
description: Command used to generate the instant database
12+
default: "snaplet db create --git --latest"
913

1014
- name: databaseUrlCommand
15+
required: false
1116
description: Command used to get the instant database url
17+
default: "snaplet db url --git"
1218

1319
- name: reset
20+
required: false
1421
description: Reset the database state on each commit
22+
default: false

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "@snaplet/netlify-preview-database-plugin",
3-
"version": "1.0.2",
4-
"main": "src/index.mjs",
3+
"version": "1.1.0",
4+
"type": "module",
5+
"main": "src/index.js",
56
"license": "MIT",
67
"description": "Create preview databases for Netlify preview deployments",
78
"repository": "https://github.com/snaplet/netlify-preview-database-plugin",
@@ -18,4 +19,4 @@
1819
"dependencies": {
1920
"node-fetch": "^3.2.10"
2021
}
21-
}
22+
}

src/create.sh

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/createPreviewDatabase.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { getPreviewDatabaseUrl } from "./getPreviewDatabaseUrl.js";
2+
3+
export async function createPreviewDatabase(ctx, options) {
4+
const { databaseCreateCommand, databaseUrlCommand, reset } = options;
5+
6+
let databaseUrl = null;
7+
try {
8+
databaseUrl = await getPreviewDatabaseUrl(ctx, { databaseUrlCommand });
9+
} catch (_) { }
10+
11+
const previewDatabaseIsDeployed = Boolean(databaseUrl);
12+
13+
if (!previewDatabaseIsDeployed) {
14+
console.log("Creating a preview database...");
15+
await ctx.run.command(databaseCreateCommand, { env: { PATH: `/opt/buildhome/.local/bin/:${process.env.PATH}` } });
16+
console.log("Preview database created");
17+
databaseUrl = await getPreviewDatabaseUrl(ctx, { databaseUrlCommand });
18+
}
19+
20+
if (previewDatabaseIsDeployed && reset) {
21+
console.log("Resetting the preview database state...");
22+
await ctx.run.command(databaseCreateCommand, { env: { PATH: `/opt/buildhome/.local/bin/:${process.env.PATH}` } });
23+
console.log("Preview database state reset");
24+
}
25+
26+
return databaseUrl;
27+
}

src/getPreviewDatabaseUrl.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export async function getPreviewDatabaseUrl(ctx, options) {
2+
const { databaseUrlCommand } = options;
3+
4+
const { stdout: databaseUrl } = await ctx.run.command(databaseUrlCommand, { env: { PATH: `/opt/buildhome/.local/bin/:${process.env.PATH}` }, stdio: "pipe" });
5+
6+
return databaseUrl;
7+
}

src/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { createPreviewDatabase } from "./createPreviewDatabase.js"
2+
import { installSnapletCLI } from "./installSnapletCLI.js";
3+
import { setEnvironmentVariable } from "./setEnvironmentVariable.js";
4+
5+
export async function onPreBuild({
6+
utils: { run },
7+
constants,
8+
netlifyConfig,
9+
inputs: {
10+
databaseEnvVar,
11+
databaseCreateCommand,
12+
databaseUrlCommand,
13+
reset,
14+
},
15+
}) {
16+
if (process.env.CONTEXT === "deploy-preview") {
17+
await installSnapletCLI({ run });
18+
19+
const databaseUrl = await createPreviewDatabase({ run }, {
20+
databaseCreateCommand,
21+
databaseUrlCommand,
22+
reset,
23+
});
24+
25+
await setEnvironmentVariable({
26+
siteId: constants.SITE_ID,
27+
branch: netlifyConfig.build.environment.BRANCH,
28+
key: databaseEnvVar,
29+
value: databaseUrl,
30+
});
31+
}
32+
};

src/index.mjs

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/installSnapletCLI.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function installSnapletCLI(ctx) {
2+
await ctx.run.command("curl -sL https://app.snaplet.dev/get-cli/ | bash", { shell: true, stdio: "ignore" });
3+
}

src/netlify.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import fetch from "node-fetch";
2+
3+
export async function netlify(path, options) {
4+
const response = await fetch(`https://api.netlify.com/api/v1/${path}`, {
5+
headers: {
6+
Authorization: `Bearer ${process.env.NETLIFY_ACCESS_TOKEN}`,
7+
"Content-Type": "application/json",
8+
},
9+
...options
10+
});
11+
12+
if (!response.ok) {
13+
throw new Error(`Netlify API error: ${response.status} ${response.statusText}`);
14+
}
15+
16+
return await response.json();
17+
}

0 commit comments

Comments
 (0)