How to modify the values of variables in plop.js in the generated page

108 views Asked by At

How to modify a page in plop and hbs

I have a plop file, I create pages, everything is ok, I also have hbs files on the basis of which files are generated depending on the plop variables.

I also have questions where the user can answer "Yes" or "No"

Now, if I answer "No", then a page is created without components, this is correct, but what if over time I need the logic of this component or pieces of code? If I re-create the file to set the variables “Yes” with the same name, let’s say “ports”, then it will say that such a page already exists.

That is, how can I modify the value of variables so that on an already created page it works for me from "No" to "Yes".

const pages = readdirSync('./src/templates/pages');

    pages.forEach(file =>
        plop.setGenerator(file, {
            description: readFile(`./src/templates/pages/${file}/readme.txt`),
            prompts: [
                {
                    type: 'input',
                    name: 'name',
                    message: 'Enter page name (snake-case): ',
                },

                // Страница таблицы
                ...(file === 'table-page'
                    ? [
                            {
                                type: 'list',
                                name: 'isHeaderSearch',
                                message: 'Using a header search component?',
                                choices: ['Yes', 'No'],
                            },
                            {
                                type: 'list',
                                name: 'isHeaderDate',
                                message: 'Using a header date component?',
                                choices: ['Yes', 'No'],
                            },
                            {
                                type: 'list',
                                name: 'isCheckbox',
                                message: 'Using a checkbox component?',
                                choices: ['Yes', 'No'],
                            },
                            {
                                type: 'list',
                                name: 'isAsyncCheckbox',
                                message: 'Using an async checkbox component?',
                                choices: ['Yes', 'No'],
                                when: answers => answers.isCheckbox === 'Yes',
                            },
                      ]
                    : []),

                // Страница редактирования
                ...(file === 'edit-page'
                    ? [
                            {
                                type: 'input',
                                name: 'item',
                                message: 'Enter name of the expected item: ',
                            },
                            {
                                type: 'list',
                                name: 'isFile',
                                message: 'Using a file component?',
                                choices: ['Yes', 'No'],
                            },
                      ]
                    : []),

                // Страница редактирования формы с картой
                ...(file === 'edit-map-page'
                    ? [
                            {
                                type: 'input',
                                name: 'item',
                                message: 'Enter name of the expected item: ',
                            },
                            {
                                type: 'list',
                                name: 'isDrawing',
                                message: 'Use drawing component?',
                                choices: ['Yes', 'No'],
                            },
                            {
                                type: 'list',
                                name: 'isFile',
                                message: 'Using a file component?',
                                choices: ['Yes', 'No'],
                            },
                      ]
                    : []),
            ],
            actions: [
                {
                    type: 'addMany',
                    destination: 'src/pages/{{name}}',
                    base: `src/templates/pages/${file}`,
                    templateFiles: `src/templates/pages/${file}/*.hbs`,
                },
                {
                    type: 'addMany',
                    destination: 'src/pages/{{name}}',
                    base: `src/templates/pages/${file}`,
                    templateFiles: `src/templates/pages/${file}/components`,
                },
                data => {
                    const { name } = data;
                    execSync(`npx prettier --write "src/pages/${name}/**/*.{js,jsx,json}"`);
                    return 'Prettier formatting complete';
                },
            ],
        }),
    );

enter image description here

I tried the advice from GPT but it didn't help

{
             type: 'modify',
             path: 'src/pages/{{name}}/actions.js',
             pattern: /{{#ifEquals isFile "Yes"}}([\s\S]*?){{\/ifEquals}}/g,
             template: '{{#ifEquals isFile "Yes"}}\n // your code for isFile\n{{/ifEquals}}',
         },
0

There are 0 answers