Skip to content

Commit

Permalink
Change schema
Browse files Browse the repository at this point in the history
  • Loading branch information
KSXGitHub committed Apr 7, 2018
1 parent 800b39a commit 572be63
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 30 deletions.
38 changes: 31 additions & 7 deletions packages/typescript/fs-tree-utils/lib/read.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import * as path from 'path'
import ramda from 'ramda'
import * as fsx from 'fs-extra'
import traverse, {DeepFunc} from './traverse'
import {Tree, TreeObject, FileContent} from './types'

export type NestedReadResult = Promise<Tree>
export type FlatReadResultValue = {[filename: string]: FileContent}

export type FlatReadResultFileContent = {[filename: string]: FileContent}

export type FlatReadResultValue = {
fileContents: FlatReadResultFileContent,
directories: string[],
files: string[],
all: string[]
}

export type FlatReadResult = Promise<FlatReadResultValue>

export async function readNested (name: string): NestedReadResult {
Expand All @@ -28,18 +38,32 @@ export async function readNested (name: string): NestedReadResult {

export async function readFlat (name: string, deep?: DeepFunc): FlatReadResult {
const array = await traverse(name, deep)
const [fileList, dirList] = ramda.partition(x => x.stats.isFile(), array)

const map = await Promise.all(
array
.filter(x => x.stats.isFile())
.map((x): [string, Promise<string>] => [x.path, fsx.readFile(x.path, 'utf8')])
fileList.map(
(x): [string, Promise<string>] =>
[x.path, fsx.readFile(x.path, 'utf8')]
)
)

let result: FlatReadResultValue = {}
let fileContents: FlatReadResultFileContent = {}
for (const [name, promise] of map) {
result[name] = await promise
fileContents[name] = await promise
}
return result

return (() => {
const directories = dirList.map(x => x.path)
const files = fileList.map(x => x.path)
const all = [...directories, ...files].sort((a, b) => a < b ? -1 : a < b ? 1 : 0)

return {
fileContents,
directories,
files,
all
}
})()
}

function addAsyncProperty<X> (x: X) {
Expand Down
4 changes: 3 additions & 1 deletion packages/typescript/fs-tree-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"main": "index.js",
"dependencies": {
"fs-extra": "^5.0.0",
"@types/fs-extra": "^5.0.1"
"ramda": "^0.25.0",
"@types/fs-extra": "^5.0.1",
"@types/ramda": "^0.25.21"
},
"devDependencies": {
"extra-jest": "^0.0.0"
Expand Down
12 changes: 12 additions & 0 deletions packages/typescript/fs-tree-utils/shrinkwrap.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
dependencies:
'@types/fs-extra': 5.0.1
'@types/ramda': 0.25.21
extra-jest: 'link:../extra-jest'
fs-extra: 5.0.0
ramda: 0.25.0
packages:
/@types/fs-extra/5.0.1:
dependencies:
Expand All @@ -13,6 +15,10 @@ packages:
dev: false
resolution:
integrity: sha512-UWkRY9X7RQHp5OhhRIIka58/gVVycL1zHZu0OTsT5LI86ABaMOSbUjAl+b0FeDhQcxclrkyft3kW5QWdMRs8wQ==
/@types/ramda/0.25.21:
dev: false
resolution:
integrity: sha512-y4Dx3t+EA5Y+s87cwUoIzo0Tsj9z7QCu4gaXrdFln4wQRgk9igBskEEe7gZ0PG8dSXtQb29oA3RS//3XquHdiw==
/fs-extra/5.0.0:
dependencies:
graceful-fs: 4.1.11
Expand All @@ -33,6 +39,10 @@ packages:
graceful-fs: 4.1.11
resolution:
integrity: sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=
/ramda/0.25.0:
dev: false
resolution:
integrity: sha512-GXpfrYVPwx3K7RQ6aYT8KPS8XViSXUVJT1ONhoKPE9VAleW42YE+U+8VEyGWt41EnEQW7gwecYJriTI0pKoecQ==
/universalify/0.1.1:
dev: false
resolution:
Expand All @@ -42,5 +52,7 @@ shrinkwrapMinorVersion: 4
shrinkwrapVersion: 3
specifiers:
'@types/fs-extra': ^5.0.1
'@types/ramda': ^0.25.21
extra-jest: ^0.0.0
fs-extra: ^5.0.0
ramda: ^0.25.0
158 changes: 136 additions & 22 deletions packages/typescript/fs-tree-utils/test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,61 @@
exports[`create function creates files/directories that yet to exist 1`] = `
Object {
"flat": Object {
"tmp/create.0/topB/middleB/bottomB": "B.B.B",
"tmp/create.0/topB/middleC/bottomA": "B.C.A",
"tmp/create.0/topB/middleC/bottomB": "B.C.B",
"tmp/create.0/topC/middleA/bottomA": "C.A.A",
"tmp/create.0/topC/middleA/bottomB": "C.A.B",
"tmp/create.0/topC/middleB/bottomA": "C.B.A",
"tmp/create.0/topC/middleB/bottomB": "C.B.B",
"tmp/create.0/topC/middleC/bottomA": "C.C.A",
"tmp/create.0/topC/middleC/bottomB": "C.C.B",
"all": Array [
"tmp/create.0/topA",
"tmp/create.0/topC",
"tmp/create.0/topC/middleA",
"tmp/create.0/topB/middleC",
"tmp/create.0/topB/middleB/bottomB",
"tmp/create.0/topB/middleC/bottomA",
"tmp/create.0/topB/middleC/bottomB",
"tmp/create.0/topC/middleA/bottomA",
"tmp/create.0/topC/middleB",
"tmp/create.0/topC/middleA/bottomB",
"tmp/create.0/topB/middleB/bottomA",
"tmp/create.0/topC/middleB/bottomA",
"tmp/create.0/topB/middleB",
"tmp/create.0/topC/middleB/bottomB",
"tmp/create.0/topB/middleA",
"tmp/create.0/topC/middleC/bottomA",
"tmp/create.0/topB",
"tmp/create.0/topC/middleC/bottomB",
"tmp/create.0/topC/middleC",
],
"directories": Array [
"tmp/create.0/topA",
"tmp/create.0/topB",
"tmp/create.0/topB/middleA",
"tmp/create.0/topB/middleB",
"tmp/create.0/topB/middleB/bottomA",
"tmp/create.0/topB/middleC",
"tmp/create.0/topC",
"tmp/create.0/topC/middleA",
"tmp/create.0/topC/middleB",
"tmp/create.0/topC/middleC",
],
"fileContents": Object {
"tmp/create.0/topB/middleB/bottomB": "B.B.B",
"tmp/create.0/topB/middleC/bottomA": "B.C.A",
"tmp/create.0/topB/middleC/bottomB": "B.C.B",
"tmp/create.0/topC/middleA/bottomA": "C.A.A",
"tmp/create.0/topC/middleA/bottomB": "C.A.B",
"tmp/create.0/topC/middleB/bottomA": "C.B.A",
"tmp/create.0/topC/middleB/bottomB": "C.B.B",
"tmp/create.0/topC/middleC/bottomA": "C.C.A",
"tmp/create.0/topC/middleC/bottomB": "C.C.B",
},
"files": Array [
"tmp/create.0/topB/middleB/bottomB",
"tmp/create.0/topB/middleC/bottomA",
"tmp/create.0/topB/middleC/bottomB",
"tmp/create.0/topC/middleA/bottomA",
"tmp/create.0/topC/middleA/bottomB",
"tmp/create.0/topC/middleB/bottomA",
"tmp/create.0/topC/middleB/bottomB",
"tmp/create.0/topC/middleC/bottomA",
"tmp/create.0/topC/middleC/bottomB",
],
},
"nested": Object {
"topA": Object {},
Expand Down Expand Up @@ -48,10 +94,32 @@ exports[`create function ignores already existed files/directories that is not m
Object {
"firstBorn": Object {
"flat": Object {
"tmp/create.1/bar": "bar",
"tmp/create.1/firstBornFiles/bar": "bar",
"tmp/create.1/firstBornFiles/foo": "foo",
"tmp/create.1/foo": "foo",
"all": Array [
"tmp/create.1/emptyBar",
"tmp/create.1/emptyFoo",
"tmp/create.1/firstBornFiles",
"tmp/create.1/bar",
"tmp/create.1/firstBornFiles/bar",
"tmp/create.1/firstBornFiles/foo",
"tmp/create.1/foo",
],
"directories": Array [
"tmp/create.1/emptyBar",
"tmp/create.1/emptyFoo",
"tmp/create.1/firstBornFiles",
],
"fileContents": Object {
"tmp/create.1/bar": "bar",
"tmp/create.1/firstBornFiles/bar": "bar",
"tmp/create.1/firstBornFiles/foo": "foo",
"tmp/create.1/foo": "foo",
},
"files": Array [
"tmp/create.1/bar",
"tmp/create.1/firstBornFiles/bar",
"tmp/create.1/firstBornFiles/foo",
"tmp/create.1/foo",
],
},
"nested": Object {
"bar": "bar",
Expand All @@ -66,15 +134,61 @@ Object {
},
"secondBorn": Object {
"flat": Object {
"tmp/create.1/bar": "bar",
"tmp/create.1/firstBornFiles/bar": "bar",
"tmp/create.1/firstBornFiles/foo": "foo",
"tmp/create.1/foo": "foo",
"tmp/create.1/newFileA": "First New File",
"tmp/create.1/newFileB": "Second New File",
"tmp/create.1/newFileC": "Third New File",
"tmp/create.1/newTreeC/emptyFile/file": "",
"tmp/create.1/newTreeC/nonEmptyFile/file": "Not empty",
"all": Array [
"tmp/create.1/emptyBar",
"tmp/create.1/bar",
"tmp/create.1/firstBornFiles/bar",
"tmp/create.1/firstBornFiles/foo",
"tmp/create.1/foo",
"tmp/create.1/newFileA",
"tmp/create.1/newFileB",
"tmp/create.1/newTreeA",
"tmp/create.1/newFileC",
"tmp/create.1/newTreeB",
"tmp/create.1/newTreeB/empty",
"tmp/create.1/newTreeC",
"tmp/create.1/newTreeC/empty",
"tmp/create.1/newTreeC/emptyFile",
"tmp/create.1/firstBornFiles",
"tmp/create.1/newTreeC/emptyFile/file",
"tmp/create.1/emptyFoo",
"tmp/create.1/newTreeC/nonEmptyFile/file",
"tmp/create.1/newTreeC/nonEmptyFile",
],
"directories": Array [
"tmp/create.1/emptyBar",
"tmp/create.1/emptyFoo",
"tmp/create.1/firstBornFiles",
"tmp/create.1/newTreeA",
"tmp/create.1/newTreeB",
"tmp/create.1/newTreeB/empty",
"tmp/create.1/newTreeC",
"tmp/create.1/newTreeC/empty",
"tmp/create.1/newTreeC/emptyFile",
"tmp/create.1/newTreeC/nonEmptyFile",
],
"fileContents": Object {
"tmp/create.1/bar": "bar",
"tmp/create.1/firstBornFiles/bar": "bar",
"tmp/create.1/firstBornFiles/foo": "foo",
"tmp/create.1/foo": "foo",
"tmp/create.1/newFileA": "First New File",
"tmp/create.1/newFileB": "Second New File",
"tmp/create.1/newFileC": "Third New File",
"tmp/create.1/newTreeC/emptyFile/file": "",
"tmp/create.1/newTreeC/nonEmptyFile/file": "Not empty",
},
"files": Array [
"tmp/create.1/bar",
"tmp/create.1/firstBornFiles/bar",
"tmp/create.1/firstBornFiles/foo",
"tmp/create.1/foo",
"tmp/create.1/newFileA",
"tmp/create.1/newFileB",
"tmp/create.1/newFileC",
"tmp/create.1/newTreeC/emptyFile/file",
"tmp/create.1/newTreeC/nonEmptyFile/file",
],
},
"nested": Object {
"bar": "bar",
Expand Down

0 comments on commit 572be63

Please sign in to comment.