-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
461 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
ignores: [ | ||
"@types/*", | ||
"@owlprotocol/tsconfig", | ||
"@owlprotocol/prettier-config", | ||
"vite" | ||
] |
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,4 @@ | ||
module.exports = { | ||
root: true, | ||
extends: ['@owlprotocol/eslint-config'], | ||
}; |
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,4 @@ | ||
src/envvars.ts | ||
src/envvars.mts | ||
src/envvars.cts | ||
src/envvars.browser.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,3 @@ | ||
module.exports = { | ||
...require("@owlprotocol/prettier-config"), | ||
}; |
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,37 @@ | ||
# @owlprotocol/envvars | ||
|
||
## 3.1.2 | ||
|
||
### Patch Changes | ||
|
||
- 951cfb5: Test our changeset/action | ||
|
||
## 3.1.1 | ||
|
||
### Patch Changes | ||
|
||
- 81c8454: Updated release with all dependencies used by ui-components (soon to release) | ||
|
||
## 3.1.0 | ||
|
||
### Minor Changes | ||
|
||
- d72ac67: Updated release | ||
|
||
## 2.0.6 | ||
|
||
### Patch Changes | ||
|
||
- b6a190d7: fix find-config dependency as a prod one | ||
|
||
## 2.0.5 | ||
|
||
### Patch Changes | ||
|
||
- d7405311: envvars missing patch bump | ||
|
||
## 2.0.2 | ||
|
||
### Patch Changes | ||
|
||
- 26e0d473: Initial Release |
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,7 @@ | ||
Copyright 2024 Owl Protocol | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, libribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,3 @@ | ||
# Envars Starter | ||
|
||
Code generation for envvars management |
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,6 @@ | ||
import { cjsLibConfig, buildLib } from "@owlprotocol/esbuild-config"; | ||
|
||
//Ignore browser files | ||
cjsLibConfig.entryPoints = ["src/index.ts", "src/dotenvConfig.ts", "src/envvars.ts", "src/isProductionOrStaging.ts"]; | ||
|
||
await buildLib(); |
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,82 @@ | ||
{ | ||
"name": "@owlprotocol/starter-envvars", | ||
"version": "3.0.0", | ||
"keywords": [], | ||
"private": true, | ||
"type": "module", | ||
"homepage": "https://github.com/owlprotocol/starter-templates", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/owlprotocol/starter-templates.git" | ||
}, | ||
"copyright": "Copyright 2024 Owl Protocol", | ||
"license": "MIT", | ||
"author": { | ||
"name": "Leo Vigna", | ||
"email": "[email protected]", | ||
"url": "https://github.com/leovigna" | ||
}, | ||
"files": [ | ||
"dist/*", | ||
"lib/*", | ||
"src/*", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"main": "./lib/cjs/index.cjs", | ||
"module": "./lib/esm/index.mjs", | ||
"types": "./lib/types/index.d.mts", | ||
"exports": { | ||
".": { | ||
"types": "./lib/types/index.d.ts", | ||
"browser": "./lib/esm/index.browser.js", | ||
"import": "./lib/esm/index.js", | ||
"require": "./lib/cjs/index.js" | ||
}, | ||
"./browser": { | ||
"types": "./lib/types/index.browser.d.ts", | ||
"browser": "./lib/esm/index.browser.js" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "pnpm run codegen && npm-run-all -p tsc esbuild", | ||
"build:watch": "npm-run-all -p tsc:watch esbuild:watch", | ||
"clean": "rimraf lib dist", | ||
"codegen": "tsx src/genEnvVars.ts", | ||
"depcheck": "depcheck", | ||
"esbuild": "node esbuild.config.mjs", | ||
"esbuild:watch": "ESBUILD_WATCH=true node esbuild.config.mjs", | ||
"lint": "eslint", | ||
"lint:fix": "eslint --fix", | ||
"tsc": "tsc", | ||
"tsc:watch": "tsc -w" | ||
}, | ||
"dependencies": { | ||
"dotenv": "^16.4.5", | ||
"find-config": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@owlprotocol/esbuild-config": "^3.0.0", | ||
"@owlprotocol/eslint-config": "^3.0.0", | ||
"@owlprotocol/prettier-config": "^3.0.0", | ||
"@owlprotocol/tsconfig": "^3.0.0", | ||
"@types/eslint": "^8.44.2", | ||
"@types/find-config": "^1.0.2", | ||
"@types/node": "^20.11.10", | ||
"eslint": "^8.55.0", | ||
"npm-run-all": "^4.1.5", | ||
"rimraf": "^3.0.2", | ||
"tsx": "^4.11.0", | ||
"typescript": "5.4.5", | ||
"vite": "^5.1.6", | ||
"depcheck": "^1.4.7" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/owlprotocol/starter-templates/issues", | ||
"email": "[email protected]" | ||
}, | ||
"funding": "https://owlprotocol.xyz", | ||
"engines": { | ||
"node": "^18.0.0" | ||
} | ||
} |
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,33 @@ | ||
import dotenv from "dotenv"; | ||
import findConfig from "find-config"; | ||
|
||
export const NODE_ENV = process.env.NODE_ENV ?? "development"; | ||
export const DOTENV_KEY = process.env.DOTENV_KEY; | ||
|
||
export const isClient = () => typeof window !== "undefined"; | ||
|
||
export function dotenvConfig(nodeEnv = NODE_ENV, dotenvKey = DOTENV_KEY) { | ||
//Only works in Node Environment | ||
if (!isClient()) { | ||
if (dotenvKey) { | ||
console.debug("Loading .env.vault "); | ||
//Load remote envvars | ||
dotenv.config({ path: findConfig(".env.vault") ?? undefined, DOTENV_KEY: dotenvKey }); | ||
} else if (nodeEnv === "development") { | ||
console.debug("Loading .env"); | ||
dotenv.config({ path: findConfig(".env") ?? undefined }); | ||
} else if (nodeEnv === "test") { | ||
console.debug("Loading .env"); | ||
dotenv.config({ path: findConfig(".env") ?? undefined }); | ||
} else if (nodeEnv === "ci") { | ||
console.debug("Loading .env.ci"); | ||
dotenv.config({ path: findConfig(".env.ci") ?? undefined }); | ||
} else if (nodeEnv === "staging") { | ||
dotenv.config({ path: findConfig(".env.staging") ?? undefined }); | ||
console.debug("Loading .env.staging"); | ||
} else if (nodeEnv === "production") { | ||
console.debug("Loading .env.production"); | ||
dotenv.config({ path: findConfig(".env.production") ?? undefined }); | ||
} | ||
} | ||
} |
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,151 @@ | ||
import { ESLint } from "eslint"; | ||
import { writeFileSync } from "fs"; | ||
|
||
export enum Platform { | ||
NODE = "NODE", | ||
BROWSER = "BROWSER", | ||
} | ||
|
||
/** Define an envvar, defaultValues, and enum values if applicable */ | ||
export interface EnvVarDef { | ||
readonly name: string; | ||
readonly defaultValue?: string; | ||
readonly enumValues?: string[]; | ||
/** Which platform envvar is supported on neutral = both browser and node */ | ||
readonly platform: "browser" | "node" | "neutral"; | ||
} | ||
|
||
export const ENVVARS: EnvVarDef[] = [ | ||
{ | ||
name: "LOG_LEVEL", | ||
platform: "neutral", | ||
defaultValue: "warn", | ||
enumValues: ["trace", "debug", "info", "warn", "error"], | ||
}, | ||
]; | ||
|
||
const NODE_ENV_VAR = { | ||
name: "NODE_ENV", | ||
defaultValue: "development", | ||
enumValues: ["development", "test", "ci", "staging", "production"], | ||
}; | ||
|
||
/** | ||
* Support both import.meta.env imports and process.env imports | ||
* @param name of envvar | ||
* @param defaultValue default hard-coded value | ||
* @returns | ||
*/ | ||
export function genEnvVarStatement(name: string, platform: Platform, defaultValue?: string): string { | ||
const varName = `export const ${name}`; | ||
let varValue: string; | ||
if (platform === Platform.NODE) { | ||
//value as process.env (NodeJS only) | ||
varValue = `process.env.${name}`; | ||
} else if (platform === Platform.BROWSER) { | ||
varValue = `import.meta.env.VITE_${name}`; | ||
} else { | ||
throw new Error(`Invalid moduleType ${platform}`); | ||
} | ||
//Add default value | ||
if (defaultValue) { | ||
varValue = `${varValue} ?? "${defaultValue}"`; | ||
} | ||
return `${varName} = ${varValue};`; | ||
} | ||
|
||
export function genEnvVarTypeDef(name: string, enumValues?: string[], defined?: boolean) { | ||
if (enumValues) return `readonly ${name}${defined ? "" : "?"}: ${enumValues.map((e) => `"${e}"`).join("|")};`; | ||
|
||
return `readonly ${name}${defined ? "" : "?"}: string;`; | ||
} | ||
|
||
/** Generate envvar exports */ | ||
export function genEnvVarsExports(envvars: EnvVarDef[], platform: Platform) { | ||
envvars = envvars.filter((e) => { | ||
if (platform === Platform.NODE) { | ||
return e.platform === "node" || e.platform === "neutral"; | ||
} else if (platform === Platform.BROWSER) { | ||
return e.platform === "browser" || e.platform === "neutral"; | ||
} | ||
}); | ||
|
||
const exports = envvars.map((e) => genEnvVarStatement(e.name, platform, e.defaultValue)).join("\n"); | ||
return exports; | ||
} | ||
|
||
/** Prefix data for the file */ | ||
export function genEnvVarsPrelude(envvars: EnvVarDef[], platform: Platform): string { | ||
envvars = envvars.filter((e) => { | ||
if (platform === Platform.NODE) { | ||
return e.platform === "node" || e.platform === "neutral"; | ||
} else if (platform === Platform.BROWSER) { | ||
return e.platform === "browser" || e.platform === "neutral"; | ||
} | ||
}); | ||
|
||
const comment = ` | ||
/** | ||
* Environment variables. We use a hybrid solution that supports both import.meta.env for static | ||
* replacement used by client bundlers (Vite, Webpack...) and process.env for NodeJS libraries. | ||
* @module Environment | ||
*/ | ||
console.debug("Loading ${platform} envvars") | ||
`; | ||
|
||
if (platform === Platform.NODE) { | ||
//NODE_ENV loaded before .env file | ||
const dotenvLoad = ` | ||
import { dotenvConfig } from "./dotenvConfig.js"; | ||
dotenvConfig();`; | ||
|
||
const types = [ | ||
genEnvVarTypeDef(NODE_ENV_VAR.name, NODE_ENV_VAR.enumValues), | ||
...envvars.map((e) => genEnvVarTypeDef(e.name, e.enumValues)), | ||
]; | ||
const globalNameSpace = `declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
${types.join("\n ")} | ||
} | ||
} | ||
} | ||
`; | ||
return [comment, globalNameSpace, dotenvLoad].join("\n"); | ||
} else if (platform === Platform.BROWSER) { | ||
const NODE_ENV_EXPORT = `export const NODE_ENV = import.meta.env.MODE ?? "development";`; | ||
|
||
const typesWithVITE = [...envvars.map((e) => genEnvVarTypeDef(`VITE_${e.name}`, e.enumValues))]; | ||
const globalNameSpace = `declare global { | ||
interface ImportMetaEnv { | ||
${typesWithVITE.join("\n ")} | ||
} | ||
} | ||
`; | ||
//Define Import Meta | ||
const viteClientDTS = `/// <reference types="vite/client" />`; | ||
return [viteClientDTS, comment, globalNameSpace, NODE_ENV_EXPORT].join("\n"); | ||
} else { | ||
throw new Error(`Invalid moduleType ${platform}`); | ||
} | ||
} | ||
|
||
export async function writeEnvVarFile(envvars: EnvVarDef[], moduleType: Platform, envVarsPath: string) { | ||
const file = `${genEnvVarsPrelude(envvars, moduleType)}\n${genEnvVarsExports(envvars, moduleType)}`; | ||
writeFileSync(envVarsPath, file); | ||
|
||
//Lint files | ||
const eslint = new ESLint({ useEslintrc: true, fix: true }); | ||
const results = await eslint.lintFiles(envVarsPath); | ||
await ESLint.outputFixes(results); | ||
} | ||
|
||
export async function main() { | ||
await Promise.all([ | ||
writeEnvVarFile(ENVVARS, Platform.BROWSER, "src/envvars.browser.ts"), | ||
writeEnvVarFile(ENVVARS, Platform.NODE, "src/envvars.ts"), | ||
]); | ||
} | ||
|
||
main(); |
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,2 @@ | ||
export * from "./envvars.browser.js"; | ||
export * from "./isProductionOrStaging.browser.js"; |
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,3 @@ | ||
export * from "./dotenvConfig.js"; | ||
export * from "./envvars.js"; | ||
export * from "./isProductionOrStaging.js"; |
21 changes: 21 additions & 0 deletions
21
packages/starter-envvars/src/isProductionOrStaging.browser.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,21 @@ | ||
import { NODE_ENV } from "./envvars.browser.js"; | ||
|
||
export function isProduction(env = NODE_ENV): env is "production" { | ||
return env === "production"; | ||
} | ||
|
||
export function isStaging(env = NODE_ENV): env is "staging" { | ||
return env === "staging"; | ||
} | ||
|
||
export function isDevelopment(env = NODE_ENV): env is "development" { | ||
return env === "development"; | ||
} | ||
|
||
export function isTest(env = NODE_ENV): env is "test" { | ||
return env === "test"; | ||
} | ||
|
||
export function isProductionOrStaging(env = NODE_ENV): env is "production" | "staging" { | ||
return isProduction(env) || isStaging(env); | ||
} |
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,21 @@ | ||
import { NODE_ENV } from "./dotenvConfig.js"; | ||
|
||
export function isProduction(env = NODE_ENV): env is "production" { | ||
return env === "production"; | ||
} | ||
|
||
export function isStaging(env = NODE_ENV): env is "staging" { | ||
return env === "staging"; | ||
} | ||
|
||
export function isDevelopment(env = NODE_ENV): env is "development" { | ||
return env === "development"; | ||
} | ||
|
||
export function isTest(env = NODE_ENV): env is "test" { | ||
return env === "test"; | ||
} | ||
|
||
export function isProductionOrStaging(env = NODE_ENV): env is "production" | "staging" { | ||
return isProduction(env) || isStaging(env); | ||
} |
Oops, something went wrong.