コード例 #1
0
        public void ManageModules(DependencyType dependencyType = DependencyType.Standard)
        {
            CheckNotDisposed();

            // Probably overkill to check out before the user actually makes changes, but this is the easiest
            // and most reliable in the current codebase.
            if (!EnsurePackageJsonCheckedOut())
            {
                ErrorHelper.ReportPackageJsonNotCheckedOut(Application.Current.MainWindow);
                return;
            }

            if (this.NpmController.RootPackage == null)
            {
                this.NpmController.Refresh();
                if (this.NpmController.RootPackage == null)
                {
                    MessageBox.Show(string.Format(CultureInfo.CurrentCulture, Resources.NodeModulesCouldNotParsePackageJsonErrorMessageText, NodejsConstants.PackageJsonFile));
                    return;
                }
            }

            using (var npmWorker = new NpmWorker(this.NpmController))
                using (var manager = new NpmPackageInstallWindow(this.NpmController, npmWorker, dependencyType))
                {
                    manager.Owner = Application.Current.MainWindow;
                    manager.ShowModal();
                }
            ReloadHierarchy();
        }
コード例 #2
0
 private void ExecNpmInstallNew(WorkspaceVisualNodeBase node)
 {
     using (var npmController = this.CreateController(node.Workspace))
         using (var npmWorker = new NpmWorker(npmController))
             using (var manager = new NpmPackageInstallWindow(npmController, npmWorker))
             {
                 manager.ShowModal();
             }
 }
コード例 #3
0
 private void ExecNpmInstallNew(string filePath)
 {
     using (var npmController = this.CreateController(filePath))
         using (var npmWorker = new NpmWorker(npmController))
             using (var manager = new NpmPackageInstallWindow(npmController, npmWorker))
             {
                 manager.ShowModal();
             }
 }
コード例 #4
0
            private async void ExecDynamic(WorkspaceVisualNodeBase node, uint nCmdID)
            {
                // Unfortunately the NpmController (and NpmCommander), used for the install, update commands
                // doesn't support running arbitrary scripts. And changing that is outside
                // the scope of these changes.
                var filePath = ((IFileNode)node).FullPath;

                if (TryGetCommand(nCmdID, filePath, out var commandName))
                {
                    var npmPath = NpmHelpers.GetPathToNpm();

                    await NpmWorker.ExecuteNpmCommandAsync(
                        npmPath,
                        executionDirectory : Path.GetDirectoryName(filePath),
                        arguments : new[] { "run-script", commandName },
                        visible : true); // show the CMD window
                }
            }
コード例 #5
0
        public override async Task <ExecutionResult> Execute(IInteractiveWindow window, string arguments)
        {
            var projectPath  = string.Empty;
            var npmArguments = arguments.Trim(' ', '\t');

            // Parse project name/directory in square brackets
            if (npmArguments.StartsWith("[", StringComparison.Ordinal))
            {
                var match = Regex.Match(npmArguments, @"(?:[[]\s*\""?\s*)(.*?)(?:\s*\""?\s*[]]\s*)");
                projectPath  = match.Groups[1].Value;
                npmArguments = npmArguments.Substring(match.Length);
            }

            // Include spaces on either side of npm arguments so that we can more simply detect arguments
            // at beginning and end of string (e.g. '--global')
            npmArguments = string.Format(CultureInfo.InvariantCulture, " {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(Resources.ReplWindowNpmInitNoYesFlagWarning);
                return(ExecutionResult.Failure);
            }

            var solution       = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution;
            var loadedProjects = solution.EnumerateLoadedProjects(onlyNodeProjects: false);

            var projectNameToDirectoryDictionary = new Dictionary <string, (string, IVsHierarchy)>(StringComparer.OrdinalIgnoreCase);

            foreach (var project in loadedProjects)
            {
                var hierarchy = (IVsHierarchy)project;

                var projectResult = hierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out var extObject);
                if (!ErrorHandler.Succeeded(projectResult))
                {
                    continue;
                }

                var dteProject = extObject as EnvDTE.Project;
                if (dteProject == null)
                {
                    continue;
                }

                var projectName = dteProject.Name;
                if (string.IsNullOrEmpty(projectName))
                {
                    continue;
                }

                // Try checking the `ProjectHome` property first
                var 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, (projectHomeDirectory, hierarchy));
                            continue;
                        }
                    }
                }

                // Otherwise, fall back to using fullname
                var projectDirectory = string.IsNullOrEmpty(dteProject.FullName) ? null : Path.GetDirectoryName(dteProject.FullName);
                if (!string.IsNullOrEmpty(projectDirectory))
                {
                    projectNameToDirectoryDictionary.Add(projectName, (projectDirectory, hierarchy));
                }
            }

            (string ProjectPath, IVsHierarchy Hierarchy)projectInfo;
            if (string.IsNullOrEmpty(projectPath) && projectNameToDirectoryDictionary.Count == 1)
            {
                projectInfo = projectNameToDirectoryDictionary.Values.First();
            }
            else
            {
                projectNameToDirectoryDictionary.TryGetValue(projectPath, out projectInfo);
            }

            NodejsProjectNode nodejsProject = null;

            projectPath = projectInfo.ProjectPath;
            if (projectInfo.Hierarchy != null)
            {
                nodejsProject = projectInfo.Hierarchy.GetProject().GetNodejsProject();
            }

            var isGlobalCommand = false;

            if (string.IsNullOrWhiteSpace(npmArguments) ||
                npmArguments.Contains(" -g ") || npmArguments.Contains(" --global "))
            {
                projectPath     = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
                isGlobalCommand = true;
            }

            // In case someone copies filename
            var projectDirectoryPath = File.Exists(projectPath) ? Path.GetDirectoryName(projectPath) : projectPath;

            if (!isGlobalCommand && !Directory.Exists(projectDirectoryPath))
            {
                window.WriteError(Resources.NpmSpecifyValidProject);
                return(ExecutionResult.Failure);
            }

            string npmPath;

            try
            {
                npmPath = NpmHelpers.GetPathToNpm(
                    nodejsProject != null ?
                    Nodejs.GetAbsoluteNodeExePath(
                        nodejsProject.ProjectHome,
                        nodejsProject.GetProjectProperty(NodeProjectProperty.NodeExePath))
                        : null);
            }
            catch (NpmNotFoundException)
            {
                Nodejs.ShowNodejsNotInstalled();
                return(ExecutionResult.Failure);
            }

            var evaluator = window.Evaluator as NodejsReplEvaluator;

            Debug.Assert(evaluator != null, "How did we end up with an evaluator that's not the nodetools evaluator?");

            var npmReplRedirector = new NpmReplRedirector(evaluator);
            await NpmWorker.ExecuteNpmCommandAsync(
                npmPath,
                projectDirectoryPath,
                new[] { npmArguments },
                redirector : npmReplRedirector);

            if (npmReplRedirector.HasErrors)
            {
                window.WriteError(string.Format(CultureInfo.CurrentCulture, Resources.NpmReplCommandCompletedWithErrors, arguments));
            }
            else
            {
                window.WriteLine(string.Format(CultureInfo.CurrentCulture, Resources.NpmSuccessfullyCompleted, arguments));
            }

            return(ExecutionResult.Success);
        }