Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nx): Some files declare dependencies or tasks (Nx targets) of a project and should render the project affected if changed #27

Closed
Closed
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
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;
};
Loading