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

Generate files formatted by elm-format #50

Closed
kraklin opened this issue Mar 21, 2022 · 7 comments
Closed

Generate files formatted by elm-format #50

kraklin opened this issue Mar 21, 2022 · 7 comments

Comments

@kraklin
Copy link

kraklin commented Mar 21, 2022

Hey,

thanks for quite a nice tool to deal with translations. We are having small problem though. The generated output is not formatted according to elm-format format. It is a problem because we are using webpack plugin and it re-generates the translations elm module on each start of the app and creates wrong formatting. As we are checking whether everything is elm-formatted on the CI, this fails all the time if we don't notice it and reformat the output afterwards.

Do you plan to update the generated template so it adheres to the elm-format? 🙏

The format of comments is wrong in my case:

{-| This file was generated by elm-i18n version 2.7.0.


-}

import List
import Maybe
import String


{-| Initialize an i18n instance based on a language


-}
init : Language -> I18n
init lang =
    case lang of
        De ->
            de

        En ->
            en


{-| Switch to another i18n instance based on a language


-}
.....

but elm-format hates those empty lines between {|-....-} so after reformatting it ends up like this:

{-| This file was generated by elm-i18n version 2.7.0.
-}

import List
import Maybe
import String


{-| Initialize an i18n instance based on a language
-}
init : Language -> I18n
init lang =
    case lang of
        De ->
            de

        En ->
            en


{-| Switch to another i18n instance based on a language
-}

We are using elm-format version 0.8.4

@kraklin
Copy link
Author

kraklin commented Mar 29, 2022

I dug little bit deeper and the problem lies with the usage of the-sett/elm-syntax-dsl and I see that you mention that in the issues the-sett/elm-syntax-dsl#42

So question is, what do you suggest to do in the meantime? Ignore that file while checking for the elm-format? Something different?

@kraklin
Copy link
Author

kraklin commented Mar 29, 2022

as a workaround I have created adhoc webpack plugin, that is being run after the emit change and that runs npm elm-format -y on the generated file. Might be useful for someone else as well

const exec = require('child_process').exec;

....

     , {
      apply: (compiler) => {
        compiler.hooks.afterEmit.tap('AfterEmitPlugin', (compilation) => {
          exec('npm run format:travelm', (err, stdout, stderr) => {
            if (stdout) process.stdout.write(stdout);
            if (stderr) process.stderr.write(stderr);
          });
        });
      }

@anmolitor
Copy link
Owner

Ah that is actually a quite nice idea for a workaround. Until this is fixed in the-sett/elm-syntax-dsl, I should add elm-format as a dependency. Thanks!

@anmolitor
Copy link
Owner

I now start elm-format as a child process in --stdin mode and pipe the generated Elm code through.
If you encounter any issues feel free to reopen. I also updated https://github.com/andreasewering/travelm-agency-webpack-plugin to use the new version.

@miniBill
Copy link
Contributor

This caused a regression - calling /bin/sh -c npm exec elm-format -- --stdin, causes this output on my system:

Usage: npm <command>

where <command> is one of:
    access, adduser, audit, bin, bugs, c, cache, ci, cit,
    clean-install, clean-install-test, completion, config,
    create, ddp, dedupe, deprecate, dist-tag, docs, doctor,
    edit, explore, fund, get, help, help-search, hook, i, init,
    install, install-ci-test, install-test, it, link, list, ln,
    login, logout, ls, org, outdated, owner, pack, ping, prefix,
    profile, prune, publish, rb, rebuild, repo, restart, root,
    run, run-script, s, se, search, set, shrinkwrap, star,
    stars, start, stop, t, team, test, token, tst, un,
    uninstall, unpublish, unstar, up, update, v, version, view,
    whoami

npm <command> -h  quick help on <command>
npm -l            display full usage info
npm help <term>   search for help on <term>
npm help npm      involved overview

Specify configs in the ini-formatted file:
    /home/minibill/.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

[email protected] /nix/store/0k391v2bmpzv6kg1p62a2lxpaf6m3m7i-nodejs-14.19.1/lib/node_modules/npm

@miniBill
Copy link
Contributor

A downside of the --stdin approach is that if there is an Elm syntax error - like if you have keys with a dash in them - there will be no output file to diagnose what went wrong

@miniBill
Copy link
Contributor

I've found the issue with npm exec: it got added in npm 7, and I had npm 6 as part of node 14

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

No branches or pull requests

3 participants