Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Add ability to search in parent folders #13

Merged
merged 2 commits into from
May 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const vscode = require('vscode');
const fs = require('fs');
const path = require('path');
const { findParentModules } = require('./find-parent-modules');
const { findChildPackages } = require('./find-child-packages');
const { showError } = require('./utils');

Expand All @@ -16,6 +17,7 @@ exports.activate = context => {

const useLastFolder = preferences.get('useLastFolder', false);
const nodeModulesPath = preferences.get('path', nodeModules);
const searchParentModules = preferences.get('searchParentModules', true);

const searchPath = (workspaceName, workspaceRoot, folderPath) => {
// Path to node_modules in this workspace folder
Expand All @@ -30,7 +32,7 @@ exports.activate = context => {
const folderFullPath = path.join(workspaceRoot, folderPath);

// Read folder, built quick pick with files/folder (and shortcuts)
fs.readdir(folderFullPath, (readErr, files) => {
fs.readdir(folderFullPath, async (readErr, files) => {
if (readErr) {
if (folderPath === nodeModulesPath) {
return showError('No node_modules folder in this workspace.');
Expand All @@ -39,14 +41,29 @@ exports.activate = context => {
return showError(`Unable to open folder ${folderPath}`);
}

if (folderPath !== nodeModulesPath) {
files.push('');
files.push(workspaceNodeModules);
files.push('..');
const isParentFolder = folderPath.includes('..');
const options = files;

// If searching in root node_modules, also include modules from parent folders, that are outside of the workspace
if (folderPath === nodeModulesPath) {
if (searchParentModules) {
const parentModules = await findParentModules(workspaceRoot, nodeModulesPath);
options.push(...parentModules);
}
} else {
// Otherwise, show option to move back to root
options.push('');
options.push(workspaceNodeModules);

// If current folder is not outside of the workspace, also add option to move a step back
if (!isParentFolder) {
options.push('..');
}
}

vscode.window.showQuickPick(files, {
placeHolder: path.join(workspaceName, folderPath)

vscode.window.showQuickPick(options, {
placeHolder: path.format({ dir: workspaceName, base: folderPath})
})
.then(selected => {
// node_modules shortcut selected
Expand Down
36 changes: 36 additions & 0 deletions find-parent-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require('fs');
const path = require('path').posix;
const util = require('util');

const fsExists = util.promisify(fs.exists);
const fsReaddir = util.promisify(fs.readdir);

// Looks for node_modules in parent folders of the workspace recursively.
// Returns a list of paths relative to workspaceRoot/nodeModulesPath
const findParentModules = async (workspaceRoot, nodeModulesPath) => {
const absoluteRootNodeModules = path.join('/', nodeModulesPath);

const find = async dir => {
const ret = [];
if (await fsExists(dir)) {
const getFilePath = file =>
path.relative(path.join(workspaceRoot, nodeModulesPath), path.join(dir, file));

const dirFiles = await fsReaddir(dir);
ret.push(...dirFiles.map(getFilePath));
}

if (dir !== absoluteRootNodeModules) {
const parent = path.join(dir, '..', '..', nodeModulesPath);
ret.push(...(await find(parent)));
}

return ret;
};

return find(path.join(workspaceRoot, '..', nodeModulesPath));
};

module.exports = {
findParentModules
};
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
"type": "string",
"default": "node_modules",
"description": "Relative path to node_modules folder."
},
"search-node-modules.searchParentModules": {
"type": "boolean",
"default": true,
"description": "Include modules from parent folders in search results."
}
}
}
Expand Down