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

--symbol-render-* (maybe all custom templates) does not work from CLI #796

Open
wojciechczerniak opened this issue Apr 7, 2023 · 2 comments

Comments

@wojciechczerniak
Copy link

I think there is a difference how CLI args and JSON config are parsed. Looks like --symbol-render-* does not work.

But maybe I'm missing something. In theory my CLI command is the same as my config file, right?

Scenario 1

With this config file, template and CLI command I'm able to generate PHP enum file:

Config:

{
    "shape": {
        "id": {
            "separator": "__",
            "generator": "%s"
        }
    },
    "mode": {
        "symbol": {
            "dest": "public/svg",
            "sprite": "sprite.svg",
            "render": {
                "php": {
                    "template": "resources/svg/template.mustache",
                    "dest": "../../app/Enums/SvgIcons.php"
                }
            }
        }
    }
}

Template

<?php

enum SvgIcons
{
    {{#shapes}}
    case {{ name }};
    {{/shapes}}
}

Output:

<?php

enum SvgIcons
{
    case HandDrawnColor__CalendarApple;
    case HandDrawnColor__CalendarGoogle;
    case HandDrawnColor__CalendarOutlook;
}

CLI:

svg-sprite --config=svg-sprite.json resources/svg/./**/*.svg

Scenario 2

But when I've tried to replicate this without config file, with CLI only using:

svg-sprite --shape-id-generator="%s" --symbol --symbol-dest="public/svg" --symbol-sprite="sprite.svg" --symbol-render-php --symbol-render-php-template="resources/svg/template.mustache" --symbol-render-php-dest="../../app/Enums/SvgIcons.php" resources/svg/./**/*.svg

It does not work. PHP file is not generated from the template.

Investigation

I think that if "php" render never finds it's way to config object.

if ('map' in option) {
optionsMap[option.map] = name;
}

But symbol.render.* does not have map property defined:

svg-sprite/bin/config.yaml

Lines 613 to 619 in 87e9898

"*":
description: Custom output renderings
template:
description: Custom output Mustache template (relative to svg-sprite basedir)
dest:
description: Custom output destination (relative to the --symbol-dest)

Therefore it's not mapped to config and config don't have this additional render template.

@XhmikosR
Copy link
Member

XhmikosR commented Apr 11, 2023

PRs welcome. CLI isn't currently tested, any help is welcome there too, see #789.

Also, I assume you are using the latest 2x version. I doubt this could be fixed in 3.x, but please try the beta version too.

@XhmikosR XhmikosR changed the title --symbol-render-* (maybe all custom templates) does not work from CLI --symbol-render-* (maybe all custom templates) does not work from CLI Apr 11, 2023
@chimok
Copy link

chimok commented May 24, 2024

Hello @wojciechczerniak,

The cli is totaly broken since 1-2 years. I use this simple script you can easy adjust to your needs. Use a name with .mjs extension because I'm using ESM imports.

#!/usr/bin/env node

import fs from 'fs';
import path from 'path';
import yargs from 'yargs'
import glob from 'fast-glob'
import { hideBin } from 'yargs/helpers'
import SVGSpriter from 'svg-sprite';

const { argv } = yargs(hideBin(process.argv)).scriptName("icon-builder.mjs")
    .usage("Usage: $0 -theme <theme>")
    .option("t", {
        alias: "theme",
        describe: "The name of the wordpress theme directory.",
        demandOption: "The theme name is required.",
        type: "string",
    });

const svgoConfig = {
    "log": "info",
    "shape": {
        "id": {
            "generator": (_name, file) => {
                const fileName = file.basename.slice(0, -4);
                return `${fileName}`;
            }
        },
        "dimension": {
            "maxWidth": 48,
            "maxHeight": 48,
            "attributes": true
        },
        "spacing": {
            "padding": 0
        },
        "transform": [
            {
                "svgo": {
                    "plugins": [
                    ]
                }
            }
        ]
    },
    "svg": {
        "xmlDeclaration": true
    },
    "mode": {
        "symbol": {
            "dest": './',
            "sprite": './web/ico.svg',
            "inline": true,
            "example": {
                "template": "./src/icons-template.html.hbs",
                "dest": `./web/app/themes/${argv.theme}/static/icons.html`
            }
        }
    },
    "variables": {}
};

// eslint-disable-next-line no-console
console.log(`Build icons for theme: ${argv.theme}`);

const spriter = new SVGSpriter(svgoConfig);
const files = glob.sync(`./web/app/themes/${argv.theme}/src/icons/*.svg`);
for (const file of files) {
    spriter.add(file, file, fs.readFileSync(file, 'utf8'));
}

spriter.compile((error, result, _data) => {
    // Run through all files that have been created for the `symbol` mode
    for (const type of Object.values(result.symbol)) {
        // Recursively create directories as needed
        fs.mkdirSync(path.dirname(type.path), { recursive: true });
        // Write the generated resource to disk
        fs.writeFileSync(type.path, type.contents);
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants