Skip to content

Commit

Permalink
Make API client options configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
imolorhe committed Jan 10, 2024
1 parent 76e2755 commit f4e9ce1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/altair-api-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"main": "build/index.js",
"private": true,
"scripts": {
"build": "tsc --build",
"build": "node ./scripts/build.cjs",
"prepare": "yarn build"
}
}
43 changes: 43 additions & 0 deletions packages/altair-api-utils/scripts/build.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

/**
* SCRIPT_DIR=$(dirname "$0")
API_UTILS_ROOT=$(cd "$SCRIPT_DIR/../" && pwd)/
cd "$API_UTILS_ROOT"
# Build the package
yarn tsc --build
# Replace process.env variables with actual values in the build directory
# Iterate over each JavaScript file in the input directory
for file in build/*.js; do
# Replace process.env.VARIABLE_NAME with the actual environment variable value
sed -i '' 's/process\.env\.([A-Za-z0-9_]+)/\"\${\1:-}\"/g' "$file" > "$file"
done
echo "Replaced process.env variables with actual values in the build directory"
*/

const API_UTILS_ROOT = path.resolve(__dirname, '../');
const BUILD_DIR = path.join(API_UTILS_ROOT, 'build');

// Build the package
execSync('yarn tsc --build', { cwd: API_UTILS_ROOT, stdio: 'inherit' });

// Replace process.env variables with actual values in the build directory
// Iterate over each JavaScript file in the input directory
fs.readdirSync(BUILD_DIR).forEach((file) => {
const filePath = path.join(BUILD_DIR, file);
const fileContents = fs.readFileSync(filePath, 'utf8');
const replacedContents = fileContents.replace(
/process\.env\.([A-Za-z0-9_]+)/g,
(_, p1) => {
if (!process.env[p1]) {
return `undefined`;
}
return `"${process.env[p1]}"`;
}
);
fs.writeFileSync(filePath, replacedContents, 'utf8');
});
19 changes: 19 additions & 0 deletions packages/altair-api-utils/scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

set -e


SCRIPT_DIR=$(dirname "$0")
API_UTILS_ROOT=$(cd "$SCRIPT_DIR/../" && pwd)/
cd "$API_UTILS_ROOT"

# Build the package
yarn tsc --build

# Replace process.env variables with actual values in the build directory
# Iterate over each JavaScript file in the input directory
for file in build/*.js; do
# Replace process.env.VARIABLE_NAME with the actual environment variable value
sed -i '' 's/process\.env\.([A-Za-z0-9_]+)/\"\${\1:-}\"/g' "$file" > "$file"
done
echo "Replaced process.env variables with actual values in the build directory"
8 changes: 6 additions & 2 deletions packages/altair-api-utils/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ export const getClientConfig = (
switch (env) {
case 'production':
return {
apiBaseUrl: 'https://api.altairgraphql.dev',
loginClientUrl: 'https://redir.altairgraphql.dev',
apiBaseUrl:
process.env.ALTAIR_API_CLIENT_BASE_URL ??
'https://api.altairgraphql.dev',
loginClientUrl:
process.env.ALTAIR_API_CLIENT_LOGIN_CLIENT_URL ??
'https://redir.altairgraphql.dev',
};
}

Expand Down
1 change: 0 additions & 1 deletion packages/altair-app/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getClientConfig } from '@altairgraphql/api-utils';
import pkg from '../../package.json';

export const environment = {
Expand Down
1 change: 0 additions & 1 deletion packages/altair-app/src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// `ng build --env=prod` then `environment.prod.ts` will be used instead.
// The list of which env maps to which file can be found in `.angular-cli.json`.
import pkg from '../../package.json';
import { getClientConfig } from '@altairgraphql/api-utils';

export const environment = {
production: false,
Expand Down

0 comments on commit f4e9ce1

Please sign in to comment.