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

writerOpts: Cannot use readFile to pass a hbs template to writerOpts. #35

Closed
thet opened this issue Oct 6, 2021 · 4 comments · Fixed by #36
Closed

writerOpts: Cannot use readFile to pass a hbs template to writerOpts. #35

thet opened this issue Oct 6, 2021 · 4 comments · Fixed by #36

Comments

@thet
Copy link
Contributor

thet commented Oct 6, 2021

Insead of just passing a string containg the whole template to writerOpts.commitPartial I would like to read a template file and pass it's contents to it.

I tried with node's async fs.promises.readFile method, but failed.
I did a top-level await and did set the whole package.json of my project as { "type": "module" } which failed.
And I tried the code below, also failing because the commits_template is promise is actuallt not being resolved:

this is my .release-it.js config:

const fs = require("fs");
const path = require("path");

const commits_template = (async function () {
    return await fs.promises.readFile(
        path.resolve(".release-it-conventional-changelog-commit.hbs")
    ).data;
})();

module.exports = {
    [...]
    plugins: {
        "@release-it/conventional-changelog": {
            [...]
            writerOpts: {
                commitPartial: commits_template,
            },
        },
    },
};

Maybe release-it needs to await the import of the .release-it.js file?
Or maybe trying to interpret the commitPartial as a path and trying to read it?

@webpro
Copy link
Contributor

webpro commented Oct 7, 2021

You could try something like fs.readFileSync().toString() or some other means to load the template synchronously.

There's currently no way to have async logic in the release-it config loading mechanism, other than a global await statement, or even using releaseIt() programmatically.

@thet
Copy link
Contributor Author

thet commented Oct 12, 2021

@webpro tnx for the hint! That did work. Documented in #36

However, I had some issues with variables. I'm using the conventionalcommits preset, so I thought I can use as basis the template from: https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-conventionalcommits/templates/commit.hbs
But the variable commitUrlFormat was not resolved.
I did then create a template as a mixture https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-conventionalcommits/templates/commit.hbs and https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-writer/templates/commit.hbs and got that resolved.

Btw, there is quite a number of packages involved for writing the changelog!

This is my full .release-it.js config:

const fs = require("fs");
const path = require("path");

const commits_template = fs
    .readFileSync(path.resolve(".release-it", "conventional-changelog-commit.hbs"))
    .toString();

module.exports = {
    npm: {
        publish: true,
    },
    git: {
        requireBranch: "master",
        commitMessage: "Release new version.",
        commitArgs: ["-n"],
    },
    plugins: {
        "@release-it/conventional-changelog": {
            infile: "CHANGES.md",
            preset: {
                name: "conventionalcommits",
                types: [
                    {
                        type: "breaking",
                        section: "Breaking Changes",
                    },
                    {
                        type: "feat",
                        section: "Features",
                    },
                    {
                        type: "fix",
                        section: "Bug Fixes",
                    },
                    {
                        type: "maint",
                        section: "Maintenance",
                    },
                ],
            },
            writerOpts: {
                commitPartial: commits_template,
            },
        },
    },
};

This is my template:

