Skip to content

fix(pre-build): local checkout import #817

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

Merged
merged 1 commit into from
May 27, 2025
Merged
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
13 changes: 8 additions & 5 deletions scripts/pre-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import path from 'path';
import latestVersion from 'latest-version';
import logger from '@docusaurus/logger';

import { copy, download } from './tasks/download-docs';
import { copyLocalDocumentation, download } from './tasks/download-docs';
import { addFrontmatterToAllDocs } from './tasks/add-frontmatter';
import { fixContent } from './tasks/md-fixers';
import { copyNewContent } from './tasks/copy-new-content';
Expand Down Expand Up @@ -54,17 +54,20 @@ const start = async (source: string): Promise<void> => {
});
await fs.writeFile(path.join(DOCS_FOLDER, '.sha'), target);
} else if (existsSync(source)) {
await copy({
target: source,
logger.info(
`Copying local docs from ${logger.green(path.resolve(logger.green(source)))}`,
);
await copyLocalDocumentation({
source,
destination: DOCS_FOLDER,
copyMatch: 'docs',
});
} else {
logger.error(`Path ${localElectron} does not exist`);
logger.error(`Local path ${logger.red(source)} does not exist`);
return process.exit(1);
}

logger.info('Copying new content');
logger.info('Copying additional content from website repository');
await copyNewContent(DOCS_FOLDER);

logger.info('Finding, validating, and uncommenting API history blocks');
Expand Down
47 changes: 31 additions & 16 deletions scripts/tasks/download-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ interface DownloadOptions {
}

interface CopyOptions {
/** The source to use for the copy action */
source: string;
/** The copy destination as an absolute path */
destination: string;
/** The source source to use for the copy action */
target: string;
/** The glob match to use to filter the copied contents */
copyMatch: string;
}
Expand All @@ -32,8 +32,8 @@ interface CopyOptions {
* Entry for each documentation page
*/
interface Entry {
/** File name of the page */
filename: string;
/** Relative path of the file with respect to `/docs/latest/` */
relativeFilePath: string;
/** Slug of the page */
slug: string;
/** Buffer contents of the page */
Expand All @@ -47,8 +47,8 @@ interface Entry {
*/
const saveContents = async (files: Entry[], destination: string) => {
for (const file of files) {
const { content, filename } = file;
const finalPath = path.join(destination, filename);
const { content, relativeFilePath } = file;
const finalPath = path.join(destination, relativeFilePath);

// These are files we do not need to copy
if (finalPath === '') {
Expand Down Expand Up @@ -101,7 +101,10 @@ const downloadFromGitHub = async (
stream.on('end', () => {
const content = Buffer.concat(chunks);
contents.push({
filename: header.name.replace(`${downloadMatch}`, ''),
relativeFilePath: header.name.replace(
`${downloadMatch}`,
'',
),
slug: path.basename(header.name, '.md'),
content,
} satisfies Entry);
Expand Down Expand Up @@ -145,22 +148,34 @@ export const download = async (userOptions: DownloadOptions) => {
* as needed.
* @param userOptions
*/
export const copy = async ({
target,
export const copyLocalDocumentation = async ({
source,
destination,
copyMatch = '.',
}: CopyOptions) => {
const filesPaths = fs.glob(`${copyMatch}/**/*`, {
cwd: target,
const dirents = fs.glob(`${copyMatch}/**/*`, {
cwd: source,
withFileTypes: true,
});

const contents = [];
const contents: Entry[] = [];

for await (const dirent of dirents) {
if (!dirent.isFile()) {
continue;
}

const sourceFilePath = path.join(dirent.parentPath, dirent.name);
// Transform path from `{REPO}/docs/my/file/path.md` to `my/file/path.md`
const relativeFilePath = path.relative(
path.join(source, 'docs'),
sourceFilePath,
);

for await (const filePath of filesPaths) {
const content = {
filename: filePath.replace(`${copyMatch}/`, ''),
content: await fs.readFile(path.join(target, filePath)),
slug: path.basename(filePath, '.md'),
relativeFilePath,
content: await fs.readFile(sourceFilePath),
slug: path.basename(dirent.name, '.md'),
};

contents.push(content);
Expand Down