( inFolders: (vscode.WorkspaceFolder | string)[] | undefined, silent = false, )
| 90 | * Finds configured npm scripts in the workspace. |
| 91 | */ |
| 92 | export async function findScripts( |
| 93 | inFolders: (vscode.WorkspaceFolder | string)[] | undefined, |
| 94 | silent = false, |
| 95 | ): Promise<IScript[] | undefined> { |
| 96 | const folders = inFolders ?? vscode.workspace.workspaceFolders ?? []; |
| 97 | |
| 98 | // 1. If there are no open folders, show an error and abort. |
| 99 | if (!folders || folders.length === 0) { |
| 100 | if (!silent) { |
| 101 | vscode.window.showErrorMessage( |
| 102 | l10n.t('You need to open a workspace folder to debug npm scripts.'), |
| 103 | ); |
| 104 | } |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // Otherwise, go through all package.json's in the folder and pull all the npm scripts we find. |
| 109 | const candidates = ( |
| 110 | await Promise.all( |
| 111 | folders.map(f => |
| 112 | vscode.workspace.findFiles( |
| 113 | new vscode.RelativePattern(f, '**/package.json'), |
| 114 | // matches https://github.com/microsoft/vscode/blob/18f743d534ef3f528c5e81e82e695b87c60d2ebf/extensions/npm/src/tasks.ts#L189 |
| 115 | '**/{node_modules,.vscode-test}/**', |
| 116 | ) |
| 117 | ), |
| 118 | ) |
| 119 | ).flat(); |
| 120 | |
| 121 | if (candidates.length === 0) { |
| 122 | if (!silent) { |
| 123 | vscode.window.showErrorMessage(l10n.t('No package.json files found in your workspace.')); |
| 124 | } |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | const scripts: IScript[] = []; |
| 129 | |
| 130 | // editCandidate is the file we'll edit if we don't find any npm scripts. |
| 131 | // We 'narrow' this as we parse to files that look more like a package.json we want |
| 132 | let editCandidate: IEditCandidate = { path: candidates[0].fsPath, score: 0 }; |
| 133 | for (const { fsPath } of new Set(candidates)) { |
| 134 | // update this now, because we know it exists |
| 135 | editCandidate = updateEditCandidate(editCandidate, { |
| 136 | path: fsPath, |
| 137 | score: 1, |
| 138 | }); |
| 139 | |
| 140 | let parsed: { scripts?: { [key: string]: string } }; |
| 141 | try { |
| 142 | parsed = JSON.parse(await readfile(fsPath)); |
| 143 | } catch (e) { |
| 144 | if (!silent) { |
| 145 | promptToOpen( |
| 146 | 'showWarningMessage', |
| 147 | l10n.t('Could not read {0}: {1}', fsPath, e.message), |
| 148 | fsPath, |
| 149 | ); |
no test coverage detected