Skip to content
Closed
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
43 changes: 43 additions & 0 deletions graphql/codegen/src/__tests__/codegen/cli-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,3 +719,46 @@ describe('multi-target cli with custom builtinNames', () => {
expect(fileNames).not.toContain('commands/context.ts');
});
});

describe('cli stashName', () => {
it('omits stashName from the store call when not configured', () => {
const single = generateCli({
tables: [carTable],
customOperations: { queries: [], mutations: [] },
config: { cli: { toolName: 'myapp' } },
});
const executor = single.files.find((f) => f.fileName === 'executor.ts');
expect(executor!.content).toContain('createConfigStore("myapp")');
});

it('passes stashName to the store while keeping toolName', () => {
const single = generateCli({
tables: [carTable],
customOperations: { queries: [], mutations: [] },
config: { cli: { toolName: 'myapp', stashName: 'shared' } },
});
const executor = single.files.find((f) => f.fileName === 'executor.ts');
expect(executor!.content).toContain('createConfigStore("myapp", {');
expect(executor!.content).toContain('stashName: "shared"');
});

it('passes stashName to multi-target executor and helpers', () => {
const multi = generateMultiTargetCli({
toolName: 'myapp',
stashName: 'shared',
targets: [
{
name: 'app',
endpoint: 'http://app.localhost/graphql',
ormImportPath: '../../generated/app/orm',
tables: [carTable],
customOperations: { queries: [], mutations: [] },
},
],
});
for (const fileName of ['executor.ts', 'helpers.ts']) {
const file = multi.files.find((f) => f.fileName === fileName);
expect(file!.content).toContain('stashName: "shared"');
}
});
});
27 changes: 20 additions & 7 deletions graphql/codegen/src/core/codegen/cli/executor-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,23 @@ function createImportDeclaration(
return decl;
}

