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

have cli watch template file for changes #91

Merged
merged 7 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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