Skip to content

Commit

Permalink
feat(nx): package.json/project.json declare dependencies or tasks (Nx…
Browse files Browse the repository at this point in the history
… targets) of a project and should render the project affected if changed
  • Loading branch information
NexZhu committed Dec 21, 2023
1 parent a25470c commit 40cf9b0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
28 changes: 19 additions & 9 deletions libs/core/src/true-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export const trueAffected = async ({
...(rootTsConfig == null
? {}
: {
tsConfigFilePath: resolve(cwd, rootTsConfig),
skipAddingFilesFromTsConfig: true,
}),
tsConfigFilePath: resolve(cwd, rootTsConfig),
skipAddingFilesFromTsConfig: true,
}),
});

const implicitDeps = (
Expand Down Expand Up @@ -71,16 +71,25 @@ export const trueAffected = async ({
({ filePath }) => project.getSourceFile(resolve(cwd, filePath)) != null
);

// These files declare dependencies or tasks (Nx targets) of a project and should render the project affected if changed
const depAndTaskDeclarationFiles = ['package.json', 'nx.json', 'project.json']

const ignoredPaths = ['./node_modules', './dist', './.git'];

const affectedPackages = new Set<string>();

const nonSourceChangedFiles: GetChangedFiles[] = changedFiles
.filter(
({ filePath }) =>
!filePath.match(/.*\.(ts|js)x?$/g) &&
project.getSourceFile(resolve(cwd, filePath)) == null
function({ filePath }) {
if (depAndTaskDeclarationFiles.includes(filePath.substring(filePath.lastIndexOf('/') + 1))) {
const pkg = getPackageNameByPath(resolve(cwd, filePath), projects, true);
if (pkg) affectedPackages.add(pkg);
}
return !filePath.match(/.*\.(ts|js)x?$/g) &&
project.getSourceFile(resolve(cwd, filePath)) == null
}
)
.flatMap(({ filePath: changedFilePath }) =>
findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths)
.flatMap(({ filePath: changedFilePath }) => findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths)
);

const filteredChangedFiles = [
Expand All @@ -99,7 +108,8 @@ export const trueAffected = async ({
.map(({ filePath }) => getPackageNameByPath(filePath, projects))
.filter((v): v is string => v != null);

const affectedPackages = new Set<string>(changedIncludedFilesPackages);
changedIncludedFilesPackages.forEach(affectedPackages.add)

const visitedIdentifiers = new Map<string, string[]>();

const findReferencesLibs = (node: Node<ts.Node>) => {
Expand Down
15 changes: 13 additions & 2 deletions libs/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ export const findRootNode = (

export const getPackageNameByPath = (
path: string,
projects: TrueAffectedProject[]
projects: TrueAffectedProject[],
// Search files in the project root as well
includesRoot = false,
): string | undefined => {
return projects.find(({ sourceRoot }) => path.includes(sourceRoot))?.name;
return projects.map(({ name, sourceRoot }) => ({
name,
root: includesRoot ? sourceRoot.substring(0, sourceRoot.lastIndexOf("/")) : sourceRoot
}))
// In case of nested project paths (for example when there's a root project.json):
// sort the paths from the longest to the shortest so the sub-directories come before their parent directories
.sort((a, b) => b.root.length - a.root.length)
.find(
({ root }) => path.includes(root)
)?.name;
};

export function findNonSourceAffectedFiles(
Expand Down

0 comments on commit 40cf9b0

Please sign in to comment.