Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
If some of the tools are not installed on the current development environment, the CLI can handle it.
  • Loading branch information
PKief committed May 26, 2018
1 parent 5c1a3f5 commit 991f3d7
Show file tree
Hide file tree
Showing 9 changed files with 317 additions and 280 deletions.
454 changes: 227 additions & 227 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "proxy-setup-cli",
"version": "1.0.1",
"version": "1.0.2",
"description": "A command line interface to control the proxy settings of various development tools.",
"main": "index.js",
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion src/tools/bower.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export const configureBowerProxy = (enableProxy: boolean, u: UserInformation) =>
resolve();
} else {
if (enableProxy) {
fs.writeFileSync(filePath, JSON.stringify(proxySettings, undefined, 2));
try {
fs.writeFileSync(filePath, JSON.stringify(proxySettings, undefined, 2));
} catch {
console.error('Error: Something went wrong while configuring the proxy for Bower! \nPlease check if the \'.bowerrc\'-config file exists in your home-directory!');
}
}
resolve();
}
Expand Down
19 changes: 13 additions & 6 deletions src/tools/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ export const configureGitProxy = (enableProxy: boolean, u: UserInformation) => {
proxyURL = `http://${u.host}:${u.port}`;
}

execSync(`git config --global http.proxy ${proxyURL}`);
execSync(`git config --global https.proxy ${proxyURL}`);
resolve();
try {
execSync(`git config --global http.proxy ${proxyURL}`);
execSync(`git config --global https.proxy ${proxyURL}`);
} catch {
console.error('Error: Proxy for Git could not be set properly!');
}
} else {
const options = {
path: true,
Expand All @@ -27,11 +30,15 @@ export const configureGitProxy = (enableProxy: boolean, u: UserInformation) => {
const configfileExists = await checkIfFileExists(filePath);
if (configfileExists === true) {
properties.parse(filePath, options, (error, obj) => {
if ('http' in obj) execSync(`git config --global --remove-section http`);
if ('https' in obj) execSync(`git config --global --remove-section https`);
try {
if ('http' in obj) execSync(`git config --global --remove-section http`);
if ('https' in obj) execSync(`git config --global --remove-section https`);
} catch {
console.error('Error: Proxy for Git could not be set properly!');
}
});
}
resolve();
}
resolve();
});
};
6 changes: 5 additions & 1 deletion src/tools/gradle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export const configureGradleProxy = (enableProxy: boolean, u: UserInformation) =
} else {
if (enableProxy) {
// create complete new config
fs.writeFileSync(filePath, properties.stringify(newSettings));
try {
fs.writeFileSync(filePath, properties.stringify(newSettings));
} catch {
console.error('Error: Something went wrong while configuring the proxy for Gradle! \nPlease check if the \'.gradle\'-folder exists in your home-directory!');
}
}
resolve();
}
Expand Down
66 changes: 35 additions & 31 deletions src/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,41 @@ export const supportedtools = ['NPM', 'Yarn', 'Bower', 'Git', 'Maven', 'Gradle']

/** Toggle the proxy settings for each tool on and off. */
export const toggletoolsProxy = async (tools: Tool[], userInformation: UserInformation) => {
for (let p of tools) {
switch (p.name) {
case 'NPM':
await configureNPMProxy(p.enableProxy, userInformation);
break;

case 'Yarn':
await configureYarnProxy(p.enableProxy, userInformation);
break;

case 'Git':
await configureGitProxy(p.enableProxy, userInformation);
break;

case 'Bower':
await configureBowerProxy(p.enableProxy, userInformation);
break;

case 'Maven':
await configureMavenProxy(p.enableProxy, userInformation);
break;

case 'Gradle':
await configureGradleProxy(p.enableProxy, userInformation);
break;

default:
console.warn(`Proxy settings for tool '${p.name}' not yet implemented!`);
break;
try {
for (let p of tools) {
switch (p.name) {
case 'NPM':
await configureNPMProxy(p.enableProxy, userInformation);
break;

case 'Yarn':
await configureYarnProxy(p.enableProxy, userInformation);
break;

case 'Git':
await configureGitProxy(p.enableProxy, userInformation);
break;

case 'Bower':
await configureBowerProxy(p.enableProxy, userInformation);
break;

case 'Maven':
await configureMavenProxy(p.enableProxy, userInformation);
break;

case 'Gradle':
await configureGradleProxy(p.enableProxy, userInformation);
break;

default:
console.warn(`Proxy settings for tool '${p.name}' not yet implemented!`);
break;
}

printInfo(p);
}

printInfo(p);
} catch {
console.error('Error: Something went wrong while configuring the proxy configurations!');
}
};
6 changes: 5 additions & 1 deletion src/tools/maven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ export const configureMavenProxy = (enableProxy: boolean, u: UserInformation) =>
// build new xml object
const builder = new xml2js.Builder();
const xml = builder.buildObject(newConfig);
fs.writeFileSync(filePath, xml);
try {
fs.writeFileSync(filePath, xml);
} catch {
console.error('Error: Something went wrong while configuring the proxy for Maven! \nPlease check if the \'.m2\'-folder exists in your home-directory!');
}
}
resolve();
}
Expand Down
19 changes: 13 additions & 6 deletions src/tools/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ export const configureNPMProxy = (enableProxy: boolean, u: UserInformation) => {
proxyURL = `http://${u.host}:${u.port}`;
}

execSync(`npm config set proxy ${proxyURL}`);
execSync(`npm config set https-proxy ${proxyURL}`);
resolve();
try {
execSync(`npm config set proxy ${proxyURL}`);
execSync(`npm config set https-proxy ${proxyURL}`);
} catch {
console.error('Error: Proxy for npm could not be set properly!');
}
} else {
execSync(`npm config rm proxy`);
execSync(`npm config rm https-proxy`);
resolve();
try {
execSync(`npm config rm proxy`);
execSync(`npm config rm https-proxy`);
} catch {
console.error('Error: Proxy for npm could not be set properly!');
}
}
resolve();
});
};
19 changes: 13 additions & 6 deletions src/tools/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ export const configureYarnProxy = (enableProxy: boolean, u: UserInformation) =>
proxyURL = `http://${u.host}:${u.port}`;
}

execSync(`yarn config --offline set proxy ${proxyURL}`);
execSync(`yarn config --offline set https-proxy ${proxyURL}`);
resolve();
try {
execSync(`yarn config --offline set proxy ${proxyURL}`);
execSync(`yarn config --offline set https-proxy ${proxyURL}`);
} catch {
console.error('Error: Proxy for yarn could not be set properly!');
}
} else {
execSync(`yarn config --offline delete proxy`);
execSync(`yarn config --offline delete https-proxy`);
resolve();
try {
execSync(`yarn config --offline delete proxy`);
execSync(`yarn config --offline delete https-proxy`);
} catch {
console.error('Error: Proxy for yarn could not be set properly!');
}
}
resolve();
});
};

0 comments on commit 991f3d7

Please sign in to comment.