Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagonapoli committed Jul 15, 2019
0 parents commit 4f80ad6
Show file tree
Hide file tree
Showing 11 changed files with 4,038 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
build
*.tmp
9 changes: 9 additions & 0 deletions jest.config.js
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',
}
46 changes: 46 additions & 0 deletions package.json
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"
}
}
7 changes: 7 additions & 0 deletions prettier.config.js
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,
}
1 change: 1 addition & 0 deletions src/__tests__/fixtures/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
51 changes: 51 additions & 0 deletions src/__tests__/fsUtils.test.ts
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)
})
})
73 changes: 73 additions & 0 deletions src/fsUtils.ts
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
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createFsReadStream, createFsWriteStream, fileExists } from './fsUtils'
16 changes: 16 additions & 0 deletions tsconfig.json
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__"]
}
10 changes: 10 additions & 0 deletions tslint.json
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
}
}

0 comments on commit 4f80ad6

Please sign in to comment.