export function generateExecutorFile(toolName: string): GeneratedFile {
/**
* `createConfigStore(toolName)`, or `createConfigStore(toolName, { stashName })`
* when the CLI shares its signed-in state with other tools of the same product.
*/
function createStoreCall(toolName: string, stashName?: string): t.CallExpression {
const args: t.Expression[] = [t.stringLiteral(toolName)];
if (stashName) {
args.push(
t.objectExpression([
t.objectProperty(t.identifier('stashName'), t.stringLiteral(stashName)),
]),
);
}
return t.callExpression(t.identifier('createConfigStore'), args);
}

export function generateExecutorFile(toolName: string, stashName?: string): GeneratedFile {
const statements: t.Statement[] = [];

statements.push(
Expand All @@ -44,9 +60,7 @@ export function generateExecutorFile(toolName: string): GeneratedFile {
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('store'),
t.callExpression(t.identifier('createConfigStore'), [
t.stringLiteral(toolName),
]),
createStoreCall(toolName, stashName),
),
]),
);
Expand Down Expand Up @@ -238,6 +252,7 @@ export function generateExecutorFile(toolName: string): GeneratedFile {
export function generateMultiTargetExecutorFile(
toolName: string,
targets: MultiTargetExecutorInput[],
stashName?: string,
): GeneratedFile {
const statements: t.Statement[] = [];

Expand All @@ -260,9 +275,7 @@ export function generateMultiTargetExecutorFile(
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('store'),
t.callExpression(t.identifier('createConfigStore'), [
t.stringLiteral(toolName),
]),
createStoreCall(toolName, stashName),
),
]),
);
Expand Down
15 changes: 11 additions & 4 deletions graphql/codegen/src/core/codegen/cli/helpers-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface HelpersGeneratorInput {
export function generateHelpersFile(
toolName: string,
targets: HelpersGeneratorInput[],
stashName?: string,
): GeneratedFile {
const statements: t.Statement[] = [];

Expand All @@ -61,14 +62,20 @@ export function generateHelpersFile(
);
}

// const store = createConfigStore('toolName');
// const store = createConfigStore('toolName', { stashName: 'product' });
const storeArgs: t.Expression[] = [t.stringLiteral(toolName)];
if (stashName) {
storeArgs.push(
t.objectExpression([
t.objectProperty(t.identifier('stashName'), t.stringLiteral(stashName)),
]),
);
}
statements.push(
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('store'),
t.callExpression(t.identifier('createConfigStore'), [
t.stringLiteral(toolName),
]),
t.callExpression(t.identifier('createConfigStore'), storeArgs),
),
]),
);
Expand Down
13 changes: 9 additions & 4 deletions graphql/codegen/src/core/codegen/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export function generateCli(options: GenerateCliOptions): GenerateCliResult {
? cliConfig.toolName
: 'app';

const executorFile = generateExecutorFile(toolName);
const stashName =
typeof cliConfig === 'object' ? cliConfig.stashName : undefined;

const executorFile = generateExecutorFile(toolName, stashName);
files.push(executorFile);

const utilsFile = generateUtilsFile();
Expand Down Expand Up @@ -127,6 +130,8 @@ export interface MultiTargetCliTarget {

export interface GenerateMultiTargetCliOptions {
toolName: string;
/** Directory identity to share signed-in state with sibling tools. */
stashName?: string;
builtinNames?: BuiltinNames;
targets: MultiTargetCliTarget[];
/** Generate a runnable index.ts entry point */
Expand Down Expand Up @@ -157,7 +162,7 @@ export function resolveBuiltinNames(
export function generateMultiTargetCli(
options: GenerateMultiTargetCliOptions,
): GenerateCliResult {
const { toolName, targets } = options;
const { toolName, stashName, targets } = options;
const files: GeneratedFile[] = [];

const targetNames = targets.map((t) => t.name);
Expand All @@ -168,7 +173,7 @@ export function generateMultiTargetCli(
endpoint: t.endpoint,
ormImportPath: t.ormImportPath,
}));
const executorFile = generateMultiTargetExecutorFile(toolName, executorInputs);
const executorFile = generateMultiTargetExecutorFile(toolName, executorInputs, stashName);
files.push(executorFile);

const utilsFile = generateUtilsFile();
Expand Down Expand Up @@ -201,7 +206,7 @@ export function generateMultiTargetCli(
name: t.name,
ormImportPath: t.ormImportPath,
}));
const helpersFile = generateHelpersFile(toolName, helpersInputs);
const helpersFile = generateHelpersFile(toolName, helpersInputs, stashName);
files.push(helpersFile);

let totalTables = 0;
Expand Down
1 change: 1 addition & 0 deletions graphql/codegen/src/core/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,7 @@ export async function generateMulti(
const firstTargetConfig = configs[names[0]];
const { files } = generateMultiTargetCli({
toolName,
stashName: cliConfig.stashName,
builtinNames: cliConfig.builtinNames,
targets: cliTargets,
entryPoint: cliConfig.entryPoint,
Expand Down
9 changes: 9 additions & 0 deletions graphql/codegen/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ export interface CliConfig {
*/
toolName?: string;

/**
* Directory identity for the stored contexts and credentials, when several
* tools are one product and should share a single signed-in state (e.g. a
* generated CLI, an agent CLI and a desktop app all using `constructive`).
* `toolName` still drives env-var prefixes and help text.
* @default toolName
*/
stashName?: string;

/**
* Override infra command names (for collision handling)
* Defaults: auth -> 'auth' (renamed to 'credentials' on collision),
Expand Down
2 changes: 1 addition & 1 deletion pgpm/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@pgsql/quotes": "^18.2.1",
"appstash": "^0.7.0",
"find-and-require-package-json": "^0.9.1",
"genomic": "^5.6.2",
"genomic": "^5.6.4",
"inquirerer": "^4.9.3",
"js-yaml": "^4.1.0",
"pg-cache": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion pgpm/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@pgpmjs/transform": "workspace:^",
"@pgpmjs/types": "workspace:^",
"csv-to-pg": "workspace:^",
"genomic": "^5.6.2",
"genomic": "^5.6.4",
"git-changed": "^0.3.0",
"glob": "^13.0.6",
"parse-package-name": "^1.0.0",
Expand Down
28 changes: 9 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading