Esempio n. 1
0
        void RunPackageAdd(PackageManagerAddCommand addCommand)
        {
            InitDiskCache();
            var project = AddProject(PathUtils.Normalize(Environment.CurrentDirectory), false);
            var currentNodePackageManager = new CurrentNodePackageManager(_dc, _logger);

            if (addCommand.PackageName.Value == null)
            {
                _logger.Error("Didn't specified name of package to add");
            }
            else
            {
                currentNodePackageManager.Add(project.Owner.Owner, addCommand.PackageName.Value, addCommand.Dev.Value);
            }
        }
Esempio n. 2
0
        void RunPackageUpgrade(PackageManagerUpgradeCommand upgradeCommand)
        {
            InitDiskCache();
            var project = AddProject(PathUtils.Normalize(Environment.CurrentDirectory), false);
            var currentNodePackageManager = new CurrentNodePackageManager(_dc, _logger);
            var before = currentNodePackageManager.GetLockedDependencies(project.Owner.Owner).ToArray();

            if (upgradeCommand.PackageName.Value == null)
            {
                currentNodePackageManager.UpgradeAll(project.Owner.Owner);
            }
            else
            {
                currentNodePackageManager.Upgrade(project.Owner.Owner, upgradeCommand.PackageName.Value);
            }

            _dc.CheckForTrueChange();
            var after = currentNodePackageManager.GetLockedDependencies(project.Owner.Owner).ToArray();
            var lines = new List <string>();

            foreach (var line in new DiffChangeLog(_dc).Generate(before, after))
            {
                lines.Add(line);
                _logger.WriteLine(line);
            }

            if (lines.Count > 0)
            {
                try
                {
                    File.WriteAllText(PathUtils.Join(project.Owner.Owner.FullPath, "DEPSCHANGELOG.md"),
                                      string.Join('\n', lines) + '\n', Encoding.UTF8);
                }
                catch (Exception e)
                {
                    _logger.Error($"Write DEPSCHANGELOG.md failed with {e}");
                }
            }
        }
