我在这个问题上苦苦挣扎,我不知道为什么 cmd 这么说replace is undefined
。
我进行了一些研究,但找不到脚本失败的任何原因。我控制台记录了整个代码流,但一切都通过了。
我目前真的很无语并试图避免,但我无法继续。
plopfile.js
const componentPlop = require('./controllers/component/component.plopfile');
module.exports = (plop) => {
plop.setWelcomeMessage('Generators');
componentPlop(plop);
};
组件.plopfile.js
const componentPlop = (plop) => {
plop.setGenerator(' Components', {
description:
'This area is responsible for creating, editing and managing Components',
prompts: [
{
name: 'componentName',
type: 'input',
message: 'Component name: ',
},
{
name: 'componentType',
type: 'list',
message: 'Component type: ',
choices: [
'animations',
'atoms',
'molecules',
'organismns',
'templates',
'layouts',
],
},
{
type: 'list',
name: 'componentTemplate',
message: 'Component Template',
default: 'none',
choices: [
{ name: 'Default', value: 'props' },
{ name: 'With Variants', value: 'variants' },
],
},
],
actions: (data) => {
let actions = [
{
type: 'add',
path: '../src/components/{{pascalCase name}}/index.ts',
templateFile: 'templates/ComponentIndex.ts.hbs',
},
{
type: 'add',
path: '../src/components/{{pascalCase name}}/{{pascalCase name}}.test.tsx',
templateFile: 'templates/test.ts.hbs',
},
];
if (data.componentTemplate === 'props') {
actions = actions.concat([
{
type: 'add',
path: '../src/components/{{pascalCase name}}/{{pascalCase name}}.tsx',
templateFile: 'templates/Component.ts.hbs',
},
{
type: 'add',
path: '../src/components/{{pascalCase name}}/{{pascalCase name}}.stories.tsx',
templateFile: 'templates/storiesWithProps.ts.hbs',
},
]);
}
if (data.componentTemplate === 'variants') {
actions = actions.concat([
{
type: 'add',
path: '../src/components/{{pascalCase name}}/{{pascalCase name}}.tsx',
templateFile: 'templates/ComponentWithVariants.ts.hbs',
},
{
type: 'add',
path: '../src/components/{{pascalCase name}}/{{pascalCase name}}.stories.tsx',
templateFile: 'templates/storiesWithVariants.ts.hbs',
},
]);
}
return actions;
},
});
};
module.exports = componentPlop;
即使在第一个动作中确实失败了,我也展示了第一个动作模板
组件.ts.hbs
import { FC } from 'react';
import tw, { styled, css, theme } from 'twin.macro';
export interface {{pascalCase name}}Props {
children: React.ReactNode;
}
const Styled{{pascalCase name}} = styled.div`
${tw` `}
`;
export const {{pascalCase name}}: FC<{{pascalCase name}}Props> = ({ children }) => {
return <Styled{{pascalCase name}}>{children}</Styled>;
};
错误
✖ ++ Cannot read property 'replace' of undefined
✖ ++ ../src/components/{{pascalCase name}}/{{pascalCase name}}.test.tsx Aborted due to previous action failure
✖ ++ ../src/components/{{pascalCase name}}/{{pascalCase name}}.tsx Aborted due to previous action failure
✖ ++ ../src/components/{{pascalCase name}}/{{pascalCase name}}.stories.tsx Aborted due to previous action failure
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
最好的祝福
诺姆