Skip to content

Commit

Permalink
feat(nx): package.json/nx.json/project.json declare dependencies or t…
Browse files Browse the repository at this point in the history
…asks (Nx targets) of a project and should render the project affected if changed
  • Loading branch information
NexZhu committed Dec 22, 2023
1 parent 684b4b2 commit d08e406
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
30 changes: 20 additions & 10 deletions libs/core/src/true-affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,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 @@ -73,17 +73,26 @@ 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 = changedFiles
.filter(
({ filePath }) =>
!filePath.match(/.*\.(ts|js)x?$/g) &&
!filePath.endsWith(lockFileName) &&
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) &&
!filePath.endsWith(lockFileName) &&
project.getSourceFile(resolve(cwd, filePath)) == null
}
)
.flatMap(({ filePath: changedFilePath }) =>
findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths)
.flatMap(({ filePath: changedFilePath }) => findNonSourceAffectedFiles(cwd, changedFilePath, ignoredPaths)
);

let changedFilesByLockfile: ChangedFiles[] = [];
Expand Down Expand Up @@ -114,7 +123,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(pkg => affectedPackages.add(pkg))

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 @@ -12,7 +12,18 @@ 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;
};

0 comments on commit d08e406

Please sign in to comment.