Skip to content

Commit

Permalink
feat: read files
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Sep 16, 2019
1 parent b7d8eb4 commit e45be6b
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/lib/path/index.ts
Expand Up @@ -4,6 +4,7 @@ import {
PathKind,
PathMeta,
PathWalker,
TextTransform,
WalkOptions
} from "../../types";
import walker from "../walker";
Expand All @@ -19,6 +20,7 @@ import meta from "./meta";
import parent from "./parent";
import { constants, pathKind, pathString } from "./parse";
import prepend from "./prepend";
import read from "./read";
import reverse from "./reverse";
import subDirectories from "./subDirectories";
import write from "./write";
Expand Down Expand Up @@ -69,6 +71,12 @@ export const path = (current: Path): FlexiPath => {
? (current as PathMeta)
: meta(current);

const readWrapper = ({
encoding = "utf8",
transform = TextTransform.Plain
}: { encoding?: string; transform?: TextTransform } = {}): any =>
read(pathMeta, { encoding, transform });

return Object.freeze({
...pathMeta,
...{
Expand All @@ -86,6 +94,7 @@ export const path = (current: Path): FlexiPath => {
parent: parent(pathMeta.path),
prepend: (...paths: Path[]) =>
prepend(pathMeta.path, ...paths.map(x => pathString(x))),
read: readWrapper,
reverse: () => reverse(pathMeta.path),
subDirectories: subDirectories(pathMeta.path),
walk: () => pathWalker(pathMeta.path),
Expand Down
23 changes: 23 additions & 0 deletions src/lib/path/read.ts
@@ -0,0 +1,23 @@
import { readFileSync } from "fs";

import { PathMeta, PathType, TextTransform } from "../../types";

const read = (
path: PathMeta,
{
encoding = "utf8",
transform = TextTransform.Plain
}: { encoding?: string; transform?: TextTransform } = {}
): any => {
if (path.type() !== PathType.File) {
return "";
}
const content = readFileSync(path.path, encoding);

if (transform === TextTransform.JSON) {
return JSON.parse(content);
}
return content;
};

export default read;
2 changes: 2 additions & 0 deletions src/types/FlexiPath.ts
Expand Up @@ -5,6 +5,7 @@ import {
Path,
PathMeta,
SubDirectoryQuery,
TextTransform,
PathWalker
} from ".";

Expand Down Expand Up @@ -91,6 +92,7 @@ export interface FlexiPath extends PathMeta {
*/
except(...paths: Path[]): FlexiPath;

read(options?: { encoding?: string; transform?: TextTransform }): any;
walk(): PathWalker;
/**
* Writes the current `path` to disk if possible
Expand Down
6 changes: 6 additions & 0 deletions src/types/PathWalker.ts
@@ -0,0 +1,6 @@
import { FlexiPath, WalkOptions } from ".";

export interface PathWalker {
forward: (options?: WalkOptions) => FlexiPath[];
back: (options?: WalkOptions) => FlexiPath;
}
4 changes: 4 additions & 0 deletions src/types/TextTransform.ts
@@ -0,0 +1,4 @@
export enum TextTransform {
Plain = 0,
JSON = 1
}
5 changes: 0 additions & 5 deletions src/types/Walker.ts
Expand Up @@ -9,8 +9,3 @@ export interface Walker {
acc?: FlexiPath
) => WalkedPath<FlexiPath>;
}

export interface PathWalker {
forward: (options?: WalkOptions) => FlexiPath[];
back: (options?: WalkOptions) => FlexiPath;
}
4 changes: 3 additions & 1 deletion src/types/index.ts
Expand Up @@ -5,6 +5,7 @@ export { PathKind } from "./PathKind";
export { PathMeta } from "./PathMeta";
export { PathType } from "./PathType";
export { PathVistor } from "./PathVistor";
export { PathWalker } from "./PathWalker";
export { PathWithBasePath } from "./PathWithBasePath";
export {
ChildQuery,
Expand All @@ -13,9 +14,10 @@ export {
Query,
SubDirectoryQuery
} from "./Query";
export { TextTransform } from "./TextTransform";
export { WalkedState } from "./WalkedState";
export { WalkedPath } from "./WalkedPath";
export { Walker, PathWalker } from "./Walker";
export { Walker } from "./Walker";
export { Walking } from "./Walking";
export { WalkOptions } from "./WalkOptions";
export { WalkUntil } from "./WalkUntil";
34 changes: 34 additions & 0 deletions test/path/read.test.ts
@@ -0,0 +1,34 @@
import flexi, { TextTransform } from "../../src";
import { testDir } from "../jest/createTestData";

interface PackageJson {
name: string;
description: string;
}

const packageJson = flexi
.path(__dirname)
.parent(x => x.name === "flexi-path")
.append("package.json");

describe("read", () => {
it("can read file", () => {
expect(packageJson.read()).not.toBeEmpty();
});

it("can read file as json", () => {
const json: PackageJson = packageJson.read({
transform: TextTransform.JSON
});

expect(json.name).toBe("flexi-path");
});

it("should be empty when path is dir", () => {
expect(flexi.path(testDir).read()).toBeEmpty();
});

it("should be empty when path is invalid", () => {
expect(flexi.path("invalid").read()).toBeEmpty();
});
});

0 comments on commit e45be6b

Please sign in to comment.