Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
feat(utils): saveFile
Browse files Browse the repository at this point in the history
  • Loading branch information
unicornware committed Oct 9, 2021
1 parent acee839 commit dca9764
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 0 deletions.
Empty file removed __mocks__/.gitkeep
Empty file.
16 changes: 16 additions & 0 deletions __mocks__/fs/promises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type fs from 'fs/promises'

/**
* @file Node Module Mock - fs/promises
* @module tests/mocks/fs/promises
* @see https://jestjs.io/docs/manual-mocks#mocking-node-modules
* @see https://jestjs.io/docs/manual-mocks#examples
* @see https://github.com/nodejs/node/blob/v16.0.0/doc/api/fs.md
*/

export default {
chmod: jest.fn(),
stat: jest.fn(),
unlink: jest.fn(),
writeFile: jest.fn()
} as jest.Mocked<Partial<typeof fs>>
8 changes: 8 additions & 0 deletions __mocks__/mkdirp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @file Node Module Mock - mkdirp
* @module tests/mocks/mkdirp
* @see https://jestjs.io/docs/manual-mocks#mocking-node-modules
* @see https://github.com/isaacs/node-mkdirp
*/

export default jest.fn()
33 changes: 33 additions & 0 deletions src/utils/__tests__/save-file.util.functional.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import fs from 'fs/promises'
import mkdirp from 'mkdirp'
import path from 'path'
import testSubject from '../save-file.util'

/**
* @file Functional Tests - saveFile
* @module trext/utils/tests/functional/saveFile
*/

jest.mock('fs/promises')

const mockFs = fs as jest.Mocked<typeof fs>
const mockMkdirp = mkdirp as jest.MockedFunction<typeof mkdirp>

describe('functional:utils/saveFile', () => {
const FILENAME: string = 'dist/hello-world.mjs'
const DATA: Buffer = Buffer.from("console.log('Hello, World!')")

beforeEach(async () => {
await testSubject(FILENAME, DATA)
})

it('should handle directory creation', () => {
expect(mockMkdirp).toBeCalledTimes(1)
expect(mockMkdirp).toBeCalledWith(path.dirname(FILENAME))
})

it('should create new file', () => {
expect(mockFs.writeFile).toBeCalledTimes(1)
expect(mockFs.writeFile).toBeCalledWith(FILENAME, DATA)
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*/

export { default as ignore404 } from './ignore-404.util'
export { default as saveFile } from './save-file.util'
26 changes: 26 additions & 0 deletions src/utils/save-file.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import fs from 'fs/promises'
import mkdirp from 'mkdirp'
import path from 'path'

/**
* @file Utilities - saveFile
* @module trext/utils/saveFile
*/

/**
* Saves a file. Creates a new directory if necessary.
*
* @async
* @param {string} filename - Name of file to save
* @param {string | Buffer} data - File content
* @return {Promise<void>} Empty promise when complete
*/
const saveFile = async (
filename: string,
data: string | Buffer
): Promise<void> => {
await mkdirp(path.dirname(filename))
return await fs.writeFile(filename, data)
}

export default saveFile

0 comments on commit dca9764

Please sign in to comment.