Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 31 additions & 39 deletions packages/core/update/Update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,56 +96,35 @@ export async function updateWorkspace(rootPath: string): Promise<boolean> {
break;
case "react":
case "webcomponents":
// the root package.json is always updated, so its own sources are too
workspaces.push(path.join(rootPath, "src"));

// fs.glob returns files only, so workspace patterns are not expanded to folders here;
// they stay in the search root and are matched together with the file patterns below
if (pkgJSON.workspaces) {
pkgJSON.workspaces.forEach((w: string) => {
// Handle workspace patterns that may contain globs
if (w.includes("*")) {
// Use glob to expand the workspace pattern
const expandedWorkspaces = fs.glob(rootPath, w);
expandedWorkspaces.forEach(expandedWorkspace => {
// Only add if it's a directory
if (fs.directoryExists(expandedWorkspace)) {
workspaces.push(expandedWorkspace);
}
});
} else {
// Direct workspace path
const workspacePath = path.join(rootPath, w);
if (fs.directoryExists(workspacePath)) {
workspaces.push(workspacePath);
}
const workspacePath = path.join(rootPath, w);
if (w.includes("*") || fs.directoryExists(workspacePath)) {
workspaces.push(workspacePath);
}
});
} else {
// For React and WebComponents projects without explicit workspaces,
// check for common project patterns like projects/* in addition to src/
workspaces.push(path.join(rootPath, "src"));

// Check for projects/* pattern common in React demo/example repositories
// Only check if projects directory exists to avoid unnecessary glob calls
const projectsDir = path.join(rootPath, "projects");
if (fs.directoryExists(projectsDir)) {
const projectsPattern = "projects/*";
const projectsWorkspaces = fs.glob(rootPath, projectsPattern);
projectsWorkspaces.forEach(projectPath => {
if (fs.directoryExists(projectPath)) {
workspaces.push(projectPath);
}
});
}
} else if (fs.directoryExists(path.join(rootPath, "projects"))) {
// projects/* is common in React and Web Components demo/example repositories
workspaces.push(path.join(rootPath, "projects", "*"));
}
break;
default:
break;
}

// once all workspaces are gotten, get all files for all workspaces
const ignored = ['node_modules', 'dist'];
for (const workspace of workspaces) {
logicFiles.push(...fs.glob(workspace, `**/*.${logicFilesExtension}`));
logicFiles.push(...fs.glob(workspace, `**/*.${logicFilesExtension}`, ignored));
for (const styleExtension of styleExtensions) {
styleFiles.push(...fs.glob(workspace, `**/*.${styleExtension}`));
styleFiles.push(...fs.glob(workspace, `**/*.${styleExtension}`, ignored));
Comment thread
dkalinovInfra marked this conversation as resolved.
Resolved
}
pkgJsonFiles.push(...fs.glob(workspace, `**/package.json`));
pkgJsonFiles.push(...fs.glob(workspace, `**/package.json`, ignored));
Comment thread
dkalinovInfra marked this conversation as resolved.
Resolved
Comment thread
dkalinovInfra marked this conversation as resolved.
Resolved
}

// For React and WebComponents projects, also include vite.config.ts files
Expand All @@ -156,11 +135,21 @@ export async function updateWorkspace(rootPath: string): Promise<boolean> {
}
}

updateFileImports(logicFiles, styleFiles, upgradeable, fs);
if (shouldUpgradeHTML) {
updateHTMLImports(htmlFiles, upgradeable, fs);
for (const workspace of workspaces) {
const filePaths = fs.glob(workspace, `**/*.html`, ignored);
if (filePaths && filePaths.length > 0) {
htmlFiles.push(...filePaths);
}
}
}

// overlapping workspace patterns can match the same file more than once
updateFileImports([...new Set(logicFiles)], [...new Set(styleFiles)], upgradeable, fs);
if (shouldUpgradeHTML) {
updateHTMLImports([...new Set(htmlFiles)], upgradeable, fs);
}
updatePackageJsonFiles(pkgJsonFiles, upgradeable, fs);
updatePackageJsonFiles([...new Set(pkgJsonFiles)], upgradeable, fs);
createNpmrc(rootPath, fs);
updateWorkflows(fs);

