Skip to content

Commit

Permalink
add eslint to cli-repl (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
lrlna committed May 12, 2020
1 parent acc6bf1 commit 674d246
Show file tree
Hide file tree
Showing 24 changed files with 335 additions and 313 deletions.
3 changes: 3 additions & 0 deletions config/eslintrc.base.js
Expand Up @@ -13,8 +13,11 @@ module.exports = {
],
rules: {
'object-curly-spacing': [2, 'always'],
'no-empty-function': 0,
'valid-jsdoc': 0,
'react/sort-comp': 0, // does not seem work as expected with typescript
'@typescript-eslint/no-empty-function': 0,
'@typescript-eslint/no-use-before-define': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/no-var-requires': 0, // seems necessary to import less files
'@typescript-eslint/no-unused-vars': 2,
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-repl/.eslintignore
@@ -0,0 +1,2 @@
node_modules
lib
1 change: 1 addition & 0 deletions packages/cli-repl/.eslintrc.js
@@ -0,0 +1 @@
module.exports = require('../../config/eslintrc.base');
6 changes: 3 additions & 3 deletions packages/cli-repl/bin/mongosh.js
@@ -1,6 +1,6 @@
#!/bin/sh
':' //; node --experimental-repl-await "$0" "$@"
#!/usr/bin/env node

/* eslint no-console: 0, no-new: 0*/
const { CliRepl, parseCliArgs, mapCliToDriver, generateUri, USAGE } = require('../lib');

process.title = 'mongosh';
Expand All @@ -18,7 +18,7 @@ try {
const driverUri = generateUri(options);
const appname = `${process.title} ${version}`;

new CliRepl(driverUri, {appname, ...driverOptions}, options);
new CliRepl(driverUri, { appname, ...driverOptions }, options);
}
} catch (e) {
console.log(e.message);
Expand Down
2 changes: 2 additions & 0 deletions packages/cli-repl/package.json
Expand Up @@ -20,6 +20,8 @@
"start-async": "node --experimental-repl-await bin/mongosh.js start --async",
"test": "mocha --timeout 15000 --colors -r ts-node/register \"./{src,test}/**/*.spec.ts\"",
"test-ci": "mocha --timeout 15000 -r ts-node/register \"./{src,test}/**/*.spec.ts\"",
"lint": "eslint \"**/*.{js,ts,tsx}\"",
"check": "npm run lint",
"prepublish": "npm run compile-ts"
},
"license": "Apache-2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-repl/src/arg-mapper.ts
Expand Up @@ -49,6 +49,6 @@ function mapCliToDriver(options: CliOptions): NodeOptions {
}
});
return nodeOptions;
};
}

export default mapCliToDriver;
3 changes: 1 addition & 2 deletions packages/cli-repl/src/arg-parser.ts
Expand Up @@ -3,7 +3,6 @@ import { USAGE } from './constants';
import i18n from '@mongosh/i18n';
import minimist from 'minimist';
import clr from './clr';
import os from 'os';

/**
* Unknown translation key.
Expand Down Expand Up @@ -66,7 +65,7 @@ const OPTIONS = {
p: 'password',
u: 'username'
},
unknown: (parameter) => {
unknown: (parameter: string): boolean => {
if (parameter === START) {
return false;
}
Expand Down
4 changes: 1 addition & 3 deletions packages/cli-repl/src/cli-options.ts
@@ -1,7 +1,7 @@
/**
* Valid options that can be parsed from the command line.
*/
interface CliOptions {
export default interface CliOptions {
_?: string[];
async?: boolean;
authenticationDatabase?: string;
Expand Down Expand Up @@ -44,5 +44,3 @@ interface CliOptions {
verbose?: boolean;
version?: boolean;
}

export default CliOptions;
4 changes: 3 additions & 1 deletion packages/cli-repl/src/cli-repl.spec.ts
Expand Up @@ -2,6 +2,8 @@ import { expect } from 'chai';

describe('cli-repl', () => {
context('placeholder', () => {
it('passes', () => {});
it('passes', () => {
expect(true).to.be.true;
});
});
});
59 changes: 30 additions & 29 deletions packages/cli-repl/src/cli-repl.ts
@@ -1,3 +1,5 @@
/* eslint no-console: 0, no-sync: 0*/

import { CliServiceProvider, NodeOptions } from '@mongosh/service-provider-server';
import formatOutput, { formatError } from './format-output';
import ShellEvaluator from '@mongosh/shell-evaluator';
Expand Down Expand Up @@ -103,46 +105,44 @@ class CliRepl {
const version = this.buildInfo.version;

this.repl = repl.start({
prompt: `> `,
prompt: '> ',
writer: this.writer,
completer: completer.bind(null, version),
});

const originalEval = util.promisify(this.repl.eval);

const customEval = async(input, context, filename, callback) => {
const customEval = async(input, context, filename, callback): Promise<any> => {
let result;
let err = null;

try {
result = await this.ShellEvaluator.customEval(originalEval, input, context, filename);
} catch (err) {
if (isRecoverableError(input)) {
return callback(new Recoverable(err));
} else {
result = err;
}
result = err;
}
callback (null, result)
callback(null, result);
};

// @ts-ignore
this.repl.eval = customEval;
(this.repl as any).eval = customEval;

const historyFile = path.join(this.mongoshDir, '.mongosh_repl_history');
const historyFile = path.join(this.mongoshDir, '.mongosh_repl_history');
const redactInfo = this.options.redactInfo;
// eslint thinks we are redefining this.repl here, we are not.
// eslint-disable-next-line no-shadow
this.repl.setupHistory(historyFile, function(err, repl) {
const warn = new MongoshWarning('Unable to set up history file. History will not be persisting in this session')
const warn = new MongoshWarning('Unable to set up history file. History will not be persisting in this session');
if (err) this.writer(warn);

// repl.history is an array of previous commands. We need to hijack the
// value we just typed, and shift it off the history array if the info is
// sensitive.
repl.on('flushHistory', function() {
// @ts-ignore
changeHistory(repl.history, redactInfo);
})
})
changeHistory((repl as any).history, redactInfo);
});
});

