Skip to content

Commit

Permalink
adding in loading the config file
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Dec 7, 2023
1 parent a8748b2 commit d2dd896
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 65 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"inquirer": "^9.2.11",
"js-yaml": "^4.1.0",
"keyv": "^4.5.4",
"ts-node": "^10.9.1",
"update-notifier": "^7.0.0"
},
"devDependencies": {
Expand All @@ -52,7 +53,6 @@
"@types/update-notifier": "^6.0.8",
"@vitest/coverage-v8": "^0.34.6",
"rimraf": "^5.0.5",
"ts-node": "^10.9.1",
"typescript": "^5.2.2",
"vitest": "^0.34.6",
"webpack": "^5.89.0",
Expand Down
16 changes: 3 additions & 13 deletions site/writr.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {WritrHelpers} from '../dist/writr.js';

export const writrOptions = {
export const options = {
templatePath: './template',
outputPath: './dist',
sitePath: './site',
Expand All @@ -11,13 +9,5 @@ export const writrOptions = {
};

export function onPrepare(writrOptions?: any) {
// Copy the template to the site path
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,
);
}
console.log('onPrepare');
};
44 changes: 43 additions & 1 deletion src/writr.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fs from 'fs-extra';
import updateNotifier from 'update-notifier';
import {register} from 'ts-node';
import packageJson from '../package.json';
import {WritrOptions} from './options.js';
import {WritrConsole} from './console.js';

export default class Writr {
private _options: WritrOptions = new WritrOptions();
private readonly _console: WritrConsole = new WritrConsole();
private _configFileModule: any = {};

constructor(options?: WritrOptions) {
if (options) {
Expand All @@ -22,10 +24,33 @@ export default class Writr {
this._options = value;
}

public execute(process: NodeJS.Process): void {
public get configFileModule(): any {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this._configFileModule;
}

public async execute(process: NodeJS.Process): Promise<void> {
// Check for updates
updateNotifier({pkg: packageJson}).notify();

// Load the Config File
await this.loadConfigFile(this.options.sitePath);

// Parse the config file
if (this._configFileModule.options) {
this.options.parseOptions(this._configFileModule.options as Record<string, any>);
}

// Run the onPrepare function
try {
if (this._configFileModule.onPrepare) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
await this._configFileModule.onPrepare(this.options);
}
} catch (error) {
this._console.error((error as Error).message);
}

const consoleProcess = this._console.parseProcessArgv(process.argv);

// Update options
Expand Down Expand Up @@ -104,6 +129,23 @@ export default class Writr {
const packageObject = JSON.parse(packageJson) as {version: string};
return packageObject.version;
}

public async loadConfigFile(sitePath: string): Promise<void> {
if (fs.existsSync(sitePath)) {
const isTypescript = fs.existsSync(`${sitePath}/writr.config.ts`);
const configFile = isTypescript ? `${sitePath}/writr.config.ts` : `${sitePath}/writr.config.js`;
if (isTypescript) {
// Typescript
register({transpileOnly: true});
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this._configFileModule = await import(configFile);
} else {
// Javascript
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this._configFileModule = await import(configFile);
}
}
}
}

export {WritrHelpers} from './helpers.js';
9 changes: 9 additions & 0 deletions test/fixtures/multi-page-site/writr.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports.options = {
templatePath: './template',
outputPath: './dist-js',
sitePath: './site',
githubPath: 'jaredwray/writr',
siteTitle: 'Writr',
siteDescription: 'Beautiful Website for Your Projects',
siteUrl: 'https://writr.org',
};
24 changes: 0 additions & 24 deletions test/fixtures/multi-page-site/writr.config.ts

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/single-page-site-error/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/single-page-site-error/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions test/fixtures/single-page-site-error/writr.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

export const options = {
templatePath: './template',
outputPath: './dist',
sitePath: './site',
githubPath: 'jaredwray/writr',
siteTitle: 'Writr',
siteDescription: 'Beautiful Website for Your Projects',
siteUrl: 'https://writr.org',
};

export function onPrepare(writrOptions?: any) {
throw new Error('onPrepare Error');
}
14 changes: 2 additions & 12 deletions test/fixtures/single-page-site/writr.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

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

export const writrOptions = {
export const options = {
templatePath: './template',
outputPath: './dist',
sitePath: './site',
Expand All @@ -12,13 +10,5 @@ export const writrOptions = {
};

export function onPrepare(writrOptions?: any) {
// Copy the template to the site path
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,
);
console.log('onPrepare');
}
69 changes: 55 additions & 14 deletions test/writr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('writr', () => {
const writrHelpers = new WritrHelpers();
expect(writrHelpers.createDoc).toBeDefined();
});
it('if no parameters then it should build', () => {
it('if no parameters then it should build', async () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';
Expand All @@ -59,7 +59,7 @@ describe('writr', () => {
}
};

writr.execute(process);
await writr.execute(process);

expect(consoleMessage).toContain('Build');
console.log = consoleLog;
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('writr', () => {
});

describe('writr execute', () => {
it('should be able to execute with no parameters', () => {
it('should be able to execute with no parameters', async () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';
Expand All @@ -141,12 +141,12 @@ describe('writr execute', () => {
}
};

writr.execute(process);
await writr.execute(process);

expect(consoleMessage).toContain('Build');
console.log = consoleLog;
});
it('should init based on the init command', () => {
it('should init based on the init command', async () => {
const writr = new Writr(defaultOptions);
const sitePath = './custom-site';
let consoleMessage = '';
Expand All @@ -158,7 +158,7 @@ describe('writr execute', () => {

process.argv = ['node', 'writr', 'init', '-s', sitePath];
try {
writr.execute(process);
await writr.execute(process);
expect(fs.existsSync(sitePath)).toEqual(true);
expect(fs.existsSync(`${sitePath}/writr.config.ts`)).toEqual(true);
expect(consoleMessage).toContain('Writr initialized.');
Expand All @@ -167,7 +167,7 @@ describe('writr execute', () => {
console.log = consoleLog;
}
});
it('should build based on the build command', () => {
it('should build based on the build command', async () => {
const writr = new Writr(defaultOptions);
const sitePath = './custom-site/dist';
const consoleLog = console.log;
Expand All @@ -179,11 +179,11 @@ describe('writr execute', () => {
}
};

writr.execute(process);
await writr.execute(process);
expect(consoleMessage).toContain('Build');
console.log = consoleLog;
});
it('should print help command', () => {
it('should print help command', async () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';
Expand All @@ -194,11 +194,11 @@ describe('writr execute', () => {
}
};

writr.execute(process);
await writr.execute(process);
expect(consoleMessage).toContain('Usage:');
console.log = consoleLog;
});
it('should show version by the version command', () => {
it('should show version by the version command', async () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';
Expand All @@ -209,11 +209,11 @@ describe('writr execute', () => {
}
};

writr.execute(process);
await writr.execute(process);
expect(consoleMessage).toContain('.');
console.log = consoleLog;
});
it('should execute serve based on the serve command', () => {
it('should execute serve based on the serve command', async () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';
Expand All @@ -224,8 +224,49 @@ describe('writr execute', () => {
}
};

writr.execute(process);
await writr.execute(process);
expect(consoleMessage).toContain('Serve');
console.log = consoleLog;
});
});

describe('writr config file', () => {
it('should be able to load the config file', async () => {
const writr = new Writr(defaultOptions);
const sitePath = 'test/fixtures/multi-page-site';
await writr.loadConfigFile(sitePath);
expect(writr.configFileModule).toBeDefined();
expect(writr.configFileModule.options).toBeDefined();
});
it('should load the config and set the options', async () => {
const writr = new Writr(defaultOptions);
const sitePath = 'test/fixtures/multi-page-site';
await writr.loadConfigFile(sitePath);
expect(writr.configFileModule).toBeDefined();
expect(writr.configFileModule.options).toBeDefined();
process.argv = ['node', 'writr', 'serve'];
await writr.execute(process);
expect(writr.options.outputPath).toEqual(writr.configFileModule.options.outputPath);
});
it('should load the config and test the onPrepare', async () => {
const writr = new Writr(defaultOptions);
const sitePath = 'test/fixtures/single-page-site';
await writr.loadConfigFile(sitePath);
expect(writr.configFileModule).toBeDefined();
expect(writr.configFileModule.options).toBeDefined();
process.argv = ['node', 'writr', 'serve'];
await writr.execute(process);
expect(writr.options.outputPath).toEqual(writr.configFileModule.options.outputPath);
});
it('should throw error onPrepare', async () => {
const writr = new Writr(defaultOptions);
writr.options.sitePath = 'test/fixtures/single-page-site-error';
process.argv = ['node', 'writr', 'serve'];
try {
await writr.execute(process);
expect.fail('Should have thrown an error');
} catch (error) {
expect(error).toBeDefined();
}
});
});

0 comments on commit d2dd896

Please sign in to comment.