Esempio n. 3
0
        public void LoadProjectJson(bool forbiddenDependencyUpdate)
        {
            DiskCache.UpdateIfNeeded(Owner);
            var packageJsonFile = Owner.TryGetChild("package.json", true);

            if (packageJsonFile is IFileCache cache)
            {
                var newChangeId = cache.ChangeId;
                if (newChangeId == PackageJsonChangeId)
                {
                    return;
                }
                MainFileNeedsToBeCompiled = false;
                JObject parsed;
                try
                {
                    parsed = JObject.Parse(cache.Utf8Content);
                }
                catch (Exception)
                {
                    parsed = new JObject();
                }

                var deps    = new HashSet <string>();
                var devdeps = new HashSet <string>();
                var hasMain = false;
                if (parsed.GetValue("typescript") is JObject parsedT)
                {
                    if (parsedT.GetValue("main") is JValue mainV)
                    {
                        MainFile      = PathUtils.Normalize(mainV.ToString());
                        TypesMainFile = null;
                        hasMain       = true;
                    }
                }

                if (parsed.GetValue("module") is JValue moduleV)
                {
                    MainFile = PathUtils.Normalize(moduleV.ToString());
                    MainFileNeedsToBeCompiled = true;
                    if (DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath, MainFile)) is IFileCache)
                    {
                        hasMain = true;
                        if (parsed.GetValue("typings") is JValue mainV)
                        {
                            TypesMainFile = PathUtils.Normalize(mainV.ToString());
                        }
                    }
                }

                if (!hasMain)
                {
                    if (parsed.GetValue("main") is JValue mainV2)
                    {
                        MainFile = PathUtils.Normalize(mainV2.ToString());
                    }
                    else
                    {
                        MainFile = "index.js";
                    }

                    if (DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath, PathUtils.ChangeExtension(MainFile, "ts")))
                        is IFileCache fileAsTs)
                    {
                        MainFile      = PathUtils.ChangeExtension(MainFile, "ts");
                        TypesMainFile = null;
                        hasMain       = true;
                    }
                    else
                    {
                        fileAsTs = DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath,
                                                                       PathUtils.ChangeExtension(MainFile, "tsx"))) as IFileCache;
                        if (fileAsTs != null)
                        {
                            MainFile      = PathUtils.ChangeExtension(MainFile, "tsx");
                            TypesMainFile = null;
                            hasMain       = true;
                        }
                    }

                    if (!hasMain)
                    {
                        if (parsed.GetValue("types") is JValue mainV)
                        {
                            TypesMainFile = PathUtils.Normalize(mainV.ToString());
                            hasMain       = true;
                        }
                    }
                }

                if (TypesMainFile == null)
                {
                    TypesMainFile = PathUtils.ChangeExtension(MainFile, "d.ts");
                    if (!IsRootProject &&
                        !(DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath, TypesMainFile)) is IFileCache))
                    {
                        var typesDts = PathUtils.Join(Owner.FullPath, $"../@types/{Owner.Name}/index.d.ts");
                        if (DiskCache.TryGetItem(typesDts) is IFileCache)
                        {
                            TypesMainFile = typesDts;
                        }
                    }
                }

                if (parsed.GetValue("dependencies") is JObject parsedV)
                {
                    foreach (var i in parsedV.Properties())
                    {
                        deps.Add(i.Name);
                    }
                }

                if (parsed.GetValue("devDependencies") is JObject parsedV2)
                {
                    foreach (var i in parsedV2.Properties())
                    {
                        devdeps.Add(i.Name);
                    }
                }

                PackageJsonChangeId = newChangeId;
                Dependencies        = deps;
                DevDependencies     = devdeps;
                Assets = ParseBobrilAssets(parsed);

                if (ProjectOptions == null)
                {
                    return;
                }
                ProjectOptions.FillProjectOptionsFromPackageJson(parsed);
                if (forbiddenDependencyUpdate || ProjectOptions.DependencyUpdate == DepedencyUpdate.Disabled)
                {
                    return;
                }
                var packageManager = new CurrentNodePackageManager(DiskCache, Logger);
                if (ProjectOptions.DependencyUpdate == DepedencyUpdate.Upgrade)
                {
                    packageManager.UpgradeAll(Owner);
                }
                else
                {
                    packageManager.Install(Owner);
                }

                DiskCache.CheckForTrueChange();
                DiskCache.ResetChange();
            }
            else
            {
                MainFile        = "index.js";
                Dependencies    = new HashSet <string>();
                DevDependencies = new HashSet <string>();
                Assets          = null;
                ProjectOptions?.FillProjectOptionsFromPackageJson(null);
            }
        }
