Skip to content

Commit

Permalink
refactor: mark all functions that import external modules as async (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored Jan 31, 2022
1 parent aa446b7 commit c56e619
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 112 deletions.
8 changes: 4 additions & 4 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
*.swp binary
*.webp binary

# Make GitHub not index certain files in the languages overview and minify their diff
# Make GitHub not index certain files in the languages overview and/or minify their diff
# See https://github.com/github/linguist/blob/master/docs/overrides.md
__fixtures__ linguist-generated
website linguist-documentation
admin linguist-documentation
__fixtures__/** linguist-generated
website/** linguist-documentation
admin/** linguist-documentation
10 changes: 5 additions & 5 deletions packages/docusaurus-migrate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function migrateDocusaurusProject(
shouldMigrateMdFiles: boolean = false,
shouldMigratePages: boolean = false,
): Promise<void> {
function createMigrationContext(): MigrationContext {
async function createMigrationContext(): Promise<MigrationContext> {
const v1Config = importFresh(`${siteDir}/siteConfig`) as VersionOneConfig;
logger.info('Starting migration from v1 to v2...');
const partialMigrationContext = {
Expand All @@ -95,7 +95,7 @@ export async function migrateDocusaurusProject(
};
}

const migrationContext = createMigrationContext();
const migrationContext = await createMigrationContext();

// TODO need refactor legacy, we pass migrationContext to all methods
const siteConfig = migrationContext.v1Config;
Expand Down Expand Up @@ -187,7 +187,7 @@ export async function migrateDocusaurusProject(
errorCount += 1;
}
try {
migratePackageFile(siteDir, deps, newDir);
await migratePackageFile(siteDir, deps, newDir);
} catch (e) {
logger.error(
`Error occurred while creating package.json file for project: ${e}`,
Expand Down Expand Up @@ -715,11 +715,11 @@ function migrateLatestDocs(
}
}

function migratePackageFile(
async function migratePackageFile(
siteDir: string,
deps: {[key: string]: string},
newDir: string,
): void {
): Promise<void> {
const packageFile = importFresh(`${siteDir}/package.json`) as {
scripts?: Record<string, string>;
dependencies?: Record<string, string>;
Expand Down
83 changes: 44 additions & 39 deletions packages/docusaurus-plugin-content-docs/src/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,159 +32,159 @@ describe('docsVersion', () => {
sidebarCollapsible: true,
};

test('no version tag provided', () => {
expect(() =>
test('no version tag provided', async () => {
await expect(() =>
cliDocsVersionCommand(
null,
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
);
expect(() =>
await expect(() =>
cliDocsVersionCommand(
undefined,
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
);
expect(() =>
await expect(() =>
cliDocsVersionCommand(
'',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: no version tag specified! Pass the version you wish to create as an argument, for example: 1.0.0."`,
);
});

test('version tag should not have slash', () => {
expect(() =>
test('version tag should not have slash', async () => {
await expect(() =>
cliDocsVersionCommand(
'foo/bar',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Do not include slash (/) or backslash (\\\\). Try something like: 1.0.0."`,
);
expect(() =>
await expect(() =>
cliDocsVersionCommand(
'foo\\bar',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Do not include slash (/) or backslash (\\\\). Try something like: 1.0.0."`,
);
});

test('version tag should not be too long', () => {
expect(() =>
test('version tag should not be too long', async () => {
await expect(() =>
cliDocsVersionCommand(
'a'.repeat(255),
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Length cannot exceed 32 characters. Try something like: 1.0.0."`,
);
});

test('version tag should not be a dot or two dots', () => {
expect(() =>
test('version tag should not be a dot or two dots', async () => {
await expect(() =>
cliDocsVersionCommand(
'..',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Do not name your version \\".\\" or \\"..\\". Try something like: 1.0.0."`,
);
expect(() =>
await expect(() =>
cliDocsVersionCommand(
'.',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Do not name your version \\".\\" or \\"..\\". Try something like: 1.0.0."`,
);
});

test('version tag should be a valid pathname', () => {
expect(() =>
test('version tag should be a valid pathname', async () => {
await expect(() =>
cliDocsVersionCommand(
'<foo|bar>',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
);
expect(() =>
await expect(() =>
cliDocsVersionCommand(
'foo\x00bar',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
);
expect(() =>
await expect(() =>
cliDocsVersionCommand(
'foo:bar',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0."`,
);
});

test('version tag already exist', () => {
expect(() =>
test('version tag already exist', async () => {
await expect(() =>
cliDocsVersionCommand(
'1.0.0',
versionedSiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: this version already exists! Use a version tag that does not already exist."`,
);
});

test('no docs file to version', () => {
test('no docs file to version', async () => {
const emptySiteDir = path.join(fixtureDir, 'empty-site');
expect(() =>
await expect(() =>
cliDocsVersionCommand(
'1.0.0',
emptySiteDir,
DEFAULT_PLUGIN_ID,
DEFAULT_OPTIONS,
),
).toThrowErrorMatchingInlineSnapshot(
).rejects.toThrowErrorMatchingInlineSnapshot(
`"[docs]: there is no docs to version!"`,
);
});

test('first time versioning', () => {
test('first time versioning', async () => {
const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
const ensureMock = jest.spyOn(fs, 'ensureDirSync').mockImplementation();
const writeMock = jest.spyOn(fs, 'writeFileSync');
Expand All @@ -205,7 +205,12 @@ describe('docsVersion', () => {
...DEFAULT_OPTIONS,
sidebarPath: path.join(simpleSiteDir, 'sidebars.json'),
};
cliDocsVersionCommand('1.0.0', simpleSiteDir, DEFAULT_PLUGIN_ID, options);
await cliDocsVersionCommand(
'1.0.0',
simpleSiteDir,
DEFAULT_PLUGIN_ID,
options,
);
expect(copyMock).toHaveBeenCalledWith(
path.join(simpleSiteDir, options.path),
path.join(
Expand Down Expand Up @@ -236,7 +241,7 @@ describe('docsVersion', () => {
ensureMock.mockRestore();
});

test('not the first time versioning', () => {
test('not the first time versioning', async () => {
const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
const ensureMock = jest.spyOn(fs, 'ensureDirSync').mockImplementation();
const writeMock = jest.spyOn(fs, 'writeFileSync');
Expand All @@ -257,7 +262,7 @@ describe('docsVersion', () => {
...DEFAULT_OPTIONS,
sidebarPath: path.join(versionedSiteDir, 'sidebars.json'),
};
cliDocsVersionCommand(
await cliDocsVersionCommand(
'2.0.0',
versionedSiteDir,
DEFAULT_PLUGIN_ID,
Expand Down Expand Up @@ -293,7 +298,7 @@ describe('docsVersion', () => {
ensureMock.mockRestore();
});

test('second docs instance versioning', () => {
test('second docs instance versioning', async () => {
const pluginId = 'community';

const copyMock = jest.spyOn(fs, 'copySync').mockImplementation();
Expand All @@ -317,7 +322,7 @@ describe('docsVersion', () => {
path: 'community',
sidebarPath: path.join(versionedSiteDir, 'community_sidebars.json'),
};
cliDocsVersionCommand('2.0.0', versionedSiteDir, pluginId, options);
await cliDocsVersionCommand('2.0.0', versionedSiteDir, pluginId, options);
expect(copyMock).toHaveBeenCalledWith(
path.join(versionedSiteDir, options.path),
path.join(
Expand Down
10 changes: 5 additions & 5 deletions packages/docusaurus-plugin-content-docs/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {loadSidebarsFile, resolveSidebarPathOption} from './sidebars';
import {DEFAULT_PLUGIN_ID} from '@docusaurus/utils';
import logger from '@docusaurus/logger';

function createVersionedSidebarFile({
async function createVersionedSidebarFile({
siteDir,
pluginId,
sidebarPath,
Expand All @@ -34,7 +34,7 @@ function createVersionedSidebarFile({
// Load current sidebar and create a new versioned sidebars file (if needed).
// Note: we don't need the sidebars file to be normalized: it's ok to let
// plugin option changes to impact older, versioned sidebars
const sidebars = loadSidebarsFile(sidebarPath);
const sidebars = await loadSidebarsFile(sidebarPath);

// Do not create a useless versioned sidebars file if sidebars file is empty
// or sidebars are disabled/false)
Expand All @@ -56,12 +56,12 @@ function createVersionedSidebarFile({
}

// Tests depend on non-default export for mocking.
export function cliDocsVersionCommand(
export async function cliDocsVersionCommand(
version: string | null | undefined,
siteDir: string,
pluginId: string,
options: PathOptions & SidebarOptions,
): void {
): Promise<void> {
// It wouldn't be very user-friendly to show a [default] log prefix,
// so we use [docs] instead of [default]
const pluginIdLogPrefix =
Expand Down Expand Up @@ -127,7 +127,7 @@ export function cliDocsVersionCommand(
throw new Error(`${pluginIdLogPrefix}: there is no docs to version!`);
}

createVersionedSidebarFile({
await createVersionedSidebarFile({
siteDir,
pluginId,
version,
Expand Down
Loading

0 comments on commit c56e619

Please sign in to comment.