-
Notifications
You must be signed in to change notification settings - Fork 354
Allow running .npm commands in Repl Window from any proj types. #808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
81ab8ba
3105515
bac5f1f
602aa9c
b19622a
db921f9
1d41401
dced2e7
5df5d69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,8 +58,15 @@ public async Task<ExecutionResult> Execute(IReplWindow window, string arguments) | |
| // at beginning and end of string (e.g. '--global') | ||
| npmArguments = string.Format(" {0} ", npmArguments); | ||
|
|
||
| // Prevent running `npm init` without the `-y` flag since it will freeze the repl window, | ||
| // waiting for user input that will never come. | ||
| if (npmArguments.Contains(" init ") && !(npmArguments.Contains(" -y ") || npmArguments.Contains(" --yes "))) { | ||
| window.WriteError(SR.GetString(SR.ReplWindowNpmInitNoYesFlagWarning)); | ||
| return ExecutionResult.Failure; | ||
| } | ||
|
|
||
| var solution = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution; | ||
| IEnumerable<IVsProject> loadedProjects = solution.EnumerateLoadedProjects(onlyNodeProjects: true); | ||
| IEnumerable<IVsProject> loadedProjects = solution.EnumerateLoadedProjects(onlyNodeProjects: false); | ||
|
|
||
| var projectNameToDirectoryDictionary = new Dictionary<string, Tuple<string, IVsHierarchy>>(StringComparer.OrdinalIgnoreCase); | ||
| foreach (IVsProject project in loadedProjects) { | ||
|
|
@@ -76,19 +83,33 @@ public async Task<ExecutionResult> Execute(IReplWindow window, string arguments) | |
| continue; | ||
| } | ||
|
|
||
| EnvDTE.Properties properties = dteProject.Properties; | ||
| if (dteProject.Properties == null) { | ||
| string projectName = dteProject.Name; | ||
| if (string.IsNullOrEmpty(projectName)) { | ||
| continue; | ||
| } | ||
|
|
||
| string projectName = dteProject.Name; | ||
| EnvDTE.Property projectHome = properties.Item("ProjectHome"); | ||
| if (projectHome == null || projectName == null) { | ||
| continue; | ||
| // Try checking the `ProjectHome` property first | ||
| EnvDTE.Properties properties = dteProject.Properties; | ||
| if (dteProject.Properties != null) { | ||
| EnvDTE.Property projectHome = null; | ||
| try { | ||
| projectHome = properties.Item("ProjectHome"); | ||
| } catch (ArgumentException) { | ||
| // noop | ||
| } | ||
|
|
||
| if (projectHome != null) { | ||
| var projectHomeDirectory = projectHome.Value as string; | ||
| if (!string.IsNullOrEmpty(projectHomeDirectory)) { | ||
| projectNameToDirectoryDictionary.Add(projectName, Tuple.Create(projectHomeDirectory, hierarchy)); | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var projectDirectory = projectHome.Value as string; | ||
| if (projectDirectory != null) { | ||
| // Otherwise, fall back to using fullname | ||
| string projectDirectory = Path.GetDirectoryName(dteProject.FullName); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this won't work when the project file is in a different location than the directory itself.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surely no one would do such an unthinkable thing ;) Any thoughts on what property to use on the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's an example of what |
||
| if (!string.IsNullOrEmpty(projectDirectory)) { | ||
| projectNameToDirectoryDictionary.Add(projectName, Tuple.Create(projectDirectory, hierarchy)); | ||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part may be attempting to be too clever. In a multi-project solution, it may be better to require users to explicitly state which project they want to run NPM for. Currently, if there is any Node project in a solution, you can't run .npm for any of the other projects in that solution. |
||
|
|
@@ -118,7 +139,7 @@ public async Task<ExecutionResult> Execute(IReplWindow window, string arguments) | |
| // In case someone copies filename | ||
| string projectDirectoryPath = File.Exists(projectPath) ? Path.GetDirectoryName(projectPath) : projectPath; | ||
|
|
||
| if (!isGlobalCommand && !(Directory.Exists(projectDirectoryPath) && File.Exists(Path.Combine(projectDirectoryPath, "package.json")))) { | ||
| if (!isGlobalCommand && !Directory.Exists(projectDirectoryPath)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you'll want to block npm init here, otherwise the repl will freeze up.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the event of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a check for This problem is also related to #578, where someone runs |
||
| window.WriteError("Please specify a valid Node.js project or project directory. If your solution contains multiple projects, specify a target project using .npm [ProjectName or ProjectDir] <npm arguments> For example: .npm [MyApp] list"); | ||
| return ExecutionResult.Failure; | ||
| } | ||
|
|
@@ -137,7 +158,6 @@ public async Task<ExecutionResult> Execute(IReplWindow window, string arguments) | |
| } | ||
|
|
||
| var npmReplRedirector = new NpmReplRedirector(window); | ||
|
|
||
| await ExecuteNpmCommandAsync( | ||
| npmReplRedirector, | ||
| npmPath, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add back null check for
projectName