From 4a705d1e5e4af79086f9ba932a5606d5db9cbc93 Mon Sep 17 00:00:00 2001 From: Aliaksandr Bahdanau <122269567+a-bahdanau@users.noreply.github.com> Date: Mon, 24 Jun 2024 16:24:18 +0300 Subject: [PATCH] feat: add scripts subdirectories support (#117) --- CHANGELOG.md | 6 ++++++ src/types/file.ts | 4 ++++ src/utils.ts | 22 +++++++++++++++------- 3 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 src/types/file.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index bdef360..8a7aa26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/types/file.ts b/src/types/file.ts new file mode 100644 index 0000000..a2e607e --- /dev/null +++ b/src/types/file.ts @@ -0,0 +1,4 @@ +export type File = { + name: string; + path: string; +}; diff --git a/src/utils.ts b/src/utils.ts index 1f22602..475a00e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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({ @@ -34,15 +35,22 @@ export function oneOrZeroOf(opti const compileEnd = '.compile.ts'; -export const findCompiles = async () => +export const findCompiles = async (): Promise => (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 => { + 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 }[], @@ -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());