forked from ipfs/ipfs-desktop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmove-repository-location.js
90 lines (74 loc) · 2.58 KB
/
move-repository-location.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const i18n = require('i18next')
const path = require('path')
const fs = require('fs-extra')
const store = require('./common/store')
const logger = require('./common/logger')
const { showDialog, recoverableErrorDialog, selectDirectory } = require('./dialogs')
const dock = require('./utils/dock')
module.exports = function ({ stopIpfs, startIpfs }) {
dock.run(async () => {
logger.info('[move repository] user prompted about effects')
const opt = showDialog({
title: i18n.t('moveRepositoryWarnDialog.title'),
message: i18n.t('moveRepositoryWarnDialog.message'),
type: 'warning',
buttons: [
i18n.t('moveRepositoryWarnDialog.action'),
i18n.t('cancel')
],
showDock: false
})
if (opt !== 0) {
logger.info('[move repository] user canceled')
return
}
logger.info('[move repository] user will pick directory')
const dir = await selectDirectory()
if (!dir) {
logger.info('[move repository] user canceled')
return
}
const config = store.get('ipfsConfig')
const currDir = config.path
const currName = path.basename(currDir)
const newDir = path.join(dir, currName)
if (currDir === newDir) {
logger.info('[move repository] new dir is the same as old dir')
return showDialog({
title: i18n.t('moveRepositorySameDirDialog.title'),
message: i18n.t('moveRepositorySameDirDialog.message', { location: newDir }),
type: 'warning',
showDock: false
})
}
if (fs.existsSync(newDir)) {
logger.info('[move repository] new dir already exists')
return showDialog({
title: i18n.t('moveRepositoryDirExists.title'),
message: i18n.t('moveRepositoryDirExists.message', { location: newDir }),
type: 'warning',
showDock: false
})
}
await stopIpfs()
try {
await fs.move(currDir, newDir)
logger.info(`[move repository] moved from ${currDir} to ${newDir}`)
} catch (err) {
logger.error(`[move repository] ${err.toString()}`)
return recoverableErrorDialog(err, {
title: i18n.t('moveRepositoryFailed.title'),
message: i18n.t('moveRepositoryFailed.message', { currDir, newDir })
})
}
config.path = newDir
store.set('ipfsConfig', config)
logger.info('[move repository] configuration updated', { withAnalytics: 'MOVE_REPOSITORY' })
showDialog({
title: i18n.t('moveRepositorySuccessDialog.title'),
message: i18n.t('moveRepositorySuccessDialog.message', { location: newDir }),
showDock: false
})
await startIpfs()
})
}