Skip to content

Commit

Permalink
Merge pull request #1 from Lalaluka/0.0.3
Browse files Browse the repository at this point in the history
chore: 0.0.3
feat: New current command
fix: typos and linting
  • Loading branch information
Lalaluka committed Oct 12, 2021
2 parents 79af695 + df634af commit f947657
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ After installation these possibilities
rc-manager load <Profilename> to load a Profile to active Configs
rc-manager delete <Profilename> to delete a Profile
rc-manager list to list available Profiles
rc-manager current show current active Profile
````

## Manual Changes
Expand Down
86 changes: 61 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const fserror = (err) => {
}

//check if npmrc exists
if(!fs.existsSync(npmrc)){
if (!fs.existsSync(npmrc)) {
console.error("No npmrc found!")
process.exit(1)
};
//Check if yarnrc exists
const yarnmode = (() => {
if(!fs.existsSync(yarnrc)){
if (!fs.existsSync(yarnrc)) {
console.info("No yarnrc found. Using npm only mode.")
return false
} else {
Expand All @@ -30,12 +30,11 @@ const yarnmode = (() => {
})();

//If Store does not exist create one with a default Profile storing current config
(function checkStoreExitence(){
if(!fs.existsSync(profile_store)){
//read current npmrc
(function checkStoreExitence() {
if (!fs.existsSync(profile_store)) {
const npmrcFile = fs.readFileSync(npmrc).toString();
const yarnrcFile = (()=> {
if(yarnmode){
const yarnrcFile = (() => {
if (yarnmode) {
return fs.readFileSync(yarnrc).toString();
} else {
return ""
Expand All @@ -56,17 +55,17 @@ const yarnmode = (() => {
}());

// Save current config as a new Profile
function saveCurrentConfig(profileName){
function saveCurrentConfig(profileName) {
const npmrcFile = fs.readFileSync(npmrc).toString();
const yarnrcFile = (()=> {
if(yarnmode){
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)){
if (!store.profiles.map(x => x.name).includes(profileName)) {
store.last_modify = Date.now();
store.profiles.push({
name: profileName,
Expand All @@ -75,29 +74,29 @@ function saveCurrentConfig(profileName){
})
fs.writeFileSync(profile_store, JSON.stringify(store, null, 2), fserror)
} else {
console.error("Profile Key allready exists!");
console.error("Profile Key already exists!");
process.exit(1)
}
}

// Load Config by Profilename
function loadConfigByName(profileName){
function loadConfigByName(profileName) {
const selectedProfile = JSON.parse(fs.readFileSync(profile_store).toString()).profiles.find(x => x.name === profileName);
if(selectedProfile){
if (selectedProfile) {
fs.writeFileSync(npmrc, selectedProfile.npmrc, fserror)
if(yarnmode){
if (yarnmode) {
fs.writeFileSync(yarnrc, selectedProfile.yarnrc)
}
} else {
console.error("No Profile with the Name "+profileName+" does not exist.", fserror);
console.error("No Profile with the Name " + profileName + " does not exist.", fserror);
process.exit(1)
}
}

//Delete Profile from Store
function deleteProfileByName(profileName){
function deleteProfileByName(profileName) {
let store = JSON.parse(fs.readFileSync(profile_store).toString())
if(store.profiles.map(x => x.name).includes(profileName)){
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)
Expand All @@ -108,16 +107,49 @@ function deleteProfileByName(profileName){
}

//List Profiles in Store
function listProfiles(){
function listProfiles() {
console.log("Available Profiles:")
JSON.parse(fs.readFileSync(profile_store).toString()).profiles.map(function(profile) {console.log(" " + profile.name)});
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]) {
switch (processArgs[0]) {
case "save":
if(processArgs[1]!==undefined){
if (processArgs[1] !== undefined) {
saveCurrentConfig(processArgs[1])
} else {
process.stdout.write(
Expand All @@ -129,7 +161,7 @@ switch(processArgs[0]) {
}
break;
case "load":
if(processArgs[1]!==undefined){
if (processArgs[1] !== undefined) {
loadConfigByName(processArgs[1])
} else {
process.stdout.write(
Expand All @@ -141,7 +173,7 @@ switch(processArgs[0]) {
}
break;
case "delete":
if(processArgs[1]!==undefined){
if (processArgs[1] !== undefined) {
deleteProfileByName(processArgs[1])
} else {
process.stdout.write(
Expand All @@ -155,14 +187,18 @@ switch(processArgs[0]) {
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'
+ ' rc-manager list to list available Profiles\n'
+ ' rc-manager current show current active Profile'
)
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rc-manager",
"version": "0.0.2",
"version": "0.0.3",
"description": "NPM and Yarn config Manager",
"bin": {
"rc-manager": "./index.js"
Expand Down

0 comments on commit f947657

Please sign in to comment.