From d202ecbb7442c8705391103f43e790c414bac954 Mon Sep 17 00:00:00 2001 From: meriadec Date: Tue, 27 Nov 2018 00:42:44 +0100 Subject: [PATCH] Add fs helper --- src/helpers/db/db.spec.js | 5 +---- src/helpers/db/index.js | 4 +--- src/helpers/fs.js | 10 ++++++++++ src/migrations/migrations.js | 10 +++------- src/migrations/migrations.spec.js | 4 +--- 5 files changed, 16 insertions(+), 17 deletions(-) create mode 100644 src/helpers/fs.js diff --git a/src/helpers/db/db.spec.js b/src/helpers/db/db.spec.js index 79260117..22ec04a5 100644 --- a/src/helpers/db/db.spec.js +++ b/src/helpers/db/db.spec.js @@ -1,15 +1,12 @@ import os from 'os' import path from 'path' -import fs from 'fs' import rimrafModule from 'rimraf' import db from 'helpers/db' import { promisify } from 'helpers/promise' +import { fsReadFile, fsWriteFile, fsMkdir } from 'helpers/fs' const rimraf = promisify(rimrafModule) -const fsReadFile = promisify(fs.readFile) -const fsWriteFile = promisify(fs.writeFile) -const fsMkdir = promisify(fs.mkdir) const accountsTransform = { get: accounts => accounts.map(account => ({ ...account, balance: Number(account.balance) })), diff --git a/src/helpers/db/index.js b/src/helpers/db/index.js index 5285bf0c..6b072415 100644 --- a/src/helpers/db/index.js +++ b/src/helpers/db/index.js @@ -1,6 +1,5 @@ // @flow -import fs from 'fs' import path from 'path' import crypto from 'crypto' import cloneDeep from 'lodash/cloneDeep' @@ -9,6 +8,7 @@ import get from 'lodash/get' import set from 'lodash/set' import logger from 'logger' +import { fsReadFile, fsUnlink } from 'helpers/fs' import { promisify, debounce } from 'helpers/promise' import { NoDBPathGiven, DBWrongPassword } from 'config/errors' @@ -18,8 +18,6 @@ type Transform = { set: any => any, } -const fsReadFile = promisify(fs.readFile) -const fsUnlink = promisify(fs.unlink) const writeFileAtomic = promisify(writeFileAtomicModule) const ALGORITHM = 'aes-256-cbc' diff --git a/src/helpers/fs.js b/src/helpers/fs.js new file mode 100644 index 00000000..b16dbf28 --- /dev/null +++ b/src/helpers/fs.js @@ -0,0 +1,10 @@ +// @flow + +import { promisify } from 'helpers/promise' +import fs from 'fs' + +export const fsReadFile = promisify(fs.readFile) +export const fsReaddir = promisify(fs.readdir) +export const fsWriteFile = promisify(fs.writeFile) +export const fsMkdir = promisify(fs.mkdir) +export const fsUnlink = promisify(fs.unlink) diff --git a/src/migrations/migrations.js b/src/migrations/migrations.js index e67767d2..4bd9d82b 100644 --- a/src/migrations/migrations.js +++ b/src/migrations/migrations.js @@ -1,16 +1,12 @@ // @flow -import fs from 'fs' import path from 'path' -import { promisify } from 'helpers/promise' +import { fsReadFile, fsUnlink } from 'helpers/fs' import db from 'helpers/db' import type { Migration } from './types' -const fsReadfile = promisify(fs.readFile) -const fsUnlink = promisify(fs.unlink) - const migrations: Migration[] = [ { doc: 'merging multiple db files into one app file', @@ -54,13 +50,13 @@ async function getFileData(dbPath, fileName) { const filePath = path.join(dbPath, `${fileName}.json`) let finalData try { - const fileContent = await fsReadfile(filePath, 'utf-8') + const fileContent = await fsReadFile(filePath, 'utf-8') const { data } = JSON.parse(fileContent) finalData = data } catch (err) { // we assume we are in that case because file is encrypted if (err instanceof SyntaxError) { - const buf = await fsReadfile(filePath) + const buf = await fsReadFile(filePath) return buf.toString('base64') } // will be stripped down by JSON.stringify diff --git a/src/migrations/migrations.spec.js b/src/migrations/migrations.spec.js index 18af426e..5e80167b 100644 --- a/src/migrations/migrations.spec.js +++ b/src/migrations/migrations.spec.js @@ -1,4 +1,3 @@ -import fs from 'fs' import os from 'os' import path from 'path' import { spawn } from 'child_process' @@ -6,13 +5,12 @@ import rimrafModule from 'rimraf' import { BigNumber } from 'bignumber.js' import { promisify } from 'helpers/promise' +import { fsReadFile, fsReaddir } from 'helpers/fs' import { runMigrations } from 'migrations' import { decodeAccountsModel, encodeAccountsModel } from 'reducers/accounts' import db from 'helpers/db' const rimraf = promisify(rimrafModule) -const fsReaddir = promisify(fs.readdir) -const fsReadFile = promisify(fs.readFile) const tmpDir = os.tmpdir()