@storm-software/k8s-tools
Version:
Tools for managing Kubernetes (k8s) infrastructure within a Nx workspace.
103 lines (99 loc) • 3.09 kB
JavaScript
import {
applyWorkspaceBaseTokens,
applyWorkspaceTokens
} from "./chunk-BUMY2HL2.mjs";
import {
findWorkspaceRoot,
getConfig,
getStopwatch,
writeDebug,
writeError,
writeFatal,
writeInfo,
writeSuccess,
writeTrace
} from "./chunk-XLWMBELQ.mjs";
// ../workspace-tools/src/base/base-generator.ts
var withRunGenerator = (name, generatorFn, generatorOptions = {
skipReadingConfig: false
}) => async (tree, _options) => {
const stopwatch = getStopwatch(name);
let options = _options;
let config;
try {
writeInfo(`\u26A1 Running the ${name} generator...
`, config);
const workspaceRoot = findWorkspaceRoot();
if (!generatorOptions.skipReadingConfig) {
writeDebug(
`Loading the Storm Config from environment variables and storm.config.js file...
- workspaceRoot: ${workspaceRoot}`,
config
);
config = await getConfig(workspaceRoot);
}
if (generatorOptions?.hooks?.applyDefaultOptions) {
writeDebug("Running the applyDefaultOptions hook...", config);
options = await Promise.resolve(
generatorOptions.hooks.applyDefaultOptions(options, config)
);
writeDebug("Completed the applyDefaultOptions hook", config);
}
writeTrace(
`Generator schema options \u2699\uFE0F
${Object.keys(options ?? {}).map((key) => ` - ${key}=${JSON.stringify(options[key])}`).join("\n")}`,
config
);
const tokenized = await applyWorkspaceTokens(
options,
{ workspaceRoot: tree.root, config },
applyWorkspaceBaseTokens
);
if (generatorOptions?.hooks?.preProcess) {
writeDebug("Running the preProcess hook...", config);
await Promise.resolve(
generatorOptions.hooks.preProcess(tokenized, config)
);
writeDebug("Completed the preProcess hook", config);
}
const result = await Promise.resolve(
generatorFn(tree, tokenized, config)
);
if (result) {
if (result.success === false || result.error && result?.error?.message && typeof result?.error?.message === "string" && result?.error?.name && typeof result?.error?.name === "string") {
throw new Error(`The ${name} generator failed to run`, {
cause: result?.error
});
} else if (result.success && result.data) {
return result;
}
}
if (generatorOptions?.hooks?.postProcess) {
writeDebug("Running the postProcess hook...", config);
await Promise.resolve(generatorOptions.hooks.postProcess(config));
writeDebug("Completed the postProcess hook", config);
}
return () => {
writeSuccess(`Completed running the ${name} generator!
`, config);
};
} catch (error) {
return () => {
writeFatal(
"A fatal error occurred while running the generator - the process was forced to terminate",
config
);
writeError(
`An exception was thrown in the generator's process
- Details: ${error.message}
- Stacktrace: ${error.stack}`,
config
);
};
} finally {
stopwatch();
}
};
export {
withRunGenerator
};