Skip to content

enable running individual npm scripts in the containing folder#79960

Merged
egamma merged 7 commits intomicrosoft:masterfrom
helen3141:runNpmFromContextMenu
Oct 14, 2019
Merged

enable running individual npm scripts in the containing folder#79960
egamma merged 7 commits intomicrosoft:masterfrom
helen3141:runNpmFromContextMenu

Conversation

@helen3141
Copy link
Contributor

No description provided.

@msftclas
Copy link

msftclas commented Aug 28, 2019

CLA assistant check
All CLA requirements met.

@egamma
Copy link
Member

egamma commented Oct 8, 2019

@helen3141 thank you for the PR (sorry for the delayed review, but there was no corresponding issue with a feature request filed).

The PR currently includes no description. Can you please provide more background what the feature does, why is it needed, and how it can be tested?

@helen3141
Copy link
Contributor Author

Hi @egamma!

The feature request originates from our team's need to easily view and run custom scripts written in the package.json file of a particular folder of interest.

The idea is that users can select any folder in the Explorer pane to view the custom scripts within the folder's package.json file(s) by right-clicking the folder and selecting the option 'Select npm Scripts to Run...'

Run npm from root

Then a command palette will pop up with all of the custom scripts within that folder:

npm command palette

You can run this branch in Debug mode with the option 'Launch VS Code' selected.

The default setting is that this feature is enabled.

You can toggle the feature on/off. Go to File>Preferences>Settings> click the 'Extensions' drop down, click 'Npm' and untick the box 'Enable Run From Folder Context Menu':

npmoptions

When the feature is enabled you can test it.

Open any folder/project. The extension will check for custom scripts in the package.json file of the folder that is being right-clicked or the root folder (detected by ExplorerFolderContext and ExplorerRootContext mechanisms, I'm guessing).

This check leverages the code already in the extension, and is executed every time a folder is right-clicked in the Explorer pane. This could potentially be computationally expensive if the project is very large with many nested package.json files and I haven't stress tested/optimized for an extreme use case like that.

When the 'Select npm Scripts to Run...' option from the context menu is selected, all the custom scripts available in the folder will be listed in the command palette that pops up.

Scripts available from the package.json file of the parent folder are prefaced with >. If the scripts are from a package.json file that is nested in a subfolder, the script will be prefaced with its relative directory + >

When the user selects an option from the command palette, that particular script will run. If you have a terminal visible, you should be able to see the console output of that script.

Thank you for your feedback and please feel free to comment with any more questions/thoughts you may have!

Copy link
Member

@egamma egamma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@helen3141 thank you for the additional background. I've reviewed the PR.

In general the default value of the setting to enable the context menu should be false. The NPM extension is bundled with VS Code and it has to be defensive with regard to contributing to the context menu, given that not all workspaces are node based, e.g., think of a Python workspace.

}
});
let result = await vscode.window.showQuickPick(Object.keys(taskMap).sort(), {
placeHolder: `Run scripts on folder ${folderInfo.fsPath}...`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The quick pick should have a title 'Run npm Script in Folder' and the place holder should be 'Select script'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your helpful feedback! I was able to implement all except the title for the quick pick as I don't think there is a property for showQuickPick

@egamma egamma merged commit 34648a0 into microsoft:master Oct 14, 2019
@egamma
Copy link
Member

egamma commented Oct 14, 2019

@helen3141 thanks again 🌷 for the PR it is now merged in.

I've removed the code that leverages the tasks cache, it still requires to do a search on the file system and it is more tricky to get it right in all the cases.

I was able to implement all except the title for the quick pick as I don't think there is a property for showQuickPick

Correct this option is not available for the simple showQuickPick API, you have to create a QuickPick by yourself to get at this option, pls see this commit 7485ffd for how it can be done. This commit also includes some wording tweaks.

One tweaks I made was to simplify the setting from npm.enableRunFromFolderContextMenu to npm.runScriptFromFolder

@egamma egamma mentioned this pull request Oct 15, 2019
@egamma
Copy link
Member

egamma commented Oct 16, 2019

@helen3141 the feature is now available in the insiders build.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants