Skip to content

Commit

Permalink
feat(schematics): update-package logic added
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorSlavov committed May 21, 2020
1 parent 1904a86 commit 197b94e
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 53 deletions.
5 changes: 4 additions & 1 deletion packages/cli/lib/PromptSession.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { BasePromptSession, GoogleAnalytics, ProjectConfig, ProjectLibrary, PromptTaskContext, Task, Util, PackageManager } from "@igniteui/cli-core";
import {
BasePromptSession, GoogleAnalytics, PackageManager, ProjectConfig,
ProjectLibrary, PromptTaskContext, Task, Util
} from "@igniteui/cli-core";
import * as inquirer from "inquirer";
import * as path from "path";
import { default as add } from "./commands/add";
Expand Down
9 changes: 6 additions & 3 deletions packages/cli/lib/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { AddTemplateArgs, GoogleAnalytics, ProjectConfig, ProjectLibrary, Template, Util, PackageManager } from "@igniteui/cli-core";
import {
AddTemplateArgs, GoogleAnalytics, PackageManager,
ProjectConfig, ProjectLibrary, Template, Util
} from "@igniteui/cli-core";
import { TemplateManager } from "../TemplateManager";
import { PromptSession } from "./../PromptSession";

Expand Down Expand Up @@ -40,7 +43,7 @@ command = {
}
},
check: argv => {
if ((!argv.name && argv.template) || (argv.name && !argv.template)) {
if ((!argv.name && argv.template) || (argv.name && !argv.template)) {
return false;
}
return true;
Expand All @@ -55,7 +58,7 @@ command = {
Util.error("Add command is supported only on existing project created with igniteui-cli", "red");
return;
}
const config = ProjectConfig.getConfig();
const config = ProjectConfig.getConfig();
if (config.project.isShowcase) {
Util.error("Showcases and quickstart projects don't support the add command", "red");
return;
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/commands/build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GoogleAnalytics, ProjectConfig, Util, PackageManager } from "@igniteui/cli-core";
import { GoogleAnalytics, PackageManager, ProjectConfig, Util } from "@igniteui/cli-core";
import * as fs from "fs-extra";
import * as path from "path";
import { TemplateManager } from "../TemplateManager";
Expand Down Expand Up @@ -50,7 +50,7 @@ command = {
fs.mkdirSync("./themes");
const source = path.join(config.project.igniteuiSource, "/css/themes/", config.project.theme.split(".")[0]);
const destination = path.join(config.project.sourceRoot, "themes");
fs.copySync(source, destination, {recursive: true});
fs.copySync(source, destination, { recursive: true });
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/lib/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GoogleAnalytics, ProjectConfig, ProjectLibrary, Util, PackageManager } from "@igniteui/cli-core";
import { GoogleAnalytics, PackageManager, ProjectConfig, ProjectLibrary, Util } from "@igniteui/cli-core";
import * as path from "path";
import { TemplateManager } from "../TemplateManager";
import { PromptSession } from "./../PromptSession";
Expand Down Expand Up @@ -132,7 +132,7 @@ command = {
const config = projTemplate.generateConfig(argv.name, theme);
for (const templatePath of projTemplate.templatePaths) {
await Util.processTemplates(templatePath, path.join(process.cwd(), argv.name),
config, projTemplate.delimiters, false);
config, projTemplate.delimiters, false);
}

Util.log(Util.greenCheck() + " Project Created");
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/migrations/update-5_0_0/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class NgTreeFileSystem implements IFileSystem {
public directoryExists(dirPath: string): boolean {
return this.tree.exists(dirPath);
}

public glob(dirPath: string, pattern: string): string[] {
return [];
}
}

export function setVirtual(tree: Tree) {
Expand Down
9 changes: 5 additions & 4 deletions packages/core/packages/PackageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export class PackageManager {
if (installNow) {
const ossVersion = this.getPackageJSON().dependencies[this.ossPackage];
const version = ossVersion ? `@"${ossVersion}"` : "";
if (this.ensureRegistryUser(config) && this.addPackage(this.fullPackage + version, verbose)) {
const errorMsg = "Something went wrong, " +
"please follow the steps in this guide: https://www.igniteui.com/help/using-ignite-ui-npm-packages";
if (this.ensureRegistryUser(config, errorMsg) && this.addPackage(this.fullPackage + version, verbose)) {
if (ossVersion) {
// TODO: Check if OSS package uninstalled successfully?
this.removePackage(this.ossPackage, verbose);
Expand Down Expand Up @@ -190,7 +192,7 @@ export class PackageManager {
}
}

public static ensureRegistryUser(config: Config): boolean {
public static ensureRegistryUser(config: Config, message: string): boolean {
const fullPackageRegistry = config.igPackageRegistry;
try {
// tslint:disable-next-line:object-literal-sort-keys
Expand Down Expand Up @@ -223,8 +225,7 @@ export class PackageManager {
return false;
}
} else {
Util.log("Something went wrong, " +
"please follow the steps in this guide: https://www.igniteui.com/help/using-ignite-ui-npm-packages", "red");
Util.log(message, "red");
return false;
}
}
Expand Down
36 changes: 0 additions & 36 deletions packages/core/util/Update.ts

This file was deleted.

82 changes: 82 additions & 0 deletions packages/igx-templates/Update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { App, FS_TOKEN, IFileSystem, PackageManager, ProjectConfig, Util } from "@igniteui/cli-core";
import * as path from "path";
import { FEED_PACKAGE, NPM_PACKAGE, resolvePackage } from "./package-resolve";

const styleExtension = ["css", "scss", "sass"];
const styleExpression = String.raw`(node_modules\/|~)${NPM_PACKAGE}(\/)`;
const logicExpression = String.raw`(from ["'])${NPM_PACKAGE}(["'])`;

export async function updateWorkspace(): Promise<boolean> {
const fs: IFileSystem = App.container.get(FS_TOKEN);

const fileString = fs.readFile("package.json");
if (resolvePackage() === FEED_PACKAGE) {
return false;
}
if (fs.directoryExists(path.join(App.workDir, "node_modules", "@infragistics"))) {
Util.log("@infragistics module already exists. Nothing to do here.");
return false;
}

// check for registry user in npm
const config = ProjectConfig.localConfig();
if (fileString) {
const pkgJSON = JSON.parse(fileString);
const errorMsg = "Something went wrong, " +
"please follow the steps in this guide: https://www.infragistics.com/products/ignite-ui-angular/angular/components/general/ignite-ui-licensing.html";
if (PackageManager.ensureRegistryUser(config, errorMsg)) {
pkgJSON.dependencies[FEED_PACKAGE] = pkgJSON.dependencies[NPM_PACKAGE];
delete pkgJSON.dependencies[NPM_PACKAGE];
fs.writeFile("package.json", JSON.stringify(pkgJSON, null, 2));
} else {
return false;
}
} else {
Util.log("Package.json not found!");
return false;
}

// get all workspace roots
const workspaces = [];
let workspaceConfig = null;
if (fs.fileExists("angular.json")) {
workspaceConfig = JSON.parse(fs.readFile("angular.json"));
} else {
Util.log("No angular.json file found! Aborting.");
return false;
}
for (const entry of Object.keys(workspaceConfig.projects)) {
workspaces.push(workspaceConfig.projects[entry].sourceRoot);
}
// once all workspace are gotten, get all files for all workspaces
const logicFiles = [];
const styleFiles = [];
for (const workspace of workspaces) {
logicFiles.push(...fs.glob(workspace, "**/*.component.ts"));
for (const extension of styleExtension) {
styleFiles.push(...fs.glob(workspace, `**/*.${extension}`));
}
}
styleFiles.push("angular.json");

for (const file of logicFiles) {
rewriteFile(fs, file, logicExpression);
}
for (const file of styleFiles) {
rewriteFile(fs, file, styleExpression);
}

return true;
}

function rewriteFile(fs: IFileSystem, fileLocation: string, regexString: string) {
let fileContent = fs.readFile(fileLocation);
if (fileContent.match(new RegExp(regexString))) {
fileContent = transformFile(fileContent, regexString);
fs.writeFile(fileLocation, fileContent);
}
}
function transformFile(fileContent: string, regexString: string) {
fileContent = fileContent.replace(new RegExp(regexString, "g"), `$1${FEED_PACKAGE}$2`);
return fileContent;
}
4 changes: 4 additions & 0 deletions packages/igx-templates/igx-ts/projects/_base/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ControlExtraConfiguration, ProjectTemplate, Util } from "@igniteui/cli-core";
import * as path from "path";
import { updateWorkspace } from "../../../Update";

export class BaseIgxProject implements ProjectTemplate {
public id: string = "base";
Expand Down Expand Up @@ -47,6 +48,9 @@ $app-palette: igx-palette($primary, $secondary);
public upgradeIgniteUIPackage(projectPath: string, packagePath: string): void {
// noop
}
public async upgradeIgniteUIPackages(projectPath: string, packagePath: string): Promise<boolean> {
return updateWorkspace();
}
public getExtraConfiguration(): ControlExtraConfiguration[] {
return [];
}
Expand Down
1 change: 1 addition & 0 deletions packages/igx-templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import * as projLib from "./igx-ts";

export * from "./package-resolve";
export * from "./IgniteUIForAngularTemplate";
export * from "./Update";
export default projLib;
4 changes: 3 additions & 1 deletion packages/igx-templates/package-resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ class MockFileSystem implements IFileSystem {
public directoryExists(dirPath: string): boolean {
throw new Error("Method not implemented.");
}

public glob(dirPath: string, pattern: string): string[] {
throw new Error("Method not implemented.");
}
}

describe("cli-config schematic", () => {
Expand Down
20 changes: 17 additions & 3 deletions packages/ng-schematics/src/update/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
import { updateWorkspace } from "@igniteui/cli-core";
import { NodePackageInstallTask } from "@angular-devkit/schematics/tasks";
import { App, GoogleAnalytics, ProjectConfig } from "@igniteui/cli-core";
import { defer } from "rxjs";
import { SchematicsTemplateManager } from "../SchematicsTemplateManager";
import { setVirtual } from "../utils/NgFileSystem";

export default function update(_options: any): Rule {
return (tree: Tree, _context: SchematicContext) => {
return (tree: Tree, context: SchematicContext) => {
App.initialize("angular-cli");
GoogleAnalytics.post({
t: "screenview",
cd: "Update"
});
const templateManager = new SchematicsTemplateManager();
const config = ProjectConfig.getConfig();
const library = templateManager.getProjectLibrary(config.project.framework, config.project.projectType);
const project = library.getProject(config.project.projectTemplate);
setVirtual(tree);
return defer(async () => {
await updateWorkspace();
const success = await (project as any).upgradeIgniteUIPackages();
if (success) {
context.addTask(new NodePackageInstallTask());
}
return tree;
});
};
Expand Down
2 changes: 1 addition & 1 deletion packages/ng-schematics/src/utils/NgFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class NgTreeFileSystem implements IFileSystem {
const entries: string[] = [];
pattern = pattern.split("**/*").pop() || pattern;

dir.visit((fullPath, entry) => {
dir.visit((_fullPath, entry) => {
if (entry?.path.endsWith(pattern)) {
entries.push(entry.path);
}
Expand Down

0 comments on commit 197b94e

Please sign in to comment.