Skip to content

Commit de8412c

Browse files
authored
fix: fixing context resolution issues with xanoscript repo generation (#74)
2 parents 7610565 + af3ad93 commit de8412c

File tree

11 files changed

+65
-38
lines changed

11 files changed

+65
-38
lines changed

.changeset/cuddly-tables-own.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@calycode/types": patch
3+
"@calycode/utils": patch
4+
"@calycode/core": patch
5+
"@calycode/cli": patch
6+
---
7+
8+
feat: added getStartDir() method to the ConfigStorage interface. Goal is to allow platform agnosticity in the context resolution.
9+
fix: fixing an issue where xanoscript generation would fail due to missing context
10+
fix: fixing package.json for package publishing, not building packages in the github action resulted with non-existent dist directory...

packages/cli/src/commands/generate-xanoscript-repo.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,27 @@ async function generateXanoscriptRepo({ instance, workspace, branch, core, print
3131
workspace,
3232
branch,
3333
});
34-
const { instanceConfig, workspaceConfig, branchConfig } = await resolveConfigs({
34+
const { instanceConfig, workspaceConfig, branchConfig, context } = await resolveConfigs({
35+
requiredFields: ['instance', 'workspace', 'branch'],
3536
cliContext: { instance, workspace, branch },
3637
core,
3738
});
3839

3940
// Resolve output dir
4041
const outputDir = replacePlaceholders(instanceConfig.xanoscript.output, {
41-
'@': await core.findProjectRoot(),
42+
'@': await findProjectRoot(),
4243
instance: instanceConfig.name,
4344
workspace: workspaceConfig.name,
4445
branch: branchConfig.label,
4546
});
4647

4748
clearDirectory(outputDir);
48-
await mkdirSync(outputDir, { recursive: true });
49+
mkdirSync(outputDir, { recursive: true });
4950

5051
const plannedWrites: { path: string; content: string }[] = await core.buildXanoscriptRepo({
51-
instance,
52-
workspace,
53-
branch,
52+
instance: context.instance,
53+
workspace: context.workspace,
54+
branch: context.branch,
5455
});
5556
await Promise.all(
5657
plannedWrites.map(async ({ path, content }) => {

packages/cli/src/node-config-storage.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import os from 'os';
1313
import { x } from 'tar';
1414
import { tmpdir } from 'os';
1515
import { join } from 'path';
16-
import { ConfigStorage, CoreContext, InstanceConfig } from '@calycode/types';
16+
import { ConfigStorage, InstanceConfig } from '@calycode/types';
1717

1818
const BASE_DIR = path.join(os.homedir(), '.xano-tools');
1919
const GLOBAL_CONFIG_PATH = path.join(BASE_DIR, 'config.json');
@@ -219,6 +219,15 @@ export const nodeConfigStorage: ConfigStorage = {
219219
fs.writeFileSync(p, token, { mode: 0o600 });
220220
},
221221

222+
/**
223+
* Get the current working directory as 'startDir' for reuse in the core methods.
224+
*
225+
* @return string
226+
*/
227+
getStartDir() {
228+
return process.cwd();
229+
},
230+
//
222231
// ----- FILESYSTEM OPS -----
223232
async mkdir(dirPath, options) {
224233
await fs.promises.mkdir(dirPath, options);

packages/cli/src/utils/commands/context-resolution.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ async function promptForContext(
2626
const selectedWorkspaceId = responses['workspace'] ?? knownContext.workspace;
2727
// Workspace could be identified by id or label or name; adjust as needed:
2828
let workspace = instanceConfig.workspaces.find(
29-
(w) =>
30-
w.id === selectedWorkspaceId ||
31-
w.name === selectedWorkspaceId ||
32-
w.label === selectedWorkspaceId
29+
(w) => w.id === selectedWorkspaceId || w.name === selectedWorkspaceId
3330
);
3431
choices = workspace?.branches || [];
3532
}

packages/core/src/features/testing/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ async function testRunner({
5151
groups,
5252
testConfig,
5353
core,
54+
storage,
5455
}: {
5556
context: CoreContext;
5657
groups: ApiGroupConfig[];
@@ -64,6 +65,7 @@ async function testRunner({
6465
customAsserts: AssertDefinition;
6566
}[];
6667
core: Caly;
68+
storage: Caly['storage'];
6769
}): Promise<
6870
{
6971
group: ApiGroupConfig;
@@ -80,7 +82,7 @@ async function testRunner({
8082
const { instance, workspace, branch } = context;
8183

8284
core.emit('start', { name: 'start-testing', payload: context });
83-
const startDir = process.cwd();
85+
const startDir = storage.getStartDir();
8486
const { instanceConfig, workspaceConfig, branchConfig } = await core.loadAndValidateContext({
8587
instance,
8688
workspace,

packages/core/src/implementations/backups.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async function exportBackupImplementation({ instance, workspace, branch, core })
1515
message: 'Loading context...',
1616
percent: 5,
1717
});
18-
const startDir = process.cwd();
18+
const startDir = core.storage.getStartDir();
1919
const { instanceConfig, workspaceConfig, branchConfig } = await core.loadAndValidateContext({
2020
instance,
2121
workspace,
@@ -103,7 +103,7 @@ async function restoreBackupImplementation({ instance, workspace, formData, core
103103
message: 'Loading context...',
104104
percent: 5,
105105
});
106-
const startDir = process.cwd();
106+
const startDir = core.storage.getStartDir();
107107
const { instanceConfig, workspaceConfig } = await core.loadAndValidateContext({
108108
instance,
109109
workspace,

packages/core/src/implementations/build-xanoscript-repo.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,33 @@ async function fetchAndProcessEntities({
4848
"description" = "Xanoscript fetching failed with message: ${xanoscript.message}"
4949
}
5050
`;
51-
tempResults.push({ path: `${path}/script.xs`, content: xanoScriptContent });
51+
tempResults.push({ path: `${path}/script.freezed.xs`, content: xanoScriptContent });
5252
}
5353

5454
return tempResults;
5555
}
5656

57-
async function buildXanoscriptRepoImplementation(
58-
storage: Caly['storage'],
59-
core: Caly,
60-
options: CoreContext
61-
): Promise<{ path: string; content: string }[]> {
57+
async function buildXanoscriptRepoImplementation({
58+
storage,
59+
core,
60+
options,
61+
}: {
62+
storage: Caly['storage'];
63+
core: Caly;
64+
options: CoreContext;
65+
}): Promise<{ path: string; content: string }[]> {
6266
const { instance, workspace, branch } = options;
6367

6468
core.emit('start', { name: 'xs-repo-generation', payload: options });
6569

70+
// [ ] THIS IS WHERE THE ROOT OF ALL EVIL IS
6671
const results: { path: string; content: string }[] = [];
67-
const startDir = process.cwd();
72+
const startDir = storage.getStartDir();
6873
const { instanceConfig, workspaceConfig, branchConfig } = await core.loadAndValidateContext({
6974
instance,
7075
workspace,
7176
branch,
72-
startDir
77+
startDir,
7378
});
7479

7580
const baseUrl = instanceConfig.url;

packages/core/src/implementations/load-and-validate-context.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,6 @@ import {
88
} from '@calycode/types';
99
import { getCurrentContextConfigImplementation } from './get-current-context';
1010

11-
function assignDefined<T>(base: T, overrides: Partial<T>): T {
12-
const result = { ...base };
13-
for (const key in overrides) {
14-
if (overrides[key] !== undefined) {
15-
result[key] = overrides[key]!;
16-
}
17-
}
18-
return result;
19-
}
20-
2111
export async function loadAndValidateContextImplementation({
2212
storage,
2313
overrides,

packages/core/src/implementations/run-tests.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ async function runTestsImplementation({
77
groups,
88
testConfig,
99
core,
10+
storage,
1011
}: {
1112
context: CoreContext;
1213
groups: ApiGroupConfig[];
@@ -20,6 +21,7 @@ async function runTestsImplementation({
2021
customAsserts: AssertDefinition;
2122
}[];
2223
core: Caly;
24+
storage: Caly['storage'];
2325
}): Promise<
2426
{
2527
group: ApiGroupConfig;
@@ -33,7 +35,7 @@ async function runTestsImplementation({
3335
}[];
3436
}[]
3537
> {
36-
return await testRunner({ context, groups, testConfig, core });
38+
return await testRunner({ context, groups, testConfig, core, storage });
3739
}
3840

3941
export { runTestsImplementation };

packages/core/src/index.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,14 @@ export class Caly extends TypedEmitter<EventMap> {
236236
workspace,
237237
branch,
238238
}): Promise<{ path: string; content: string }[]> {
239-
const response = await buildXanoscriptRepoImplementation(this.storage, this, {
240-
instance,
241-
workspace,
242-
branch,
239+
const response = await buildXanoscriptRepoImplementation({
240+
storage: this.storage,
241+
core: this,
242+
options: {
243+
instance,
244+
workspace,
245+
branch,
246+
},
243247
});
244248
return response;
245249
}
@@ -301,7 +305,13 @@ export class Caly extends TypedEmitter<EventMap> {
301305
}[];
302306
}[]
303307
> {
304-
return await runTestsImplementation({ context, groups, testConfig, core: this });
308+
return await runTestsImplementation({
309+
context,
310+
groups,
311+
testConfig,
312+
core: this,
313+
storage: this.storage,
314+
});
305315
}
306316

307317
// ----- SEMI-UTIL METHODS ----- //

0 commit comments

Comments
 (0)