Skip to content

Commit

Permalink
fix(package-manager): manually update pckgJSON instead of letting npm…
Browse files Browse the repository at this point in the history
… do it, #751

Co-authored-by: Damyan Petev <damyan.petev@gmail.com>
  • Loading branch information
ViktorSlavov and damyanpetev committed Jun 1, 2020
1 parent cd9cab5 commit 6fa4c0c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
27 changes: 20 additions & 7 deletions packages/core/packages/PackageManager.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { exec, spawnSync } from "child_process";
import * as path from "path";
import { TemplateManager } from "../../cli/lib/TemplateManager";
import { Config, ProjectTemplate } from "../types";
import { ProjectConfig, Util } from "../util";
import { Config, FS_TOKEN, IFileSystem, ProjectTemplate } from "../types";
import { App, ProjectConfig, Util } from "../util";

import componentsConfig = require("./components");

export class PackageManager {
private static ossPackage: string = "ignite-ui";
private static fullPackage: string = "@infragistics/ignite-ui-full";
private static get fs(): IFileSystem {
return App.container.get<IFileSystem>(FS_TOKEN);
}

private static get jsonPath(): string {
return path.join(process.cwd(), "package.json");
}

private static installQueue: Array<Promise<{ packageName, error, stdout, stderr }>> = [];

Expand Down Expand Up @@ -153,11 +160,17 @@ export class PackageManager {
}

public static async queuePackage(packageName: string, verbose = false) {
const command = this.getInstallCommand(this.getManager(), packageName);
const packName = packageName.split(/@[^\/]+$/)[0];
if (this.getPackageJSON().dependencies[packName] || this.installQueue.find(x => x["packageName"] === packName)) {
const command = this.getInstallCommand(this.getManager(), packageName).replace("--save", "--no-save");
const [ packName, version ] = packageName.split(/@(?=[^\/]+$)/);
const packageJSON = this.getPackageJSON();
if (!packageJSON.dependencies) {
packageJSON.dependencies = {};
}
if (packageJSON.dependencies[packName] || this.installQueue.find(x => x["packageName"] === packName)) {
return;
}
packageJSON.dependencies[packName] = version;
this.fs.writeFile(this.jsonPath, Util.formatPackageJson(packageJSON));
// D.P. Concurrent install runs should be supported
// https://github.com/npm/npm/issues/5948
// https://github.com/npm/npm/issues/2500
Expand Down Expand Up @@ -236,8 +249,8 @@ export class PackageManager {
}

protected static getPackageJSON(): { "dependencies": { [x: string]: string } } {
const filePath = path.join(process.cwd(), "package.json");
return require(filePath);
const content = this.fs.readFile(this.jsonPath);
return JSON.parse(content);
}

private static getInstallCommand(managerCommand: string, packageName: string): string {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ export class Util {
}
}

public static formatPackageJson(json: { dependencies: { [key: string]: string } }, sort = true): string {
if (sort) {
json.dependencies =
Object.keys(json.dependencies)
.sort()
.reduce((result, key) => (result[key] = json.dependencies[key]) && result, {});
}
return JSON.stringify(json, null, 2) + "\n";
}

public static version(filePath?: string): string {
const configuration = require(filePath || "../package.json");
return configuration.version;
Expand Down
28 changes: 19 additions & 9 deletions spec/unit/packageManager-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Config, PackageManager, ProjectConfig, Util } from "@igniteui/cli-core";
import { App, Config, IFileSystem, PackageManager, ProjectConfig, Util } from "@igniteui/cli-core";
import * as cp from "child_process";
import * as path from "path";
import { resetSpy } from "../helpers/utils";
Expand Down Expand Up @@ -31,11 +31,12 @@ describe("Unit - Package Manager", () => {
spyOn(ProjectConfig, "setConfig");
spyOn(PackageManager, "addPackage").and.returnValue(true);
spyOn(path, "join").and.returnValue("fakemodule.json");
spyOn(require("module"), "_load").and.callFake((modulePath: string) => {
if (modulePath === "fakemodule.json") {
return mockRequire;
}
});
const mockFs: Partial<IFileSystem> = {
readFile: jasmine.createSpy().and.returnValue(JSON.stringify(mockRequire)),
writeFile: jasmine.createSpy()
};
// should ignore already installed
spyOn(App.container, "get").and.returnValue(mockFs);
spyOn(Util, "execSync").and.callFake((cmd: string, opts) => {
if (cmd.includes("whoami")) {
throw new Error("");
Expand Down Expand Up @@ -365,7 +366,7 @@ describe("Unit - Package Manager", () => {
expect(Util.log).toHaveBeenCalledTimes(0);
expect(cp.exec).toHaveBeenCalledTimes(1);
expect(cp.exec).toHaveBeenCalledWith(
`npm install test-pack --quiet --save`, {}, jasmine.any(Function));
`npm install test-pack --quiet --no-save`, {}, jasmine.any(Function));
done();
});

Expand All @@ -375,8 +376,12 @@ describe("Unit - Package Manager", () => {
"test-pack": "19.2"
}
};
const mockFs: Partial<IFileSystem> = {
readFile: jasmine.createSpy().and.returnValue(JSON.stringify(mockRequire)),
writeFile: jasmine.createSpy()
};
// should ignore already installed
spyOn(require("module"), "_load").and.returnValue(mockRequire);
spyOn(App.container, "get").and.returnValue(mockFs);
spyOn(Util, "log");
const execSpy = spyOn(cp, "exec");
PackageManager.queuePackage("test-pack");
Expand All @@ -396,8 +401,13 @@ describe("Unit - Package Manager", () => {
dependencies: {}
};
// should ignore already installed
spyOn(require("module"), "_load").and.returnValue(mockRequire);
const mockFs: Partial<IFileSystem> = {
readFile: jasmine.createSpy().and.returnValue(JSON.stringify(mockRequire)),
writeFile: jasmine.createSpy()
};
// spyOn(require("module"), "_load").and.returnValue(mockRequire);
spyOn(Util, "log");
spyOn(App.container, "get").and.returnValue(mockFs);
const execSpy = spyOn(cp, "exec").and.callFake((cmd, opts, callback) => {
setTimeout(() => callback(null, [1], [2]), 20);
});
Expand Down

0 comments on commit 6fa4c0c

Please sign in to comment.