Skip to content

Commit

Permalink
use public prefixes during schema scaffold (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim authored May 27, 2024
1 parent 0ff07e9 commit f7f7612
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-teachers-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dmno": patch
---

respect env var public prefixes (ex: NEXT*PUBLIC*) during schema scaffold
51 changes: 8 additions & 43 deletions packages/core/src/cli/lib/init-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export async function initDmnoForService(workspaceInfo: ScannedWorkspaceInfo, se
}

// INSTALL KNOWN INTEGRATIONS
const installedIntegrationPublicPrefixes: Array<string> = [];
for (const knownIntegrationDep in KNOWN_INTEGRATIONS_MAP) {
// currently we check dependencies, but we could look for specific config files instead
if (packageJsonDeps[knownIntegrationDep]) {
Expand Down Expand Up @@ -223,6 +224,12 @@ export async function initDmnoForService(workspaceInfo: ScannedWorkspaceInfo, se
// TODO: add some typing on the meta provider stuff...
const integrationMetaRaw = await fs.promises.readFile(integrationMetaFile, 'utf8');
const integrationMeta = parseJSONC(integrationMetaRaw);

// track the public env var prefix (ex: `NEXT_PUBLIC_`)
if (integrationMeta.publicPrefix) {
installedIntegrationPublicPrefixes.push(integrationMeta.publicPrefix);
}

// currently we are assuming only a single codemod and that it will be for a config file
// but will likely change
const configCodeMods = integrationMeta.installationCodemods?.[0];
Expand Down Expand Up @@ -326,7 +333,7 @@ export async function initDmnoForService(workspaceInfo: ScannedWorkspaceInfo, se
}

const envVarsFromCode = await findEnvVarsInCode(service.path);
const inferredSchema = await inferDmnoSchema(dotEnvFiles, envVarsFromCode);
const inferredSchema = await inferDmnoSchema(dotEnvFiles, envVarsFromCode, installedIntegrationPublicPrefixes);
const schemaMtsCode = generateDmnoConfigInitialCode(service.isRoot, serviceName, inferredSchema);
configFileGenerated = true;

Expand Down Expand Up @@ -515,48 +522,6 @@ export async function initDmnoForService(workspaceInfo: ScannedWorkspaceInfo, se
// }


// INSTALL KNOWN INTEGRATIONS
if (!workspaceInfo.isMonorepo || !service.isRoot) {
for (const knownIntegrationDep in KNOWN_INTEGRATIONS_MAP) {
if (packageJsonDeps[knownIntegrationDep]) {
const suggestedDmnoIntegration = KNOWN_INTEGRATIONS_MAP[
knownIntegrationDep as keyof typeof KNOWN_INTEGRATIONS_MAP
];

if (suggestedDmnoIntegration.package === 'dmno') {
console.log(setupStepMessage(`dmno + ${knownIntegrationDep} - natively supported integration`, {
type: 'noop',
docs: suggestedDmnoIntegration.docs,
}));
} else if (packageJsonDeps[suggestedDmnoIntegration.package]) {
console.log(setupStepMessage(`dmno + ${knownIntegrationDep} - integration already installed`, {
type: 'noop',
package: suggestedDmnoIntegration.package,
packageVersion: packageJsonDeps[suggestedDmnoIntegration.package],
docs: suggestedDmnoIntegration.docs,
}));
} else {
console.log(`It looks like this package uses ${kleur.green(knownIntegrationDep)}!`);
const confirmIntegrationInstall = await confirm({
message: `Would you like to install the ${kleur.green(suggestedDmnoIntegration.package)} package?`,
});

if (!confirmIntegrationInstall) {
console.log('No worries - you can always install it later.');
} else {
installPackage(servicePath, workspaceInfo.packageManager, suggestedDmnoIntegration.package, false);
await reloadPackageJson();

console.log(setupStepMessage(`dmno + ${knownIntegrationDep} integration installed!`, { package: suggestedDmnoIntegration.package, packageVersion: packageJsonDeps[suggestedDmnoIntegration.package] }));
}
}
break;
}
}
}



// SECRETS PLUGINS
// TODO
// if (service.isRoot) {}
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/cli/lib/schema-scaffold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { LoadedDotEnvFile } from '../../lib/dotenv-utils';
import { joinAndCompact } from './formatting';
import { asyncEachLimit } from '../../lib/async-utils';


// data structure to store our scaffolded dmno config that will be transformed into actual code
// it will be a subset of our actual config, and we'll need to store the
export type DmnoConfigScaffoldItem = {
Expand All @@ -18,16 +17,22 @@ export type DmnoConfigScaffoldItem = {
};
type DmnoConfigScaffold = Record<string, DmnoConfigScaffoldItem>;


export async function inferDmnoSchema(dotEnvFiles: Array<LoadedDotEnvFile>, envVarsFromCode: EnvVarsFromCode) {
export async function inferDmnoSchema(
dotEnvFiles: Array<LoadedDotEnvFile>,
envVarsFromCode: EnvVarsFromCode,
publicPrefixes?: Array<string>,
) {
const dmnoSchema: DmnoConfigScaffold = {};

for (const dotEnvFile of dotEnvFiles) {
for (const itemKey in dotEnvFile.items) {
const dotEnvItem = dotEnvFile.items[itemKey];

const itemHasPublicPrefix = _.some(publicPrefixes, (prefix) => itemKey.startsWith(prefix));

dmnoSchema[itemKey] ||= {
// we default everything is sensitive, and can then undo it later
sensitive: true,
...!itemHasPublicPrefix && { sensitive: true },
};

let coercedItemVal: any | undefined;
Expand Down

0 comments on commit f7f7612

Please # to comment.