From 7663f4b311067d2c1984c1f6a879c072c2dfeb59 Mon Sep 17 00:00:00 2001 From: Jared Rhizor Date: Thu, 22 Dec 2022 12:54:08 -0800 Subject: [PATCH] have cli watch template file for changes (#91) There was a bug previously where if this was run from a subdirectory, it would always use the default remote template, since it wouldn't find the local one where it expected. That's fixed in this PR. Also, while it would detect a change in specified template file in the Stoat config file, it wouldn't update automatically on changes within the template itself, which would require restarting `stoat local`. This wouldn't be that big of a deal except the one of the biggest reasons to use local mode is to fine tune the template. I created https://github.com/stoat-dev/stoat-action/issues/90 to track an issue that would make it trivial to locally iterate on templates including non-`static_hosting` plugins. Maybe we could even have a copy button on debug pages next to the merged configs that just gives you the command to run locally to preview that exact config. --- .husky/pre-commit | 12 ---- action/.husky/pre-commit | 5 ++ cli/.husky/pre-commit | 5 ++ cli/package.json | 4 +- cli/src/helpers/local/configFileGlobal.ts | 69 ++++++++++++++++++++++- 5 files changed, 78 insertions(+), 17 deletions(-) delete mode 100755 .husky/pre-commit create mode 100755 action/.husky/pre-commit create mode 100755 cli/.husky/pre-commit diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100755 index fcca4628..00000000 --- a/.husky/pre-commit +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env sh -. "$(dirname -- "$0")/_/husky.sh" - -( - cd action - npx lint-staged -) - -( - cd cli - npx lint-staged -) diff --git a/action/.husky/pre-commit b/action/.husky/pre-commit new file mode 100755 index 00000000..f195bec7 --- /dev/null +++ b/action/.husky/pre-commit @@ -0,0 +1,5 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +cd action +npx lint-staged diff --git a/cli/.husky/pre-commit b/cli/.husky/pre-commit new file mode 100755 index 00000000..e4220545 --- /dev/null +++ b/cli/.husky/pre-commit @@ -0,0 +1,5 @@ +#!/usr/bin/env sh +. "$(dirname -- "$0")/_/husky.sh" + +cd cli +npx lint-staged diff --git a/cli/package.json b/cli/package.json index 931e4fb9..ffc8a781 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "stoat", - "version": "0.0.7", + "version": "0.0.8", "description": "Stoat CLI", "main": "src/index.ts", "bin": { @@ -15,7 +15,7 @@ "build": "ncc build --source-map --license licenses.txt", "local": "yarn build && npm i -g", "test": "jest --coverage", - "prepare": "cd .. && husky install action/.husky" + "prepare": "cd .. && husky install cli/.husky" }, "repository": { "type": "git", diff --git a/cli/src/helpers/local/configFileGlobal.ts b/cli/src/helpers/local/configFileGlobal.ts index b1a7b5c1..b114fad3 100644 --- a/cli/src/helpers/local/configFileGlobal.ts +++ b/cli/src/helpers/local/configFileGlobal.ts @@ -1,21 +1,84 @@ import chalk from 'chalk'; -import fs from 'fs'; +import fs, { FSWatcher, readFileSync, WatchEventType } from 'fs'; import { getTypedStoatConfig, readStoatConfig } from '../../../../action/src/configHelpers'; import { StoatConfigSchema } from '../../../../action/src/schemas/stoatConfigSchema'; -import { getTemplate } from '../../../../action/src/templateHelpers'; +import { getRemoteDefaultTemplate, getTemplateFormat } from '../../../../action/src/templateHelpers'; import { Template } from '../../../../action/src/types'; -import { findStoatConfigPath } from '../pathHelpers'; +import { findGitRoot, findStoatConfigPath } from '../pathHelpers'; +import path from 'path'; + +// supports reading the local template from a subdirectory +const getLocalTemplate = (commentTemplatePath: string): Template => { + const gitRoot = findGitRoot(process.cwd()); + const fullCommentTemplateFile = path.join(gitRoot, commentTemplatePath); + const template = readFileSync(fullCommentTemplateFile).toString().trim(); + const format = getTemplateFormat(fullCommentTemplateFile); + return { template, format }; +}; + +// supports reading the local template from a subdirectory if necessary +const getTemplate = async (ghOwner: string, ghRepo: string, stoatConfig: StoatConfigSchema): Promise