Beispiel #1
0
        public async Task <int> InvokeAsync(InvocationContext context)
        {
            var rawVersion = context.ParseResult.ValueForArgument("version");

            if (rawVersion == null)
            {
                await Console.Error.WriteLineAsync($"Missing version argument");

                return(1);
            }

            Version version;

            try
            {
                version = VersionParser.StrictParse(rawVersion.ToString() !);
            }
            catch (ArgumentException)
            {
                await Console.Error.WriteLineAsync($"Invalid version argument: {rawVersion}");

                return(1);
            }

            //
            // Is the requested version installed?
            //

            var nodeVersion = _nodeLocal.GetInstalledVersions().Find(v => v.Version.Equals(version));

            if (nodeVersion == null)
            {
                await Console.Error.WriteLineAsync($"{version} not installed");

                return(1);
            }

            //
            // Uninstall it
            //

            try
            {
                Directory.Delete(nodeVersion.Path, true);
            }
            catch (IOException)
            {
                await Console.Error.WriteLineAsync($"Unable to delete {nodeVersion.Path}");

                return(1);
            }

            Console.WriteLine("Done");
            return(0);
        }
Beispiel #2
0
        public Task <int> InvokeAsync(InvocationContext context)
        {
            var versions = _nodeJs.GetInstalledVersions();

            if (versions.Count == 0)
            {
                Console.WriteLine("None installed");
                return(Task.Factory.StartNew(() => 0));
            }

            Console.WriteLine();
            versions.ForEach(v =>
            {
                var prefix = v.IsActive ? "  * " : "    ";
                Console.WriteLine($"{prefix}{v.Version}");
            });
            Console.WriteLine();

            return(Task.Factory.StartNew(() => 0));
        }
Beispiel #3
0
        public async Task <int> InvokeAsync(InvocationContext context)
        {
            var rawVersion = context.ParseResult.ValueForArgument("version");

            if (rawVersion == null)
            {
                await Console.Error.WriteLineAsync($"Missing version argument");

                return(1);
            }

            Version version;

            if (rawVersion.ToString()?.ToLower() == "latest")
            {
                try
                {
                    version = _nodeWeb.GetLatestNodeVersion();
                }
                catch (Exception)
                {
                    await Console.Error.WriteLineAsync("Unable to determine latest Node.js version.");

                    return(1);
                }
            }
            else if (rawVersion.ToString()?.Split(".").Length < 3)
            {
                try
                {
                    version = _nodeWeb.GetLatestNodeVersion(rawVersion.ToString());
                }
                catch (Exception)
                {
                    await Console.Error.WriteLineAsync($"Unable to get latest Node.js version " +
                                                       $"with prefix {rawVersion}.");

                    return(1);
                }
            }
            else
            {
                try
                {
                    version = VersionParser.Parse(rawVersion.ToString());
                }
                catch (ArgumentException)
                {
                    await Console.Error.WriteLineAsync($"Invalid version argument: {rawVersion}");

                    return(1);
                }
            }

            //
            // Is the requested version already installed?
            //

            if (_nodeLocal.GetInstalledVersions().FindIndex(v => v.Version.Equals(version)) != -1)
            {
                await Console.Error.WriteLineAsync($"{version} already installed");

                return(1);
            }

            //
            // Download it
            //

            var downloadUrl = _nodeWeb.GetDownloadUrl(version);
            var zipPath     = Path.Join(_globalContext.StoragePath, Path.GetFileName(downloadUrl));
            var progressBar = new ProgressBar(100, "Download progress", new ProgressBarOptions
            {
                ProgressCharacter   = '\u2593',
                ForegroundColor     = ConsoleColor.Yellow,
                ForegroundColorDone = ConsoleColor.Green,
            });

            var webClient = new WebClient();

            webClient.DownloadProgressChanged += (s, e) => { progressBar.Tick(e.ProgressPercentage); };
            webClient.DownloadFileCompleted   += (s, e) => { progressBar.Dispose(); };

            try
            {
                await webClient.DownloadFileTaskAsync(downloadUrl, zipPath).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                await Console.Error.WriteLineAsync("Unable to download the Node.js zip file.");

                if (e.InnerException == null)
                {
                    return(1);
                }
                await Console.Error.WriteLineAsync(e.InnerException.Message);

                await Console.Error.WriteLineAsync("You may need to run this command from an " +
                                                   "elevated prompt. (Run as Administrator)");

                return(1);
            }

            Console.WriteLine("Extracting...");
            ConsoleSpinner.Instance.Update();
            var timer = new Timer(250);

            timer.Elapsed += (s, e) => ConsoleSpinner.Instance.Update();
            timer.Enabled  = true;
            ZipFile.ExtractToDirectory(zipPath, _globalContext.StoragePath);
            timer.Enabled = false;
            ConsoleSpinner.Reset();
            File.Delete(zipPath);

            Console.WriteLine($"Done. To use, run `nodeswap use {version}`");
            return(0);
        }
Beispiel #4
0
        public async Task <int> InvokeAsync(InvocationContext context)
        {
            var rawVersion = context.ParseResult.ValueForArgument("version");

            if (rawVersion == null)
            {
                await Console.Error.WriteLineAsync($"Missing version argument");

                return(1);
            }

            Version version;

            try
            {
                version = VersionParser.StrictParse(rawVersion.ToString() !);
            }
            catch (ArgumentException)
            {
                await Console.Error.WriteLineAsync($"Invalid version argument: {rawVersion}");

                return(1);
            }

            //
            // Is the requested version installed?
            //

            var nodeVersion = _nodeLocal.GetInstalledVersions().Find(v => v.Version.Equals(version));

            if (nodeVersion == null)
            {
                await Console.Error.WriteLineAsync($"{version} not installed");

                return(1);
            }

            //
            // Replace the symlink
            //

            if (Directory.Exists(_globalContext.SymlinkPath))
            {
                try
                {
                    Directory.Delete(_globalContext.SymlinkPath);
                }
                catch (IOException)
                {
                    await Console.Error.WriteLineAsync(
                        $"Unable to delete the symlink at {_globalContext.SymlinkPath}. Be sure you are running this in an elevated terminal (i.e. Run as Administrator).");

                    return(1);
                }
            }

            CreateSymbolicLink(_globalContext.SymlinkPath, nodeVersion.Path, SymbolicLink.Directory);
            if (!Directory.Exists(_globalContext.SymlinkPath))
            {
                await Console.Error.WriteLineAsync(
                    $"Unable to create the symlink at {_globalContext.SymlinkPath}. Be sure you are running this in an elevated terminal (i.e. Run as Administrator).");

                return(1);
            }

            //
            // Track it
            //

            // ReSharper disable once MethodHasAsyncOverload
            File.WriteAllText(_globalContext.ActiveVersionTrackerFilePath, nodeVersion.Version.ToString());
            Console.WriteLine("Done");
            return(0);
        }