Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

small fixes nov 26 #172

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-gifts-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dmno": patch
---

fix dmno resolve --format=json, cwd based detection of current package/service
13 changes: 6 additions & 7 deletions packages/core/src/cli/commands/resolve.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ program.action(async (opts: {
checkForSchemaErrors(workspace);
checkForConfigErrors(service, { showAll: opts?.showAll });

async function getExposedConfigValues() {
const injectedJson = await ctx.dmnoServer.makeRequest('getInjectedJson', service.serviceName);
let exposedConfig = service.configNodes;
function getExposedConfigValues() {
const values = {} as Record<string, any>;
for (const itemKey in injectedJson) {
if (itemKey.startsWith('$')) continue;
if (injectedJson[itemKey].value && opts.public) continue;
values[itemKey] = injectedJson[itemKey].value;
for (const itemKey in service.configNodes) {
const item = service.configNodes[itemKey];
// --public option skips anything sensitive
if (item.isSensitive && opts.public) continue;
values[itemKey] = item.resolvedValue;
}
return values;
}
Expand Down
40 changes: 28 additions & 12 deletions packages/core/src/config-loader/dmno-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path, { dirname } from 'node:path';
import fs from 'node:fs';
import crypto from 'node:crypto';
import { fileURLToPath } from 'node:url';

import _ from 'lodash-es';
import getPort from 'get-port';
import { Server as SocketIoServer } from 'socket.io';
import uWS from 'uWebSockets.js';
Expand All @@ -18,23 +18,22 @@ import { findDmnoServices } from './find-services';
import { MIME_TYPES_BY_EXT, uwsBodyParser, uwsValidateClientCert } from '../lib/uws-utils';
import { UseAtPhases } from '../config-engine/configraph-adapter';


const __dirname = dirname(fileURLToPath(import.meta.url));

// TODO: do we want to allow changing the host? or just always use localhost?
// see old config-server.ts for details

// TODO: do we want to allow toggling OFF ssl for the web ui?

const DEFAULT_PORT = 3666; // DMNO on a telephone :)


function getCurrentPackageName() {
function getCurrentPackageNameFromPackageManager() {
// rely on package manager to detect current package
if (process.env.npm_package_name !== undefined) return process.env.npm_package_name;
if (process.env.PNPM_PACKAGE_NAME !== undefined) return process.env.PNPM_PACKAGE_NAME;
}

const DEFAULT_PORT = 3666; // DMNO on a telephone :)

export class DmnoServer {
private serverId?: string;
private serverPort?: number;
Expand Down Expand Up @@ -453,20 +452,37 @@ export class DmnoServer {
return this.makeRequest('loadFullSchema');
}


// TODO: this isnt necessarily in the right place
// but the logic was moved from ConfigServerClient and it is convenient to have
// this within whatever will be imported and used within integrations (vite plugins)
// this within whatever will be imported and used within integrations (vite plugins)
async getCurrentPackageConfig() {
const packageName = getCurrentPackageName();
if (packageName === '') {
throw new Error('To use dmno, you must set a package "name" in your package.json file');
const workspace = await this.getWorkspace();

// we try to detect current package from package manager injected env vars
let packageName = getCurrentPackageNameFromPackageManager();
// but if running a script directly, we must figure it out another way
// so we compare CWD to the service paths and choose the most specific one
if (!packageName) {
const cwd = process.cwd();
const possibleServices = _.sortBy(
_.values(_.pickBy(workspace.services, (s) => cwd.startsWith(s.path))),
(s) => s.path.length,
);
// if we are not inside any of our dmno services, we don't know what to do
if (!possibleServices.length) {
throw new Error('Unable to detect current package from CWD. Try running via your package manager.');
}
packageName = possibleServices.pop()!.packageName;
}

if (!packageName) {
throw new Error('Unable to detect current dmno package.');
}
// what to do if we can't figure out a package name?

const workspace = await this.getWorkspace();
const service = Object.values(workspace.services).find((s) => s.packageName === packageName);
if (!service) {
throw new Error(`Unable to select service - ${packageName}`);
throw new Error(`Unable to select service by package name - ${packageName}`);
}

const injectedEnv = await this.makeRequest('getInjectedJson', service.serviceName);
Expand Down