Skip to content
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

refactor: handle unexpected dumi version when detecting #2072

Merged
merged 5 commits into from
May 6, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/features/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
VERSION_2_LEVEL_NAV,
} from '@/constants';
import type { IApi } from '@/types';
import { isVersionInRange } from '@/utils';
import { parseModule } from '@umijs/bundler-utils';
import { execSync } from 'child_process';
import fs from 'fs';
import hostedGit from 'hosted-git-info';
import path from 'path';
import { deepmerge, lodash, resolve, semver, winPath } from 'umi/plugin-utils';
import { deepmerge, lodash, resolve, winPath } from 'umi/plugin-utils';
import { safeExcludeInMFSU } from '../derivative';
import loadTheme, { IThemeLoadResult } from './loader';

Expand Down Expand Up @@ -89,7 +90,7 @@ function checkMinor2ByPkg(pkg: IApi['pkg']) {
const ver =
pkg.peerDependencies?.dumi || pkg.devDependencies?.dumi || '^2.0.0';

return semver.subset(ver, VERSION_2_LEVEL_NAV, { includePrerelease: true });
return isVersionInRange(ver, VERSION_2_LEVEL_NAV);
}

export default (api: IApi) => {
Expand Down
7 changes: 3 additions & 4 deletions src/loaders/markdown/transformer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { IParsedBlockAsset } from '@/assetParsers/block';
import type { ILocalesConfig, IRouteMeta } from '@/client/theme-api/types';
import { VERSION_2_DEPRECATE_SOFT_BREAKS } from '@/constants';
import type { IApi, IDumiConfig, IDumiTechStack } from '@/types';
import { isVersionInRange } from '@/utils';
import enhancedResolve, { type ResolveOptions } from 'enhanced-resolve';
import type { IRoute } from 'umi';
import { semver } from 'umi/plugin-utils';
import type { Plugin, Processor } from 'unified';
import type { Data } from 'vfile';
import rehypeDemo from './rehypeDemo';
Expand Down Expand Up @@ -95,9 +95,8 @@ function keepSoftBreak(pkg: IApi['pkg']) {
if (pkg?.name?.startsWith('@examples/') || pkg?.name === 'dumi') return false;

const ver = pkg?.devDependencies?.dumi ?? pkg?.dependencies?.dumi ?? '^2.0.0';
return !semver.subset(ver, VERSION_2_DEPRECATE_SOFT_BREAKS, {
includePrerelease: true,
});

return !isVersionInRange(ver, VERSION_2_DEPRECATE_SOFT_BREAKS);
}

async function applyUnifiedPlugin(opts: {
Expand Down
20 changes: 19 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Range, RangeOptions } from '@umijs/utils/compiled/semver';
import { createHash } from 'crypto';
import Cache from 'file-system-cache';
import fs from 'fs';
import yaml from 'js-yaml';
import path from 'path';
import { lodash, logger, winPath } from 'umi/plugin-utils';
import { lodash, logger, semver, winPath } from 'umi/plugin-utils';
import { FS_CACHE_DIR } from './constants';

/**
Expand Down Expand Up @@ -222,3 +223,20 @@ export function generateMetaChunkName(
export function getContentHash(content: string, length = 8) {
return createHash('md5').update(content).digest('hex').slice(0, length);
}

/**
* check if version is in range
*/
export function isVersionInRange(
version: string,
range: string | Range,
options: RangeOptions = { includePrerelease: true },
) {
if (semver.valid(version)) {
return semver.satisfies(version, range, options);
}
if (semver.validRange(version)) {
return semver.subset(version, range, options);
}
return false;
}