Skip to content

Commit

Permalink
adding in serve
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredwray committed Jan 16, 2024
1 parent 78348e6 commit 898cef0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
"dependencies": {
"axios": "^1.6.2",
"ecto": "^2.2.4",
"fastify": "^4.25.2",
"fastify-static": "^4.7.0",
"express": "^4.18.2",
"feed": "^4.2.2",
"fs-extra": "^11.2.0",
"gray-matter": "^4.0.3",
Expand All @@ -49,6 +48,7 @@
"update-notifier": "^7.0.0"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/fs-extra": "^11.0.4",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.10.5",
Expand Down
5 changes: 5 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class WritrOptions {
public siteTitle = 'Writr';
public siteDescription = 'Beautiful Website for Your Projects';
public siteUrl = 'https://writr.org';
public port = 3000;

constructor(options?: Record<string, unknown>) {
if (options) {
Expand Down Expand Up @@ -42,5 +43,9 @@ export class WritrOptions {
if (options.siteUrl) {
this.siteUrl = options.siteUrl as string;
}

if (options.port) {
this.port = options.port as number;
}
}
}
21 changes: 20 additions & 1 deletion src/writr.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type http from 'node:http';
import fs from 'fs-extra';
import updateNotifier from 'update-notifier';
import express from 'express';
import {register} from 'ts-node';
import packageJson from '../package.json';
import {WritrOptions} from './options.js';
Expand All @@ -10,6 +12,7 @@ export default class Writr {
private _options: WritrOptions = new WritrOptions();
private readonly _console: WritrConsole = new WritrConsole();
private _configFileModule: any = {};
private _server: http.Server | undefined;

constructor(options?: WritrOptions) {
if (options) {
Expand Down Expand Up @@ -81,7 +84,7 @@ export default class Writr {
}

case 'serve': {
this._console.log('Serve');
this._console.log('Serving...');
break;
}

Expand Down Expand Up @@ -146,6 +149,22 @@ export default class Writr {
}
}
}

public async serve(options: WritrOptions): Promise<void> {
if (this._server) {
this._server.close();
}

const app = express();
const port = options.port || 3000;
const outputPath = options.outputPath || './dist';

app.use(express.static(outputPath));

this._server = app.listen(port, () => {
this._console.log(`Writr listening at http://localhost:${port}`);
});
}
}

export {WritrHelpers} from './helpers.js';
2 changes: 2 additions & 0 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ describe('WritrOptions', () => {
siteTitle: 'Custom Title',
siteDescription: 'Custom Description',
siteUrl: 'https://custom-url.com',
port: 8080,
});
expect(options.templatePath).toEqual('./custom-template');
expect(options.outputPath).toEqual('./custom-dist');
Expand All @@ -101,6 +102,7 @@ describe('WritrOptions', () => {
expect(options.siteTitle).toEqual('Custom Title');
expect(options.siteDescription).toEqual('Custom Description');
expect(options.siteUrl).toEqual('https://custom-url.com');
expect(options.port).toEqual(8080);
});
});
});
21 changes: 3 additions & 18 deletions test/writr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,6 @@ describe('writr execute', () => {
expect(consoleMessage).toContain('.');
console.log = consoleLog;
});
it('should execute serve based on the serve command', async () => {
const writr = new Writr(defaultOptions);
const consoleLog = console.log;
let consoleMessage = '';
process.argv = ['node', 'writr', 'serve'];
console.log = message => {
if (typeof message === 'string') {
consoleMessage = message;
}
};

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

describe('writr config file', () => {
Expand All @@ -268,7 +253,7 @@ describe('writr config file', () => {
}
};

process.argv = ['node', 'writr', 'serve'];
process.argv = ['node', 'writr', 'version'];
await writr.execute(process);
expect(writr.options.outputPath).toEqual(writr.configFileModule.options.outputPath);
console.log = consoleLog;
Expand All @@ -287,7 +272,7 @@ describe('writr config file', () => {
}
};

process.argv = ['node', 'writr', 'serve'];
process.argv = ['node', 'writr', 'version'];
await writr.execute(process);
expect(writr.options.outputPath).toEqual(writr.configFileModule.options.outputPath);
console.log = consoleLog;
Expand All @@ -311,7 +296,7 @@ describe('writr config file', () => {
}
};

process.argv = ['node', 'writr', 'serve'];
process.argv = ['node', 'writr', 'version'];
try {
await writr.execute(process);
expect.fail('Should have thrown an error');
Expand Down

0 comments on commit 898cef0

Please sign in to comment.