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 3 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
2 changes: 1 addition & 1 deletion 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 Down
73 changes: 70 additions & 3 deletions cli/src/helpers/local/configFileGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,88 @@
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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick. The code is not auto formatted. husky is probably not working for cli.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to what I think is a better husky configuration. There's no root .husky now, only .husky/pre-commit for subprojects.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

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