Skip to content

Commit

Permalink
fix(nx-dev): skip docs for private package (#17843)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi authored Jun 30, 2023
1 parent 88c655f commit 0608318
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 45 deletions.
34 changes: 33 additions & 1 deletion scripts/documentation/internal-link-checker.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { workspaceRoot } from '@nx/devkit';
import { XMLParser } from 'fast-xml-parser';
import { existsSync, readJSONSync } from 'fs-extra';
import * as glob from 'glob';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
Expand Down Expand Up @@ -82,6 +83,33 @@ function readSiteMapLinks(filePath: string): string[] {
return sitemap.urlset.url.map((obj) => obj.loc);
}

/**
* This function checks if a link is for a private package.
* When link is for a private package, it is not included in the sitemap.
* However, some shared docs might be written for this private package during development.
* @param link e.g. /packages/vite/generators/configuration
* @returns true if the link is for a private package or NODE_ENV is not development, false otherwise.
*/
function checkLinkIsForPrivatePackage(link: string) {
// skip this check in dev mode
if (process.env.NODE_ENV === 'development') {
return false;
}
const pathSegments = link.split('/').filter(Boolean);
if (pathSegments[0] === 'packages') {
const packageJsonPath = join(
workspaceRoot,
pathSegments[0],
pathSegments[1],
'package.json'
);
if (existsSync(packageJsonPath)) {
return readJSONSync(packageJsonPath).private ?? false;
}
}
return false;
}

// Main
const documentLinks = extractAllLinks(join(workspaceRoot, 'docs'));
const sitemapLinks = readSiteMapIndex(
Expand All @@ -91,8 +119,12 @@ const sitemapLinks = readSiteMapIndex(
const errors: Array<{ file: string; link: string }> = [];
for (let file in documentLinks) {
for (let link of documentLinks[file]) {
if (!sitemapLinks.includes(['https://nx.dev', link].join('')))
if (
!sitemapLinks.includes(['https://nx.dev', link].join('')) &&
!checkLinkIsForPrivatePackage(link)
) {
errors.push({ file, link });
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@nx/nx-dev/data-access-packages';
import { NxSchema, PackageMetadata } from '@nx/nx-dev/models-package';
import { generateJsonFile, generateMarkdownFile } from '../utils';
import { getPackageMetadataList } from './package-metadata';
import { findPackageMetadataList } from './package-metadata';
import { schemaResolver } from './schema.resolver';

function processSchemaData(data: NxSchema, path: string): NxSchema {
Expand All @@ -37,7 +37,7 @@ export function generatePackageSchemas(): Promise<void[]> {
console.log(`${chalk.blue('i')} Generating Package Schemas`);
const absoluteRoot = resolve(join(__dirname, '../../../'));

const packages = getPackageMetadataList(absoluteRoot, 'packages', 'docs').map(
const packages = findPackageMetadataList(absoluteRoot, 'packages').map(
(packageMetadata) => {
const getCurrentSchemaPath = pathResolver(absoluteRoot);
if (!!packageMetadata.executors.length) {
Expand Down Expand Up @@ -116,7 +116,7 @@ export function generatePackageSchemas(): Promise<void[]> {

const outputPath: string = join(absoluteRoot, 'docs', 'generated');
const outputPackagesPath: string = join(outputPath, 'packages');
const fileGenerationPromises = [];
const fileGenerationPromises: Promise<void>[] = [];

// Generates all documents and schemas into their own directories per packages.
packages.forEach((p) => {
Expand Down
92 changes: 51 additions & 41 deletions scripts/documentation/package-schemas/package-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,18 @@ function getSchemaList(

/**
* Generate the package metadata by exploring the directory path given.
* This function will look for all the packages in the given directory under packagesDirectory.
* It will then look for the package.json file and read the description and name of the package.
* It will also look for the generators.json and executors.json files and read the schema of each generator and executor.
* It will also look for the documents.json file and read the documents of each package.
* If the package is private and NODE_ENV is not development, it will not be included in the metadata.
* @param absoluteRoot
* @param packagesDirectory
* @param documentationDirectory
* @returns Configuration
*/
export function getPackageMetadataList(
export function findPackageMetadataList(
absoluteRoot: string,
packagesDirectory: string = 'packages',
documentationDirectory: string = 'docs'
packagesDirectory: string = 'packages'
): PackageData[] {
const packagesDir = resolve(join(absoluteRoot, packagesDirectory));

Expand All @@ -114,52 +117,59 @@ export function getPackageMetadataList(
.itemList.map((item) => convertToDocumentMetadata(item));

// Do not use map.json, but add a documentation property on the package.json directly that can be easily resolved
return sync(`${packagesDir}/*`, { ignore: [`${packagesDir}/cli`] }).map(
(folderPath): PackageData => {
return sync(`${packagesDir}/*`, { ignore: [`${packagesDir}/cli`] })
.map((folderPath: string): PackageData => {
const folderName = folderPath.substring(packagesDir.length + 1);
const relativeFolderPath = folderPath.replace(absoluteRoot, '');
const packageJson = readJsonSync(
join(folderPath, 'package.json'),
'utf8'
);
const isPrivate =
packageJson.private && process.env.NODE_ENV !== 'development'; // skip this check in dev mode
const hasDocumentation = additionalApiReferences.find(
(pkg) => pkg.id === folderName
);

return {
githubRoot: 'https://github.com/nrwl/nx/blob/master',
name: folderName,
packageName: packageJson.name,
description: packageJson.description,
root: relativeFolderPath,
source: join(relativeFolderPath, '/src'),
documents: !!hasDocumentation
? hasDocumentation.itemList.map((item) => ({
...item,
path: item.path,
file: item.file,
content: readFileSync(join('docs', item.file + '.md'), 'utf8'),
}))
: [],
generators: getSchemaList(
{
absoluteRoot,
folderName,
return isPrivate
? null
: {
githubRoot: 'https://github.com/nrwl/nx/blob/master',
name: folderName,
packageName: packageJson.name,
description: packageJson.description,
root: relativeFolderPath,
},
'generators.json',
['generators']
),
executors: getSchemaList(
{
absoluteRoot,
folderName,
root: relativeFolderPath,
},
'executors.json',
['executors', 'builders']
),
};
}
);
source: join(relativeFolderPath, '/src'),
documents: !!hasDocumentation
? hasDocumentation.itemList.map((item) => ({
...item,
path: item.path,
file: item.file,
content: readFileSync(
join('docs', item.file + '.md'),
'utf8'
),
}))
: [],
generators: getSchemaList(
{
absoluteRoot,
folderName,
root: relativeFolderPath,
},
'generators.json',
['generators']
),
executors: getSchemaList(
{
absoluteRoot,
folderName,
root: relativeFolderPath,
},
'executors.json',
['executors', 'builders']
),
};
})
.filter(Boolean);
}

1 comment on commit 0608318

@vercel
Copy link

@vercel vercel bot commented on 0608318 Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.