Skip to content

Commit

Permalink
change dmno/load behaviour, docs updates, dmno init improvements (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim authored May 10, 2024
1 parent 8477ca6 commit 9527262
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 71 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-scissors-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dmno": patch
---

Load `DMNO_PUBLIC_CONFIG` when using `import 'dmno/load'`
6 changes: 5 additions & 1 deletion example-repo/packages/api/.dmno/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ export default defineDmnoService({
value: OnePassBackend.item(),
},

PUBLIC_EXAMPLE: {
value: 'non sensitive',
},

SECRET_EXAMPLE: {
extends: DmnoBaseTypes.string,
required: true,
value: OnePassBackend.itemByLink("https://start.1password.com/open/i?a=I3GUA2KU6BD3FBHA47QNBIVEV4&h=dmnoinc.1password.com&i=bphvvrqjegfmd5yoz4buw2aequ&v=ut2dftalm3ugmxc6klavms6tfq", 'username'),
sensitive: true,
},
SWITCHED_EXAMPLE: {
value: switchByNodeEnv({
Expand Down Expand Up @@ -69,7 +74,6 @@ export default defineDmnoService({
color: '336791', // postgres brand color :)
},

required: true,
sensitive: true,
},
},
Expand Down
35 changes: 0 additions & 35 deletions example-repo/packages/api/src/init-env.ts

This file was deleted.

1 change: 1 addition & 0 deletions example-repo/packages/api/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ router.get('/', async (ctx) => {
systemStatus: 'nope',
envCheck: DMNO_CONFIG.API_ONLY || 'env-var-not-loaded',
dmnoTest: DMNO_CONFIG.PORT,
public: DMNO_PUBLIC_CONFIG.PUBLIC_EXAMPLE,
};
});

Expand Down
3 changes: 2 additions & 1 deletion example-repo/packages/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"include": [
"tsup.config.ts",
"src/**/*.ts",
"./.dmno/.typegen/global.d.ts"
"./.dmno/.typegen/global.d.ts",
"./.dmno/.typegen/global-public.d.ts"
]
}
48 changes: 25 additions & 23 deletions packages/core/src/app-init/app-init-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,33 @@ const execAsync = util.promisify(exec);
export async function loadGlobalDmnoConfig() {
// if the process is running via `dmno run`, the full serialized env will be injected
// so we can get properly typed (instead of all strings) env vars and re-inject into process.dmnoEnv
if (process.env.DMNO_LOADED_ENV) {
const parsedLoadedEnv = JSON.parse(process.env.DMNO_LOADED_ENV);
if (!process.env.DMNO_LOADED_ENV) {
throw new Error('Unable to find injected DMNO config - run this code via `dmno run` - see https://dmno.dev/docs/reference/cli/run for more info');

console.log('loading dmno env from dmno run', parsedLoadedEnv);
(globalThis as any).DMNO_CONFIG = new Proxy({}, {
get(o, key) {
console.log('get dmno config', key);
return parsedLoadedEnv[key].value;
},
});
} else {
try {
const configResult = await execAsync('pnpm exec dmno load -f json');
const configValuesOnly = JSON.parse(configResult.stdout.toString());
(globalThis as any).DMNO_CONFIG = new Proxy({}, {
get(o, key) {
return configValuesOnly[key];
},
});
} catch (err) {
// console.log('caught error while trying to load dmno config');
console.log((err as any).stdout.toString());
throw err;
}
// TODO: we can call out to the CLI instead
}

const parsedLoadedEnv = JSON.parse(process.env.DMNO_LOADED_ENV);

(globalThis as any).DMNO_CONFIG = new Proxy({}, {
get(o, key) {
if (!(key in parsedLoadedEnv)) {
throw new Error(`Config item \`${key.toString()}\` does not exist`);
}
return parsedLoadedEnv[key].value;
},
});
(globalThis as any).DMNO_PUBLIC_CONFIG = new Proxy({}, {
get(o, key) {
if (!(key in parsedLoadedEnv)) {
throw new Error(`Config item \`${key.toString()}\` does not exist`);
}
if (parsedLoadedEnv[key].sensitive) {
throw new Error(`Config item \`${key.toString()}\` is sensitive - use \`DMNO_CONFIG.${key.toString()}\` instead`);
}
return parsedLoadedEnv[key].value;
},
});
}

// TODO: provide sync version?
2 changes: 1 addition & 1 deletion packages/core/src/cli/commands/run.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const program = new DmnoCommand('run')
.option('-w,--watch', 'watch config for changes and restart command')
.argument('external command')
.example('dmno run --service service1 -- echo $SERVICE1_CONFIG', 'Runs the echo command with the resolved config for service1')
.example('dmno run —-servce service1 -- somecommand --some-option=(printenv SOME_VAR)', 'Runs the somecommand with the resolved config using SOME_VAR via printenv');
.example('dmno run —-service service1 -- somecommand --some-option=(printenv SOME_VAR)', 'Runs the somecommand with the resolved config using SOME_VAR via printenv');

addServiceSelection(program);
addCacheFlags(program);
Expand Down
13 changes: 9 additions & 4 deletions packages/core/src/cli/lib/init-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ const KNOWN_INTEGRATIONS_MAP: Record<string, { package: string, docs?: string }>
},
express: {
package: 'dmno',
docs: 'https://dmno.dev/docs/integrations/node',
},
koa: {
package: 'dmno',
docs: 'https://dmno.dev/docs/integrations/node',
},
};

Expand Down Expand Up @@ -320,7 +322,10 @@ export async function initDmnoForService(workspaceInfo: ScannedWorkspaceInfo, se
];

if (suggestedDmnoIntegration.package === 'dmno') {
console.log(setupStepMessage(`DMNO + ${knownIntegrationDep} - natively supported integration`, { type: 'noop' }));
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',
Expand All @@ -331,16 +336,16 @@ export async function initDmnoForService(workspaceInfo: ScannedWorkspaceInfo, se
} 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?`,
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, false);
installPackage(servicePath, workspaceInfo.packageManager, suggestedDmnoIntegration.package, false);
await reloadPackageJson();

console.log(setupStepMessage(`DMNO + ${knownIntegrationDep} integration installed!`, { package: suggestedDmnoIntegration, packageVersion: packageJsonDeps[suggestedDmnoIntegration] }));
console.log(setupStepMessage(`DMNO + ${knownIntegrationDep} integration installed!`, { package: suggestedDmnoIntegration.package, packageVersion: packageJsonDeps[suggestedDmnoIntegration.package] }));
}
}
break;
Expand Down
4 changes: 3 additions & 1 deletion packages/docs-site/src/components/CliCommand.astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
import type { ImageMetadata } from "astro";
import { Image } from "astro:assets";
import { Code } from "@astrojs/starlight/components";
import { Aside, Code } from "@astrojs/starlight/components";
import allCommandsSpec from "@/utils/cli.json";
import EncryptedVaultSpec from "@/utils/cli-plugin-vault.json";
import DmnoExecPathAside from './DmnoExecPathAside.md';
export const images = import.meta.glob<{ default: ImageMetadata }>(
"../assets/tapes/*.gif",
Expand Down Expand Up @@ -54,6 +55,7 @@ const commandSpec = getCommand(name, plugin)!;
})}
{commandSpec.examples.length ? (
<div>
<DmnoExecPathAside />
<h4>Example(s)</h4>
<Code code={formatExamples(commandSpec.examples)} lang="bash" />
</div>
Expand Down
3 changes: 3 additions & 0 deletions packages/docs-site/src/components/DmnoExecPathAside.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:::note[PATH & node_modules/.bin]
The `dmno` cli is installed as a depedency in your project and is available in your `node_modules/.bin` directory. Generally the best way to run it is via your package manager, for example `pnpm exec dmno`. For simplicity's sake, we will omit the `pnpm exec`/`pnpm dlx` prefix in the examples below.
:::
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ import TabbedCode from '@/components/TabbedCode.astro';

4. ### **Install framework specific integrations**

We provide [drop-in integrations](/docs/integrations/overview) for many popular frameworks, and more are in the works. `dmno init` is smart enough to install the relevant integrations for each service. You can also read more about each integration on their respective [pages](docs/integrations/overview).
We provide [drop-in integrations](/docs/integrations/overview) for many popular frameworks, and more are in the works. `dmno init` is smart enough to install the relevant integrations for each service. You can also read more about each integration on their respective [pages](/docs/integrations/overview).

5. ### **Use `DMNO_CONFIG` to access your config**

We recommend migrating to `DMNO_CONFIG` as it provides helpful improvements like TypeScript autocompletion and IntelliSense.
We recommend migrating to `DMNO_CONFIG` as it provides helpful improvements like TypeScript autocompletion and IntelliSense.

For example:
```diff lang="ts" ins="DMNO_CONFIG."
Expand Down
21 changes: 21 additions & 0 deletions packages/docs-site/src/content/docs/docs/integrations/nextjs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ const nextConfig = {
+export default dmnoNextConfigPlugin()(nextConfig);
```


### Set up TypeScript

You must include the DMNO generated types in your tsconfig to get access to `DMNO_CONFIG` and `DMNO_PUBLIC_CONFIG`.

```diff lang="json" title="tsconfig.json"
{
"extends": "astro/tsconfigs/strict",
"include": [
"next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts",
// add types for DMNO_CONFIG
+ "./.dmno/.typegen/global.d.ts",
+ "./.dmno/.typegen/global-public.d.ts",
// ...
],
// ...
}
```
After that, DMNO will automatically update your definitions as you add new items to your schema. 🎉


### Injecting config

Unfortunately, NextJS has limited options to reliably inject code into their framework. So, we need to do a little more manual work to get things wired up correctly.
Expand Down
20 changes: 20 additions & 0 deletions packages/docs-site/src/content/docs/docs/integrations/node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ To get started, install `dmno` and then set up your config schema according to t

<TabbedCode packageName="dmno" />


### Set up TypeScript

You must include the DMNO generated types in your tsconfig to get access to `DMNO_CONFIG` and `DMNO_PUBLIC_CONFIG`.

```diff lang="json" title="tsconfig.json"
{
"include": [
"src/**/*.ts",
// add types for DMNO_CONFIG
+ "./.dmno/.typegen/global.d.ts",
+ "./.dmno/.typegen/global-public.d.ts",
// ...
],
// ...
}
```
After that, DMNO will automatically update your definitions as you add new items to your schema. 🎉


### Loading your config

You must trigger the loading of the `DMNO_CONFIG` global, ideally as the _first thing_ you do in your application code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Then our server file, where we have access to eveything via `DMNO_CONFIG`. This
// ...
}
```
After that, DMNO will automatically update your definitions as you add new items to your schema. 🎉


## Accessing config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ description: The `dmno` cli provides a number of commands to help you run and ma
---

import TabbedCode from '@/components/TabbedCode.astro';
import DmnoExecPathAside from '@/components/DmnoExecPathAside.md';


The `dmno` cli was installed when you [installed](/docs/get-started/quickstart) `dmno` and is available as a depedency in your project.

The `dmno` cli provides a number of commands to help you run and manage your DMNO projects.

:::note
The `dmno` cli is installed as a depedency in your project and is available in your `node_modules/.bin` directory. You can also add it to your `PATH` to run it without using `npm exec`/`npx` or the equivalent in other package managers. For simplicity's sake, we will omit the `npm exec`/`npx` prefix in the examples below.
:::
<DmnoExecPathAside />


### Commands

Expand Down

0 comments on commit 9527262

Please sign in to comment.