Skip to content

Commit

Permalink
feat(index.ts): Prompt install
Browse files Browse the repository at this point in the history
Installer asks to install stryker if not instaled. Added some nice colors for readability.
  • Loading branch information
Olaf Haalstra authored and Olaf Haalstra committed Jan 28, 2017
1 parent 2aebd00 commit a467a88
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@
"stryker-api": "^0.4.1"
},
"dependencies": {
"chalk": "^1.1.1"
"chalk": "^1.1.1",
"inquirer": "^3.0.1"
},
"devDependencies": {
"@types/chai-as-promised": "0.0.29",
"@types/chalk": "^0.4.31",
"@types/estree": "0.0.34",
"@types/inquirer": "0.0.32",
"@types/lodash": "^4.14.4",
"@types/mocha": "^2.2.32",
"@types/node": "^6.0.45",
"@types/sinon": "^1.16.31",
"@types/sinon-chai": "^2.7.27",
"@types/chalk": "^0.4.31",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"grunt": "^1.0.1",
Expand All @@ -56,7 +58,8 @@
"mocha": "^3.1.0",
"sinon": "^1.17.6",
"sinon-chai": "^2.8.0",
"stryker-api": "^0.4.1",
"stryker": "^0.5.7",
"stryker-api": "^0.4.2",
"tslint": "^4.4.2",
"typescript": "^2.1.4"
}
Expand Down
42 changes: 23 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,41 @@
import * as fs from 'fs';
import * as path from 'path';
import * as chalk from 'chalk';
import * as inquirer from 'inquirer';

const NODE_MODULES_DIRNAME = 'node_modules';
const STRYKER_DIRNAME = 'stryker';
const BIN_DIRNAME = 'bin';
const STRYKER_FILENAME = 'stryker';

function fileExists(filePath: string) {
try {
return fs.statSync(filePath).isFile();
} catch (error) {
return false;
}
}

function strykerNotFound() {
console.error(chalk.red.bold('Cannot find Stryker!\nPlease install Stryker using "npm install stryker".'));
process.exit(1);
try {
return fs.statSync(filePath).isFile();
} catch (error) {
return false;
}
}

const baseDir = process.cwd();

const strykerPathFromNode = path.resolve(baseDir, NODE_MODULES_DIRNAME, STRYKER_DIRNAME, BIN_DIRNAME, STRYKER_FILENAME);
const strykerPathFromBuild = path.resolve(baseDir, BIN_DIRNAME, STRYKER_FILENAME);

try {
if (fileExists(strykerPathFromNode)) {
require(strykerPathFromNode);
} else if (fileExists(strykerPathFromBuild)) {
require(strykerPathFromBuild);
if (fileExists(strykerPathFromNode)) {
require(strykerPathFromNode);
} else if (fileExists(strykerPathFromBuild)) {
require(strykerPathFromBuild);
} else {
console.log(chalk.yellow('Stryker is currently not installed.'));
inquirer.prompt([{ type: 'confirm', name: 'install', message: 'Do you want to automatically install Stryker?', default: 'true' }]).then((answers) => {
if (answers['install']) {
// Install stryker;
require('child_process').execSync('npm i --save-dev stryker stryker-api');
console.log(chalk.green('Stryker installation done.'));
console.log('Get started by using ' + chalk.blue('`stryker init`') + '.');
} else {
strykerNotFound();
console.log('I understand. You can install Stryker manually using `npm install stryker`.');
}
} catch (error) {
strykerNotFound();
}
})
}

0 comments on commit a467a88

Please sign in to comment.