Skip to content

Commit 251296f

Browse files
committed
fix: bin completions & run bin command now scans all folders in fs up to the root
it was fixed to make it work in files outside of any workspace
1 parent 4cb7f48 commit 251296f

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,14 @@
228228
"vscode-uri": "^3.0.3",
229229
"which-pm": "^2.0.0",
230230
"yamljs": "^0.3.0"
231+
},
232+
"prettier": {
233+
"semi": false,
234+
"singleQuote": true,
235+
"proseWrap": "never",
236+
"tabWidth": 4,
237+
"trailingComma": "all",
238+
"arrowParens": "avoid",
239+
"printWidth": 160
231240
}
232241
}

src/commands/runBinCommand.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,27 @@ export const runBinCommand = async () => {
2929
}
3030

3131
export const getBinCommands = async () => {
32-
const documentPath = vscode.window.activeTextEditor?.document.uri.fsPath
33-
const workspacePath = vscode.workspace.workspaceFolders?.[0]!.uri.fsPath
34-
if (!workspacePath) throw new Error('available only in workspace')
32+
const _docUri = vscode.window.activeTextEditor?.document.uri
33+
if (!_docUri) return []
34+
const folderUri = vscode.Uri.joinPath(_docUri, '..')
35+
const workspaceUri = vscode.workspace.workspaceFolders?.[0]!.uri
36+
const isFileInWorkspace = !workspaceUri || folderUri.toString().startsWith(workspaceUri.toString())
3537

3638
const nodeModulesPaths = await (async () => {
37-
if (documentPath) {
38-
const findUpArgs = [
39-
'node_modules',
40-
{
41-
cwd: documentPath,
42-
stopAt: workspacePath,
43-
type: 'directory',
44-
},
45-
] as const
46-
if (getExtensionSetting('runBinCommand.searchLocation') === 'nearest') {
47-
const path = await findUp(...findUpArgs)
48-
return path !== undefined && [path]
49-
}
50-
51-
return findUpMultiple(...findUpArgs)
39+
const findUpArgs = [
40+
'node_modules',
41+
{
42+
cwd: folderUri.fsPath,
43+
stopAt: isFileInWorkspace ? workspaceUri!.fsPath : undefined,
44+
type: 'directory',
45+
},
46+
] as const
47+
if (getExtensionSetting('runBinCommand.searchLocation') === 'nearest') {
48+
const path = await findUp(...findUpArgs)
49+
return path !== undefined && [path]
5250
}
5351

54-
const nodeModulesPath = join(workspacePath, 'node_modules')
55-
if (fs.existsSync(nodeModulesPath)) return [nodeModulesPath]
56-
return []
52+
return findUpMultiple(...findUpArgs)
5753
})()
5854
if (!nodeModulesPaths || nodeModulesPaths.length === 0) throw new Error('no node_modules in current workspace')
5955

src/configurationType.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ export type Configuration = {
152152
// "All bin commands from all node_modules from cwd up to root of workspace",
153153
// "All bin commands from nearest node_modules"
154154
// ]
155+
/**
156+
* @default allUpToRoot
157+
*/
155158
'runBinCommand.searchLocation': 'allUpToRoot' | 'nearest'
156159
// 'editor.suggestPackagesToInstall': { [identifier: string]: boolean }
157160
/**

src/configurationTypeCache.jsonc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// GENERATED. DON'T EDIT MANUALLY
2-
// md5hash: fd6807fee82c0259817e00d5a0a9b619
2+
// md5hash: 90717a6b6c67a68e25ab99d99671936f
33
{
44
"type": "object",
55
"properties": {
@@ -145,7 +145,6 @@
145145
"type": "boolean"
146146
},
147147
"runBinCommand.searchLocation": {
148-
"description": "From where to pick bin commands",
149148
"default": "allUpToRoot",
150149
"enum": [
151150
"allUpToRoot",

src/packageJsonComplete.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ export const registerPackageJsonCompletions = () => {
2323
const root = parseTree(document.getText())!
2424
const location = getLocation(document.getText(), offset)
2525
const { path } = location
26-
const node = findNodeAtLocation(root, path)
2726
const jsonCompletingInfo = getJsonCompletingInfo(location, document, position)
2827
if (!jsonCompletingInfo) return
2928
const { insideStringRange } = jsonCompletingInfo
30-
const folder = Utils.joinPath(document.uri, '..')
29+
const folderUri = Utils.joinPath(document.uri, '..')
3130
if (insideStringRange) {
3231
if (location.matches(['name'])) {
33-
const folderName = Utils.basename(folder)
32+
const folderName = Utils.basename(folderUri)
3433
return jsonValuesToCompletions([folderName])
3534
}
3635

@@ -74,7 +73,7 @@ export const registerPackageJsonCompletions = () => {
7473
compareString = compareString.replace(/\s$/, '')
7574
for (const [key, filesGlob] of Object.entries(pathAutoComplete))
7675
if (compareString === key) {
77-
const files = await vscode.workspace.findFiles(new vscode.RelativePattern(folder, filesGlob), '**/node_modules/**')
76+
const files = await vscode.workspace.findFiles(new vscode.RelativePattern(folderUri, filesGlob), '**/node_modules/**')
7877
return files.map(uri => ({
7978
label: Utils.basename(uri),
8079
kind: vscode.CompletionItemKind.File,
@@ -106,12 +105,9 @@ export const registerPackageJsonCompletions = () => {
106105
}),
107106
)
108107
}
109-
//
110108
}
111109

112110
// if (location.matches(['peerDependenciesMeta'])) return jsonValuesToCompletions(['test'])
113-
114-
// console.log(getLocation(document.getText(), offset).matches(['scripts', '*']))
115111
return undefined
116112
},
117113
},

0 commit comments

Comments
 (0)