-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·210 lines (197 loc) · 6 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const profile_store = process.env.NPMRC_STORE || path.join(process.env.HOME || process.env.USERPROFILE, 'rc_switcher_store.json');
const npmrc = process.env.NPMRC || path.join(process.env.HOME || process.env.USERPROFILE, '.npmrc');
const yarnrc = process.env.YARNRC || path.join(process.env.HOME || process.env.USERPROFILE, '.yarnrc');
const fserror = (err) => {
if (err) {
console.error(err)
return
}
}
//check if npmrc exists
if (!fs.existsSync(npmrc)) {
console.error("No npmrc found!")
process.exit(1)
};
//Check if yarnrc exists
const yarnmode = (() => {
if (!fs.existsSync(yarnrc)) {
console.info("No yarnrc found. Using npm only mode.")
return false
} else {
return true
}
})();
//If Store does not exist create one with a default Profile storing current config
(function checkStoreExitence() {
if (!fs.existsSync(profile_store)) {
const npmrcFile = fs.readFileSync(npmrc).toString();
const yarnrcFile = (() => {
if (yarnmode) {
return fs.readFileSync(yarnrc).toString();
} else {
return ""
}
})()
const data = JSON.stringify({
"last_modify": Date.now(),
"profiles": [
{
"name": "default",
"npmrc": npmrcFile,
"yarnrc": yarnrcFile
}
]
}, null, 2);
fs.writeFileSync(profile_store, data, fserror)
}
}());
// Save current config as a new Profile
function saveCurrentConfig(profileName) {
const npmrcFile = fs.readFileSync(npmrc).toString();
const yarnrcFile = (() => {
if (yarnmode) {
return fs.readFileSync(yarnrc).toString();
} else {
return ""
}
})()
let store = JSON.parse(fs.readFileSync(profile_store).toString())
if (!store.profiles.map(x => x.name).includes(profileName)) {
store.last_modify = Date.now();
store.profiles.push({
name: profileName,
"npmrc": npmrcFile,
"yarnrc": yarnrcFile
})
fs.writeFileSync(profile_store, JSON.stringify(store, null, 2), fserror)
} else {
console.error("Profile Key already exists!");
process.exit(1)
}
}
// Load Config by Profilename
function loadConfigByName(profileName) {
const selectedProfile = JSON.parse(fs.readFileSync(profile_store).toString()).profiles.find(x => x.name === profileName);
if (selectedProfile) {
fs.writeFileSync(npmrc, selectedProfile.npmrc, fserror)
if (yarnmode) {
fs.writeFileSync(yarnrc, selectedProfile.yarnrc)
}
} else {
console.error("No Profile with the Name " + profileName + " does not exist.", fserror);
process.exit(1)
}
}
//Delete Profile from Store
function deleteProfileByName(profileName) {
let store = JSON.parse(fs.readFileSync(profile_store).toString())
if (store.profiles.map(x => x.name).includes(profileName)) {
store.last_modify = Date.now();
store.profiles = store.profiles.filter(prop => prop.name !== profileName)
fs.writeFileSync(profile_store, JSON.stringify(store, null, 2), fserror)
} else {
console.error("Profile does not exists!");
process.exit(1)
}
}
//List Profiles in Store
function listProfiles() {
console.log("Available Profiles:")
JSON.parse(fs.readFileSync(profile_store).toString()).profiles.map(function (profile) { console.log(" " + profile.name) });
}
//Show current active Profile
function showActive() {
const npmrcFile = fs.readFileSync(npmrc).toString();
const yarnrcFile = (() => {
if (yarnmode) {
return fs.readFileSync(yarnrc).toString();
} else {
return ""
}
})()
JSON.parse(fs.readFileSync(profile_store).toString()).profiles.map(function (profile) {
if (profile.npmrc == npmrcFile && compareYarnFiles(profile.yarnrc, yarnrcFile)) {
console.log("Active Profile: " + profile.name)
}
})
}
//Comparing Yarnfiles with out the "lastUpdateCheck" line if yarnmode return true
function compareYarnFiles(yarnrcoriginal, yarnrcFile) {
if (yarnmode) {
let splityarnprofile = yarnrcoriginal.split("\n");
let splityarnfile = yarnrcFile.split("\n");
for (i = 0; i < splityarnprofile.length; i++) {
if (!splityarnprofile[i].startsWith("lastUpdateCheck")) {
if (splityarnprofile[i] !== splityarnfile[i]) {
return false;
}
}
}
}
return true;
}
const processArgs = process.argv.slice(2)
switch (processArgs[0]) {
case "save":
if (processArgs[1] !== undefined) {
saveCurrentConfig(processArgs[1])
} else {
process.stdout.write(
'No Profilename provided.\n'
+ ' save command follows:\n'
+ ' rc-manager save <Profilename>'
)
process.exit(1)
}
break;
case "load":
if (processArgs[1] !== undefined) {
loadConfigByName(processArgs[1])
} else {
process.stdout.write(
'No Profilename provided.\n'
+ ' save command follows:\n'
+ ' rc-manager load <Profilename>'
)
process.exit(1)
}
break;
case "delete":
if (processArgs[1] !== undefined) {
deleteProfileByName(processArgs[1])
} else {
process.stdout.write(
'No Profilename provided.\n'
+ ' save command follows:\n'
+ ' rc-manager delete <Profilename>'
)
process.exit(1)
}
break;
case "list":
listProfiles()
break;
case "current":
showActive()
break;
case "--help":
process.stdout.write(
'rc-manager switch between npm and yarn configs\n'
+ ' rc-manager --help for help.\n'
+ ' rc-manager save <Profilename> to save the current Configs to a new Profile\n'
+ ' rc-manager load <Profilename> to load a Profile to active Configs\n'
+ ' rc-manager delete <Profilename> to delete a Profile\n'
+ ' rc-manager list to list available Profiles\n'
+ ' rc-manager current show current active Profile'
)
break;
default:
process.stdout.write(
'Command ' + processArgs[0] + ' is not supported.\n'
+ ' run --help for help.'
)
process.exit(1)
}