iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌿

Simplifying Commands with Taskfile

に公開2件

Introduction

I use several commands regularly when writing Zenn articles.
In this post, I will try to simplify these commands using Taskfile.

https://taskfile.dev/

Preparing to use Taskfile

First, let's proceed with the installation.

The official documentation lists several methods, but I will use Homebrew since I am on a Mac.

brew install go-task

After installation, run the task command with the --init option to generate a Taskfile.yml.

task --init

Now we are ready to use Taskfile.

Defining Tasks

I will define the following zenn-cli commands as tasks.
(I just can't seem to memorize these commands 😭)

  • Creating a new article (npx zenn new:article)
  • Previewing (npx zenn preview)

I will also include textlint (npx textlint).

The task definitions in Taskfile.yml consist of the task name as the key under tasks, followed by cmds where the commands to execute are written.

Here is the actual implementation:

# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: '3'

silent: true

tasks:
  new:
    cmds:
      - npx zenn new:article --emoji 🌿 --published false
    desc: 'Add a new article'

  preview:
    cmds:
      - npx zenn preview
    desc: 'Preview content'

  textlint:
    cmds:
      - npx textlint -f pretty-error {{.CLI_ARGS}}
    ignore_error: true
    desc: 'Run textlint'

For creating a new article, I have specified emoji and publication status (set to private) as options.

silent: true is a setting to output only the command results.
desc is the task description.

When you run the task command with the --list (-l) option, it is used to display the description when listing the tasks.

task --list
task: Available tasks for this project:
* new:            Add a new article
* preview:        Preview content
* textlint:       Run textlint

{{.CLI_ARGS}} used in the textlint task is used to receive arguments from the command line.
ignore_error: true suppresses errors if textlint fails, allowing the command to continue to the end.

Using Tasks

You can execute a task by specifying the task name after the task command.

task new
created: articles/fda95a19a93757.md

The file is created with the specified emoji and private setting.

cat articles/fda95a19a93757.md
---
title: ""
emoji: "🌿"
type: "tech" # tech: Technical article / idea: Idea
topics: []
published: false
---

When running the textlint task, you need to specify the file name as an argument after --.

task textlint -- articles/fda95a19a93757.md

Summary

In this post, I converted the various commands I use when writing Zenn articles into tasks to try out Taskfile.

I didn't do anything complex; I just simplified the argument specification when running commands, but it is quite convenient to use.

Taskfile has many other useful features.
I look forward to reading the official documentation and utilizing it more!

https://taskfile.dev/docs/guide

GitHubで編集を提案

Discussion

rakiraki

手前味噌でアレなんですけど、zenn cli を使わなくてもテンプレート化したファイルをコピーしつつ日付をtaskfileで生成してファイル名にすると管理が楽です。

https://github.com/officel/zenn

なおなお

コメントありがとうございます

なるほど、確かにそのようなやり方もありますね

今後の改善の参考にしますね!