-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 4f80ad6
Showing
11 changed files
with
4,038 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
build | ||
*.tmp |
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,9 @@ | ||
module.exports = { | ||
roots: ['<rootDir>/src'], | ||
transform: { | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
testRegex: '/__tests__/.*\\.(test|spec)\\.[jt]sx?$', | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
testEnvironment: 'node', | ||
} |
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,46 @@ | ||
{ | ||
"name": "@hermes-serverless/fs-utils", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "build/index.js", | ||
"types": "build/index.d.ts", | ||
"files": [ | ||
"build/**/*" | ||
], | ||
"scripts": { | ||
"test": "jest --logHeapUsage", | ||
"build": "rm -rf build && yarn tsc", | ||
"prepare": "yarn build", | ||
"prepublishOnly": "yarn test && yarn lint", | ||
"lint": "tslint -c tslint.json 'src/**/*.ts'", | ||
"format": "prettier --config ./prettier.config.js --write \"src/**/*.ts\" \"src/**/*.js\"", | ||
"preversion": "yarn lint && yarn format", | ||
"version": "git add --all", | ||
"postversion": "git push && git push --tags" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/hermes-serverless/fs-utils" | ||
}, | ||
"author": "Tiago Nápoli <napoli.tiago96@gmail.com>", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/hermes-serverless/fs-utils/issues" | ||
}, | ||
"homepage": "https://github.com/hermes-serverless/fs-utils#readme", | ||
"dependencies": { | ||
"@hermes-serverless/custom-promises": "^0.0.1" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^24.0.11", | ||
"@types/node": "^12.6.1", | ||
"jest": "^24.7.1", | ||
"prettier": "^1.17.0", | ||
"ts-jest": "^24.0.2", | ||
"ts-node": "^8.1.0", | ||
"tslint": "^5.16.0", | ||
"tslint-config-airbnb": "^5.11.1", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.5.3" | ||
} | ||
} |
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,7 @@ | ||
module.exports = { | ||
trailingComma: 'es5', | ||
tabWidth: 2, | ||
semi: false, | ||
singleQuote: true, | ||
printWidth: 100, | ||
} |
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 @@ | ||
hello world |
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,51 @@ | ||
import { ReadStream, WriteStream } from 'fs' | ||
import path from 'path' | ||
import { createFsReadStream, createFsWriteStream, fileExists } from '..' | ||
|
||
describe('Test createFsReadStream', () => { | ||
test('Open a file to read', async () => { | ||
const stream = await createFsReadStream(path.join(__dirname, 'fixtures', 'hello')) | ||
expect(stream).toBeInstanceOf(ReadStream) | ||
expect(stream.listenerCount('open')).toBe(0) | ||
expect(stream.listenerCount('error')).toBe(0) | ||
}) | ||
|
||
test('Rejects with ENOENT', async () => { | ||
expect.assertions(1) | ||
try { | ||
await createFsReadStream(path.join(__dirname, 'fixtures', 'no-file')) | ||
} catch (err) { | ||
expect(err.code).toBe('ENOENT') | ||
} | ||
}) | ||
}) | ||
|
||
describe('Test createFsWriteStream', () => { | ||
test('Open a file to write', async () => { | ||
const stream = await createFsWriteStream(path.join(__dirname, 'fixtures', 'new-file.tmp')) | ||
expect(stream).toBeInstanceOf(WriteStream) | ||
expect(stream.listenerCount('open')).toBe(0) | ||
expect(stream.listenerCount('error')).toBe(0) | ||
}) | ||
|
||
test('Rejects with ENOENT', async () => { | ||
expect.assertions(1) | ||
try { | ||
await createFsWriteStream(path.join(__dirname, 'fixtures', 'hello'), { flags: 'wx' }) | ||
} catch (err) { | ||
expect(err.code).toBe('EEXIST') | ||
} | ||
}) | ||
}) | ||
|
||
describe('Test fileExists', () => { | ||
test("File doesn't exist", async () => { | ||
await expect(fileExists(path.join(__dirname, 'fixtures', 'file-doesnt-exist'))).resolves.toBe( | ||
false | ||
) | ||
}) | ||
|
||
test('File exist', async () => { | ||
await expect(fileExists(path.join(__dirname, 'fixtures', 'hello'))).resolves.toBe(true) | ||
}) | ||
}) |
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,73 @@ | ||
import { Waiter } from '@hermes-serverless/custom-promises' | ||
import fs, { PathLike } from 'fs' | ||
import { Readable, Writable } from 'stream' | ||
import util from 'util' | ||
|
||
const setupWaiter = (stream: Readable | Writable, waiter: Waiter<any>) => { | ||
const onOpen = () => { | ||
waiter.resolve(stream) | ||
} | ||
|
||
const onError = (err: Error) => { | ||
waiter.reject(err) | ||
} | ||
|
||
const clear = () => { | ||
stream.removeListener('open', onOpen) | ||
stream.removeListener('error', onError) | ||
} | ||
|
||
stream.once('error', onError) | ||
stream.once('open', onOpen) | ||
waiter.then(clear, clear) | ||
} | ||
|
||
export const createFsReadStream = ( | ||
path: PathLike, | ||
options?: | ||
| string | ||
| { | ||
flags?: string | ||
encoding?: string | ||
fd?: number | ||
mode?: number | ||
autoClose?: boolean | ||
start?: number | ||
end?: number | ||
highWaterMark?: number | ||
} | ||
): Promise<Readable> => { | ||
const openWaiter: Waiter<Readable> = new Waiter() | ||
const stream = fs.createReadStream(path, options) | ||
setupWaiter(stream, openWaiter) | ||
return openWaiter.finish() | ||
} | ||
|
||
export const createFsWriteStream = ( | ||
path: PathLike, | ||
options?: | ||
| string | ||
| { | ||
flags?: string | ||
encoding?: string | ||
fd?: number | ||
mode?: number | ||
autoClose?: boolean | ||
start?: number | ||
} | ||
): Promise<Writable> => { | ||
const openWaiter: Waiter<Writable> = new Waiter() | ||
const stream = fs.createWriteStream(path, options) | ||
setupWaiter(stream, openWaiter) | ||
return openWaiter.finish() | ||
} | ||
|
||
export const fileExists = async (filepath: string) => { | ||
try { | ||
await util.promisify(fs.access)(filepath) | ||
return true | ||
} catch (err) { | ||
if (err.code === 'ENOENT') return false | ||
throw err | ||
} | ||
} |
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 @@ | ||
export { createFsReadStream, createFsWriteStream, fileExists } from './fsUtils' |
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,16 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"resolveJsonModule": true, | ||
"esModuleInterop": true, | ||
"target": "es6", | ||
"noImplicitAny": true, | ||
"moduleResolution": "node", | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"outDir": "build", | ||
"declaration": true | ||
}, | ||
"include": ["./src/**/*"], | ||
"exclude": ["node_modules", "./src/__tests__"] | ||
} |
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,10 @@ | ||
{ | ||
"extends": ["tslint-config-airbnb", "tslint-config-prettier"], | ||
"rules": { | ||
"align": false, | ||
"import-name": false, | ||
"prefer-template": false, | ||
"function-name": false, | ||
"variable-name": false | ||
} | ||
} |
Oops, something went wrong.