Browse Source

Add fs helper

gre-patch-1
meriadec 6 years ago
parent
commit
d202ecbb74
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 5
      src/helpers/db/db.spec.js
  2. 4
      src/helpers/db/index.js
  3. 10
      src/helpers/fs.js
  4. 10
      src/migrations/migrations.js
  5. 4
      src/migrations/migrations.spec.js

5
src/helpers/db/db.spec.js

@ -1,15 +1,12 @@
import os from 'os' import os from 'os'
import path from 'path' import path from 'path'
import fs from 'fs'
import rimrafModule from 'rimraf' import rimrafModule from 'rimraf'
import db from 'helpers/db' import db from 'helpers/db'
import { promisify } from 'helpers/promise' import { promisify } from 'helpers/promise'
import { fsReadFile, fsWriteFile, fsMkdir } from 'helpers/fs'
const rimraf = promisify(rimrafModule) const rimraf = promisify(rimrafModule)
const fsReadFile = promisify(fs.readFile)
const fsWriteFile = promisify(fs.writeFile)
const fsMkdir = promisify(fs.mkdir)
const accountsTransform = { const accountsTransform = {
get: accounts => accounts.map(account => ({ ...account, balance: Number(account.balance) })), get: accounts => accounts.map(account => ({ ...account, balance: Number(account.balance) })),

4
src/helpers/db/index.js

@ -1,6 +1,5 @@
// @flow // @flow
import fs from 'fs'
import path from 'path' import path from 'path'
import crypto from 'crypto' import crypto from 'crypto'
import cloneDeep from 'lodash/cloneDeep' import cloneDeep from 'lodash/cloneDeep'
@ -9,6 +8,7 @@ import get from 'lodash/get'
import set from 'lodash/set' import set from 'lodash/set'
import logger from 'logger' import logger from 'logger'
import { fsReadFile, fsUnlink } from 'helpers/fs'
import { promisify, debounce } from 'helpers/promise' import { promisify, debounce } from 'helpers/promise'
import { NoDBPathGiven, DBWrongPassword } from 'config/errors' import { NoDBPathGiven, DBWrongPassword } from 'config/errors'
@ -18,8 +18,6 @@ type Transform = {
set: any => any, set: any => any,
} }
const fsReadFile = promisify(fs.readFile)
const fsUnlink = promisify(fs.unlink)
const writeFileAtomic = promisify(writeFileAtomicModule) const writeFileAtomic = promisify(writeFileAtomicModule)
const ALGORITHM = 'aes-256-cbc' const ALGORITHM = 'aes-256-cbc'

10
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)

10
src/migrations/migrations.js

@ -1,16 +1,12 @@
// @flow // @flow
import fs from 'fs'
import path from 'path' import path from 'path'
import { promisify } from 'helpers/promise' import { fsReadFile, fsUnlink } from 'helpers/fs'
import db from 'helpers/db' import db from 'helpers/db'
import type { Migration } from './types' import type { Migration } from './types'
const fsReadfile = promisify(fs.readFile)
const fsUnlink = promisify(fs.unlink)
const migrations: Migration[] = [ const migrations: Migration[] = [
{ {
doc: 'merging multiple db files into one app file', 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`) const filePath = path.join(dbPath, `${fileName}.json`)
let finalData let finalData
try { try {
const fileContent = await fsReadfile(filePath, 'utf-8') const fileContent = await fsReadFile(filePath, 'utf-8')
const { data } = JSON.parse(fileContent) const { data } = JSON.parse(fileContent)
finalData = data finalData = data
} catch (err) { } catch (err) {
// we assume we are in that case because file is encrypted // we assume we are in that case because file is encrypted
if (err instanceof SyntaxError) { if (err instanceof SyntaxError) {
const buf = await fsReadfile(filePath) const buf = await fsReadFile(filePath)
return buf.toString('base64') return buf.toString('base64')
} }
// will be stripped down by JSON.stringify // will be stripped down by JSON.stringify

4
src/migrations/migrations.spec.js

@ -1,4 +1,3 @@
import fs from 'fs'
import os from 'os' import os from 'os'
import path from 'path' import path from 'path'
import { spawn } from 'child_process' import { spawn } from 'child_process'
@ -6,13 +5,12 @@ import rimrafModule from 'rimraf'
import { BigNumber } from 'bignumber.js' import { BigNumber } from 'bignumber.js'
import { promisify } from 'helpers/promise' import { promisify } from 'helpers/promise'
import { fsReadFile, fsReaddir } from 'helpers/fs'
import { runMigrations } from 'migrations' import { runMigrations } from 'migrations'
import { decodeAccountsModel, encodeAccountsModel } from 'reducers/accounts' import { decodeAccountsModel, encodeAccountsModel } from 'reducers/accounts'
import db from 'helpers/db' import db from 'helpers/db'
const rimraf = promisify(rimrafModule) const rimraf = promisify(rimrafModule)
const fsReaddir = promisify(fs.readdir)
const fsReadFile = promisify(fs.readFile)
const tmpDir = os.tmpdir() const tmpDir = os.tmpdir()

Loading…
Cancel
Save