this.repl.on('exit', () => {
this.serviceProvider.close(true);
Expand Down Expand Up @@ -183,7 +183,7 @@ class CliRepl {
// error is thrown here for atlas and DataLake connections.
// don't actually throw, as this is only used to log out non-genuine
// mongodb connections
this.bus.emit('mongosh:error', e)
this.bus.emit('mongosh:error', e);
return null;
}
}
Expand All @@ -194,7 +194,7 @@ class CliRepl {
createMongoshDir(): void {
try {
mkdirp.sync(this.mongoshDir);
} catch(e) {
} catch (e) {
this.bus.emit('mongosh:error', e);
throw e;
}
Expand All @@ -215,7 +215,7 @@ class CliRepl {
try {
fd = fs.openSync(configPath, 'wx');
this.userId = new ObjectId(Date.now());
this.enableTelemetry = true ;
this.enableTelemetry = true;
this.disableGreetingMessage = false;
this.bus.emit('mongosh:new-user', this.userId, this.enableTelemetry);
this.writeConfigFileSync(configPath);
Expand All @@ -228,9 +228,8 @@ class CliRepl {
this.bus.emit('mongosh:update-user', this.userId, this.enableTelemetry);
return;
}
this.bus.emit('mongosh:error', err)
this.bus.emit('mongosh:error', err);
throw err;

} finally {
if (fd !== undefined) fs.closeSync(fd);
}
Expand All @@ -254,26 +253,26 @@ class CliRepl {

if (enabled) {
return i18n.__('cli-repl.cli-repl.enabledTelemetry');
} else {
return i18n.__('cli-repl.cli-repl.disabledTelemetry');
}

return i18n.__('cli-repl.cli-repl.disabledTelemetry');
}

/** write file sync given path and contents
*
* @param {string} path - path to file
* @param {string} filePath - path to file
*/
writeConfigFileSync(path: string): void {
writeConfigFileSync(filePath: string): void {
const config = {
userId: this.userId,
enableTelemetry: this.enableTelemetry,
disableGreetingMessage: this.disableGreetingMessage
};

try {
fs.writeFileSync(path, JSON.stringify(config));
} catch(err) {
this.bus.emit('mongosh:error', err)
fs.writeFileSync(filePath, JSON.stringify(config));
} catch (err) {
this.bus.emit('mongosh:error', err);
throw err;
}
}
Expand All @@ -289,11 +288,11 @@ class CliRepl {
this.bus.emit('mongosh:error', result);
this.ShellEvaluator.revertState();

return formatOutput({type: 'Error', value: result});
return formatOutput({ type: 'Error', value: result });
}

return formatOutput(result);
}
};

/**
* The greeting for the shell.
Expand All @@ -314,7 +313,7 @@ class CliRepl {
isPasswordMissing(driverOptions: NodeOptions): boolean {
return driverOptions.auth &&
driverOptions.auth.user &&
!driverOptions.auth.password
!driverOptions.auth.password;
}

/**
Expand All @@ -330,6 +329,8 @@ class CliRepl {
replace: '*'
};
read(readOptions, (error, password) => {
if (error) return console.log(formatError(error));

driverOptions.auth.password = password;
this.connect(driverUri, driverOptions);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli-repl/src/clr.ts
@@ -1,5 +1,5 @@
import ansi from 'ansi-escape-sequences';

export default function clr(text, style) {
export default function clr(text: string, style: any): string {
return process.stdout.isTTY ? ansi.format(text, style) : text;
}

0 comments on commit 674d246

Please sign in to comment.