-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·145 lines (132 loc) · 3.9 KB
/
index.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env node
const fs = require('fs')
const { exec } = require('child_process')
const path = require('path')
const chalk = require('chalk')
const clear = require('clear')
const figlet = require('figlet')
const inquirer = require('inquirer')
const homedir = require('os').homedir()
const createNewProfile = () => {
inquirer
.prompt([
{
type: 'input',
name: 'newProfileName',
message: 'Enter a profile name'
}
])
.then(({ newProfileName }) => {
// make sure it is unique
const exists = fs.existsSync(path.join(qriswitchDir, `.${newProfileName}`))
if (exists) {
console.log('that profile name already exists')
return
} else {
// create a temporary directory with .qri and .ipfs directories in it
const profileDir = path.join(qriswitchDir, `.${newProfileName}`)
const qriDir = path.join(profileDir, '.qri')
const ipfsDir = path.join(profileDir, '.ipfs')
fs.mkdirSync(profileDir)
fs.mkdirSync(qriDir)
fs.mkdirSync(ipfsDir)
}
switchToProfile(newProfileName)
})
}
const useDefaultProfile = () => {
// clear .env, unset environment variables
exec(`
rm -rf ~/.qriswitch/.env &&
echo 'export QRISWITCH_PROFILE=default qri profile' >> ~/.qriswitch/.env &&
echo 'unset QRI_PATH' >> ~/.qriswitch/.env &&
launchctl unsetenv QRI_PATH &&
echo 'unset IPFS_PATH' >> ~/.qriswitch/.env &&
launchctl unsetenv IPFS_PATH
`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log('switched profile to default profile')
console.log('QRI_PATH is not set')
console.log('IPFS_PATH is not set')
console.log('To restart this terminal, run `source ~/.qriswitch/.env`')
})
}
const switchToProfile = (profileName) => {
const profileDir = path.join(qriswitchDir, `.${profileName}`)
const qriDir = path.join(profileDir, '.qri')
const ipfsDir = path.join(profileDir, '.ipfs')
// clear .env, set environment variables
exec(`
rm -rf ~/.qriswitch/.env &&
echo 'export QRISWITCH_PROFILE=${profileName}' >> ~/.qriswitch/.env &&
echo 'export QRI_PATH=${qriDir}' >> ~/.qriswitch/.env &&
launchctl setenv QRI_PATH ${qriDir} &&
echo 'export IPFS_PATH=${ipfsDir}' >> ~/.qriswitch/.env &&
launchctl setenv IPFS_PATH ${ipfsDir}
`, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`)
return
}
if (stderr) {
console.log(`stderr: ${stderr}`)
return
}
console.log(`switched profile to ${profileName}`)
console.log(`QRI_PATH is ${qriDir}`)
console.log(`IPFS_PATH is ${ipfsDir}`)
console.log('To restart this terminal, run `source ~/.qriswitch/.env`')
})
}
const prompt = () => {
const profiles = fs.readdirSync(path.join(homedir, '.qriswitch'))
.filter(d => d !== '.env')
.map(d => d.split('.')[1])
inquirer
.prompt([
{
type: 'list',
name: 'profile',
message: 'Choose a Profile',
choices: [
...profiles,
new inquirer.Separator(),
'use default qri profile',
'create a new profile'
]
}
])
.then(({ profile }) => {
if (profile === 'use default qri profile') {
useDefaultProfile()
return
}
if (profile === 'create a new profile') {
createNewProfile()
return
}
clear()
switchToProfile(profile)
})
}
// make sure $HOME/.qriswitch exists
const qriswitchDir = path.join(homedir, '.qriswitch')
if (!fs.existsSync(qriswitchDir)) {
fs.mkdirSync(qriswitchDir)
}
clear()
// make a neato ascii art logo
console.log(
chalk.yellow(
figlet.textSync('qriswitch', { horizontalLayout: 'full' })
)
)
console.log(`current profile: ${process.env.QRISWITCH_PROFILE}`)
prompt()