create-react-app 在createReactApp.js中的getPackageInfo函数不正确地解析了具有范围和版本的包,

mzsu5hc0  于 2个月前  发布在  React
关注(0)|答案(2)|浏览(24)

描述bug

getPackageInfo('@scope/cra-template-custom@1.2.3') 应该返回

{
      name: '@scope/cra-template-custom',
      version: '1.2.3',
}

但实际上返回的是:

{
      name: '@scope/cra-template-custom',
      version: 'scope/cra-template-custom',
}

与此相关的代码来自 createReactApp.js,位于

} else if (installPackage.match(/.+@/)) {
    // Do not match @scope/ when stripping off @version or @tag
    return Promise.resolve({
      name: installPackage.charAt(0) + installPackage.substr(1).split('@')[0],
      version: installPackage.split('@')[1],
    });
  }


对于 name 属性,它在分割之前会去掉初始的 '@'。
对于 version 属性,缺少 .substr(1)
packageMatch 正则表达式可能是一个更通用的方法,可以在这里使用。
getPackageInfo 用于 run 函数中验证模板版本与 react-scripts 版本的兼容性。因为 getPackageInfo 返回了一个垃圾版本属性,所以在 if (!semver.valid(packageVersion)) { 处跳过了这个检查。

重现步骤

我没有一个很好的场景来演示这个问题,因为事情只是继续进行,但我确实画了一些单元测试草图。你需要为这些测试导出 getPackageInfo 才能运行。
getPackageInfo.test.js

'use strict';

const { getPackageInfo } = require('../createReactApp');

describe('getPackageInfo', () => {
  it('cra-template gives name=cra-template', async () => {
    await expect(getPackageInfo('cra-template')).resolves.toStrictEqual({
      name: 'cra-template',
    });
  });

  it('@scope/cra-template-custom gives name=@scope/cra-template-custom', async () => {
    await expect(
      getPackageInfo('@scope/cra-template-custom')
    ).resolves.toStrictEqual({
      name: '@scope/cra-template-custom',
    });
  });

  it('cra-template@1.2.3 gives name=cra-template, version=1.2.3', async () => {
    await expect(getPackageInfo('cra-template@1.2.3')).resolves.toStrictEqual({
      name: 'cra-template',
      version: '1.2.3',
    });
  });

  it('@scope/cra-template-custom@1.2.3 gives name=@scope/cra-template-custom, version=1.2.3', async () => {
    await expect(
      getPackageInfo('@scope/cra-template-custom@1.2.3')
    ).resolves.toStrictEqual({
      name: '@scope/cra-template-custom',
      version: '1.2.3',
    });
  });
});

预期行为

这个函数应该解析具有任意组合范围和版本的包。

实际行为

它没有正确解析具有范围和版本的包。

mrphzbgm

mrphzbgm1#

这个问题已经被自动标记为过时,因为它没有任何最近的活动。如果没有发生任何进一步的活动,它将在5天后被关闭。

tct7dpnv

tct7dpnv2#

如何让某人对这个问题进行初步评估?我需要一个PR吗?

相关问题