MCPcopy Create free account
hub / github.com/Snapchat/Valdi / findScripts

Function findScripts

valdi/vscode_debugger/src/ui/debugNpmScript.ts:71–155  ·  view source on GitHub ↗
(
  inFolders: string[] | undefined,
  silent = false,
)

Source from the content-addressed store, hash-verified

69 * Finds configured npm scripts in the workspace.
70 */
71export async function findScripts(
72 inFolders: string[] | undefined,
73 silent = false,
74): Promise<IScript[] | undefined> {
75 const folders = inFolders ?? vscode.workspace.workspaceFolders?.map(f => f.uri.fsPath) ?? [];
76
77 // 1. If there are no open folders, show an error and abort.
78 if (!folders || folders.length === 0) {
79 if (!silent) {
80 vscode.window.showErrorMessage(
81 localize(
82 'debug.npm.noWorkspaceFolder',
83 'You need to open a workspace folder to debug npm scripts.',
84 ),
85 );
86 }
87 return;
88 }
89
90 // Otherwise, go through all package.json's in the folder and pull all the npm scripts we find.
91 const candidates = folders.map(directory => path.join(directory, 'package.json'));
92 const scripts: IScript[] = [];
93
94 // editCandidate is the file we'll edit if we don't find any npm scripts.
95 // We 'narrow' this as we parse to files that look more like a package.json we want
96 let editCandidate: IEditCandidate = { path: candidates[0], score: 0 };
97 for (const packageJson of candidates) {
98 if (!fs.existsSync(packageJson)) {
99 continue;
100 }
101
102 // update this now, because we know it exists
103 editCandidate = updateEditCandidate(editCandidate, {
104 path: packageJson,
105 score: 1,
106 });
107
108 let parsed: { scripts?: { [key: string]: string } };
109 try {
110 parsed = JSON.parse(await readfile(packageJson));
111 } catch (e) {
112 if (!silent) {
113 promptToOpen(
114 'showWarningMessage',
115 localize('debug.npm.parseError', 'Could not read {0}: {1}', packageJson, e.message),
116 packageJson,
117 );
118 }
119 // set the candidate to 'undefined', since we already displayed an error
120 // and if there are no other candidates then that alone is fine.
121 editCandidate = updateEditCandidate(editCandidate, { path: undefined, score: 3 });
122 continue;
123 }
124
125 // update this now, because we know it is valid
126 editCandidate = updateEditCandidate(editCandidate, { path: undefined, score: 2 });
127
128 if (!parsed.scripts) {

Callers 3

getFromNpmScriptsMethod · 0.90
getFromNpmScriptsMethod · 0.90
debugNpmScriptFunction · 0.85

Calls 9

readfileFunction · 0.90
updateEditCandidateFunction · 0.85
promptToOpenFunction · 0.85
keysMethod · 0.65
sortMethod · 0.65
mapMethod · 0.45
joinMethod · 0.45
parseMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected