diff --git a/nx-dev/data-access-documents/src/lib/documents.api.ts b/nx-dev/data-access-documents/src/lib/documents.api.ts index 31c5381c69..6ef60c5d92 100644 --- a/nx-dev/data-access-documents/src/lib/documents.api.ts +++ b/nx-dev/data-access-documents/src/lib/documents.api.ts @@ -25,14 +25,13 @@ export const flavorList: { export class DocumentsApi { constructor( private readonly options: { - previewRoot: string; - archiveRoot: string; + publicDocsRoot: string; versions: VersionMetadata[]; documentsMap: Map; } ) { - if (!options.archiveRoot || !options.previewRoot) { - throw new Error('archive and preview roots cannot be undefined'); + if (!options.publicDocsRoot) { + throw new Error('public docs root cannot be undefined'); } } @@ -59,14 +58,8 @@ export class DocumentsApi { if (!file.data.title) { file.data.title = extractTitle(originalContent) ?? path[path.length - 1]; } - return { - filePath: relative( - versionId === 'preview' - ? this.options.previewRoot - : this.options.archiveRoot, - docPath - ), + filePath: join(this.options.publicDocsRoot, docPath), data: file.data, content: file.content, excerpt: file.excerpt, @@ -117,16 +110,12 @@ export class DocumentsApi { } getDocumentsRoot(version: string): string { - if (version === 'preview') { - return this.options.previewRoot; - } - const versionPath = this.options.versions.find( (x) => x.id === version )?.path; if (versionPath) { - return join(this.options.archiveRoot, versionPath); + return join(this.options.publicDocsRoot, versionPath); } throw new Error(`Cannot find root for ${version}`); diff --git a/nx-dev/data-access-documents/src/lib/test-utils.ts b/nx-dev/data-access-documents/src/lib/test-utils.ts index c39c2609f5..6b26dafd31 100644 --- a/nx-dev/data-access-documents/src/lib/test-utils.ts +++ b/nx-dev/data-access-documents/src/lib/test-utils.ts @@ -36,7 +36,6 @@ export function createDocumentApiOptions() { ), ], ]), - previewRoot: join(__dirname, '../../../../docs'), - archiveRoot: join(__dirname, '../../../nx-dev/public/documentation'), + publicDocsRoot: join(__dirname, '../../../nx-dev/public/documentation'), }; } diff --git a/nx-dev/nx-dev/lib/api.ts b/nx-dev/nx-dev/lib/api.ts index 8af1cb10d2..e84dacdecc 100644 --- a/nx-dev/nx-dev/lib/api.ts +++ b/nx-dev/nx-dev/lib/api.ts @@ -1,4 +1,3 @@ -import { join } from 'path'; import { DocumentMetadata, DocumentsApi, @@ -8,42 +7,23 @@ import { // Imports JSON directly so they can be bundled into the app and functions. // Also provides some test safety. -import previewDocuments from '../../../docs/map.json'; import previousDocuments from '../public/documentation/previous/map.json'; import latestDocuments from '../public/documentation/latest/map.json'; -import archiveVersionsData from '../public/documentation/versions.json'; +import versionsData from '../public/documentation/versions.json'; export function loadDocumentsData(): Map { const map = new Map(); map.set('latest', latestDocuments); map.set('previous', previousDocuments); - if (process.env.VERCEL_ENV !== 'production') { - map.set('preview', previewDocuments); - } return map; } export function loadVersionsData(): VersionMetadata[] { - const versions: VersionMetadata[] = archiveVersionsData; - if (process.env.VERCEL_ENV !== 'production') { - versions.push({ - name: 'Preview', - id: 'preview', - release: 'preview', - path: 'preview', - default: false, - hidden: true, - }); - } - return versions; + return versionsData; } export const documentsApi = new DocumentsApi({ - previewRoot: join(process.env.NX_WORKSPACE_ROOT, 'docs'), - archiveRoot: join( - process.env.NX_WORKSPACE_ROOT, - 'nx-dev/nx-dev/public/documentation' - ), + publicDocsRoot: 'nx-dev/nx-dev/public/documentation', documentsMap: loadDocumentsData(), versions: loadVersionsData(), }); diff --git a/nx-dev/nx-dev/pages/api/preview-asset.ts b/nx-dev/nx-dev/pages/api/preview-asset.ts deleted file mode 100644 index a6af6c8074..0000000000 --- a/nx-dev/nx-dev/pages/api/preview-asset.ts +++ /dev/null @@ -1,39 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from 'next'; -import { join } from 'path'; -import * as send from 'send'; - -// nx-ignore-next-line -import { appRootPath } from '@nrwl/tao/src/utils/app-root'; - -// This is only guaranteed during local dev since Vercel will not contain the entire workspace during runtime -const previewRootPath = join(appRootPath, 'docs'); - -export default function (req: NextApiRequest, res: NextApiResponse) { - return new Promise((resolve) => { - if (Array.isArray(req.query.uri) || Array.isArray(req.query.document)) { - res.writeHead(422, { 'Content-Type': 'text/plain' }); - res.end('Invalid URI'); - } else { - const uri = decodeURIComponent(req.query.uri); - const document = decodeURIComponent(req.query.document); - const src = uri.startsWith('.') ? join(document, '..', uri) : uri; - const stream = send(req, src, { - // Files outside of the root are forbidden and will result in 404. - root: previewRootPath, - }); - - stream.on('end', () => { - resolve(); - }); - - stream.on('error', (err) => { - res.status(404); - res.write(`Not Found ${err.message}`); - res.end(); - resolve(); - }); - - stream.pipe(res); - } - }); -} diff --git a/nx-dev/nx-dev/project.json b/nx-dev/nx-dev/project.json index d8b1c6a40e..d551818e84 100644 --- a/nx-dev/nx-dev/project.json +++ b/nx-dev/nx-dev/project.json @@ -8,56 +8,33 @@ { "target": "build-base", "projects": "self" - }, - { - "target": "copy-preview", - "projects": "self" - }, - { - "target": "sitemap", - "projects": "self" } ], "executor": "@nrwl/workspace:run-commands", "options": { - "root": "nx-dev/nx-dev", "outputPath": "dist/nx-dev/nx-dev", - "command": "echo Build complete!" + "command": "nx run nx-dev:sitemap" } }, "sitemap": { - "dependsOn": [ - { - "target": "copy-preview", - "projects": "self" - } - ], "executor": "@nrwl/workspace:run-commands", - "outputs": ["dist/nx-dev/nx-dev/public"], + "outputs": [], "options": { "command": "npx next-sitemap --config ./nx-dev/nx-dev/next-sitemap.js" } }, - "copy-preview": { - "dependsOn": [ - { - "target": "build-base", - "projects": "self" - } - ], + "serve": { "executor": "@nrwl/workspace:run-commands", - "outputs": ["dist/nx-dev/nx-dev/public"], + "outputs": [], "options": { - "command": "rm -rf dist/nx-dev/nx-dev/public/documentation/preview && cp -r docs dist/nx-dev/nx-dev/public/documentation/preview" + "commands": [ + "ts-node -P ./scripts/tsconfig.scripts.json ./scripts/documentation/nx-dev-docs-latest-sync.ts --watch", + "nx run nx-dev:serve-nextjs" + ], + "parallel": true } }, "build-base": { - "dependsOn": [ - { - "target": "build", - "projects": "dependencies" - } - ], "executor": "@nrwl/next:build", "outputs": ["{options.outputPath}"], "options": { @@ -68,10 +45,10 @@ "production": {} } }, - "serve": { + "serve-nextjs": { "executor": "@nrwl/next:server", "options": { - "buildTarget": "nx-dev:build", + "buildTarget": "nx-dev:build-base", "dev": true }, "configurations": { diff --git a/nx-dev/nx-dev/public/documentation/latest/react/guides/storybook/storybook-v6.md b/nx-dev/nx-dev/public/documentation/latest/react/guides/storybook/storybook-v6.md index 6f94471a97..e1f0b66a15 100644 --- a/nx-dev/nx-dev/public/documentation/latest/react/guides/storybook/storybook-v6.md +++ b/nx-dev/nx-dev/public/documentation/latest/react/guides/storybook/storybook-v6.md @@ -38,7 +38,7 @@ The `@nrwl/react:storybook-migrate-defaults-5-to-6` generator will not exactly d That way, you can have working Storybook instances for all your projects just by running -``` +```bash nx g @nrwl/react:storybook-migrate-defaults-5-to-6 ``` diff --git a/nx-dev/nx-dev/public/documentation/latest/react/migration/migration-cra.md b/nx-dev/nx-dev/public/documentation/latest/react/migration/migration-cra.md index f3e4b8421b..d67c882eb6 100644 --- a/nx-dev/nx-dev/public/documentation/latest/react/migration/migration-cra.md +++ b/nx-dev/nx-dev/public/documentation/latest/react/migration/migration-cra.md @@ -10,7 +10,7 @@ You can use the [`cra-to-nx`](https://www.npmjs.com/package/cra-to-nx) tool, tha Just `cd` into your Create-React-App (CRA) project and run the following command: -``` +```bash npx cra-to-nx ``` diff --git a/nx-dev/nx-dev/public/documentation/latest/shared/incremental-builds.md b/nx-dev/nx-dev/public/documentation/latest/shared/incremental-builds.md index 990abb1128..9de23746cf 100644 --- a/nx-dev/nx-dev/public/documentation/latest/shared/incremental-builds.md +++ b/nx-dev/nx-dev/public/documentation/latest/shared/incremental-builds.md @@ -17,13 +17,13 @@ This provides the best dev experience for small and medium-size applications, be Nx has **publishable libraries**. As the name suggests, such libraries are meant to be built and published to some package registry s.t. they can be consumed also from outside the Nx workspace. The executor for building a publishable library does more than just building. It makes sure the output is properly compressed and might even produce more bundles s.t. the package can be consumed in a variety of ways (e.g. also produces UMD bundles). -``` +```bash nx g @nrwl/react:lib mylib --publishable --importPath=@myorg/mylib ``` On the other hand, the executor of a **buildable library**, performs a subset of the operations compared to the publishable library's executor. That's because buildable libraries are not intended to be published and thus only produce the minimum necessary output for the incremental build scenario to work. For example, no UMD bundles or minification is being done. The main goal of the executor is to perform the build as fast as possible. -``` +```bash nx g @nrwl/react:lib mylib --buildable ``` diff --git a/nx-dev/nx-dev/public/documentation/latest/shared/monorepo-tags.md b/nx-dev/nx-dev/public/documentation/latest/shared/monorepo-tags.md index 2a954c2c2f..db57ae037e 100644 --- a/nx-dev/nx-dev/public/documentation/latest/shared/monorepo-tags.md +++ b/nx-dev/nx-dev/public/documentation/latest/shared/monorepo-tags.md @@ -132,7 +132,7 @@ Projects without any tags cannot depend on any other projects. If you add the fo If you try to violate the constrains, you will get an error: -``` +```bash A project tagged with "scope:admin" can only depend on projects tagged with "scoped:shared" or "scope:admin". ``` diff --git a/nx-dev/nx-dev/public/documentation/versions.json b/nx-dev/nx-dev/public/documentation/versions.json index a030495c66..ddfa9a94af 100644 --- a/nx-dev/nx-dev/public/documentation/versions.json +++ b/nx-dev/nx-dev/public/documentation/versions.json @@ -1,6 +1,6 @@ [ { - "name": "Latest (v12.9.0)", + "name": "Latest", "id": "latest", "release": "12.9.0", "path": "latest", diff --git a/nx.json b/nx.json index ebb0de8e08..386d69ea42 100644 --- a/nx.json +++ b/nx.json @@ -20,7 +20,6 @@ "test", "lint", "e2e", - "copy-preview", "sitemap" ], "canTrackAnalytics": false, diff --git a/scripts/documentation/nx-dev-docs-latest-sync.ts b/scripts/documentation/nx-dev-docs-latest-sync.ts new file mode 100644 index 0000000000..2a916400ed --- /dev/null +++ b/scripts/documentation/nx-dev-docs-latest-sync.ts @@ -0,0 +1,76 @@ +import * as chalk from 'chalk'; +import * as yargs from 'yargs'; +import { watch } from 'chokidar'; +import * as shell from 'shelljs'; +import { BehaviorSubject } from 'rxjs'; +import { debounceTime, filter, switchMapTo, tap } from 'rxjs/operators'; + +/** + * Available colours + */ +const { bgGreen, white } = chalk; + +const argv = yargs + .command( + 'Usage: $0', + 'Sync the public latest folder with the /docs folder one time' + ) + .example( + '$0 --watch', + 'Sync the public latest folder with the /docs folder whenever changes are done' + ) + .option('watch', { + alias: 'w', + demandOption: false, + type: 'boolean', + description: 'Enable the watch mode', + }).argv; + +function sync(): void { + return shell.exec( + 'rsync -avrR --delete ./docs/./ ./nx-dev/nx-dev/public/documentation/latest', + { sync: true } + ); +} + +function main(isWatched: boolean) { + if (isWatched) { + const isReady$ = new BehaviorSubject(false); + const syncR$ = new BehaviorSubject(null); + + /** + * If we do not debounce, the sync will happen for every file detect by the watcher + */ + isReady$ + .pipe( + filter((isReady) => isReady), + tap(() => + console.log( + bgGreen( + white( + ' => DOCS SYNC ENABLED & READY: You can modify `/docs`, changes will be synced ' + ) + ) + ) + ), + switchMapTo(syncR$), + debounceTime(1000) + ) + .subscribe(() => sync()); + + return watch('./docs', { + ignored: /(^|[\/\\])\../, // ignore dotfiles + persistent: true, + awaitWriteFinish: true, + }) + .on('ready', () => isReady$.next(true)) + .on('add', (path) => syncR$.next(path)) + .on('addDir', (path) => syncR$.next(path)) + .on('change', (path) => syncR$.next(path)) + .on('unlink', (path) => syncR$.next(path)); + } + + return sync(); +} + +main(argv.watch as boolean); diff --git a/scripts/documentation/nx-dev-release-content.ts b/scripts/documentation/nx-dev-release-content.ts index 521477f1cc..bad376e5da 100644 --- a/scripts/documentation/nx-dev-release-content.ts +++ b/scripts/documentation/nx-dev-release-content.ts @@ -12,7 +12,6 @@ import { readJsonFile, writeJsonFile, } from '../../packages/tao/src/utils/fileutils'; -import { execSync } from 'child_process'; /** * Available colours @@ -32,6 +31,10 @@ const argv = yargs '$0', 'Will update the nx.dev content with Nx releases for the current releases in the `nx-dev/nx-dev/public/documentation/versions.json`' ) + .example( + '$0 --latestRelease master', + 'Will update the nx.dev content with the latest master as "latest" version' + ) .option('previousRelease', { alias: 'p', demandOption: false,