async function migrateVersionedDocs()

in packages/docusaurus-migrate/src/index.ts [466:520]


async function migrateVersionedDocs(
  context: MigrationContext,
  versions: string[],
  versionRegex: RegExp,
) {
  const {siteDir, newDir, shouldMigrateMdFiles} = context;
  await Promise.all(
    versions.reverse().map(async (version, index) => {
      if (index === 0) {
        await fs.copy(
          path.join(siteDir, '..', context.v1Config.customDocsPath || 'docs'),
          path.join(newDir, 'versioned_docs', `version-${version}`),
        );
        await fs.copy(
          path.join(siteDir, 'versioned_docs', `version-${version}`),
          path.join(newDir, 'versioned_docs', `version-${version}`),
        );
        return;
      }
      try {
        await fs.mkdirs(
          path.join(newDir, 'versioned_docs', `version-${version}`),
        );
        await fs.copy(
          path.join(newDir, 'versioned_docs', `version-${versions[index - 1]}`),
          path.join(newDir, 'versioned_docs', `version-${version}`),
        );
        await fs.copy(
          path.join(siteDir, 'versioned_docs', `version-${version}`),
          path.join(newDir, 'versioned_docs', `version-${version}`),
        );
      } catch {
        await fs.copy(
          path.join(newDir, 'versioned_docs', `version-${versions[index - 1]}`),
          path.join(newDir, 'versioned_docs', `version-${version}`),
        );
      }
    }),
  );
  const files = await walk(path.join(newDir, 'versioned_docs'));
  await Promise.all(
    files.map(async (pathToFile) => {
      if (path.extname(pathToFile) === '.md') {
        const content = await fs.readFile(pathToFile, 'utf-8');
        await fs.outputFile(
          pathToFile,
          sanitizedFileContent(
            content.replace(versionRegex, ''),
            shouldMigrateMdFiles,
          ),
        );
      }
    }),
  );
}