Skip to content

Commit

Permalink
Merge pull request #239 from jaredwray/adding-in-serve-command
Browse files Browse the repository at this point in the history
adding in serve command
  • Loading branch information
jaredwray authored Jan 18, 2024
2 parents 0771f31 + bc8a5e5 commit 7a1903d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/writr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export default class Writr {
this._options = value;
}

public get server(): http.Server | undefined {
return this._server;
}

public get configFileModule(): any {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this._configFileModule;
Expand Down Expand Up @@ -84,7 +88,9 @@ export default class Writr {
}

case 'serve': {
this._console.log('Serving...');
const builder = new WritrBuilder(this.options);
await builder.build();
await this.serve(this.options);
break;
}

Expand Down Expand Up @@ -156,8 +162,8 @@ export default class Writr {
}

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

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

Expand Down
51 changes: 51 additions & 0 deletions test/writr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,57 @@ describe('writr execute', () => {
expect(consoleMessage).toContain('.');
console.log = consoleLog;
});
it('should serve the site', async () => {
const options = new WritrOptions();
options.sitePath = 'test/fixtures/single-page-site';
options.outputPath = 'test/fixtures/single-page-site/dist3';
const writr = new Writr(options);
process.argv = ['node', 'writr', 'serve'];

try {
await writr.execute(process);
} finally {
fs.rmdirSync(options.outputPath, {recursive: true});
if (writr.server) {
writr.server.close();
}
}
});
it('should serve the site and reset the server if exists', async () => {
const options = new WritrOptions();
options.sitePath = 'test/fixtures/single-page-site';
options.outputPath = 'test/fixtures/single-page-site/dist3';
const writr = new Writr(options);
process.argv = ['node', 'writr', 'serve'];

try {
await writr.serve(options);
await writr.execute(process);
} finally {
fs.rmdirSync(options.outputPath, {recursive: true});
if (writr.server) {
writr.server.close();
}
}
});
it('should serve the site on a specified port', async () => {
const options = new WritrOptions();
options.sitePath = 'test/fixtures/single-page-site';
options.outputPath = 'test/fixtures/single-page-site/dist3';
const writr = new Writr(options);
process.argv = ['node', 'writr', 'serve', '-p', '8181'];

try {
await writr.execute(process);

expect(writr.server).toBeDefined();
} finally {
fs.rmdirSync(options.outputPath, {recursive: true});
if (writr.server) {
writr.server.close();
}
}
});
});

describe('writr config file', () => {
Expand Down

0 comments on commit 7a1903d

Please sign in to comment.