fix(nx-dev): skip docs for private package (#17843)
This commit is contained in:
parent
88c655f5a9
commit
0608318449
@ -1,5 +1,6 @@
|
|||||||
import { workspaceRoot } from '@nx/devkit';
|
import { workspaceRoot } from '@nx/devkit';
|
||||||
import { XMLParser } from 'fast-xml-parser';
|
import { XMLParser } from 'fast-xml-parser';
|
||||||
|
import { existsSync, readJSONSync } from 'fs-extra';
|
||||||
import * as glob from 'glob';
|
import * as glob from 'glob';
|
||||||
import { readFileSync } from 'node:fs';
|
import { readFileSync } from 'node:fs';
|
||||||
import { join } from 'node:path';
|
import { join } from 'node:path';
|
||||||
@ -82,6 +83,33 @@ function readSiteMapLinks(filePath: string): string[] {
|
|||||||
return sitemap.urlset.url.map((obj) => obj.loc);
|
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
|
// Main
|
||||||
const documentLinks = extractAllLinks(join(workspaceRoot, 'docs'));
|
const documentLinks = extractAllLinks(join(workspaceRoot, 'docs'));
|
||||||
const sitemapLinks = readSiteMapIndex(
|
const sitemapLinks = readSiteMapIndex(
|
||||||
@ -91,8 +119,12 @@ const sitemapLinks = readSiteMapIndex(
|
|||||||
const errors: Array<{ file: string; link: string }> = [];
|
const errors: Array<{ file: string; link: string }> = [];
|
||||||
for (let file in documentLinks) {
|
for (let file in documentLinks) {
|
||||||
for (let link of documentLinks[file]) {
|
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 });
|
errors.push({ file, link });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import {
|
|||||||
} from '@nx/nx-dev/data-access-packages';
|
} from '@nx/nx-dev/data-access-packages';
|
||||||
import { NxSchema, PackageMetadata } from '@nx/nx-dev/models-package';
|
import { NxSchema, PackageMetadata } from '@nx/nx-dev/models-package';
|
||||||
import { generateJsonFile, generateMarkdownFile } from '../utils';
|
import { generateJsonFile, generateMarkdownFile } from '../utils';
|
||||||
import { getPackageMetadataList } from './package-metadata';
|
import { findPackageMetadataList } from './package-metadata';
|
||||||
import { schemaResolver } from './schema.resolver';
|
import { schemaResolver } from './schema.resolver';
|
||||||
|
|
||||||
function processSchemaData(data: NxSchema, path: string): NxSchema {
|
function processSchemaData(data: NxSchema, path: string): NxSchema {
|
||||||
@ -37,7 +37,7 @@ export function generatePackageSchemas(): Promise<void[]> {
|
|||||||
console.log(`${chalk.blue('i')} Generating Package Schemas`);
|
console.log(`${chalk.blue('i')} Generating Package Schemas`);
|
||||||
const absoluteRoot = resolve(join(__dirname, '../../../'));
|
const absoluteRoot = resolve(join(__dirname, '../../../'));
|
||||||
|
|
||||||
const packages = getPackageMetadataList(absoluteRoot, 'packages', 'docs').map(
|
const packages = findPackageMetadataList(absoluteRoot, 'packages').map(
|
||||||
(packageMetadata) => {
|
(packageMetadata) => {
|
||||||
const getCurrentSchemaPath = pathResolver(absoluteRoot);
|
const getCurrentSchemaPath = pathResolver(absoluteRoot);
|
||||||
if (!!packageMetadata.executors.length) {
|
if (!!packageMetadata.executors.length) {
|
||||||
@ -116,7 +116,7 @@ export function generatePackageSchemas(): Promise<void[]> {
|
|||||||
|
|
||||||
const outputPath: string = join(absoluteRoot, 'docs', 'generated');
|
const outputPath: string = join(absoluteRoot, 'docs', 'generated');
|
||||||
const outputPackagesPath: string = join(outputPath, 'packages');
|
const outputPackagesPath: string = join(outputPath, 'packages');
|
||||||
const fileGenerationPromises = [];
|
const fileGenerationPromises: Promise<void>[] = [];
|
||||||
|
|
||||||
// Generates all documents and schemas into their own directories per packages.
|
// Generates all documents and schemas into their own directories per packages.
|
||||||
packages.forEach((p) => {
|
packages.forEach((p) => {
|
||||||
|
|||||||
@ -94,15 +94,18 @@ function getSchemaList(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate the package metadata by exploring the directory path given.
|
* 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 absoluteRoot
|
||||||
* @param packagesDirectory
|
* @param packagesDirectory
|
||||||
* @param documentationDirectory
|
|
||||||
* @returns Configuration
|
* @returns Configuration
|
||||||
*/
|
*/
|
||||||
export function getPackageMetadataList(
|
export function findPackageMetadataList(
|
||||||
absoluteRoot: string,
|
absoluteRoot: string,
|
||||||
packagesDirectory: string = 'packages',
|
packagesDirectory: string = 'packages'
|
||||||
documentationDirectory: string = 'docs'
|
|
||||||
): PackageData[] {
|
): PackageData[] {
|
||||||
const packagesDir = resolve(join(absoluteRoot, packagesDirectory));
|
const packagesDir = resolve(join(absoluteRoot, packagesDirectory));
|
||||||
|
|
||||||
@ -114,52 +117,59 @@ export function getPackageMetadataList(
|
|||||||
.itemList.map((item) => convertToDocumentMetadata(item));
|
.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
|
// 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(
|
return sync(`${packagesDir}/*`, { ignore: [`${packagesDir}/cli`] })
|
||||||
(folderPath): PackageData => {
|
.map((folderPath: string): PackageData => {
|
||||||
const folderName = folderPath.substring(packagesDir.length + 1);
|
const folderName = folderPath.substring(packagesDir.length + 1);
|
||||||
const relativeFolderPath = folderPath.replace(absoluteRoot, '');
|
const relativeFolderPath = folderPath.replace(absoluteRoot, '');
|
||||||
const packageJson = readJsonSync(
|
const packageJson = readJsonSync(
|
||||||
join(folderPath, 'package.json'),
|
join(folderPath, 'package.json'),
|
||||||
'utf8'
|
'utf8'
|
||||||
);
|
);
|
||||||
|
const isPrivate =
|
||||||
|
packageJson.private && process.env.NODE_ENV !== 'development'; // skip this check in dev mode
|
||||||
const hasDocumentation = additionalApiReferences.find(
|
const hasDocumentation = additionalApiReferences.find(
|
||||||
(pkg) => pkg.id === folderName
|
(pkg) => pkg.id === folderName
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return isPrivate
|
||||||
githubRoot: 'https://github.com/nrwl/nx/blob/master',
|
? null
|
||||||
name: folderName,
|
: {
|
||||||
packageName: packageJson.name,
|
githubRoot: 'https://github.com/nrwl/nx/blob/master',
|
||||||
description: packageJson.description,
|
name: folderName,
|
||||||
root: relativeFolderPath,
|
packageName: packageJson.name,
|
||||||
source: join(relativeFolderPath, '/src'),
|
description: packageJson.description,
|
||||||
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,
|
root: relativeFolderPath,
|
||||||
},
|
source: join(relativeFolderPath, '/src'),
|
||||||
'generators.json',
|
documents: !!hasDocumentation
|
||||||
['generators']
|
? hasDocumentation.itemList.map((item) => ({
|
||||||
),
|
...item,
|
||||||
executors: getSchemaList(
|
path: item.path,
|
||||||
{
|
file: item.file,
|
||||||
absoluteRoot,
|
content: readFileSync(
|
||||||
folderName,
|
join('docs', item.file + '.md'),
|
||||||
root: relativeFolderPath,
|
'utf8'
|
||||||
},
|
),
|
||||||
'executors.json',
|
}))
|
||||||
['executors', 'builders']
|
: [],
|
||||||
),
|
generators: getSchemaList(
|
||||||
};
|
{
|
||||||
}
|
absoluteRoot,
|
||||||
);
|
folderName,
|
||||||
|
root: relativeFolderPath,
|
||||||
|
},
|
||||||
|
'generators.json',
|
||||||
|
['generators']
|
||||||
|
),
|
||||||
|
executors: getSchemaList(
|
||||||
|
{
|
||||||
|
absoluteRoot,
|
||||||
|
folderName,
|
||||||
|
root: relativeFolderPath,
|
||||||
|
},
|
||||||
|
'executors.json',
|
||||||
|
['executors', 'builders']
|
||||||
|
),
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.filter(Boolean);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user