-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Extensible: Create FileSystemRepresentation * Refactoring: Move most jobs to subclasses of FileSystemRepresentation
- Loading branch information
Showing
5 changed files
with
108 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/typescript/fs-tree-utils/lib/classes/fs-representation/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import * as path from 'path' | ||
import * as fsx from 'fs-extra' | ||
import {WriteTreeObject, WriteTree} from '../../types' | ||
import create from '../../create' | ||
|
||
export abstract class FileSystemRepresentation { | ||
abstract async write (target: string): Promise<void> | ||
} | ||
|
||
export namespace FileSystemRepresentation { | ||
export class File extends FileSystemRepresentation { | ||
private readonly content: File.Content | ||
|
||
constructor (content: File.Content) { | ||
super() | ||
this.content = content | ||
} | ||
|
||
async write (filename: string) { | ||
await fsx.writeFile(filename, this.content) | ||
} | ||
} | ||
|
||
export namespace File { | ||
export type Content = string | Buffer | ||
} | ||
|
||
export class Directory extends FileSystemRepresentation { | ||
readonly content: Directory.Content | ||
|
||
constructor (content: Directory.Content) { | ||
super() | ||
this.content = content | ||
} | ||
|
||
async write (dirname: string) { | ||
if (fsx.existsSync(dirname)) { | ||
const stats = await fsx.stat(dirname) | ||
if (!stats.isDirectory()) { | ||
throw new Error(`Entity ${dirname} exists but is not directory`) | ||
} | ||
} else { | ||
await fsx.mkdir(dirname) | ||
} | ||
|
||
await Promise.all( | ||
Object | ||
.entries(this.content) | ||
.map(([key, val]): [string, WriteTree] => [path.join(dirname, key), val]) | ||
.map(([newContainer, newTree]) => create(newTree, newContainer)) | ||
) | ||
} | ||
} | ||
|
||
export namespace Directory { | ||
export type Content = WriteTreeObject | ||
} | ||
|
||
export class Symlink extends FileSystemRepresentation { | ||
private readonly linkTarget: string | ||
private readonly type?: Symlink.Options.Type | ||
|
||
constructor (linkTarget: string, options: Symlink.Options = {}) { | ||
super() | ||
this.linkTarget = linkTarget | ||
Object.assign(this, options) | ||
} | ||
|
||
async write (linkName: string) { | ||
await fsx.symlink(linkName, this.linkTarget, this.type) | ||
} | ||
} | ||
|
||
export namespace Symlink { | ||
export interface Options { | ||
readonly type?: Options.Type | ||
} | ||
|
||
export namespace Options { | ||
export type Type = 'dir' | 'file' | 'junction' | ||
} | ||
} | ||
} | ||
|
||
export default FileSystemRepresentation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import FileSystemRepresentation from './fs-representation' | ||
export {FileSystemRepresentation} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,12 @@ | ||
import FileSystemRepresentation from '../classes/fs-representation' | ||
|
||
export type FileContent = string | ||
export type TreeObject = {[name: string]: TreeObject | FileContent} | ||
export type TreeNode = FileContent | TreeObject | ||
export type Tree = TreeNode | ||
|
||
export type WriteFileContent = FileContent | ||
export type WriteFunction = (name: string) => Promise<void> | void | ||
export type WriteTreeObject = {readonly [name: string]: WriteTreeObject | WriteFileContent} | ||
export type WriteTreeNode = WriteFileContent | WriteFunction | WriteTreeObject | FileSystemRepresentation | ||
export type WriteTree = WriteTreeNode |