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

Feature: default to delete orphaned files, with -keepOrphanedFiles argument flag to disable feature #325

Merged
merged 9 commits into from
Dec 17, 2023
21 changes: 17 additions & 4 deletions cmd/templ/generatecmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ type Arguments struct {
IncludeVersion bool
IncludeTimestamp bool
// PPROFPort is the port to run the pprof server on.
PPROFPort int
PPROFPort int
KeepOrphanedFiles bool
}

var defaultWorkerCount = runtime.NumCPU()
Expand Down Expand Up @@ -124,7 +125,6 @@ func runCmd(ctx context.Context, w io.Writer, args Arguments) (err error) {
if args.Proxy != "" {
p = proxy.New(args.ProxyPort, target)
}

fmt.Fprintln(w, "Processing path:", args.Path)
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = time.Millisecond * 500
Expand All @@ -134,7 +134,7 @@ func runCmd(ctx context.Context, w io.Writer, args Arguments) (err error) {
var firstRunComplete bool
fileNameToLastModTime := make(map[string]time.Time)
for !firstRunComplete || args.Watch {
changesFound, errs := processChanges(ctx, w, fileNameToLastModTime, args.Path, args.GenerateSourceMapVisualisations, opts, args.WorkerCount)
changesFound, errs := processChanges(ctx, w, fileNameToLastModTime, args.Path, args.GenerateSourceMapVisualisations, opts, args.WorkerCount, args.KeepOrphanedFiles)
if len(errs) > 0 {
if errors.Is(errs[0], context.Canceled) {
return errs[0]
Expand Down Expand Up @@ -203,7 +203,7 @@ func shouldSkipDir(dir string) bool {
return false
}

func processChanges(ctx context.Context, stdout io.Writer, fileNameToLastModTime map[string]time.Time, path string, generateSourceMapVisualisations bool, opts []generator.GenerateOpt, maxWorkerCount int) (changesFound int, errs []error) {
func processChanges(ctx context.Context, stdout io.Writer, fileNameToLastModTime map[string]time.Time, path string, generateSourceMapVisualisations bool, opts []generator.GenerateOpt, maxWorkerCount int, keepOrphanedFiles bool) (changesFound int, errs []error) {
sem := make(chan struct{}, maxWorkerCount)
var wg sync.WaitGroup

Expand All @@ -220,6 +220,19 @@ func processChanges(ctx context.Context, stdout io.Writer, fileNameToLastModTime
if info.IsDir() {
return nil
}
if !keepOrphanedFiles && strings.HasSuffix(fileName, "_templ.go") {
// Make sure the generated file is orphaned
// by checking if the corresponding .templ file exists.
if _, err := os.Stat(strings.TrimSuffix(fileName, "_templ.go") + ".templ"); err == nil {
// The .templ file exists, so we don't delete the generated file.
return nil
}
if err = os.Remove(fileName); err != nil {
return fmt.Errorf("failed to remove file: %w", err)
}
logWarning(stdout, "Deleted orphaned file %q\n", fileName)
return nil
}
if strings.HasSuffix(fileName, ".templ") {
lastModTime := fileNameToLastModTime[fileName]
fileInfo, err := info.Info()
Expand Down
4 changes: 4 additions & 0 deletions cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Args:
Number of workers to use when generating code. (default runtime.NumCPUs)
-pprof
Port to run the pprof server on.
-keep-orphaned-files
Keeps orphaned generated templ files. (default false)
-help
Print help and exit.

Expand Down Expand Up @@ -120,6 +122,7 @@ func generateCmd(w io.Writer, args []string) (code int) {
proxyPortFlag := cmd.Int("proxyport", 7331, "")
workerCountFlag := cmd.Int("w", runtime.NumCPU(), "")
pprofPortFlag := cmd.Int("pprof", 0, "")
keepOrphanedFilesFlag := cmd.Bool("keep-orphaned-files", false, "")
helpFlag := cmd.Bool("help", false, "")
err := cmd.Parse(args)
if err != nil || *helpFlag {
Expand All @@ -138,6 +141,7 @@ func generateCmd(w io.Writer, args []string) (code int) {
IncludeVersion: *includeVersionFlag,
IncludeTimestamp: *includeTimestampFlag,
PPROFPort: *pprofPortFlag,
KeepOrphanedFiles: *keepOrphanedFilesFlag,
})
if err != nil {
color.New(color.FgRed).Fprint(w, "(✗) ")
Expand Down
Loading