Skip to content

Commit

Permalink
add prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
ranyitz committed Mar 10, 2018
1 parent 41798fa commit dd4781d
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 70 deletions.
6 changes: 5 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"extends": ["airbnb-base", "plugin:jest/recommended"],
"extends": ["airbnb-base", "plugin:jest/recommended", "plugin:prettier/recommended"],
"env": {
"jest": true,
"node": true
},
"rules": {
"prettier/prettier": ["error", {
"singleQuote": true,
"trailingComma": "all"
}],
"no-console": "warn",
"no-plusplus": "off",
"no-restricted-syntax": "off",
Expand Down
4 changes: 2 additions & 2 deletions __tests__/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const { execSync } = require('child_process');
const { resolveFixture } = require('./utils');

const qnmBin = require.resolve('../bin/qnm');
const runCommand = (command, { cwd }) => execSync(`${qnmBin} ${command}`, { cwd, encoding: 'utf-8' });
const runCommand = (command, { cwd }) =>
execSync(`${qnmBin} ${command}`, { cwd, encoding: 'utf-8' });

describe('CLI', () => {
describe('qnm with no arguments', () => {
Expand Down Expand Up @@ -49,4 +50,3 @@ test
});
});
});

6 changes: 4 additions & 2 deletions __tests__/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const path = require('path');
const Workespace = require('../src/workspace/workspace');

module.exports.resolveFixture = fixure => path.resolve(__dirname, './fixtures', fixure);
module.exports.resolveFixture = fixure =>
path.resolve(__dirname, './fixtures', fixure);

module.exports.resolveWorkspace = fixure => Workespace.loadSync(exports.resolveFixture(fixure));
module.exports.resolveWorkspace = fixure =>
Workespace.loadSync(exports.resolveFixture(fixure));
31 changes: 10 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@
"engines": {
"node": ">=8.7"
},
"keywords": [
"query",
"node_modules",
"version",
"cli"
],
"keywords": ["query", "node_modules", "version", "cli"],
"author": "Ran Yitzhaki",
"license": "MIT",
"scripts": {
Expand All @@ -23,19 +18,20 @@
"precommit": "lint-staged"
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
"*.js": ["eslint --fix", "git add"],
"*.{md,json}": ["prettier --write", "git add"]
},
"devDependencies": {
"eslint": "~4.18.2",
"eslint-config-airbnb-base": "~12.1.0",
"eslint-config-prettier": "~2.9.0",
"eslint-plugin-import": "~2.9.0",
"eslint-plugin-jest": "~21.13.0",
"eslint-plugin-prettier": "~2.6.0",
"husky": "~0.14.3",
"jest": "~22.4.2",
"lint-staged": "~7.0.0"
"lint-staged": "~7.0.0",
"prettier": "^1.11.1"
},
"dependencies": {
"archy": "~1.0.0",
Expand All @@ -48,15 +44,8 @@
},
"jest": {
"testEnvironment": "node",
"testMatch": [
"**/*.spec.js"
],
"testPathIgnorePatterns": [
"/node_modules/",
"/fixtures/"
],
"watchPathIgnorePatterns": [
"node_modules"
]
"testMatch": ["**/*.spec.js"],
"testPathIgnorePatterns": ["/node_modules/", "/fixtures/"],
"watchPathIgnorePatterns": ["node_modules"]
}
}
3 changes: 1 addition & 2 deletions src/actions/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ const isEmpty = require('lodash/isEmpty');
const { noModulesMessage } = require('../printer');
const renderModuleList = require('../render/render-module-list');

