watchmod

package module
v0.0.0-...-d105f5d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2023 License: BSD-3-Clause Imports: 12 Imported by: 0

README

watchmod

For use with Go's templates, Sass, TypeScript, Javascript/CSS/HTML minification versioning, and anything else needing triggering while you dev.

We've found it useful for:

  • File Versioning (See FileVer)
  • Sass
  • TypeScript
  • esbuild
    • Javascript minification
    • Javascript bundling
  • CSS minification
  • HTML minification

How to

watchmod by default looks for a config at $PWD/watchmod.json5. When installed and a config exists, simply run

watchmod

For system wide install with Go:

go install cmd/watchmod.go

Also, $GOBIN should be in path (in something like .profile, PATH=$GOBIN:$PATH)

watchmod may be run without installing

go run cmd/main.go -config=$WATCH/watchmod.json5

Full Example Config:

{
	"WatchCommand":{
	"$WATCH":"$WATCH/example.sh",
	"$WATCH/test":"$WATCH/example.sh",
	},
	"ExcludeFiles":[
		"app.min.js",
	],
	"ExcludeStrings":[
		"min.js",
		"join.js",
		"map",
	],
	"RunCmdOnStart":true
}

Config Options

WatchCommand map[string]string

"WatchCommand" is in key:value.

"WatchCommand":{
	dir:command,
	dir:command,
}

Where dir is the directory or file to watch and command is the command to run.

ExcludeFiles []string

"ExcludeFiles" are the names of any files to exclude from triggering.

ExcludeStrings []string

"ExcludeStrings" are substrings of any file name to exclude. For example, if needed to exclude the resulting example.min.js file from triggering, "min.js" will result in it's exclusion.

RunCmdOnStart bool

"RunCmdOnStart" will run all commands from "WatchCommand" on start.

Flag options

  • config (default=config.json5) Location of config file.
  • daemon (default=true) If set to false, it will run commands in config and exit, instead of running in daemon mode and listening for changes.

JSON Errors

If command output is JSON, watchmod print the error if the JSON has the field "success" with the bool false and/or the field "error" with a not nil value. (If command output is not JSON it is not parsed.)

Command output that includes JSON of

{"success":false} 

Or

{"error":"call had an error"}

Results in a log like the following:

2023/05/23 13:58:52 ⚠️ watchmod command error: test/echo_error_test.sh
{"success":false,"foo":"bar",...}

Notes

  • Expands environmental vars in flags and config file.
  • Config support JSON5 for comments and trailing commas. Alternatively, config may be JSON as JSON5 is a superset of JSON.
  • Uses fsnotify to watch for file changes.
  • Inspired by qbit's boring project.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var FC flags
View Source
var FlagsParsed = false

Functions

func Excluded

func Excluded(fileName string) (excluded bool)

func ParseFlags

func ParseFlags()

func Run

func Run()
Example

ExampleRun

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"time"
)

// ExampleRun
func main() {
	ParseFlags()
	FC.Daemon = false
	Run()
}

// Create a file, write to it, and delete it.
func touch() {
	fileName := "temp.txt"
	_, err := os.Stat(fileName)
	if os.IsNotExist(err) {
		file, err := os.Create("temp.txt")
		if err != nil {
			log.Fatal(err)
		}
		defer file.Close()
	} else {
		currentTime := time.Now().Local()
		err = os.Chtimes(fileName, currentTime, currentTime)
		if err != nil {
			fmt.Println(err)
		}
	}

	b := []byte("hello watchmod!")
	err = ioutil.WriteFile(fileName, b, 0644)
	if err != nil {
		fmt.Println("Could not write to: " + fileName)
		return
	}

	e := os.Remove(fileName)
	if e != nil {
		fmt.Println("Could not remove: " + fileName)
	}
}
Output:

Config path: watchmod.json5
Flag `daemon` set to false.  Running commands in config and exiting.

func Watch

func Watch(dir, cmd string)

Watch is for each dir/cmd to watch/run.

Types

type Config

type Config struct {
	WatchCommand   map[string]string
	ExcludeFiles   []string
	ExcludeStrings []string
	RunCmdOnStart  bool
	PrintStdOut    bool
}
var C Config

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL