Skip to content

Commit 06a680c

Browse files
committed
feat: add encrypted deployment utility
1 parent dde29e1 commit 06a680c

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

packages/scripts/src/commands/deployment/common.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import { existsSync } from "fs";
2+
import { logWarning } from "shared";
3+
import { readJSONSync } from "fs-extra";
4+
import path from "path";
5+
16
import { DEPLOYMENT_CONFIG_EXAMPLE_DEFAULTS, getDefaultAppConfig } from "data-models";
27
import type { IDeploymentConfig, IDeploymentConfigJson } from "data-models";
3-
import path from "path";
48

59
import { DEPLOYMENTS_PATH } from "../../paths";
6-
import { loadDeploymentJson } from "./utils";
10+
import { getStackFileNames, loadDeploymentJson } from "./utils";
711

812
// re-export of type for convenience
913
export type { IDeploymentConfigJson };
@@ -16,6 +20,28 @@ export function generateDeploymentConfig(name: string): IDeploymentConfig {
1620
return config;
1721
}
1822

23+
/**
24+
* Read a json file from deployment encrypted folder
25+
* @param deploymentName name of deployment folder to read config from
26+
* @param filename name of json to read config from (should already be decrypted)
27+
* @returns parsed json if exists, null if does not exist
28+
**/
29+
export function loadEncryptedConfig(filename: string) {
30+
// Determine path to deployment config ts that called function for source of encrypted config
31+
const configTsPath = getStackFileNames().find((filePath) => filePath.includes(".idems_app"));
32+
const encryptedConfigPath = path.resolve(configTsPath, "../", "encrypted");
33+
const target = path.resolve(encryptedConfigPath, filename);
34+
if (!existsSync(target)) {
35+
logWarning({
36+
msg1: `Encrypted config does not exist,\nsome features may be unavailable`,
37+
msg2: filename,
38+
});
39+
console.log(target);
40+
return null;
41+
}
42+
return readJSONSync(target);
43+
}
44+
1945
/** Load and extend an existing deployment config **/
2046
export function extendDeploymentConfig(options: {
2147
parent: string;

packages/scripts/src/commands/deployment/utils.ts

+21
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,24 @@ export function convertStringsToFunctions<T>(data: T) {
101101
});
102102
return data;
103103
}
104+
105+
/**
106+
* Utility function to determine the filename stack of current function invocation.
107+
* Uses Node v8 stack trace api to determine call stacks (in same way error stacks are managed)
108+
* Adapted from https://github.com/sindresorhus/callsites and https://v8.dev/docs/stack-trace-api
109+
*/
110+
export function getStackFileNames() {
111+
// take a copy of default stack track prepare method to revert after use
112+
const _prepareStackTrace = Error.prepareStackTrace;
113+
try {
114+
// alter stack trace method to remove self reference and return stack as filenames
115+
Error.prepareStackTrace = (_, callSites) => {
116+
return callSites.slice(1).map((callSite) => callSite.getFileName());
117+
};
118+
// create error to generate stacktrace and return (typed to match altered stack trace)
119+
return new Error().stack as any as string[];
120+
} finally {
121+
// revert stack trace method
122+
Error.prepareStackTrace = _prepareStackTrace;
123+
}
124+
}

0 commit comments

Comments
 (0)