Skip to content

Commit

Permalink
Merge branch 'main' into chore/devwallet/addtitles
Browse files Browse the repository at this point in the history
# Conflicts:
#	packages/apps/dev-wallet/src/pages/networks/Components/network-form.tsx
#	packages/libs/kode-ui/src/patterns/SideBarLayout/components/LayoutProvider.tsx
#	packages/libs/kode-ui/src/patterns/SideBarLayout/components/SideBarAside.tsx
  • Loading branch information
sstraatemans committed Nov 4, 2024
2 parents 5b2dbc9 + 4c46642 commit def9386
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 53 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-pigs-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@kadena/kadena-cli': patch
---

Fixed malformed json error in tx send
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const LayoutProvider: FC<ILayoutProvider> = ({ children }) => {
const setAsideTitle = (value?: string) => {
setAsideTitleState(value);
};

const setAsideRef = (value?: HTMLDivElement | null) => {
setAsideRefState(value ? value : null);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const SideBarAside: FC<
handleSetAsideExpanded(!!location?.hash);
}, [location?.hash]);

console.log(asideRef);
return (
<>
<Stack
Expand Down Expand Up @@ -82,7 +81,7 @@ export const SideBarAside: FC<
</header>

<Stack className={asideContentClass}>
<div ref={ref}></div>
<div ref={ref} />
</Stack>
</aside>
</>
Expand Down
3 changes: 2 additions & 1 deletion packages/tools/kadena-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
"lint": "eslint src --ext .js,.ts --fix",
"test": "vitest run",
"test:coverage": "vitest run --coverage",
"test:watch": "vitest"
"test:watch": "vitest",
"watch": "tsc --watch"
},
"dependencies": {
"@inquirer/prompts": "3.3.0",
Expand Down
35 changes: 19 additions & 16 deletions packages/tools/kadena-cli/src/commands/tx/utils/txHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ import type {
IWalletKeyPair,
} from '../../../services/wallet/wallet.types.js';
import type { CommandResult } from '../../../utils/command.util.js';
import { isNotEmptyObject, notEmpty } from '../../../utils/globalHelpers.js';
import {
isNotEmptyObject,
loadUnknownFile,
notEmpty,
} from '../../../utils/globalHelpers.js';
import { log } from '../../../utils/logger.js';
import { createTable } from '../../../utils/table.js';
import type { ISavedTransaction } from './storage.js';
Expand Down Expand Up @@ -109,27 +113,26 @@ export async function getAllTransactions(
): Promise<{ fileName: string; signed: boolean }[]> {
try {
const files = await services.filesystem.readDir(directory);

// Since naming convention is not enforced, we need to check the content of the files
const transactionFiles = (
await Promise.all(
files.map(async (fileName) => {
if (!fileName.endsWith('.json')) return null;
const filePath = join(directory, fileName);
const content = await services.filesystem.readFile(filePath);
if (content === null) return null;
const JSONParsedContent = JSON.parse(content);
const parsed = IUnsignedCommandSchema.safeParse(JSONParsedContent);
if (parsed.success) {
const isSignedTx = isSignedTransaction(
parsed.data as IUnsignedCommand,
try {
const content = await loadUnknownFile(
path.join(directory, fileName),
);
return {
fileName,
signed: isSignedTx,
};
const parsed = IUnsignedCommandSchema.safeParse(content);
if (parsed.success) {
const signed = isSignedTransaction(
parsed.data as IUnsignedCommand,
);
return { fileName, signed };
}
return null;
} catch (_error) {
return null;
}

return null;
}),
)
).filter(notEmpty);
Expand Down
18 changes: 4 additions & 14 deletions packages/tools/kadena-cli/src/services/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import {
YAML_EXT,
} from '../../constants/config.js';
import {
detectArrayFileParseType,
formatZodError,
getFileParser,
loadUnknownFile,
notEmpty,
safeYamlParse,
} from '../../utils/globalHelpers.js';
Expand Down Expand Up @@ -146,14 +145,9 @@ export class ConfigService implements IConfigService {

public async getPlainKey(
filepath: string,
/* How to parse file, defaults to yaml */
type?: 'yaml' | 'json',
): ReturnType<IConfigService['getPlainKey']> {
const file = await this.services.filesystem.readFile(filepath);
if (file === null || type === undefined) return null;

const parser = getFileParser(type);
const parsed = plainKeySchema.safeParse(parser(file));
const file = await loadUnknownFile(filepath);
const parsed = plainKeySchema.safeParse(file);
if (!parsed.success) return null;

const alias = path.basename(filepath);
Expand All @@ -171,12 +165,8 @@ export class ConfigService implements IConfigService {
): ReturnType<IConfigService['getPlainKeys']> {
const dir = directory ?? process.cwd();
const files = await this.services.filesystem.readDir(dir);
const filepaths = files.map((file) => path.join(dir, file));
const parsableFiles = detectArrayFileParseType(filepaths);
const keys = await Promise.all(
parsableFiles.map(async (file) =>
this.getPlainKey(file.filepath, file.type),
),
files.map(async (file) => this.getPlainKey(path.join(dir, file))),
);
return keys.filter(notEmpty);
}
Expand Down
34 changes: 14 additions & 20 deletions packages/tools/kadena-cli/src/utils/globalHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path';
import sanitize from 'sanitize-filename';
import type { ZodError } from 'zod';
import { MAX_CHAIN_IDS, MAX_CHARACTERS_LENGTH } from '../constants/config.js';
import { services } from '../services/index.js';

/**
* Assigns a value to an object's property if the value is neither undefined nor an empty string.
Expand Down Expand Up @@ -191,34 +192,27 @@ export const safeYamlParse = <T extends unknown>(value: string): T | null => {
}
};

export function detectFileParseType(filepath: string): 'yaml' | 'json' | null {
export function detectFileParseType(
filepath: string,
): (<T extends unknown>(value: string) => T | null) | null {
const ext = path.extname(filepath);
if (ext === '.yaml' || ext === '.yml') {
return 'yaml';
return safeYamlParse;
}
if (ext === '.json') {
return 'json';
return safeJsonParse;
}
return null;
}

export function detectArrayFileParseType(
filepaths: string[],
): { filepath: string; type: 'yaml' | 'json' }[] {
return filepaths.reduce(
(memo, filepath) => {
const type = detectFileParseType(filepath);
if (type) memo.push({ filepath, type });
return memo;
},
[] as { filepath: string; type: 'yaml' | 'json' }[],
);
}

export function getFileParser(
type: 'yaml' | 'json',
): <T extends unknown>(value: string) => T | null {
return type === 'yaml' ? safeYamlParse : safeJsonParse;
export async function loadUnknownFile(
filepath: string,
): Promise<unknown | null> {
const parser = detectFileParseType(filepath);
if (parser === null) return null;
const file = await services.filesystem.readFile(filepath);
if (file === null) return null;
return parser(file);
}

export const generateAllChainIds = (): ChainId[] =>
Expand Down

0 comments on commit def9386

Please sign in to comment.