Esempio n. 4
0
        public void LoadProjectJson(bool forbiddenDependencyUpdate, ProjectOptions?parentProjectOptions)
        {
            if (Virtual)
            {
                PackageJsonChangeId = -1;
                Dependencies        = new();
                DevDependencies     = new();
                Assets = null;
                return;
            }
            DiskCache.UpdateIfNeeded(Owner);
            var packageJsonFile = Owner.TryGetChild("package.json");

            if (packageJsonFile is IFileCache cache)
            {
                var newChangeId = cache.ChangeId;
                if (newChangeId == PackageJsonChangeId)
                {
                    return;
                }

                var rp = PathUtils.RealPath(packageJsonFile.FullPath);
                if (rp != packageJsonFile.FullPath)
                {
                    if (DiskCache.TryGetItem(rp) is IFileCache realPackageJsonFile)
                    {
                        Owner         = realPackageJsonFile.Parent !;
                        Owner.Project = this;
                        cache         = realPackageJsonFile;
                        newChangeId   = cache.ChangeId;
                    }
                }

                ProjectOptions.FinalCompilerOptions = null;
                JObject parsed;
                try
                {
                    parsed = JObject.Parse(cache.Utf8Content);
                }
                catch (Exception)
                {
                    parsed = new();
                }

                var deps    = new HashSet <string>();
                var devdeps = new HashSet <string>();
                var hasMain = false;
                if (parsed.GetValue("typescript") is JObject parsedT)
                {
                    if (parsedT.GetValue("main") is JValue mainV)
                    {
                        MainFile = PathUtils.Normalize(mainV.ToString());
                        if (DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath, MainFile)) is IFileCache)
                        {
                            TypesMainFile = null;
                            hasMain       = true;
                        }
                    }
                }

                if (!hasMain && parsed.GetValue("browser") is JValue browserMain)
                {
                    MainFile = PathUtils.Normalize(browserMain.ToString());
                    if (DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath, MainFile)) is IFileCache)
                    {
                        hasMain = true;
                    }
                }

                var name = parsed.GetValue("name") is JValue vname?vname.ToString() : "";

                if (!hasMain && parsed.GetValue("module") is JValue moduleV)
                {
                    if (name != "moment")
                    {
                        MainFile = PathUtils.Normalize(moduleV.ToString());
                        if (DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath, MainFile)) is IFileCache)
                        {
                            hasMain = true;
                        }
                    }
                }

                if (parsed.GetValue("typings") is JValue typingsV)
                {
                    TypesMainFile = PathUtils.Normalize(typingsV.ToString());
                }

                if (!hasMain)
                {
                    if (parsed.GetValue("main") is JValue mainV2)
                    {
                        MainFile = PathUtils.Normalize(mainV2.ToString());
                        if (PathUtils.GetExtension(MainFile).IsEmpty)
                        {
                            MainFile += ".js";
                        }
                    }
                    else
                    {
                        MainFile = "index.js";
                    }

                    if (name == "@stomp/stompjs")
                    {
                        MainFile = "esm6/index.js";
                        hasMain  = true;
                    }

                    if (DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath, PathUtils.ChangeExtension(MainFile, "ts")))
                        is IFileCache fileAsTs)
                    {
                        MainFile      = PathUtils.ChangeExtension(MainFile, "ts");
                        TypesMainFile = null;
                        hasMain       = true;
                    }
                    else
                    {
                        fileAsTs = DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath,
                                                                       PathUtils.ChangeExtension(MainFile, "tsx"))) as IFileCache;
                        if (fileAsTs != null)
                        {
                            MainFile      = PathUtils.ChangeExtension(MainFile, "tsx");
                            TypesMainFile = null;
                            hasMain       = true;
                        }
                    }

                    if (!hasMain)
                    {
                        if (parsed.GetValue("types") is JValue mainV)
                        {
                            TypesMainFile = PathUtils.Normalize(mainV.ToString());
                            hasMain       = true;
                        }
                    }
                }

                if (TypesMainFile == null)
                {
                    TypesMainFile = PathUtils.ChangeExtension(MainFile, "d.ts");
                    if (!IsRootProject &&
                        !(DiskCache.TryGetItem(PathUtils.Join(Owner.FullPath, TypesMainFile)) is IFileCache))
                    {
                        var typesDts = PathUtils.Join(Owner.FullPath, $"../@types/{Owner.Name}/index.d.ts");
                        if (DiskCache.TryGetItem(typesDts) is IFileCache)
                        {
                            TypesMainFile = typesDts;
                        }
                    }
                }

                if (parsed.GetValue("dependencies") is JObject parsedV)
                {
                    foreach (var i in parsedV.Properties())
                    {
                        deps.Add(i.Name);
                    }
                }

                if (parsed.GetValue("devDependencies") is JObject parsedV2)
                {
                    foreach (var i in parsedV2.Properties())
                    {
                        devdeps.Add(i.Name);
                    }
                }

                PackageJsonChangeId = newChangeId;
                Dependencies        = deps;
                DevDependencies     = devdeps;
                Assets = ParseBobrilAssets(parsed);

                if (ProjectOptions == null)
                {
                    return;
                }
                ProjectOptions.FillProjectOptionsFromPackageJson(parsed, Owner);
                if (forbiddenDependencyUpdate || ProjectOptions.DependencyUpdate == DepedencyUpdate.Disabled)
                {
                    return;
                }
                var packageManager = new CurrentNodePackageManager(DiskCache, Logger);
                if (ProjectOptions.DependencyUpdate == DepedencyUpdate.Upgrade)
                {
                    packageManager.UpgradeAll(Owner);
                }
                else
                {
                    packageManager.Install(Owner);
                }

                DiskCache.CheckForTrueChange();
                DiskCache.ResetChange();
            }
            else
            {
                PackageJsonChangeId = -1;
                MainFile            = "index.js";
                Dependencies        = new();
                DevDependencies     = new();
                Assets = null;
                ProjectOptions?.FillProjectOptionsFromPackageJson(null, Owner);
            }
        }