Skip to content

Commit

Permalink
refactor: handle unexpected dumi version when detecting (#2072)
Browse files Browse the repository at this point in the history
* fix: edge case

* Revert "fix: edge case"

This reverts commit e883a73.

* chore: improve code robustness

* chore: update docs

* chore: fix
  • Loading branch information
Wxh16144 committed May 6, 2024
1 parent 92ed416 commit 9665ec6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
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;
}

0 comments on commit 9665ec6

Please sign in to comment.