-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(adapter-next): add
project:environment:read
and `project:envir…
…onment:update` hooks
- Loading branch information
1 parent
642a460
commit 9886096
Showing
9 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/adapter-next/src/hooks/project-environment-read.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import type { ProjectEnvironmentReadHook } from "@slicemachine/plugin-kit"; | ||
import { | ||
checkHasProjectFile, | ||
readProjectFile, | ||
} from "@slicemachine/plugin-kit/fs"; | ||
import * as dotenv from "dotenv"; | ||
|
||
import type { PluginOptions } from "../types"; | ||
import { | ||
DEFAULT_ENVIRONMENT_VARIABLE_FILE_PATH, | ||
PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME, | ||
} from "../constants"; | ||
|
||
export const projectEnvironmentRead: ProjectEnvironmentReadHook< | ||
PluginOptions | ||
> = async (_data, { options, helpers }) => { | ||
const environmentVariableFilePath = | ||
options.environmentVariableFilePath || | ||
DEFAULT_ENVIRONMENT_VARIABLE_FILE_PATH; | ||
|
||
const hasEnvironmentVariableFile = await checkHasProjectFile({ | ||
filename: environmentVariableFilePath, | ||
helpers, | ||
}); | ||
|
||
if (!hasEnvironmentVariableFile) { | ||
return { | ||
environment: undefined, | ||
}; | ||
} | ||
|
||
const contents = await readProjectFile({ | ||
filename: environmentVariableFilePath, | ||
helpers, | ||
}); | ||
|
||
const vars = dotenv.parse(contents); | ||
|
||
const environment = vars[PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME]; | ||
|
||
if (environment) { | ||
return { | ||
environment, | ||
}; | ||
} | ||
|
||
return { | ||
environment: undefined, | ||
}; | ||
}; |
124 changes: 124 additions & 0 deletions
124
packages/adapter-next/src/hooks/project-environment-update.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import type { ProjectEnvironmentUpdateHook } from "@slicemachine/plugin-kit"; | ||
import { | ||
checkHasProjectFile, | ||
readProjectFile, | ||
writeProjectFile, | ||
} from "@slicemachine/plugin-kit/fs"; | ||
|
||
import type { PluginOptions } from "../types"; | ||
import { | ||
DEFAULT_ENVIRONMENT_VARIABLE_FILE_PATH, | ||
PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME, | ||
} from "../constants"; | ||
|
||
type BuildVariableLineArgs = { | ||
environment: string; | ||
}; | ||
|
||
const buildVariableLine = (args: BuildVariableLineArgs): string => { | ||
return `${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME}=${args.environment}`; | ||
}; | ||
|
||
type AppendEnvironmentVariableArgs = { | ||
contents: string; | ||
environment: string; | ||
}; | ||
|
||
const appendEnvironmentVariable = ( | ||
args: AppendEnvironmentVariableArgs, | ||
): string => { | ||
let res = args.contents.toString(); | ||
|
||
if (!res.endsWith("\n")) { | ||
res += "\n"; | ||
} | ||
|
||
res += buildVariableLine({ environment: args.environment }) + "\n"; | ||
|
||
return res; | ||
}; | ||
|
||
type UpdateEnvironmentVariableArgs = { | ||
contents: string; | ||
environment: string; | ||
}; | ||
|
||
const updateEnvironmentVariable = ( | ||
args: UpdateEnvironmentVariableArgs, | ||
): string => { | ||
return args.contents.replace( | ||
new RegExp(`^${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME}=.*$\n?`), | ||
`${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME}=${args.environment}`, | ||
); | ||
}; | ||
|
||
type RemoveEnvironmentVariableArgs = { | ||
contents: string; | ||
}; | ||
|
||
const removeEnvironmentVariable = ( | ||
args: RemoveEnvironmentVariableArgs, | ||
): string => { | ||
return args.contents.replace( | ||
new RegExp(`^${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME}=.*$\n?`), | ||
"", | ||
); | ||
}; | ||
|
||
export const projectEnvironmentUpdate: ProjectEnvironmentUpdateHook< | ||
PluginOptions | ||
> = async ({ environment }, { options, helpers }) => { | ||
const environmentVariableFilePath = | ||
options.environmentVariableFilePath || | ||
DEFAULT_ENVIRONMENT_VARIABLE_FILE_PATH; | ||
|
||
const variableRegExp = new RegExp( | ||
`^${PRISMIC_ENVIRONMENT_ENVIRONMENT_VARIABLE_NAME}=.*$`, | ||
"m", | ||
); | ||
|
||
const hasEnvironmentVariableFile = await checkHasProjectFile({ | ||
filename: environmentVariableFilePath, | ||
helpers, | ||
}); | ||
|
||
let contents; | ||
|
||
if (hasEnvironmentVariableFile) { | ||
const existingContents = await readProjectFile({ | ||
filename: environmentVariableFilePath, | ||
helpers, | ||
encoding: "utf8", | ||
}); | ||
|
||
const hasExistingVariable = variableRegExp.test(existingContents); | ||
|
||
if (environment === undefined) { | ||
contents = removeEnvironmentVariable({ contents: existingContents }); | ||
} else if (hasExistingVariable) { | ||
contents = updateEnvironmentVariable({ | ||
contents: existingContents, | ||
environment, | ||
}); | ||
} else { | ||
contents = appendEnvironmentVariable({ | ||
contents: existingContents, | ||
environment, | ||
}); | ||
} | ||
} else { | ||
if (environment === undefined) { | ||
// noop | ||
|
||
return; | ||
} | ||
|
||
contents = appendEnvironmentVariable({ contents: "", environment }); | ||
} | ||
|
||
await writeProjectFile({ | ||
filename: environmentVariableFilePath, | ||
contents: contents, | ||
helpers, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/adapter-next/test/plugin-project-environment-read.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { expect, test } from "vitest"; | ||
import { createSliceMachinePluginRunner } from "@slicemachine/plugin-kit"; | ||
import * as fs from "node:fs/promises"; | ||
import * as path from "node:path"; | ||
|
||
import adapter from "../src"; | ||
|
||
test("returns the active environment", async (ctx) => { | ||
await fs.writeFile( | ||
path.join(ctx.project.root, ".env.local"), | ||
"NEXT_PUBLIC_PRISMIC_ENVIRONMENT=foo", | ||
); | ||
|
||
const res = await ctx.pluginRunner.callHook( | ||
"project:environment:read", | ||
undefined, | ||
); | ||
|
||
expect(res.data[0].environment).toBe("foo"); | ||
}); | ||
|
||
test("reads from the configured file path", async (ctx) => { | ||
ctx.project.config.adapter.options.environmentVariableFilePath = ".bar"; | ||
const pluginRunner = createSliceMachinePluginRunner({ | ||
project: ctx.project, | ||
nativePlugins: { | ||
[ctx.project.config.adapter.resolve]: adapter, | ||
}, | ||
}); | ||
await pluginRunner.init(); | ||
|
||
await fs.writeFile( | ||
path.join(ctx.project.root, ".bar"), | ||
"NEXT_PUBLIC_PRISMIC_ENVIRONMENT=foo", | ||
); | ||
|
||
const res = await pluginRunner.callHook( | ||
"project:environment:read", | ||
undefined, | ||
); | ||
|
||
expect(res.data[0].environment).toBe("foo"); | ||
}); | ||
|
||
test("returns undefined if the env file does not exist", async (ctx) => { | ||
const res = await ctx.pluginRunner.callHook( | ||
"project:environment:read", | ||
undefined, | ||
); | ||
|
||
expect(res.data[0].environment).toBe(undefined); | ||
}); | ||
|
||
test("returns undefined if the env file does not contain the variable", async (ctx) => { | ||
await fs.writeFile(path.join(ctx.project.root, ".env.local"), "FOO=bar"); | ||
|
||
const res = await ctx.pluginRunner.callHook( | ||
"project:environment:read", | ||
undefined, | ||
); | ||
|
||
expect(res.data[0].environment).toBe(undefined); | ||
}); |
Oops, something went wrong.