Exemplo n.º 1
0
 internal override int ExecCommandOnNode(Guid cmdGroup, uint cmd, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
 {
     if (cmdGroup == Guids.NodejsCmdSet)
     {
         switch (cmd)
         {
         case PkgCmdId.cmdidOpenReplWindow:
             NodejsPackage.Instance.OpenReplWindow();
             return(VSConstants.S_OK);
         }
     }
     else if (cmdGroup == Guids.NodejsNpmCmdSet)
     {
         try
         {
             NpmHelpers.GetPathToNpm(
                 Nodejs.GetAbsoluteNodeExePath(
                     this.ProjectHome,
                     this.Project.GetNodejsProject().GetProjectProperty(NodeProjectProperty.NodeExePath)
                     ));
         }
         catch (NpmNotFoundException)
         {
             Nodejs.ShowNodejsNotInstalled();
             return(VSConstants.S_OK);
         }
     }
     return(base.ExecCommandOnNode(cmdGroup, cmd, nCmdexecopt, pvaIn, pvaOut));
 }
Exemplo n.º 2
0
        protected override int ExecCommandThatDependsOnSelectedNodes(Guid cmdGroup, uint cmdId, uint cmdExecOpt, IntPtr vaIn, IntPtr vaOut, CommandOrigin commandOrigin, IList <HierarchyNode> selectedNodes, out bool handled)
        {
            if (cmdGroup == Guids.NodejsNpmCmdSet)
            {
                try {
                    NpmHelpers.GetPathToNpm(this.Project.GetNodejsProject().GetProjectProperty(NodejsConstants.NodeExePath));
                } catch (NpmNotFoundException) {
                    Nodejs.ShowNodejsNotInstalled();
                    handled = true;
                    return(VSConstants.S_OK);
                }

                switch (cmdId)
                {
                case PkgCmdId.cmdidNpmManageModules:
                    if (!ShowManageModulesCommandOnNode(selectedNodes))
                    {
                        ModulesNode.ManageModules();
                        handled = true;
                        return(VSConstants.S_OK);
                    }

                    var node = selectedNodes[0] as AbstractNpmNode;
                    if (node != null)
                    {
                        var abstractNpmNode = node;
                        abstractNpmNode.ManageNpmModules();
                        handled = true;
                        return(VSConstants.S_OK);
                    }
                    break;
                }
            }
            return(base.ExecCommandThatDependsOnSelectedNodes(cmdGroup, cmdId, cmdExecOpt, vaIn, vaOut, commandOrigin, selectedNodes, out handled));
        }
Exemplo n.º 3
0
        protected override int ExecCommandThatDependsOnSelectedNodes(Guid cmdGroup, uint cmdId, uint cmdExecOpt, IntPtr vaIn, IntPtr vaOut, CommandOrigin commandOrigin, IList <HierarchyNode> selectedNodes, out bool handled)
        {
            if (cmdGroup == Guids.NodejsNpmCmdSet)
            {
                try {
                    NpmHelpers.GetPathToNpm(
                        Nodejs.GetAbsoluteNodeExePath(
                            ProjectHome,
                            Project.GetNodejsProject().GetProjectProperty(NodeProjectProperty.NodeExePath)
                            ));
                } catch (NpmNotFoundException) {
                    Nodejs.ShowNodejsNotInstalled();
                    handled = true;
                    return(VSConstants.S_OK);
                }

                switch (cmdId)
                {
                case PkgCmdId.cmdidNpmManageModules:
                    if (!ShowManageModulesCommandOnNode(selectedNodes))
                    {
                        ModulesNode.ManageModules();
                        handled = true;
                        return(VSConstants.S_OK);
                    }

                    var node = selectedNodes[0] as AbstractNpmNode;
                    if (node != null)
                    {
                        var abstractNpmNode = node;
                        abstractNpmNode.ManageNpmModules();
                        handled = true;
                        return(VSConstants.S_OK);
                    }
                    break;
                }
            }
            else if (cmdGroup == Guids.NodejsCmdSet)
            {
                switch (cmdId)
                {
                case PkgCmdId.cmdidSetAsNodejsStartupFile:
                    // Set the StartupFile project property to the Url of this node
                    SetProjectProperty(
                        CommonConstants.StartupFile,
                        CommonUtils.GetRelativeFilePath(ProjectHome, selectedNodes[0].Url)
                        );
                    handled = true;
                    return(VSConstants.S_OK);

                case PkgCmdId.cmdidAddFileCommand:
                    NewFileMenuGroup.NewFileUtilities.CreateNewFile(projectNode: this, containerId: selectedNodes[0].ID);
                    handled = true;
                    return(VSConstants.S_OK);
                }
            }

            return(base.ExecCommandThatDependsOnSelectedNodes(cmdGroup, cmdId, cmdExecOpt, vaIn, vaOut, commandOrigin, selectedNodes, out handled));
        }
Exemplo n.º 4
0
 protected string GetPathToNpm()
 {
     if (null == _pathToNpm || !File.Exists(_pathToNpm))
     {
         _pathToNpm = NpmHelpers.GetPathToNpm();
     }
     return(_pathToNpm);
 }
Exemplo n.º 5
0
 protected string GetPathToNpm()
 {
     if (string.IsNullOrEmpty(this.pathToNpm) || !File.Exists(this.pathToNpm))
     {
         this.pathToNpm = NpmHelpers.GetPathToNpm();
     }
     return(this.pathToNpm);
 }
        public async Task TestNpmCommandProcessExitSucceeds()
        {
            var npmPath    = NpmHelpers.GetPathToNpm();
            var redirector = new NpmCommand.NpmCommandRedirector(new NpmBinCommand(null, false));

            for (int j = 0; j < 200; j++)
            {
                await NpmHelpers.ExecuteNpmCommandAsync(
                    redirector,
                    npmPath,
                    null,
                    new[] { "config", "get", "registry" },
                    null);
            }
        }
Exemplo n.º 7
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
                }
            }
