-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathextension.js
168 lines (139 loc) · 6.69 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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');
const { sortFiles } = require('./sort-files');
var lastFolder = '';
var lastWorkspaceName = '';
var lastWorkspaceRoot = '';
const nodeModules = 'node_modules';
exports.activate = context => {
const searchNodeModules = vscode.commands.registerCommand('extension.search', () => {
const preferences = vscode.workspace.getConfiguration('search-node-modules');
const useLastFolder = preferences.get('useLastFolder', false);
const nodeModulesPath = preferences.get('path', nodeModules);
const searchParentModules = preferences.get('searchParentModules', true);
const orderPriority = preferences.get('orderPriority', []);
const searchPath = (workspaceName, workspaceRoot, folderPath) => {
// Path to node_modules in this workspace folder
const workspaceNodeModules = path.join(workspaceName, nodeModulesPath);
// Reset last folder
lastFolder = '';
lastWorkspaceName = '';
lastWorkspaceRoot = '';
// Path to current folder
const folderFullPath = path.join(workspaceRoot, folderPath);
// Read folder, built quick pick with files/folder (and shortcuts)
fs.readdir(folderFullPath, async (readErr, files) => {
if (readErr) {
if (folderPath === nodeModulesPath) {
return showError('No node_modules folder in this workspace.');
}
return showError(`Unable to open folder ${folderPath}`);
}
const isParentFolder = folderPath.includes('..');
const options = sortFiles(files, orderPriority);
// 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(options, {
placeHolder: path.format({ dir: workspaceName, base: folderPath})
})
.then(selected => {
// node_modules shortcut selected
if (selected === workspaceNodeModules) {
searchPath(workspaceName, workspaceRoot, nodeModulesPath);
} else {
const selectedPath = path.join(folderPath, selected);
const selectedFullPath = path.join(workspaceRoot, selectedPath);
// If selected is a folder, traverse it,
// otherwise open file.
fs.stat(selectedFullPath, (statErr, stats) => {
if (stats.isDirectory()) {
searchPath(workspaceName, workspaceRoot, selectedPath);
} else {
lastWorkspaceName = workspaceName;
lastWorkspaceRoot = workspaceRoot;
lastFolder = folderPath;
vscode.workspace.openTextDocument(selectedFullPath, selectedPath)
.then(vscode.window.showTextDocument);
}
});
}
});
});
};
const getProjectFolder = async (workspaceFolder) => {
const packages = await findChildPackages(workspaceFolder.uri.fsPath);
// If in a lerna/yarn monorepo, prompt user to select which project to traverse
if (packages.length > 0) {
const selected = await vscode.window.showQuickPick(
[
{ label: workspaceFolder.name, packageDir: '' }, // First option is the root dir
...packages.map(packageDir => ({ label: path.join(workspaceFolder.name, packageDir), packageDir }))
]
, { placeHolder: 'Select Project' }
);
if (!selected) {
return;
}
return {
name: selected.label,
path: path.join(workspaceFolder.uri.fsPath, selected.packageDir)
};
}
// Otherwise, use the root folder
return {
name: workspaceFolder.name,
path: workspaceFolder.uri.fsPath
};
};
const getWorkspaceFolder = async () => {
// If in a multifolder workspace, prompt user to select which one to traverse.
if (vscode.workspace.workspaceFolders.length > 1) {
const selected = await vscode.window.showQuickPick(vscode.workspace.workspaceFolders.map(folder => ({
label: folder.name,
folder
})), {
placeHolder: 'Select workspace folder'
});
if (!selected) {
return;
}
return selected.folder;
}
// Otherwise, use the first one
const folder = vscode.workspace.workspaceFolders[0];
return folder;
};
// Open last folder if there is one
if (useLastFolder && lastFolder) {
return searchPath(lastWorkspaceName, lastWorkspaceRoot, lastFolder);
}
// Must have at least one workspace folder
if (!vscode.workspace.workspaceFolders.length) {
return showError('You must have a workspace opened.');
}
getWorkspaceFolder().then(folder => folder && getProjectFolder(folder)).then(folder => {
if (folder) {
searchPath(folder.name, folder.path, nodeModulesPath);
}
});
});
context.subscriptions.push(searchNodeModules);
};
exports.deactivate = () => {};