2022-08-24 19:25:27 +02:00
|
|
|
#!/usr/bin/env node
|
2023-03-21 18:37:22 +01:00
|
|
|
const fs = require('fs').promises;
|
2021-02-04 19:15:23 +01:00
|
|
|
const path = require('path');
|
2019-11-22 18:03:51 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const chalk = require('chalk');
|
2020-07-10 05:15:29 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const createStaticServer = require('./create-static-server');
|
|
|
|
const { parsePort } = require('./lib/parse-port');
|
2019-11-22 18:03:51 +01:00
|
|
|
|
2023-03-21 18:37:22 +01:00
|
|
|
const fsStat = fs.stat;
|
2021-02-04 19:15:23 +01:00
|
|
|
const DEFAULT_PORT = 9080;
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
const onResponse = (request, response) => {
|
|
|
|
if (response.statusCode >= 400) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(
|
|
|
|
chalk`{gray '-->'} {red ${response.statusCode}} ${request.url}`,
|
|
|
|
);
|
2019-11-22 18:03:51 +01:00
|
|
|
} else if (response.statusCode >= 200 && response.statusCode < 300) {
|
2020-11-03 00:41:28 +01:00
|
|
|
console.log(
|
|
|
|
chalk`{gray '-->'} {green ${response.statusCode}} ${request.url}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-11-22 18:03:51 +01:00
|
|
|
} else {
|
2020-11-03 00:41:28 +01:00
|
|
|
console.log(
|
|
|
|
chalk`{gray '-->'} {green.dim ${response.statusCode}} ${request.url}`,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-11-22 18:03:51 +01:00
|
|
|
const onRequest = (request, response) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(chalk`{gray '<--'} {blue [${request.method}]} ${request.url}`);
|
|
|
|
response.on('finish', () => onResponse(request, response));
|
|
|
|
};
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
const startServer = ({ port, rootDirectory }) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const server = createStaticServer(rootDirectory);
|
2019-11-22 18:03:51 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
server.on('request', onRequest);
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
server.listen(port, () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(`Running at http://localhost:${port}`);
|
|
|
|
});
|
|
|
|
};
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
const parseDirectoryArgument = async (pathString) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const resolvedPath = path.resolve(pathString);
|
|
|
|
const directoryStats = await fsStat(resolvedPath);
|
2019-11-22 18:03:51 +01:00
|
|
|
if (!directoryStats.isDirectory()) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(`Invalid path '${pathString}'; must be a directory`);
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return resolvedPath;
|
|
|
|
};
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
const main = async () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const args = process.argv.slice(2);
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
port: process.env.port || DEFAULT_PORT,
|
|
|
|
rootDirectory: path.resolve('.'),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-11-22 18:03:51 +01:00
|
|
|
|
|
|
|
while (args.length) {
|
2020-11-03 00:41:28 +01:00
|
|
|
if (/^(--port|-p)$/u.test(args[0])) {
|
2019-11-22 18:03:51 +01:00
|
|
|
if (args[1] === undefined) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error('Missing port argument');
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
options.port = parsePort(args[1]);
|
|
|
|
args.splice(0, 2);
|
2019-11-22 18:03:51 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
options.rootDirectory = await parseDirectoryArgument(args[0]);
|
|
|
|
args.splice(0, 1);
|
2019-11-22 18:03:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
startServer(options);
|
|
|
|
};
|
2019-11-22 18:03:51 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
main().catch((error) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(error);
|
|
|
|
process.exit(1);
|
|
|
|
});
|