Exemplo n.º 8
0
        public async Task <ExecutionResult> Execute(IReplWindow window, string arguments)
        {
            string projectPath  = string.Empty;
            string 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(SR.GetString(SR.ReplWindowNpmInitNoYesFlagWarning));
                return(ExecutionResult.Failure);
            }

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

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

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

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

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

                string projectName = dteProject.Name;
                if (string.IsNullOrEmpty(projectName))
                {
                    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;
                        }
                    }
                }

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

            Tuple <string, IVsHierarchy> projectInfo;

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

            NodejsProjectNode nodejsProject = null;

            if (projectInfo != null)
            {
                projectPath = projectInfo.Item1;
                if (projectInfo.Item2 != null)
                {
                    nodejsProject = projectInfo.Item2.GetProject().GetNodejsProject();
                }
            }

            bool 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
            string projectDirectoryPath = File.Exists(projectPath) ? Path.GetDirectoryName(projectPath) : projectPath;

            if (!isGlobalCommand && !Directory.Exists(projectDirectoryPath))
            {
                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);
            }

            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 npmReplRedirector = new NpmReplRedirector(window);

            await ExecuteNpmCommandAsync(
                npmReplRedirector,
                npmPath,
                projectDirectoryPath,
                new[] { npmArguments },
                null);

            if (npmReplRedirector.HasErrors)
            {
                window.WriteError(SR.GetString(SR.NpmReplCommandCompletedWithErrors, arguments));
            }
            else
            {
                window.WriteLine(SR.GetString(SR.NpmSuccessfullyCompleted, arguments));
            }

            if (nodejsProject != null)
            {
                await nodejsProject.CheckForLongPaths(npmArguments);
            }

            return(ExecutionResult.Success);
        }
Exemplo n.º 9
0
        private string GetNpmPathFromNodePathInProject()
        {
            var props = this.ProjectMgr.NodeProperties as NodejsProjectNodeProperties;

            return(NpmHelpers.GetPathToNpm(props != null ? props.NodeExePath : null));
        }