autofold
Version:
A lightweight utility for creating directory structures and files from a tree-like string representation.
244 lines (243 loc) • 7.2 kB
TypeScript
type FolderNode = {
type: "folder" | "file";
name: string;
children?: FolderNode[];
};
type NestedTree = {
[key: string]: null | string | NestedTree;
};
/**
* Transforms a tree string representation into an array of FolderNode objects.
* The input string represents a directory structure using ASCII characters (e.g., ├, └, │, ─).
* @param input - The string representation of the directory tree.
* @returns An array of FolderNode objects representing the parsed directory structure.
* @example
* ```typescript
* const treeString = `
* project
* ├── src
* │ ├── index.ts
* │ └── utils
* │ └── helper.ts
* └── package.json
* `;
* const result = transformTreeString(treeString);
* // Returns:
* // [
* // {
* // type: 'folder',
* // name: 'project',
* // children: [
* // {
* // type: 'folder',
* // name: 'src',
* // children: [
* // { type: 'file', name: 'index.ts' },
* // {
* // type: 'folder',
* // name: 'utils',
* // children: [{ type: 'file', name: 'helper.ts' }]
* // }
* // ]
* // },
* // { type: 'file', name: 'package.json' }
* // ]
* // }
* // ]
* ```
*/
declare function transformTreeString(input: string): FolderNode[];
/**
* Transforms a nested object (NestedTree) into an array of FolderNode objects.
* The input object represents a directory structure where folders are objects and files are null or empty objects with a file extension.
* @param input - The nested object representing the directory structure.
* @returns An array of FolderNode objects representing the parsed directory structure.
* @example
* ```typescript
* const nestedTree = {
* autofold: {
* '.bunfig.toml': null,
* src: {
* 'index.ts': null,
* utils: {
* 'helper.ts': null
* }
* }
* }
* };
* const result = transformNestedTree(nestedTree);
* // Returns:
* // [
* // {
* // type: 'folder',
* // name: 'autofold',
* // children: [
* // { type: 'file', name: '.bunfig.toml' },
* // {
* // type: 'folder',
* // name: 'src',
* // children: [
* // { type: 'file', name: 'index.ts' },
* // {
* // type: 'folder',
* // name: 'utils',
* // children: [{ type: 'file', name: 'helper.ts' }]
* // }
* // ]
* // }
* // ]
* // }
* // ]
* ```
*/
declare function transformNestedTree(input: NestedTree): FolderNode[];
/**
* Transforms a flat object with boolean values into an array of FolderNode objects.
* The keys represent file or folder paths, and the values indicate their presence.
* @param input - A flat object where keys are file/folder paths and values are booleans.
* @returns An array of FolderNode objects representing the parsed directory structure.
* @example
* ```typescript
* const flatObject = {
* 'autofold': true,
* 'autofold/.bunfig.toml': true,
* 'autofold/src': true,
* 'autofold/src/index.ts': true,
* 'autofold/src/utils': true,
* 'autofold/src/utils/helper.ts': true
* };
* const result = transformFlatObject(flatObject);
* // Returns:
* // [
* // {
* // type: 'folder',
* // name: 'autofold',
* // children: [
* // { type: 'file', name: '.bunfig.toml' },
* // {
* // type: 'folder',
* // name: 'src',
* // children: [
* // { type: 'file', name: 'index.ts' },
* // {
* // type: 'folder',
* // name: 'utils',
* // children: [{ type: 'file', name: 'helper.ts' }]
* // }
* // ]
* // }
* // ]
* // }
* // ]
* ```
*/
declare function transformFlatObject(input: Record<string, boolean>): FolderNode[];
/**
* Transforms an array of path segments into an array of FolderNode objects.
* Each path segment array represents a file or folder path.
* @param paths - An array of string arrays, where each inner array represents a path.
* @returns An array of FolderNode objects representing the parsed directory structure.
* @example
* ```typescript
* const pathSegments = [
* ['autofold'],
* ['autofold', '.bunfig.toml'],
* ['autofold', 'src'],
* ['autofold', 'src', 'index.ts'],
* ['autofold', 'src', 'utils'],
* ['autofold', 'src', 'utils', 'helper.ts']
* ];
* const result = parsePathSegments(pathSegments);
* // Returns:
* // [
* // {
* // type: 'folder',
* // name: 'autofold',
* // children: [
* // { type: 'file', name: '.bunfig.toml' },
* // {
* // type: 'folder',
* // name: 'src',
* // children: [
* // { type: 'file', name: 'index.ts' },
* // {
* // type: 'folder',
* // name: 'utils',
* // children: [{ type: 'file', name: 'helper.ts' }]
* // }
* // ]
* // }
* // ]
* // }
* // ]
* ```
*/
declare function parsePathSegments(paths: string[][]): FolderNode[];
/**
* Creates a directory tree from an array of path strings.
* Each path represents a file or folder in the directory structure.
* @param paths - An array of strings representing file or folder paths.
* @returns An array of FolderNode objects representing the parsed directory structure.
* @example
* ```typescript
* const paths = [
* 'autofold/.bunfig.toml',
* 'autofold/src/index.ts',
* 'autofold/src/utils/helper.ts'
* ];
* const result = createDirectoryTree(paths);
* // Returns:
* // [
* // {
* // type: 'folder',
* // name: 'autofold',
* // children: [
* // { type: 'file', name: '.bunfig.toml' },
* // {
* // type: 'folder',
* // name: 'src',
* // children: [
* // { type: 'file', name: 'index.ts' },
* // {
* // type: 'folder',
* // name: 'utils',
* // children: [{ type: 'file', name: 'helper.ts' }]
* // }
* // ]
* // }
* // ]
* // }
* // ]
* ```
*/
declare function createDirectoryTree(paths: string[]): FolderNode[];
/**
* Creates a physical directory and file structure from a FolderNode structure.
* @param structure - The FolderNode representing the directory/file structure.
* @param base - The base directory path where the structure will be created (default: '.').
* @param parentPath - The parent path for recursive calls (default: '').
* @returns A Promise that resolves when the structure is created.
* @example
* ```typescript
* const structure = {
* type: 'folder',
* name: 'project',
* children: [
* {
* type: 'folder',
* name: 'src',
* children: [{ type: 'file', name: 'index.ts' }]
* },
* { type: 'file', name: 'package.json' }
* ]
* };
* await createStructure(structure, './output');
* // Creates:
* // ./output/project
* // ./output/project/src
* // ./output/project/src/index.ts
* // ./output/project/package.json
* ```
*/
declare function createStructure(structure: FolderNode, base?: string, parentPath?: string): Promise<void>;
export { transformTreeString, transformNestedTree, transformFlatObject, parsePathSegments, createStructure, createDirectoryTree };