Skip to content

Commit

Permalink
cwd based detection of current service/package
Browse files Browse the repository at this point in the history
  • Loading branch information
theoephraim committed Nov 26, 2024
1 parent 1d8ff1b commit f3f66c7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
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
38 changes: 27 additions & 11 deletions packages/core/src/config-loader/dmno-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f3f66c7

Please sign in to comment.