module.exports = (workspace) => {
module.exports = workspace => {
const moduleOccurrencesList = workspace.list();

if (isEmpty(moduleOccurrencesList)) {
return noModulesMessage();
}


return renderModuleList(moduleOccurrencesList);
};
5 changes: 4 additions & 1 deletion src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ try {

program
.arguments('[module]', 'prints module version from the node_modules')
.option('-m, --match', 'works like grep, and prints modules which the provided string matches')
.option(
'-m, --match',
'works like grep, and prints modules which the provided string matches',
)
.option('-d, --debug', 'see full error messages, mostly for debugging');

program.parse(process.argv);
Expand Down
7 changes: 4 additions & 3 deletions src/completions/setup-completions.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const Workspace = require('../workspace/workspace');
const tabtab = require('tabtab');

module.exports = (preDefinedCommands) => {
module.exports = preDefinedCommands => {
const tab = tabtab({ cache: false });

tab.on('qnm', (data, done) => {
if (data.words !== 1) {
return done();
}

const tabtabCommands = preDefinedCommands.map(command => `${command}:command`);
const tabtabCommands = preDefinedCommands.map(
command => `${command}:command`,
);

try {
const workspace = Workspace.loadSync();
Expand All @@ -23,4 +25,3 @@ module.exports = (preDefinedCommands) => {

tab.start();
};

34 changes: 23 additions & 11 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,45 @@ function printSingleVersion(moduleObject) {
}

function paintDiffInBold(from, to) {
return to.split('').map((char, index) => {
if (char !== from.charAt(index)) {
return chalk.bold(char);
}
return to
.split('')
.map((char, index) => {
if (char !== from.charAt(index)) {
return chalk.bold(char);
}

return char;
}).join('');
return char;
})
.join('');
}

module.exports.printVersions = (modules) => {
module.exports.printVersions = modules => {
return modules.map(m => printSingleVersion(m.toObject())).join('\n');
};

module.exports.printModulesList = (modulesList, { match } = {}) => {
return modulesList.map(([name, nodeModule]) => `${highlightMatch(name, match)} ${exports.printVersions(nodeModule)}`).join('\n');
return modulesList
.map(
([name, nodeModule]) =>
`${highlightMatch(name, match)} ${exports.printVersions(nodeModule)}`,
)
.join('\n');
};

module.exports.notFoundModuleMessage = (name, suggestions) => {
let message = chalk.red(`Could not find any module by the name "${name}".`);

if (!isEmpty(suggestions)) {
message += chalk.red(` Did you mean "${paintDiffInBold(name, suggestions[0])}?"`);
message += chalk.red(
` Did you mean "${paintDiffInBold(name, suggestions[0])}?"`,
);
}

return message;
};

module.exports.notMatchModuleMessage = str => chalk.red(`Could not find any module that matches "${str}"`);
module.exports.notMatchModuleMessage = str =>
chalk.red(`Could not find any module that matches "${str}"`);

module.exports.noModulesMessage = () => chalk.red('Could not find any module in the node_modules directory');
module.exports.noModulesMessage = () =>
chalk.red('Could not find any module in the node_modules directory');
5 changes: 3 additions & 2 deletions src/render/render-module-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const renderModuleOccurrences = require('./render-module-occurrences');

module.exports = (moduleOccurrencesList, options) => {
return moduleOccurrencesList
.map(([, moduleOccurrences]) => renderModuleOccurrences(moduleOccurrences, options))
.map(([, moduleOccurrences]) =>
renderModuleOccurrences(moduleOccurrences, options),
)
.join('\n');
};

6 changes: 3 additions & 3 deletions src/render/render-module-occurrences.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const archy = require('archy');
const chalk = require('chalk');

const highlightMatch = (str, match) => str.split(match).join(chalk.magenta(match));
const highlightMatch = (str, match) =>
str.split(match).join(chalk.magenta(match));

const buildWithAncestors = (m) => {
const buildWithAncestors = m => {
let hierarchy = [m.version];

if (m.parent) {
Expand All @@ -25,4 +26,3 @@ module.exports = (moduleOccurrences, { match } = {}) => {

return tree;
};

8 changes: 4 additions & 4 deletions src/suggest/get-suggestions.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const levenshtein = require('js-levenshtein');

module.exports = (input, options, maxDistance = 2) => {
return options.map((opt) => {
return [opt, levenshtein(input, opt)];
})
return options
.map(opt => {
return [opt, levenshtein(input, opt)];
})
.filter(([, distance]) => distance <= maxDistance)
.sort(([, distanceA], [, distanceB]) => distanceA - distanceB)
.map(([option]) => option);
};

50 changes: 34 additions & 16 deletions src/workspace/modules-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class ModulesMap extends Map {
const moduleOccurrences = this.get(name);

if (!moduleOccurrences) {
throw new Error(`The node module "${name}" does not exist in ${this.root}`);
throw new Error(
`The node module "${name}" does not exist in ${this.root}`,
);
}

return moduleOccurrences.map(m => m.load());
Expand All @@ -38,24 +40,40 @@ class ModulesMap extends Map {
const nodeModulesPath = path.resolve(root, 'node_modules');

if (fs.existsSync(nodeModulesPath)) {
const modulesNames = fs.readdirSync(nodeModulesPath).filter(isNotHiddenDirectory);
const modulesNames = fs
.readdirSync(nodeModulesPath)
.filter(isNotHiddenDirectory);

flattenDeep(modulesNames.map((name) => {
if (isScope(name)) {
const subScopeModules = fs.readdirSync(path.join(nodeModulesPath, name));
flattenDeep(
modulesNames.map(name => {
if (isScope(name)) {
const subScopeModules = fs.readdirSync(
path.join(nodeModulesPath, name),
);

return subScopeModules.map((subName) => {
const fullName = path.join(name, subName);
const nodeModule = new NodeModule({ nodeModulesPath, name: fullName, parent });
modulesMap.addModule(fullName, nodeModule);
return nodeModule;
});
}
return subScopeModules.map(subName => {
const fullName = path.join(name, subName);
const nodeModule = new NodeModule({
nodeModulesPath,
name: fullName,
parent,
});
modulesMap.addModule(fullName, nodeModule);
return nodeModule;
});
}

const nodeModule = new NodeModule({ nodeModulesPath, name, parent });
modulesMap.addModule(name, nodeModule);
return nodeModule;
})).forEach(nodeModule => traverseNodeModules(nodeModule.path, nodeModule));
const nodeModule = new NodeModule({
nodeModulesPath,
name,
parent,
});
modulesMap.addModule(name, nodeModule);
return nodeModule;
}),
).forEach(nodeModule =>
traverseNodeModules(nodeModule.path, nodeModule),
);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/workspace/node-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ module.exports = class NodeModule {
}

load() {
const packageJsonPath = path.resolve(this.nodeModulesPath, this.name, 'package.json');
const packageJsonPath = path.resolve(
this.nodeModulesPath,
this.name,
'package.json',
);

try {
this._packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
Expand All @@ -49,4 +53,3 @@ module.exports = class NodeModule {
}
}
};

0 comments on commit dd4781d

Please sign in to comment.