Expand Down Expand Up @@ -241,6 +230,9 @@ function updatePackageJsonFiles(
for (const pkgJsonFile of pkgJsonFiles) {
const fileString = fs.readFile(pkgJsonFile);
const pkgJSON = JSON.parse(fileString);
if (!pkgJSON.dependencies) {
continue;
}
for (const packageDef of packageDefs) {
if (!pkgJSON.dependencies.hasOwnProperty(packageDef.trial)) {
continue;
Expand Down
9 changes: 7 additions & 2 deletions packages/core/util/FileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ export class FsFileSystem implements IFileSystem {
}
}

public glob(dirPath: string, pattern: string): string[] {
public glob(dirPath: string, pattern: string, ignorePatterns?: string[]): string[] {
// NB!: glob 8+ patterns use `\` as escape only, so ensure posix-style:
const globPattern = path.join(dirPath, pattern).replace(/\\/g, "/");
return glob.sync(globPattern, { nodir: true });
// anchored to dirPath, so a project that itself lives under e.g. a `dist` folder is not ignored;
// when dirPath has a wildcard the anchor is its parent, since the wildcard can match an ignored folder
const wildcard = dirPath.indexOf("*");
const ignoreRoot = wildcard < 0 ? dirPath : path.dirname(dirPath.slice(0, wildcard) + "x");
const ignore = ignorePatterns?.map(p => path.join(ignoreRoot, "**", p, "**").replace(/\\/g, "/"));
return glob.sync(globPattern, { nodir: true, ignore });
}
}
22 changes: 22 additions & 0 deletions packages/ng-schematics/src/utils/NgFileSystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ describe("NgTreeFileSystem", () => {
expect(results.some(r => r.includes("node_modules"))).toBeFalse();
});

it("should return files from every level when ignorePatterns are passed", () => {
tree.create("/src/main.ts", "");
tree.create("/src/node_modules/lib/index.ts", "");

const results = fs.glob("/src", "**/*.ts", ["node_modules"]);
expect(results.sort()).toEqual([
"/src/app/app.component.ts",
"/src/app/app.module.ts",
"/src/app/shared/shared.component.ts",
"/src/environments/environment.prod.ts",
"/src/environments/environment.ts",
"/src/main.ts"
]);
});

it("should match the pattern relative to dirPath when ignorePatterns are passed", () => {
tree.create("/package.json", "");
tree.create("/src/package.json", "");

expect(fs.glob("", "package.json", ["node_modules"])).toEqual(["/package.json"]);
});

it("should return an empty array when dirPath does not exist", () => {
const results = fs.glob("/nonexistent", "**/*.ts");
expect(results).toEqual([]);
Expand Down
21 changes: 9 additions & 12 deletions packages/ng-schematics/src/utils/NgFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ export class NgTreeFileSystem implements IFileSystem {
};

if (ignorePatterns?.length) {
const recurse = (dir: DirEntry): void => {
for (const subdirPath of dir.subdirs) {
const recurse = (current: DirEntry): void => {
for (const file of current.subfiles) {
const filePath = path.posix.join(current.path, file);
if (minimatch(path.posix.relative(dir.path, filePath), pattern)) {
entries.push(filePath);
}
}
for (const subdirPath of current.subdirs) {
if (ignorePatterns.every(p => !minimatch(subdirPath, p))) {
const subDir = dir.dir(subdirPath);
if (subDir.subdirs.length) {
recurse(subDir);
continue;
}
for (const file of dir.subfiles) {
if (minimatch(file, pattern) && ignorePatterns.every(p => !minimatch(file, p))) {
entries.push(path.posix.normalize(`${dir.path}/${file}`));
}
}
recurse(current.dir(subdirPath));
}
}
};
Expand Down
Loading