diff --git a/package.json b/package.json index bbd6081ccf6..f23856d9789 100644 --- a/package.json +++ b/package.json @@ -45,8 +45,9 @@ "prettier:check": "prettier --cache --cache-strategy metadata --check .", "check:spelling": "cspell --cache --no-progress '**/*'", "check:integrations": "npm run build:npm && npm run build:deno && mocha --full-trace integrationTests/*-test.ts", - "start": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus start ./website", - "build:website": "DOCUSAURUS_GENERATED_FILES_DIR_NAME=\"$(pwd)/.docusaurus\" docusaurus build --out-dir=\"$(pwd)/websiteDist\" ./website", + "serve": "docusaurus serve --dir websiteDist/ --config website/docusaurus.config.cjs", + "start": "npm run build:website && npm run serve", + "build:website": "ts-node resources/build-docusaurus.ts", "build:npm": "ts-node resources/build-npm.ts", "build:deno": "ts-node resources/build-deno.ts", "diff:npm": "ts-node resources/diff-npm-package.ts", diff --git a/resources/build-docusaurus.ts b/resources/build-docusaurus.ts new file mode 100644 index 00000000000..47451c967b2 --- /dev/null +++ b/resources/build-docusaurus.ts @@ -0,0 +1,46 @@ +// The whole script is a hack to allow building Docusaurus in ESM project +// https://github.com/facebook/docusaurus/issues/6520 +// Should be just: `docusaurus build --out-dir ./websiteDist ./website` + +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; + +import { localRepoPath, npm, readPackageJSON } from './utils.js'; + +const tmpDir = path.join(os.tmpdir(), 'graphql-run-docusaurus'); +fs.rmSync(tmpDir, { recursive: true, force: true }); +fs.mkdirSync(tmpDir); + +const packageJSON = readPackageJSON(); +delete packageJSON.type; +fs.writeFileSync(tmpDirPath('package.json'), JSON.stringify(packageJSON)); + +copyToTmpDir('package-lock.json'); +copyToTmpDir('tsconfig.json'); +copyToTmpDir('src'); +copyToTmpDir('website'); + +npm(['install', 'ci'], { cwd: tmpDir }); + +const env = { + ...process.env, + DOCUSAURUS_GENERATED_FILES_DIR_NAME: tmpDirPath('.docusaurus'), +}; +const docusaurusArgs = [ + 'build', + '--out-dir', + localRepoPath('websiteDist'), + tmpDirPath('website'), +]; +npm(['exec', 'docusaurus', '--', ...docusaurusArgs], { env, cwd: tmpDir }); + +function copyToTmpDir(relativePath: string) { + fs.cpSync(localRepoPath(relativePath), tmpDirPath(relativePath), { + recursive: true, + }); +} + +function tmpDirPath(...paths: ReadonlyArray): string { + return path.join(tmpDir, ...paths); +}