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

feat: add scripts subdirectories support #117

Merged
merged 3 commits into from
Jun 24, 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added

- Added support for scripts in subdirectories, for example `scripts/counter/deploy.ts`

## [0.21.0] - 2024-05-27

### Changed
Expand Down
4 changes: 4 additions & 0 deletions src/types/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type File = {
name: string;
path: string;
};
22 changes: 15 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from 'path';
import fs from 'fs/promises';
import { UIProvider } from './ui/UIProvider';
import { SCRIPTS_DIR, WRAPPERS_DIR } from './paths';
import { File } from './types/file';

export const tonDeepLink = (address: Address, amount: bigint, body?: Cell, stateInit?: Cell) =>
`ton://transfer/${address.toString({
Expand Down Expand Up @@ -34,15 +35,22 @@ export function oneOrZeroOf<T extends { [k: string]: boolean | undefined }>(opti

const compileEnd = '.compile.ts';

export const findCompiles = async () =>
export const findCompiles = async (): Promise<File[]> =>
(await fs.readdir(WRAPPERS_DIR))
.filter((f) => f.endsWith(compileEnd))
.map((f) => ({ path: path.join(WRAPPERS_DIR, f), name: f.slice(0, f.length - compileEnd.length) }));

export const findScripts = async () =>
(await fs.readdir(SCRIPTS_DIR))
.filter((f) => f.endsWith('.ts'))
.map((f) => ({ path: path.join(SCRIPTS_DIR, f), name: path.parse(f).name }));
export const findScripts = async (): Promise<File[]> => {
const dirents = await fs.readdir(SCRIPTS_DIR, { recursive: true, withFileTypes: true });
const scripts = dirents.filter((dirent) => dirent.isFile() && dirent.name.endsWith('.ts'));

return scripts
.map((script) => ({
name: path.join(script.path.slice(SCRIPTS_DIR.length), script.name),
path: path.join(SCRIPTS_DIR, script.path, script.name),
}))
.sort((a, b) => (a.name >= b.name ? 1 : -1));
};

export async function selectOption(
options: { name: string; value: string }[],
Expand All @@ -64,14 +72,14 @@ export async function selectOption(
}

export async function selectFile(
files: { name: string; path: string }[],
files: File[],
opts: {
ui: UIProvider;
hint?: string;
import?: boolean;
},
) {
let selected: { name: string; path: string };
let selected: File;

if (opts.hint) {
const found = files.find((f) => f.name.toLowerCase() === opts.hint?.toLowerCase());
Expand Down