Skip to content

Commit 7a6dc3e

Browse files
committed
feat: Prepare to read from dotenv
1 parent 4b4b7ac commit 7a6dc3e

File tree

7 files changed

+82
-16
lines changed

7 files changed

+82
-16
lines changed

.env-template

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# The full base URL of the Code Expert web application
2+
CX_WEB_URL="{{CX_WEB_URL}}"
3+
# The full base URL of the Code Expert API
4+
CX_API_URL="{{CX_API_URL}}"

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ dist
77
build/
88
*.tsbuildinfo
99

10+
### dotenv ###
11+
.env
12+
.env.*
13+
1014
### Yarn ###
1115
.yarn/*
1216
!.yarn/releases

package.json

+12-11
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,32 @@
33
"version": "0.0.18",
44
"description": "",
55
"scripts": {
6-
"setup": "yarn build:ts",
6+
"setup": "scripts/setup",
77
"clean:setup": "yarn cache clean && rm -f .yarn/install-state.gz && rm -rf node_modules && find . -type f -name '*.tsbuildinfo' -delete && find packages -type d -name 'build' -exec rm -rf '{}' + -depth",
88
"build": "concurrently \"yarn:build:*\"",
9-
"build:ts": "tsc --build",
10-
"build:server": "vite build",
9+
"build:ts": "dotenv -- tsc --build",
10+
"build:server": "dotenv -- vite build",
1111
"clean:build": "rm -rf dist && rm -rf src-tauri/target",
12-
"dev": "tauri dev",
12+
"dev": "dotenv -- tauri dev",
1313
"=== TEST ===": "",
1414
"test": "yarn lint && yarn test:unit",
15-
"test:unit": "vitest run",
16-
"watch:test": "vitest watch",
15+
"test:unit": "dotenv -- vitest run",
16+
"watch:test": "dotenv -- vitest watch",
1717
"=== LINT ===": "",
1818
"lint": "concurrently \"yarn:lint:*\"",
1919
"lint:format": "prettier --check --cache --cache-strategy content \"./src/**/*.{js,jsx,ts,tsx,json,less,html}\"",
2020
"lint:static": "eslint . --cache --cache-strategy content --ext .js,.jsx,.ts,.tsx",
2121
"lint:ts": "yarn build:ts",
2222
"=== STORYBOOK ===": "",
23-
"storybook": "storybook dev -p 6006",
24-
"storybook:build": "storybook build",
23+
"storybook": "dotenv -- storybook dev -p 6006",
24+
"storybook:build": "dotenv -- storybook build",
2525
"storybook:test": "npx chromatic --project-token=af98efe0a6a9",
2626
"=== UTILITIES ===": "",
2727
"clean": "concurrently \"yarn:clean:*\"",
28-
"dev:server": "vite",
29-
"dev:preview": "vite preview",
28+
"dev:server": "dotenv -- vite",
29+
"dev:preview": "dotenv -- vite preview",
3030
"=== LIFE CYCLE HOOKS ===": "",
31-
"postinstall": "yarn setup"
31+
"postinstall": "SKIP_INSTALL=true yarn setup"
3232
},
3333
"license": "MIT",
3434
"resolutions": {
@@ -88,6 +88,7 @@
8888
"@vitejs/plugin-react": "^3.0.0",
8989
"chromatic": "^6.17.0",
9090
"concurrently": "^8.0.1",
91+
"dotenv-cli": "^7.2.1",
9192
"eslint": "^8.34.0",
9293
"eslint-import-resolver-typescript": "^3.5.3",
9394
"eslint-plugin-import": "^2.27.5",

scripts/setup

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
set -eu
3+
4+
# Install Node modules unless skipped
5+
if [ "${SKIP_INSTALL:-false}" != "true" ]; then
6+
yarn
7+
fi
8+
9+
# Initialize dotenv files if none have been created yet
10+
if [ ! -f ".env" ]; then
11+
sed \
12+
-e 's|{{CX_WEB_URL}}|http://localhost:3000|' \
13+
-e 's|{{CX_API_URL}}|http://localhost:3100|' \
14+
.env-template > ".env"
15+
fi
16+
17+
# Generate TS types
18+
yarn build:ts

src/api/index.ts

-5
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ export interface Api {
2828
readConfigFile<T>(name: string, decoder: iots.Decoder<unknown, T>): taskOption.TaskOption<T>;
2929
hasConfigFile(name: string): task.Task<boolean>;
3030
logout(): taskOption.TaskOption<void>;
31-
CXUrl: string;
32-
APIUrl: string;
3331
}
3432

3533
export const api: Api = {
@@ -77,7 +75,4 @@ export const api: Api = {
7775
),
7876
hasConfigFile: (name) => () => exists(name, { dir: BaseDirectory.AppLocalData }),
7977
logout: () => api.settingWrite('accessToken', null),
80-
//TODO how to switch to production during build??
81-
CXUrl: 'http://localhost:3000',
82-
APIUrl: 'http://localhost:3100',
8378
};

src/config.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { either, identity, iots, pipe } from '@code-expert/prelude';
2+
3+
interface UrlBrand {
4+
readonly Url: unique symbol;
5+
}
6+
7+
const Url = iots.brand(
8+
iots.string,
9+
(s): s is iots.Branded<string, UrlBrand> => iots.string.is(s) && s.length > 0,
10+
'Url',
11+
);
12+
13+
const Config = iots.type({
14+
CX_WEB_URL: Url,
15+
CX_API_URL: Url,
16+
});
17+
18+
export type Config = iots.TypeOf<typeof Config>;
19+
20+
export const config: Config = pipe(
21+
process.env,
22+
Config.decode,
23+
either.fold((errors) => {
24+
throw new Error(
25+
'Parsing environment variables failed. Did you create a .env file in the root directory?\n\n' +
26+
iots.formatValidationErrors(errors).join('\n'),
27+
);
28+
}, identity),
29+
);

yarn.lock

+15
Original file line numberDiff line numberDiff line change
@@ -5451,6 +5451,7 @@ __metadata:
54515451
conditional-type-checks: ^1.0.6
54525452
copy-to-clipboard: ^3.3.3
54535453
date-fns: ^2.29.3
5454+
dotenv-cli: ^7.2.1
54545455
eslint: ^8.34.0
54555456
eslint-import-resolver-typescript: ^3.5.3
54565457
eslint-plugin-import: ^2.27.5
@@ -6052,6 +6053,20 @@ __metadata:
60526053
languageName: node
60536054
linkType: hard
60546055

6056+
"dotenv-cli@npm:^7.2.1":
6057+
version: 7.2.1
6058+
resolution: "dotenv-cli@npm:7.2.1"
6059+
dependencies:
6060+
cross-spawn: ^7.0.3
6061+
dotenv: ^16.0.0
6062+
dotenv-expand: ^10.0.0
6063+
minimist: ^1.2.6
6064+
bin:
6065+
dotenv: cli.js
6066+
checksum: 8ca27fd44ba261b974a0342ec831a714fa1cb6d6e98d4b5db8e94f111951af1fb129a64d007264b1901337c43a8ef877071f1836b6992944fd6970c16367aee0
6067+
languageName: node
6068+
linkType: hard
6069+
60556070
"dotenv-expand@npm:^10.0.0":
60566071
version: 10.0.0
60576072
resolution: "dotenv-expand@npm:10.0.0"

0 commit comments

Comments
 (0)