UNPKG

987 BJavaScriptView Raw
1'use strict'
2
3const os = require('os')
4const path = require('path')
5const fs = require('fs-extra')
6const debug = require('debug')
7const nanoid = require('nanoid')
8
9const log = debug('ipfsd-ctl:utils')
10
11const removeRepo = async (repoPath) => {
12 try {
13 await fs.remove(repoPath)
14 } catch (err) {
15 // ignore
16 }
17}
18
19const repoExists = async (repoPath) => {
20 const exists = await fs.pathExists(path.join(repoPath, 'config'))
21 return exists
22}
23
24const defaultRepo = (type) => {
25 return path.join(
26 os.homedir(),
27 type === 'js' || type === 'proc' ? '.jsipfs' : '.ipfs'
28 )
29}
30
31const checkForRunningApi = (repoPath) => {
32 let api
33 try {
34 api = fs.readFileSync(path.join(repoPath, 'api'))
35 } catch (err) {
36 log('Unable to open api file')
37 }
38
39 return api ? api.toString() : null
40}
41
42const tmpDir = (type = '') => {
43 return path.join(os.tmpdir(), `${type}_ipfs_${nanoid()}`)
44}
45
46module.exports = {
47 removeRepo,
48 repoExists,
49 defaultRepo,
50 checkForRunningApi,
51 tmpDir
52}