Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Johan
committed
Sep 16, 2019
1 parent
b7d8eb4
commit e45be6b
Showing
8 changed files
with
81 additions
and
6 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
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,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; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { FlexiPath, WalkOptions } from "."; | ||
|
||
export interface PathWalker { | ||
forward: (options?: WalkOptions) => FlexiPath[]; | ||
back: (options?: WalkOptions) => FlexiPath; | ||
} |
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,4 @@ | ||
export enum TextTransform { | ||
Plain = 0, | ||
JSON = 1 | ||
} |
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
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,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(); | ||
}); | ||
}); |