Skip to content

Commit

Permalink
have cli watch template file for changes (#91)
Browse files Browse the repository at this point in the history
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 #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.
  • Loading branch information
jrhizor authored Dec 22, 2022
1 parent 9621761 commit 7663f4b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
12 changes: 0 additions & 12 deletions .husky/pre-commit

This file was deleted.

5 changes: 5 additions & 0 deletions action/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

cd action
npx lint-staged
5 changes: 5 additions & 0 deletions cli/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

cd cli
npx lint-staged
4 changes: 2 additions & 2 deletions cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stoat",
"version": "0.0.7",
"version": "0.0.8",
"description": "Stoat CLI",
"main": "src/index.ts",
"bin": {
Expand All @@ -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",
Expand Down
69 changes: 66 additions & 3 deletions cli/src/helpers/local/configFileGlobal.ts
Original file line number Diff line number Diff line change
@@ -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<Template> => {
const { comment_template_file } = stoatConfig;
if (comment_template_file === undefined || comment_template_file === '') {
return getRemoteDefaultTemplate(ghOwner, ghRepo, stoatConfig);
}
return getLocalTemplate(comment_template_file);
};

interface TemplateWatcher {
commentTemplateFile: string;
fileWatcher: FSWatcher | undefined;
}

const getTemplateWatcher = async (
oldTemplateWatcher: TemplateWatcher,
commentTemplateFile: string
): Promise<TemplateWatcher> => {
if (oldTemplateWatcher !== undefined) {
if (oldTemplateWatcher.commentTemplateFile === commentTemplateFile) {
return oldTemplateWatcher;
} else {
oldTemplateWatcher.fileWatcher?.close();
}
}

if (commentTemplateFile === undefined || commentTemplateFile === '') {
return {
commentTemplateFile: commentTemplateFile,
fileWatcher: undefined
};
}

const gitRoot = findGitRoot(process.cwd());
const absolutePath = path.join(gitRoot, commentTemplateFile);
const fileWatcher = fs.watch(absolutePath, async (eventType: WatchEventType) => {
if (eventType === 'change') {
try {
await ConfigFileGlobal.update();
} catch (e) {
// do nothing
}
}
});

return {
commentTemplateFile: commentTemplateFile,
fileWatcher: fileWatcher
};
};

export default class ConfigFileGlobal {
private static schema: StoatConfigSchema | undefined;
private static template: Template | undefined;
private static templateWatcher: TemplateWatcher;

static async update() {
const configFilePath = findStoatConfigPath(process.cwd());
const stoatConfig = readStoatConfig(configFilePath);
this.schema = await getTypedStoatConfig(stoatConfig);
this.template = await getTemplate('', '', this.schema);
this.templateWatcher = await getTemplateWatcher(this.templateWatcher, stoatConfig.comment_template_file);
}

static getSchema() {
Expand Down

0 comments on commit 7663f4b

Please sign in to comment.