*{{#if scope}} **{{scope}}:**
{{~/if}} {{#if subject}}
  {{~subject}}
{{~else}}
  {{~header}}
{{~/if}}

{{~!-- commit link --}} {{#if @root.linkReferences~}}
  ([{{shortHash}}](
  {{~#if @root.repository}}
    {{~#if @root.host}}
      {{[email protected]}}/
    {{~/if}}
    {{~#if @root.owner}}
      {{[email protected]}}/
    {{~/if}}
    {{[email protected]}}
  {{~else}}
    {{[email protected]}}
  {{~/if}}/
  {{[email protected]}}/{{hash}}))
{{~else}}
  {{~hash}}
{{~/if}}

{{~!-- commit references --}}
{{~#if references~}}
  , closes
  {{~#each references}} {{#if @root.linkReferences~}}
    [
    {{~#if this.owner}}
      {{~this.owner}}/
    {{~/if}}
    {{~this.repository}}#{{this.issue}}](
    {{~#if @root.repository}}
      {{~#if @root.host}}
        {{[email protected]}}/
      {{~/if}}
      {{~#if this.repository}}
        {{~#if this.owner}}
          {{~this.owner}}/
        {{~/if}}
        {{~this.repository}}
      {{~else}}
        {{~#if @root.owner}}
          {{[email protected]}}/
        {{~/if}}
          {{[email protected]}}
        {{~/if}}
    {{~else}}
      {{[email protected]}}
    {{~/if}}/
    {{[email protected]}}/{{this.issue}})
  {{~else}}
    {{~#if this.owner}}
      {{~this.owner}}/
    {{~/if}}
    {{~this.repository}}#{{this.issue}}
  {{~/if}}{{/each}}
{{~/if}}

{{#if body}}

  {{~body}}
{{~/if}}
{{#if footer}}

  {{~footer}}
{{~/if}}

@Lydanne
Copy link

Lydanne commented Jul 21, 2023

@webpro tnx for the hint! That did work. Documented in #36TNX的提示!这确实奏效了。记录在 #36

However, I had some issues with variables. I'm using the conventionalcommits preset, so I thought I can use as basis the template from: https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-conventionalcommits/templates/commit.hbs但是,我在变量方面遇到了一些问题。我正在使用 conventionalcommits 预设,所以我认为我可以使用以下模板作为基础:https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-conventionalcommits/templates/commit.hbs But the variable commitUrlFormat was not resolved.但是变量 commitUrlFormat 没有解决。 I did then create a template as a mixture https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-conventionalcommits/templates/commit.hbs and https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-writer/templates/commit.hbs and got that resolved.然后,我确实创建了一个模板作为 https://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-conventionalcommits/templates/commit.hbshttps://github.com/conventional-changelog/conventional-changelog/blob/master/packages/conventional-changelog-writer/templates/commit.hbs 的混合,并解决了这个问题。

Btw, there is quite a number of packages involved for writing the changelog!顺便说一句,编写更改日志涉及相当多的软件包!

This is my full .release-it.js config:这是我的完整 .release-it.js 配置:

const fs = require("fs");
const path = require("path");

const commits_template = fs
    .readFileSync(path.resolve(".release-it", "conventional-changelog-commit.hbs"))
    .toString();

module.exports = {
    npm: {
        publish: true,
    },
    git: {
        requireBranch: "master",
        commitMessage: "Release new version.",
        commitArgs: ["-n"],
    },
    plugins: {
        "@release-it/conventional-changelog": {
            infile: "CHANGES.md",
            preset: {
                name: "conventionalcommits",
                types: [
                    {
                        type: "breaking",
                        section: "Breaking Changes",
                    },
                    {
                        type: "feat",
                        section: "Features",
                    },
                    {
                        type: "fix",
                        section: "Bug Fixes",
                    },
                    {
                        type: "maint",
                        section: "Maintenance",
                    },
                ],
            },
            writerOpts: {
                commitPartial: commits_template,
            },
        },
    },
};

This is my template:这是我的模板:

*{{#if scope}} **{{scope}}:**
{{~/if}} {{#if subject}}
  {{~subject}}
{{~else}}
  {{~header}}
{{~/if}}

{{~!-- commit link --}} {{#if @root.linkReferences~}}
  ([{{shortHash}}](
  {{~#if @root.repository}}
    {{~#if @root.host}}
      {{[email protected]}}/
    {{~/if}}
    {{~#if @root.owner}}
      {{[email protected]}}/
    {{~/if}}
    {{[email protected]}}
  {{~else}}
    {{[email protected]}}
  {{~/if}}/
  {{[email protected]}}/{{hash}}))
{{~else}}
  {{~hash}}
{{~/if}}

{{~!-- commit references --}}
{{~#if references~}}
  , closes
  {{~#each references}} {{#if @root.linkReferences~}}
    [
    {{~#if this.owner}}
      {{~this.owner}}/
    {{~/if}}
    {{~this.repository}}#{{this.issue}}](
    {{~#if @root.repository}}
      {{~#if @root.host}}
        {{[email protected]}}/
      {{~/if}}
      {{~#if this.repository}}
        {{~#if this.owner}}
          {{~this.owner}}/
        {{~/if}}
        {{~this.repository}}
      {{~else}}
        {{~#if @root.owner}}
          {{[email protected]}}/
        {{~/if}}
          {{[email protected]}}
        {{~/if}}
    {{~else}}
      {{[email protected]}}
    {{~/if}}/
    {{[email protected]}}/{{this.issue}})
  {{~else}}
    {{~#if this.owner}}
      {{~this.owner}}/
    {{~/if}}
    {{~this.repository}}#{{this.issue}}
  {{~/if}}{{/each}}
{{~/if}}

{{#if body}}

  {{~body}}
{{~/if}}
{{#if footer}}

  {{~footer}}
{{~/if}}

Where to find a tutorial on writing this template?

@thet
Copy link
Contributor Author

thet commented Jul 21, 2023

@WumaCoder I didn't find a tutorial for that but read through the available options https://github.com/release-it/conventional-changelog/blob/master/README.md to configure conventional-commits and copy pasted an existing template and tweaked it via trail and error and reading source code.

But the template syntax is handlebars and you can find info over here: https://handlebarsjs.com/guide/

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

Successfully merging a pull request may close this issue.

3 participants