1 | 'use strict'
|
2 |
|
3 | const os = require('os')
|
4 | const path = require('path')
|
5 | const fs = require('fs-extra')
|
6 | const debug = require('debug')
|
7 | const nanoid = require('nanoid')
|
8 |
|
9 | const log = debug('ipfsd-ctl:utils')
|
10 |
|
11 | const removeRepo = async (repoPath) => {
|
12 | try {
|
13 | await fs.remove(repoPath)
|
14 | } catch (err) {
|
15 |
|
16 | }
|
17 | }
|
18 |
|
19 | const repoExists = async (repoPath) => {
|
20 | const exists = await fs.pathExists(path.join(repoPath, 'config'))
|
21 | return exists
|
22 | }
|
23 |
|
24 | const defaultRepo = (type) => {
|
25 | return path.join(
|
26 | os.homedir(),
|
27 | type === 'js' || type === 'proc' ? '.jsipfs' : '.ipfs'
|
28 | )
|
29 | }
|
30 |
|
31 | const 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 |
|
42 | const tmpDir = (type = '') => {
|
43 | return path.join(os.tmpdir(), `${type}_ipfs_${nanoid()}`)
|
44 | }
|
45 |
|
46 | module.exports = {
|
47 | removeRepo,
|
48 | repoExists,
|
49 | defaultRepo,
|
50 | checkForRunningApi,
|
51 | tmpDir
|
52 | }
|