Skip to content

Commit

Permalink
Merge pull request #210 from jaredwray/fixing-helpers-to-be-more-func…
Browse files Browse the repository at this point in the history
…tional

fixing helpers to be more functional
  • Loading branch information
jaredwray authored Dec 3, 2023
2 parents 4026029 + ddced3e commit 8d9a81a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
15 changes: 7 additions & 8 deletions site/writr.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
import writr from '../dist/writr.js';

import writr, {WritrHelpers} from '../dist/writr.js';

export const writrOptions = {
templatePath: './template',
Expand All @@ -11,15 +11,14 @@ export const writrOptions = {
siteUrl: 'https://writr.org',
};

export async function onPrepare(writrOptions?: any) {
export function onPrepare(writrOptions?: any) {
// Copy the template to the site path

const removeImage = async (content: string) => content.replace('![Writr](site/logo.png)', '');

writr.helpers.createDoc(
const removeImage = (content: string) => content.replace('![Writr](site/logo.png)', '');
const writrHelpers = new WritrHelpers();
writrHelpers.createDoc(
'../README.md',
'./site/README.md',
undefined, // No need to set the front matter
[removeImage],
removeImage,
);
}
8 changes: 6 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs-extra';
import yaml from 'js-yaml';

export class WritrHelpers {
createDoc(path: string, destination: string, frontMatter: Record<string, string>, contentFn?: (content: string) => string): void {
createDoc(path: string, destination: string, frontMatter?: Record<string, string>, contentFn?: (content: string) => string): void {
const content = fs.readFileSync(path, 'utf8');

let newContent = this.setFrontMatterInContent(content, frontMatter);
Expand Down Expand Up @@ -38,7 +38,11 @@ export class WritrHelpers {
fs.writeFileSync(path, newContent, 'utf8');
}

setFrontMatterInContent(content: string, frontMatter: Record<string, string>): string {
setFrontMatterInContent(content: string, frontMatter?: Record<string, string>): string {
if (!frontMatter) {
return content;
}

const match = /^---\r?\n([\s\S]+?)\r?\n---\r?\n([\s\S]*)/.exec(content);

if (match) {
Expand Down
10 changes: 3 additions & 7 deletions src/writr.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {WritrOptions} from './options.js';
import {WritrHelpers} from './helpers.js';
import {WritrConsole} from './console.js';

export class Writr {
export default class Writr {
private _options: WritrOptions = new WritrOptions();
private readonly _helpers: WritrHelpers = new WritrHelpers();
private readonly _console: WritrConsole = new WritrConsole();

constructor(options?: WritrOptions) {
Expand All @@ -21,10 +19,6 @@ export class Writr {
this._options = value;
}

public get helpers(): WritrHelpers {
return this._helpers;
}

public execute(process: NodeJS.Process): void {
// Get the arguments
const args = process.argv.slice(2);
Expand All @@ -34,3 +28,5 @@ export class Writr {
}
}
}

export {WritrHelpers} from './helpers.js';
5 changes: 5 additions & 0 deletions test/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ describe('WritrHelpers', () => {
const newContent = helpers.setFrontMatterInContent('# Hello World', {title: 'Writr1'});
expect(newContent).toEqual('---\ntitle: Writr1\n---\n# Hello World');
});
it('should do nothing with no frontmatter', () => {
const helpers = new WritrHelpers();
const newContent = helpers.setFrontMatterInContent('# Hello World', undefined);
expect(newContent).toEqual('# Hello World');
});
});
describe('setFrontMatterToFile', () => {
it('should get and append FrontMatter to new file', () => {
Expand Down
8 changes: 5 additions & 3 deletions test/writr.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import process from 'node:process';
import {expect, it, describe} from 'vitest';
import {Writr} from '../src/writr.js';
import Writr, {WritrHelpers} from '../src/writr.js';
import {WritrOptions} from '../src/options.js';

const defaultOptions: WritrOptions = new WritrOptions({
Expand Down Expand Up @@ -39,11 +39,13 @@ describe('writr', () => {
});
it('should be able to get the helpers', () => {
const writr = new Writr(defaultOptions);
expect(writr.helpers).toBeDefined();
const writrHelpers = new WritrHelpers();
expect(writrHelpers).toBeDefined();
});
it('should be able to get the helpers via static', () => {
const writr = new Writr(defaultOptions);
expect(writr.helpers.createDoc).toBeDefined();
const writrHelpers = new WritrHelpers();
expect(writrHelpers.createDoc).toBeDefined();
});
it('if no parameters then it should print help', () => {
const writr = new Writr(defaultOptions);
Expand Down

0 comments on commit 8d9a81a

Please sign in to comment.