feat(v2): v1-v2 migration cli automate migration from v1 to v2 (#3015)

* add poc for migration tool

* fix version

* fix path

* fix some type error

* fix type errors

* fix commad

* delete package.lock.json

* ignore lib folder in eslint

* ignore lib in prettier

* fix missing version

* fix sidebar in next

* use primaey color

* fix navbar link

* fix footer

* sanatize front-matter for gray-matter

* add e2e test

* fixworkflow

* add unit test

* chore(v2): fix eslint error

* refactor(v2): use descriptive variable names

* refactor(v2): refactor createProjectStructure

* refactor(v2): refactor migrateDocusaurusProject

* refactor(v2): fix eslint errors

* fix(v2): fix mistake

* use path.join

* remove console.log

* try automate migrating md file to mdx

* fix types not found

* fix types

* remove unused fixture

* use package.json for version

* add support for pages

* add support

* sanitize fortmatter only in case of special character

* chore(v2): add color to dependencies

* feat(v2): initialize config with range of color and add logs

* chore(v2): add glob dependency

* fix(v2): fix color generation

* fix(v2): fix type issue

* feat(v2): add all unknown fields to customFields

* fix(v2): fix css mistake

* fix lerna

* fix(v2): fix github actions

* feat(v2): add option flag for migrating pages

* fix special character

* remove depulicate build

* fix test

* remove unwanted file

* fix frontmatter

* remove unused file

* rerun the test

* fix links

* feat(v2): filter out more fields from customFields

* feat(v2): filter out deprecated fields from customFields

* fix items

* fix types

* merge master

* revert docs

* fix doc

* fix broken link

* fix test

* test

* fix ci

* fix ci

* fix ci

* fix ci

* fix frontmatter

* log custom fields

Co-authored-by: teikjun <teikjunhci@gmail.com>
This commit is contained in:
Anshul Goyal 2020-07-24 16:22:16 +05:30 committed by GitHub
parent 0ecd71ec06
commit a78f703366
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
52 changed files with 3740 additions and 1368 deletions

View file

@ -0,0 +1,10 @@
---
title: Introducing Docusaurus
author: Joel Marcey
authorURL: http://twitter.com/JoelMarcey
authorImageURL: https://graph.facebook.com/611217057/picture/?height=200&width=200
authorFBID: 611217057
authorTwitter: JoelMarcey
---
# blog

View file

@ -0,0 +1,19 @@
{
"name": "docusaurus-1-website",
"version": "2.0.0-alpha.58",
"private": true,
"scripts": {
"start": "docusaurus-start",
"build": "docusaurus-build",
"publish-gh-pages": "docusaurus-publish",
"examples": "docusaurus-examples",
"write-translations": "docusaurus-write-translations",
"docusaurus-version": "docusaurus-version",
"rename-version": "docusaurus-rename-version",
"crowdin-upload": "crowdin --config ../crowdin.yaml upload sources --auto-update -b master",
"crowdin-download": "crowdin --config ../crowdin.yaml download -b master"
},
"dependencies": {
"docusaurus": "*"
}
}

View file

@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const React = require('react');
function Index(props) {
return <></>
}
module.exports = Index;

View file

@ -0,0 +1,6 @@
{
"API": [
"commands",
"doc-markdown",
]
}

View file

@ -0,0 +1,82 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* List of projects/orgs using your project for the users page */
const users = {
caption: 'DevSpace',
image: '/img/users/devspace.svg',
infoLink: 'https://devspace.cloud/docs/',
fbOpenSource: false,
pinned: false,
};
const siteConfig = {
title: 'Docusaurus',
tagline: 'Easy to Maintain Open Source Documentation Websites',
url: 'https://docusaurus.io',
baseUrl: '/',
organizationName: 'facebook',
projectName: 'docusaurus',
cname: 'docusaurus.io',
noIndex: false,
users,
editUrl: 'https://github.com/facebook/docusaurus/edit/master/docs/',
headerLinks: [
{doc: 'installation', label: 'Docs'},
{doc: 'tutorial-setup', label: 'Tutorial'},
{page: 'users', label: 'Users'},
{blog: true, label: 'Blog'},
{
href: 'https://github.com/facebook/docusaurus',
label: 'GitHub',
external: true,
},
],
headerIcon: 'img/docusaurus.svg',
footerIcon: 'img/docusaurus_monochrome.svg',
favicon: 'img/docusaurus.ico',
algolia: {
apiKey: '3eb9507824b8be89e7a199ecaa1a9d2c',
indexName: 'docusaurus',
algoliaOptions: {
facetFilters: ['language:LANGUAGE', 'version:VERSION'],
},
},
colors: {
primaryColor: '#2E8555',
secondaryColor: '#205C3B',
},
translationRecruitingLink: 'https://crowdin.com/project/docusaurus',
copyright: `Copyright © ${new Date().getFullYear()} Facebook Inc.`,
usePrism: ['jsx'],
highlight: {
theme: 'atom-one-dark',
},
scripts: [
'https://buttons.github.io/buttons.js',
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
'/js/code-blocks-buttons.js',
],
gaTrackingId: 'UA-44373548-31',
facebookAppId: '199138890728411',
facebookComments: true,
twitter: 'true',
twitterUsername: 'docusaurus',
ogImage: 'img/docusaurus.png',
twitterImage: 'img/docusaurus.png',
onPageNav: 'separate',
cleanUrl: true,
scrollToTop: true,
scrollToTopOptions: {
zIndex: 100,
},
enableUpdateTime: true,
enableUpdateBy: true,
docsSideNavCollapsible: true,
};
module.exports = siteConfig;

View file

@ -0,0 +1,7 @@
---
id: version-1.10.x-commands
title: CLI Commands
original_id: commands
---
# Doc

View file

@ -0,0 +1,7 @@
---
id: version-1.9.x-commands
title: CLI Commands
original_id: commands
---
# Doc

View file

@ -0,0 +1,7 @@
---
id: version-1.9.x-doc-markdown
title: Markdown Features
original_id: doc-markdown
---
# Doc

View file

@ -0,0 +1,8 @@
{
"version-1.9.x-docs": {
"API": [
"version-1.9.x-commands",
"version-1.9.x-doc-markdown"
]
}
}

View file

@ -0,0 +1,10 @@
---
title: Introducing Docusaurus
author: Joel Marcey
authorURL: http://twitter.com/JoelMarcey
authorImageURL: https://graph.facebook.com/611217057/picture/?height=200&width=200
authorFBID: 611217057
authorTwitter: JoelMarcey
---
# Blog

View file

@ -0,0 +1,19 @@
{
"name": "docusaurus-1-website",
"version": "2.0.0-alpha.58",
"private": true,
"scripts": {
"start": "docusaurus-start",
"build": "docusaurus-build",
"publish-gh-pages": "docusaurus-publish",
"examples": "docusaurus-examples",
"write-translations": "docusaurus-write-translations",
"docusaurus-version": "docusaurus-version",
"rename-version": "docusaurus-rename-version",
"crowdin-upload": "crowdin --config ../crowdin.yaml upload sources --auto-update -b master",
"crowdin-download": "crowdin --config ../crowdin.yaml download -b master"
},
"dependencies": {
"docusaurus": "*"
}
}

View file

@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const React = require('react');
function Index(props) {
return <></>
}
module.exports = Index;

View file

@ -0,0 +1,6 @@
{
"API": [
"commands",
"doc-markdown",
]
}

View file

@ -0,0 +1,82 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* List of projects/orgs using your project for the users page */
const users = {
caption: 'DevSpace',
image: '/img/users/devspace.svg',
infoLink: 'https://devspace.cloud/docs/',
fbOpenSource: false,
pinned: false,
};
const siteConfig = {
title: 'Docusaurus',
tagline: 'Easy to Maintain Open Source Documentation Websites',
url: 'https://docusaurus.io',
baseUrl: '/',
organizationName: 'facebook',
projectName: 'docusaurus',
cname: 'docusaurus.io',
noIndex: false,
users,
editUrl: 'https://github.com/facebook/docusaurus/edit/master/docs/',
headerLinks: [
{doc: 'installation', label: 'Docs'},
{doc: 'tutorial-setup', label: 'Tutorial'},
{page: 'users', label: 'Users'},
{blog: true, label: 'Blog'},
{
href: 'https://github.com/facebook/docusaurus',
label: 'GitHub',
external: true,
},
],
headerIcon: 'img/docusaurus.svg',
footerIcon: 'img/docusaurus_monochrome.svg',
favicon: 'img/docusaurus.ico',
algolia: {
apiKey: '3eb9507824b8be89e7a199ecaa1a9d2c',
indexName: 'docusaurus',
algoliaOptions: {
facetFilters: ['language:LANGUAGE', 'version:VERSION'],
},
},
colors: {
primaryColor: '#2E8555',
secondaryColor: '#205C3B',
},
translationRecruitingLink: 'https://crowdin.com/project/docusaurus',
copyright: `Copyright © ${new Date().getFullYear()} Facebook Inc.`,
usePrism: ['jsx'],
highlight: {
theme: 'atom-one-dark',
},
scripts: [
'https://buttons.github.io/buttons.js',
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
'/js/code-blocks-buttons.js',
],
gaTrackingId: 'UA-44373548-31',
facebookAppId: '199138890728411',
facebookComments: true,
twitter: 'true',
twitterUsername: 'docusaurus',
ogImage: 'img/docusaurus.png',
twitterImage: 'img/docusaurus.png',
onPageNav: 'separate',
cleanUrl: true,
scrollToTop: true,
scrollToTopOptions: {
zIndex: 100,
},
enableUpdateTime: true,
enableUpdateBy: true,
docsSideNavCollapsible: true,
};
module.exports = siteConfig;

View file

@ -0,0 +1,7 @@
---
id: version-1.10.x-commands
title: CLI Commands
original_id: commands
---
# Doc

View file

@ -0,0 +1,7 @@
---
id: version-1.9.x-commands
title: CLI Commands
original_id: commands
---
# Doc

View file

@ -0,0 +1,7 @@
---
id: version-1.9.x-doc-markdown
title: Markdown Features
original_id: doc-markdown
---
# Doc

View file

@ -0,0 +1,8 @@
{
"version-1.9.x-docs": {
"API": [
"version-1.9.x-commands",
"version-1.9.x-doc-markdown"
]
}
}

View file

@ -0,0 +1 @@
[ "1.10.x","1.9.10", "1.9.x"]

View file

@ -0,0 +1,6 @@
---
id: commands
title: CLI Commands
---
## Doc

View file

@ -0,0 +1,6 @@
---
id: doc-markdown
title: Markdown Features
---
## Doc

View file

@ -0,0 +1,19 @@
{
"name": "docusaurus-1-website",
"version": "2.0.0-alpha.58",
"private": true,
"scripts": {
"start": "docusaurus-start",
"build": "docusaurus-build",
"publish-gh-pages": "docusaurus-publish",
"examples": "docusaurus-examples",
"write-translations": "docusaurus-write-translations",
"docusaurus-version": "docusaurus-version",
"rename-version": "docusaurus-rename-version",
"crowdin-upload": "crowdin --config ../crowdin.yaml upload sources --auto-update -b master",
"crowdin-download": "crowdin --config ../crowdin.yaml download -b master"
},
"dependencies": {
"docusaurus": "*"
}
}

View file

@ -0,0 +1,17 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const React = require('react');
function Index(props) {
return <></>
}
module.exports = Index;

View file

@ -0,0 +1,6 @@
{
"API": [
"commands",
"doc-markdown",
]
}

View file

@ -0,0 +1,82 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/* List of projects/orgs using your project for the users page */
const users = {
caption: 'DevSpace',
image: '/img/users/devspace.svg',
infoLink: 'https://devspace.cloud/docs/',
fbOpenSource: false,
pinned: false,
};
const siteConfig = {
title: 'Docusaurus',
tagline: 'Easy to Maintain Open Source Documentation Websites',
url: 'https://docusaurus.io',
baseUrl: '/',
organizationName: 'facebook',
projectName: 'docusaurus',
cname: 'docusaurus.io',
noIndex: false,
users,
editUrl: 'https://github.com/facebook/docusaurus/edit/master/docs/',
headerLinks: [
{doc: 'installation', label: 'Docs'},
{doc: 'tutorial-setup', label: 'Tutorial'},
{page: 'users', label: 'Users'},
{blog: true, label: 'Blog'},
{
href: 'https://github.com/facebook/docusaurus',
label: 'GitHub',
external: true,
},
],
headerIcon: 'img/docusaurus.svg',
footerIcon: 'img/docusaurus_monochrome.svg',
favicon: 'img/docusaurus.ico',
algolia: {
apiKey: '3eb9507824b8be89e7a199ecaa1a9d2c',
indexName: 'docusaurus',
algoliaOptions: {
facetFilters: ['language:LANGUAGE', 'version:VERSION'],
},
},
colors: {
primaryColor: '#2E8555',
secondaryColor: '#205C3B',
},
translationRecruitingLink: 'https://crowdin.com/project/docusaurus',
copyright: `Copyright © ${new Date().getFullYear()} Facebook Inc.`,
usePrism: ['jsx'],
highlight: {
theme: 'atom-one-dark',
},
scripts: [
'https://buttons.github.io/buttons.js',
'https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.0/clipboard.min.js',
'/js/code-blocks-buttons.js',
],
gaTrackingId: 'UA-44373548-31',
facebookAppId: '199138890728411',
facebookComments: true,
twitter: 'true',
twitterUsername: 'docusaurus',
ogImage: 'img/docusaurus.png',
twitterImage: 'img/docusaurus.png',
onPageNav: 'separate',
cleanUrl: true,
scrollToTop: true,
scrollToTopOptions: {
zIndex: 100,
},
enableUpdateTime: true,
enableUpdateBy: true,
docsSideNavCollapsible: true,
};
module.exports = siteConfig;

View file

@ -0,0 +1,55 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {migrateDocusaurusProject} from '../index';
import path from 'path';
import fs from 'fs-extra';
describe('migration test', () => {
test('simple website', () => {
const siteDir = path.join(
__dirname,
'__fixtures__',
'simple_website',
'website',
);
const newDir = path.join(__dirname, '__fixtures__', 'migrated_simple_site');
migrateDocusaurusProject(siteDir, newDir);
fs.removeSync(newDir);
});
test('complex website', () => {
const siteDir = path.join(
__dirname,
'__fixtures__',
'complex_website',
'website',
);
const newDir = path.join(
__dirname,
'__fixtures__',
'migrated_complex_site',
);
migrateDocusaurusProject(siteDir, newDir);
fs.removeSync(newDir);
});
test('missing versions', () => {
const siteDir = path.join(
__dirname,
'__fixtures__',
'missing_version_website',
'website',
);
const newDir = path.join(
__dirname,
'__fixtures__',
'migrated_missing_version_site',
);
migrateDocusaurusProject(siteDir, newDir);
fs.removeSync(newDir);
});
});

View file

@ -0,0 +1,52 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {RawData, Data} from './types';
function splitHeader(content: string): RawData {
// New line characters need to handle all operating systems.
const lines = content.split(/\r?\n/);
if (lines[0] !== '---') {
return {};
}
let i = 1;
for (; i < lines.length - 1; i = 1 + i) {
if (lines[i] === '---') {
break;
}
}
return {
header: lines.slice(1, i + 1).join('\n'),
content: lines.slice(i + 1).join('\n'),
};
}
export default function extractMetadata(content: string): Data {
const metadata: {[key: string]: string} = {};
const both = splitHeader(content);
if (!both.content) {
if (!both.header) {
return {metadata, rawContent: content};
}
return {metadata, rawContent: both.header};
}
// New line characters => to handle all operating systems.
const lines = (both.header ?? '').split(/\r?\n/);
for (let i = 0; i < lines.length - 1; i += 1) {
const keyvalue = lines[i].split(':');
const key = keyvalue[0].trim();
let value = keyvalue.slice(1).join(':').trim();
try {
value = JSON.parse(value);
} catch (err) {
// Ignore the error as it means it's not a JSON value.
}
metadata[key] = value;
}
return {metadata, rawContent: both.content};
}

View file

@ -0,0 +1,748 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import * as fs from 'fs-extra';
import importFresh from 'import-fresh';
import chalk from 'chalk';
import glob from 'glob';
import Color from 'color';
import {
VersionOneConfig,
VersionTwoConfig,
ClassicPresetEntries,
SidebarEntries,
} from './types';
import extractMetadata from './frontMatter';
import migratePage from './transform';
import sanitizeMD from './sanitizeMD';
import path from 'path';
const DOCUSAURUS_VERSION = (importFresh('../package.json') as {version: string})
.version;
export function walk(dir: string): Array<string> {
let results: Array<string> = [];
const list = fs.readdirSync(dir);
list.forEach((file: string) => {
const fullPath = `${dir}/${file}`;
const stat = fs.statSync(fullPath);
if (stat && stat.isDirectory()) {
results = results.concat(walk(fullPath));
} else {
results.push(fullPath);
}
});
return results;
}
function sanitizedFileContent(
content: string,
migrateMDFiles: boolean,
): string {
const extractedData = extractMetadata(content);
const extractedMetaData = Object.entries(extractedData.metadata).reduce(
(metaData, value) => {
return `${metaData}\n${value[0]}: ${
value[0] === 'tags' ||
!!String(value[1]).match(/^(\w| |\.|-)+$/m) ||
String(value[1]).match(/^("|').+("|')$/)
? value[1]
: `"${value[1]}"`
}`;
},
'',
);
const sanitizedData = `---${extractedMetaData}\n---\n${
migrateMDFiles
? sanitizeMD(extractedData.rawContent)
: extractedData.rawContent
}`;
return sanitizedData;
}
export async function migrateDocusaurusProject(
siteDir: string,
newDir: string,
shouldMigrateMdFiles: boolean = false,
shouldMigratePages: boolean = false,
): Promise<void> {
const siteConfig = importFresh(`${siteDir}/siteConfig`) as VersionOneConfig;
console.log('Starting migration from v1 to v2...');
const config = createConfigFile(siteConfig);
const classicPreset = config.presets[0][1];
const deps: Record<string, string> = {
'@docusaurus/core': DOCUSAURUS_VERSION,
'@docusaurus/preset-classic': DOCUSAURUS_VERSION,
clsx: '^1.1.1',
react: '^16.10.2',
'react-dom': '^16.10.2',
};
try {
createClientRedirects(siteConfig, deps, config);
console.log(
chalk.green('Successfully created client redirect for non clean URL'),
);
} catch (errorInClientRedirect) {
console.log(
chalk.red(`Error while creating redirects: ${errorInClientRedirect}`),
);
}
if (shouldMigratePages) {
try {
createPages(newDir, siteDir);
console.log(
chalk.green(
'Successfully created pages (check migration page for more details)',
),
);
} catch (errorInMigratingPages) {
console.log(
chalk.red(
`Error occurred while creating pages: ${errorInMigratingPages}`,
),
);
}
} else {
try {
createDefaultLandingPage(newDir);
console.log(
chalk.green(
'Successfully created landing page (check migration page for more details)',
),
);
} catch (errorInLandingPage) {
console.log(
chalk.red(
`Error occurred while creating landing page: ${errorInLandingPage}`,
),
);
}
}
try {
migrateStaticFiles(siteDir, newDir);
console.log(chalk.green('Successfully migrated static folder'));
} catch (errorInStatic) {
console.log(
chalk.red(`Error occurred while copying static folder: ${errorInStatic}`),
);
}
try {
migrateBlogFiles(siteDir, newDir, classicPreset, shouldMigrateMdFiles);
} catch (errorInMigratingBlogs) {
console.log(
chalk.red(
`Error occurred while migrating blogs: ${errorInMigratingBlogs}`,
),
);
}
try {
handleVersioning(siteDir, newDir, config, shouldMigrateMdFiles);
} catch (errorInVersion) {
console.log(
chalk.red(
`Error occurred while migrating versioned docs: ${errorInVersion}`,
),
);
}
try {
migrateLatestDocs(siteDir, newDir, shouldMigrateMdFiles, classicPreset);
} catch (errorInDoc) {
chalk.red(`Error occurred while migrating docs: ${errorInDoc}`);
}
try {
migrateLatestSidebar(siteDir, newDir, classicPreset, siteConfig);
} catch (error) {
console.log(chalk.red(`Error occurred while migrating sidebar: ${error}`));
}
try {
fs.writeFileSync(
path.join(newDir, 'docusaurus.config.js'),
`module.exports=${JSON.stringify(config, null, 2)}`,
);
console.log(
chalk.green(
`Successfully created a new config file with new navbar and footer config`,
),
);
} catch (error) {
console.log(
chalk.red(`Error occurred while creating config file: ${error}`),
);
}
try {
migratePackageFile(siteDir, deps, newDir);
} catch (error) {
console.log(
chalk.red(
`Error occurred while creating package.json file for project: ${error}`,
),
);
}
console.log('Completed migration from v1 to v2');
}
export function createConfigFile(
siteConfig: VersionOneConfig,
): VersionTwoConfig {
const homePageId = siteConfig.headerLinks?.filter((value) => value.doc)[0]
.doc;
const customConfigFields: Record<string, any> = {};
// add fields that are unknown to v2 to customConfigFields
Object.keys(siteConfig).forEach((key: any) => {
const knownFields = [
'title',
'tagline',
'url',
'baseUrl',
'organizationName',
'projectName',
'scripts',
'stylesheets',
'favicon',
'cname',
'noIndex',
'headerLinks',
'headerIcon',
'footerIcon',
'algolia',
'colors',
'copyright',
'editUrl',
'facebookComments',
'usePrism',
'highlight',
'twitterUsername',
'scrollToTopOptions',
'twitter',
'twitterImage',
'onPageNav',
'cleanUrl',
'ogImage',
'scrollToTop',
'enableUpdateTime',
'enableUpdateBy',
'docsSideNavCollapsible',
'gaTrackingId',
];
const value = siteConfig[key as keyof typeof siteConfig];
if (value !== undefined && !knownFields.includes(key)) {
customConfigFields[key] = value;
}
});
console.log(
`${chalk.yellow(
'Following Fields from siteConfig.js will be added to docusaurus.config.js in `customFields`',
)}\n${chalk.yellow(Object.keys(customConfigFields).join('\n'))}`,
);
const result: VersionTwoConfig = {
title: siteConfig.title ?? '',
tagline: siteConfig.tagline,
url: siteConfig.url ?? '',
baseUrl: siteConfig.baseUrl ?? '',
organizationName: siteConfig.organizationName,
projectName: siteConfig.projectName,
scripts: siteConfig.scripts,
stylesheets: siteConfig.stylesheets,
favicon: siteConfig.favicon ?? '',
customFields: customConfigFields,
onBrokenLinks: 'log',
presets: [
[
'@docusaurus/preset-classic',
{
docs: {
homePageId,
showLastUpdateAuthor: true,
showLastUpdateTime: true,
editUrl: siteConfig.editUrl,
},
blog: {},
theme: {},
},
],
],
plugins: [],
themeConfig: {
navbar: {
title: siteConfig.title,
logo: siteConfig.headerIcon
? {
src: siteConfig.headerIcon,
}
: undefined,
items: (siteConfig.headerLinks ?? [])
.map((link) => {
if (link.doc) {
return {
to: `docs/${link.doc === homePageId ? '' : link.doc}`,
label: link.label,
position: 'left',
};
}
if (link.page) {
return {
to: `/${link.page}`,
label: link.label,
position: 'left',
};
}
if (link.href) {
return {href: link.href, label: link.label, position: 'left'};
}
return null;
})
.filter(Boolean),
},
image: siteConfig.ogImage ? siteConfig.ogImage : undefined,
footer: {
links: siteConfig.twitterUsername
? [
{
title: 'Community',
items: [
{
label: 'Twitter',
to: `https://twitter.com/${siteConfig.twitterUsername}`,
},
],
},
]
: [],
copyright: siteConfig.copyright,
logo: {
src: siteConfig.footerIcon,
},
},
algolia: siteConfig.algolia ? siteConfig.algolia : undefined,
gtag: siteConfig.gaTrackingId
? {
trackingID: siteConfig.gaTrackingId,
}
: undefined,
},
};
return result;
}
function createClientRedirects(
siteConfig: VersionOneConfig,
deps: {[key: string]: string},
config: VersionTwoConfig,
): void {
if (!siteConfig.cleanUrl) {
deps['@docusaurus/plugin-client-redirects'] = DOCUSAURUS_VERSION;
config.plugins.push([
'@docusaurus/plugin-client-redirects',
{fromExtensions: ['html']},
]);
}
}
function createPages(newDir: string, siteDir: string): void {
fs.mkdirpSync(path.join(newDir, 'src', 'pages'));
if (fs.existsSync(path.join(siteDir, 'pages', 'en'))) {
try {
fs.copySync(
path.join(siteDir, 'pages', 'en'),
path.join(newDir, 'src', 'pages'),
);
const files = glob.sync('**/*.js', {
cwd: path.join(newDir, 'src', 'pages'),
});
files.forEach((file) => {
const filePath = path.join(newDir, 'src', 'pages', file);
const content = String(fs.readFileSync(filePath));
fs.writeFileSync(filePath, migratePage(content));
});
} catch (error) {
console.log(chalk.red(`Unable to migrate Pages : ${error}`));
createDefaultLandingPage(newDir);
}
} else {
console.log('Ignoring Pages');
}
}
function createDefaultLandingPage(newDir: string) {
const indexPage = `import Layout from "@theme/Layout";
import React from "react";
export default () => {
return <Layout />;
};
`;
fs.mkdirpSync(`${newDir}/src/pages/`);
fs.writeFileSync(`${newDir}/src/pages/index.js`, indexPage);
}
function migrateStaticFiles(siteDir: string, newDir: string): void {
if (fs.existsSync(path.join(siteDir, 'static'))) {
fs.copySync(path.join(siteDir, 'static'), path.join(newDir, 'static'));
} else {
fs.mkdirSync(path.join(newDir, 'static'));
}
}
function migrateBlogFiles(
siteDir: string,
newDir: string,
classicPreset: ClassicPresetEntries,
migrateMDFiles: boolean,
): void {
if (fs.existsSync(path.join(siteDir, 'blog'))) {
fs.copySync(path.join(siteDir, 'blog'), path.join(newDir, 'blog'));
const files = walk(path.join(newDir, 'blog'));
files.forEach((file) => {
const content = String(fs.readFileSync(file));
fs.writeFileSync(file, sanitizedFileContent(content, migrateMDFiles));
});
classicPreset.blog.path = 'blog';
console.log(
chalk.green(
`Successfully migrated blogs to version 2 with change in frontmatter`,
),
);
} else {
console.log(chalk.yellow(`Blog not found. Skipping migration for blog`));
}
}
function handleVersioning(
siteDir: string,
newDir: string,
config: VersionTwoConfig,
migrateMDFiles: boolean,
): void {
if (fs.existsSync(path.join(siteDir, 'versions.json'))) {
const loadedVersions: Array<string> = JSON.parse(
String(fs.readFileSync(path.join(siteDir, 'versions.json'))),
);
fs.copyFile(
path.join(siteDir, 'versions.json'),
path.join(newDir, 'versions.json'),
);
const versions = loadedVersions.reverse();
const versionRegex = new RegExp(`version-(${versions.join('|')})-`, 'mgi');
migrateVersionedSidebar(siteDir, newDir, versions, versionRegex, config);
fs.mkdirpSync(path.join(newDir, 'versioned_docs'));
migrateVersionedDocs(
versions,
siteDir,
newDir,
versionRegex,
migrateMDFiles,
);
console.log(
chalk.green(
`Successfully migrated version docs and sidebar. The following doc versions have been created: \n${loadedVersions.join(
'\n',
)}`,
),
);
} else {
console.log(
chalk.yellow(
'Versioned docs not found. Skipping migration for versioned docs',
),
);
}
}
function migrateVersionedDocs(
versions: string[],
siteDir: string,
newDir: string,
versionRegex: RegExp,
migrateMDFiles: boolean,
): void {
versions.reverse().forEach((version, index) => {
if (index === 0) {
fs.copySync(
path.join(siteDir, '..', 'docs'),
path.join(newDir, 'versioned_docs', `version-${version}`),
);
fs.copySync(
path.join(siteDir, 'versioned_docs', `version-${version}`),
path.join(newDir, 'versioned_docs', `version-${version}`),
);
return;
}
try {
fs.mkdirsSync(path.join(newDir, 'versioned_docs', `version-${version}`));
fs.copySync(
path.join(newDir, 'versioned_docs', `version-${versions[index - 1]}`),
path.join(newDir, 'versioned_docs', `version-${version}`),
);
fs.copySync(
path.join(siteDir, 'versioned_docs', `version-${version}`),
path.join(newDir, 'versioned_docs', `version-${version}`),
);
} catch {
fs.copySync(
path.join(newDir, 'versioned_docs', `version-${versions[index - 1]}`),
path.join(newDir, 'versioned_docs', `version-${version}`),
);
}
});
const files = walk(path.join(newDir, 'versioned_docs'));
files.forEach((pathToFile) => {
const content = fs.readFileSync(pathToFile).toString();
fs.writeFileSync(
pathToFile,
sanitizedFileContent(content.replace(versionRegex, ''), migrateMDFiles),
);
});
}
function migrateVersionedSidebar(
siteDir: string,
newDir: string,
versions: string[],
versionRegex: RegExp,
config: VersionTwoConfig,
): void {
if (fs.existsSync(path.join(siteDir, 'versioned_sidebars'))) {
fs.mkdirpSync(path.join(newDir, 'versioned_sidebars'));
const sidebars: {
entries: SidebarEntries;
version: string;
}[] = [];
versions.forEach((version, index) => {
let sidebarEntries: SidebarEntries;
const sidebarPath = path.join(
siteDir,
'versioned_sidebars',
`version-${version}-sidebars.json`,
);
try {
fs.statSync(sidebarPath);
sidebarEntries = JSON.parse(String(fs.readFileSync(sidebarPath)));
} catch {
sidebars.push({version, entries: sidebars[index - 1].entries});
return;
}
const newSidebar = Object.entries(sidebarEntries).reduce(
(topLevel: {[key: string]: any}, value) => {
const key = value[0].replace(versionRegex, '');
// eslint-disable-next-line no-param-reassign
topLevel[key] = Object.entries(value[1]).reduce(
(
acc: {[key: string]: Array<Record<string, unknown> | string>},
val,
) => {
acc[val[0].replace(versionRegex, '')] = (val[1] as Array<
any
>).map((item) => {
if (typeof item === 'string') {
return item.replace(versionRegex, '');
}
return {
type: 'category',
label: item.label,
ids: item.ids.map((id: string) =>
id.replace(versionRegex, ''),
),
};
});
return acc;
},
{},
);
return topLevel;
},
{},
);
sidebars.push({version, entries: newSidebar});
});
sidebars.forEach((sidebar) => {
const newSidebar = Object.entries(sidebar.entries).reduce(
(acc: {[key: string]: any}, val) => {
const key = `version-${sidebar.version}/${val[0]}`;
// eslint-disable-next-line prefer-destructuring
acc[key] = Object.entries(val[1]).map((value) => {
return {
type: 'category',
label: value[0],
items: (value[1] as Array<any>).map((sidebarItem) => {
if (typeof sidebarItem === 'string') {
return {
type: 'doc',
id: `version-${sidebar.version}/${sidebarItem}`,
};
}
return {
type: 'category',
label: sidebarItem.label,
items: sidebarItem.ids.map((id: string) => ({
type: 'doc',
id: `version-${sidebar.version}/${id}`,
})),
};
}),
};
});
return acc;
},
{},
);
fs.writeFileSync(
path.join(
newDir,
'versioned_sidebars',
`version-${sidebar.version}-sidebars.json`,
),
JSON.stringify(newSidebar, null, 2),
);
});
config.themeConfig.navbar.items.push({
label: 'Version',
to: 'docs',
position: 'right',
items: [
{
label: versions[versions.length - 1],
to: 'docs/',
activeBaseRegex: `docs/(?!${versions.join('|')}|next)`,
},
...versions
.reverse()
.slice(1)
.map((version) => ({
label: version,
to: `docs/${version}/`,
})),
{
label: 'Master/Unreleased',
to: `docs/next/`,
activeBaseRegex: `docs/next/(?!support|team|resources)`,
},
],
});
}
}
function migrateLatestSidebar(
siteDir: string,
newDir: string,
classicPreset: ClassicPresetEntries,
siteConfig: VersionOneConfig,
): void {
try {
fs.copyFileSync(
path.join(siteDir, 'sidebars.json'),
path.join(newDir, 'sidebars.json'),
);
classicPreset.docs.sidebarPath = path.join(newDir, 'sidebars.json');
} catch {
console.log(
chalk.yellow(`Sidebar not found. Skipping migration for sidebar`),
);
}
if (siteConfig.colors) {
const primaryColor = Color(siteConfig.colors.primaryColor);
const css = `:root{
--ifm-color-primary-lightest: ${primaryColor.darken(-0.3).hex()};
--ifm-color-primary-lighter: ${primaryColor.darken(-0.15).hex()};
--ifm-color-primary-light: ${primaryColor.darken(-0.1).hex()};
--ifm-color-primary: ${siteConfig.colors.primaryColor};
--ifm-color-primary-dark: ${primaryColor.darken(0.1).hex()};
--ifm-color-primary-darker: ${primaryColor.darken(0.15).hex()};
--ifm-color-primary-darkest: ${primaryColor.darken(0.3).hex()};
}
`;
fs.mkdirpSync(path.join(newDir, 'src', 'css'));
fs.writeFileSync(path.join(newDir, 'src', 'css', 'customTheme.css'), css);
classicPreset.theme.customCss = path.join(
newDir,
'src',
'css',
'customTheme.css',
);
}
}
function migrateLatestDocs(
siteDir: string,
newDir: string,
migrateMDFiles: boolean,
classicPreset: ClassicPresetEntries,
): void {
if (fs.existsSync(path.join(siteDir, '..', 'docs'))) {
const docsPath = path.join(
path.relative(newDir, path.join(siteDir, '..')),
'docs',
);
classicPreset.docs.path = docsPath;
const files = walk(path.join(siteDir, '..', 'docs'));
files.forEach((file) => {
const content = String(fs.readFileSync(file));
fs.writeFileSync(file, sanitizedFileContent(content, migrateMDFiles));
});
console.log(chalk.green(`Successfully migrated docs to version 2`));
} else {
console.log(
chalk.yellow(`Docs folder not found. Skipping migration for docs`),
);
}
}
function migratePackageFile(
siteDir: string,
deps: {[key: string]: string},
newDir: string,
): void {
const packageFile = importFresh(`${siteDir}/package.json`) as {
[key: string]: any;
};
packageFile.scripts = {
...packageFile.scripts,
start: 'docusaurus start',
build: 'docusaurus build',
swizzle: 'docusaurus swizzle',
deploy: 'docusaurus deploy',
docusaurus: 'docusaurus',
};
if (packageFile.dependencies) {
delete packageFile.dependencies.docusaurus;
}
if (packageFile.devDependencies) {
delete packageFile.devDependencies.docusaurus;
}
packageFile.dependencies = {
...packageFile.dependencies,
...deps,
};
fs.writeFileSync(
path.join(newDir, 'package.json'),
JSON.stringify(packageFile, null, 2),
);
console.log(chalk.green(`Successfully migrated package.json file`));
}
export async function migrateMDToMDX(
siteDir: string,
newDir: string,
): Promise<void> {
fs.mkdirpSync(newDir);
fs.copySync(siteDir, newDir);
const files = walk(newDir);
files.forEach((file) => {
fs.writeFileSync(
file,
sanitizedFileContent(String(fs.readFileSync(file)), true),
);
});
console.log(`Succesfully migrated ${siteDir} to ${newDir}`);
}

View file

@ -0,0 +1,52 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import markdown from 'remark-parse';
import toJsx from '@mapbox/hast-util-to-jsx';
import unified from 'unified';
import parse from 'rehype-parse';
import visit from 'unist-util-visit';
import remarkStringify from 'remark-stringify';
import htmlTags from 'html-tags';
import toText from 'hast-util-to-string';
const tags = htmlTags.reduce((acc: {[key: string]: boolean}, tag) => {
acc[tag] = true;
return acc;
}, {});
export default function sanitizeMD(code: string): string {
const markdownTree = unified()
.use(markdown as any)
.parse(code);
visit(markdownTree, 'code', (node) => {
node.value = `\n<!--${node.value}-->\n`;
});
visit(markdownTree, 'inlineCode', (node) => {
node.value = `<!--${node.value}-->`;
});
const markdownString = unified()
.use(remarkStringify as any, {fence: '`', fences: true})
.stringify(markdownTree);
const htmlTree = unified()
.use(parse as any)
.parse(markdownString);
visit(htmlTree, 'element', (node) => {
if (!tags[node.tagName as string]) {
node.type = 'text';
node.value = node.tagName + toText(node);
delete node.children;
delete node.tagName;
}
});
return toJsx(htmlTree)
.replace(/\{\/\*|\*\/\}/g, '')
.replace(/\{\/\*|\*\/\}/g, '')
.replace(/<html><head \/><body>|<\/body><\/html>/g, '');
}

View file

@ -0,0 +1,201 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import jscodeshift from 'jscodeshift';
const empty = () =>
jscodeshift.arrowFunctionExpression(
[jscodeshift.identifier('props')],
jscodeshift.jsxElement(
jscodeshift.jsxOpeningElement(jscodeshift.jsxIdentifier('div'), [
jscodeshift.jsxSpreadAttribute(jscodeshift.identifier('props')),
]),
jscodeshift.jsxClosingElement(jscodeshift.jsxIdentifier('div')),
),
);
const property = (key: string, value: jscodeshift.ArrowFunctionExpression) =>
jscodeshift.objectProperty(jscodeshift.identifier(key), value);
const processCallExpression = (
node: jscodeshift.ASTPath<jscodeshift.VariableDeclarator>,
) => {
const args = (node?.value?.init as any)?.arguments[0];
if (args.type === 'Literal') {
if (args.value.includes('../../core/CompLibrary')) {
const newDeclartor = jscodeshift.variableDeclarator(
node.value.id,
jscodeshift.objectExpression([
property('Container', empty()),
property('GridBlock', empty()),
property('MarkdownBlock', empty()),
]),
);
jscodeshift(node).replaceWith(newDeclartor);
}
}
if (args.type === 'TemplateLiteral') {
if (
args.quasis
.map((element: jscodeshift.TemplateElement) => element.value.raw)
.join('')
.match(/\/core\//)
) {
const newDeclartor = jscodeshift.variableDeclarator(
node.value.id,
empty(),
);
jscodeshift(node).replaceWith(newDeclartor);
}
}
};
const processMemberExpression = (
node: jscodeshift.ASTPath<jscodeshift.VariableDeclarator>,
) => {
const args = (node?.value?.init as any)?.object?.arguments[0];
if (args.type === 'Literal') {
if (args.value === '../../core/CompLibrary.js') {
const newDeclartor = jscodeshift.variableDeclarator(
node.value.id,
jscodeshift.objectExpression([
property('Container', empty()),
property('GridBlock', empty()),
property('MarkdownBlock', empty()),
]),
);
jscodeshift(node).replaceWith(newDeclartor);
} else if (args.value.match(/server/)) {
const newDeclartor = jscodeshift.variableDeclarator(
node.value.id,
empty(),
);
jscodeshift(node).replaceWith(newDeclartor);
}
}
if (args.type === 'TemplateLiteral') {
if (
args.quasis
.map((ele: jscodeshift.TemplateElement) => ele.value.raw)
.join('')
.match(/\/core\//)
) {
const newDeclartor = jscodeshift.variableDeclarator(
node.value.id,
empty(),
);
jscodeshift(node).replaceWith(newDeclartor);
}
}
};
export default function transformer(file: string): string {
const root = jscodeshift(file);
const r = getImportDeclaratorPaths(root);
r.forEach((node) => {
if (node?.value?.init?.type === 'CallExpression') {
processCallExpression(node);
} else if (node?.value?.init?.type === 'MemberExpression') {
processMemberExpression(node);
}
});
if (r[r.length - 1]) {
jscodeshift(r[r.length - 1].parent).insertAfter(
jscodeshift.importDeclaration(
[jscodeshift.importDefaultSpecifier(jscodeshift.identifier('Layout'))],
jscodeshift.literal('@theme/Layout'),
),
);
}
root
.find(jscodeshift.AssignmentExpression, {
operator: '=',
left: {
type: 'MemberExpression',
object: {
name: 'module',
},
property: {
name: 'exports',
},
},
right: {
type: 'Identifier',
},
})
.filter(function (p) {
return p.parentPath.parentPath.name === 'body';
})
.forEach(function (p) {
const exportDecl = jscodeshift.exportDeclaration(
true,
jscodeshift.arrowFunctionExpression(
[jscodeshift.identifier('props')],
jscodeshift.jsxElement(
jscodeshift.jsxOpeningElement(
jscodeshift.jsxIdentifier('Layout'),
[],
),
jscodeshift.jsxClosingElement(jscodeshift.jsxIdentifier('Layout')),
[
jscodeshift.jsxElement(
jscodeshift.jsxOpeningElement(
jscodeshift.jsxIdentifier((p.value.right as any).name),
[
jscodeshift.jsxSpreadAttribute(
jscodeshift.identifier('props'),
),
],
true,
),
),
],
),
),
);
exportDecl.comments = p.parentPath.value.comments;
jscodeshift(p.parentPath).replaceWith(exportDecl);
});
return root.toSource();
}
function getDefaultImportDeclarators(rootAst: jscodeshift.Collection) {
// var ... = require('y')
return rootAst
.find(jscodeshift.VariableDeclarator, {
init: {
callee: {
name: 'require',
},
},
})
.filter((variableDeclarator) => {
return !!variableDeclarator.value;
});
}
function getNamedImportDeclarators(rootAst: jscodeshift.Collection) {
// var ... = require('y').x
return rootAst.find(jscodeshift.VariableDeclarator, {
init: {
object: {
callee: {
name: 'require',
},
},
},
});
}
function getImportDeclaratorPaths(variableDeclaration: jscodeshift.Collection) {
const defaultImports = getDefaultImportDeclarators(variableDeclaration);
const namedImports = getNamedImportDeclarators(variableDeclaration);
return [...defaultImports.paths(), ...namedImports.paths()];
}

View file

@ -0,0 +1,126 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export type RawData = {
header?: string;
content?: string;
};
export type Data = {
metadata: {[key: string]: string};
rawContent: string;
};
export type ClassicPresetEntries = {
docs: {[key: string]: any};
blog: {[key: string]: any};
theme: {[key: string]: any};
};
export type SidebarEntries = {
[key: string]:
| Record<string, unknown>
| Array<Record<string, unknown> | string>;
};
export interface VersionTwoConfig {
baseUrl: string;
favicon: string;
tagline?: string;
title: string;
url: string;
organizationName?: string;
projectName?: string;
githubHost?: string;
onBrokenLinks: string;
plugins: Array<[string, {[key: string]: any}]>;
themes?: [];
presets: [[string, ClassicPresetEntries]];
themeConfig: {
gtag?: {
trackingID?: string;
};
navbar: {
title?: string;
logo?: {
src?: string;
};
items: Array<Record<string, unknown> | null>;
};
image?: string;
footer: {
links: Array<{
title: string;
items: Array<{
label: string;
to: string;
}>;
}>;
copyright?: string;
logo: {
src?: string;
};
};
algolia?: Record<string, unknown>;
};
customFields: {
[key: string]: unknown;
};
scripts?: (
| string
| {
src: string;
[key: string]: unknown;
}
)[];
stylesheets?: (
| string
| {
href: string;
[key: string]: unknown;
}
)[];
}
export type VersionOneConfig = {
title?: string;
tagline?: string;
url?: string;
baseUrl?: string;
defaultVersionShown?: string;
organizationName?: string;
projectName?: string;
noIndex?: string;
headerLinks?: Array<any>;
headerIcon?: string;
favicon?: string;
colors?: any;
copyright?: string;
editUrl?: string;
users?: Array<Record<string, unknown>>;
disableHeaderTitle?: string;
disableTitleTagline?: string;
separateCss?: Array<Record<string, unknown>>;
footerIcon?: string;
translationRecruitingLink?: string;
algolia?: Record<string, unknown>;
gaTrackingId?: string;
highlight?: Record<string, unknown>;
markdownPlugins?: Array<() => void>;
scripts?: Array<{src: string; [key: string]: any} | string>;
stylesheets?: Array<{href: string; [key: string]: any} | string>;
facebookAppId?: string;
facebookComments?: true;
facebookPixelId?: string;
twitter?: string;
twitterUsername?: string;
twitterImage?: string;
ogImage?: string;
cleanUrl?: boolean;
scrollToTop?: boolean;
scrollToTopOptions?: Record<string, unknown>;
};