Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"templates"
],
"engines": {
"node": "22 || 24 || 26"
"node": "^22.12.0 || 24 || 26"
},
"repository": {
"type": "git",
Expand All @@ -55,7 +55,7 @@
"dependencies": {
"@types/connect": "^3.4.35",
"basic-auth-connect": "^1.1.0",
"commander": "^10.0.0",
"commander": "^15.1.0",
"compression": "^1.7.0",
"connect": "^3.7.0",
"destroy": "^1.0.4",
Expand Down
140 changes: 92 additions & 48 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ interface ServeOptions {
debug?: boolean;
}

/**
* Optional dependency overrides for constructing the CLI command.
*/
export interface CreateCliOptions {
serverFactory?: typeof server;
cwd?: string;
env?: Record<string, string>;
}

const PORT = "3474";
const HOSTNAME = "localhost";
const CONFIG_FILENAME = ["superstatic.json", "firebase.json"];
Expand All @@ -48,62 +57,97 @@ const pkgVersion =
? rawPkg.version
: "0.0.0";

let env: Record<string, string> | undefined = undefined;
try {
const parsedEnv: unknown = JSON.parse(
fs.readFileSync(path.resolve(ENV_FILENAME), "utf8"),
);
if (isPlainObject(parsedEnv)) {
const stringEnv: Record<string, string> = {};
for (const [key, value] of Object.entries(parsedEnv)) {
if (
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean"
) {
stringEnv[key] = String(value);
function loadEnv(cwd?: string): Record<string, string> | undefined {
try {
const envPath = cwd
? path.resolve(cwd, ENV_FILENAME)
: path.resolve(ENV_FILENAME);
const parsedEnv: unknown = JSON.parse(fs.readFileSync(envPath, "utf8"));
if (isPlainObject(parsedEnv)) {
const stringEnv: Record<string, string> = {};
for (const [key, value] of Object.entries(parsedEnv)) {
if (
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean"
) {
stringEnv[key] = String(value);
}
}
return stringEnv;
}
env = stringEnv;
} catch {
// do nothing
}
} catch {
// do nothing
return undefined;
}

const cli = new Command();
/**
* Creates a configured Superstatic CLI Commander instance.
* @param cliOptions optional overrides for testing.
* @returns configured Commander program.
*/
export function createCli(cliOptions: CreateCliOptions = {}): Command {
const serverFactory = cliOptions.serverFactory ?? server;
const env = "env" in cliOptions ? cliOptions.env : loadEnv(cliOptions.cwd);

const program = new Command();

cli.name("superstatic").version(pkgVersion, "-v, --version");
program.name("superstatic").version(pkgVersion, "-v, --version");

cli
.command("serve", { isDefault: true })
.argument("[folder]")
.option("-p, --port <port>", "Port", PORT)
.option("--host, --hostname <hostname>", "Hostname", HOSTNAME)
.option("-c, --config <config>", "Filename of config", CONFIG_FILENAME)
.option("--debug")
.option("--gzip")
.option("--compression")
.description("start server")
.action((folder: string | undefined, options: ServeOptions) => {
return new Promise<void>((resolve) => {
const parsedPort =
options.port !== undefined ? Number(options.port) : undefined;
const isValidPort = parsedPort !== undefined && !Number.isNaN(parsedPort);
const app = server({
cwd: path.join(process.cwd(), folder ?? ""),
config: options.config,
port: isValidPort ? parsedPort : Number(PORT),
hostname: options.hostname,
compression: options.compression,
gzip: options.gzip,
debug: options.debug,
env: env,
program
.command("serve", { isDefault: true })
.argument("[folder]")
.option("-p, --port <port>", "Port", PORT)
.option("--host, --hostname <hostname>", "Hostname", HOSTNAME)
.option("-c, --config <config>", "Filename of config", CONFIG_FILENAME)
.option("--debug")
.option("--gzip")
.option("--compression")
.description("start server")
.action((folder: string | undefined, options: ServeOptions) => {
return new Promise<void>((resolve) => {
const trimmedPort = options.port?.trim();
const parsedPort =
trimmedPort !== undefined && trimmedPort !== ""
? Number(trimmedPort)
: undefined;
const isValidPort =
parsedPort !== undefined &&
Number.isInteger(parsedPort) &&
parsedPort >= 0 &&
parsedPort <= 65535;
const port = isValidPort ? parsedPort : Number(PORT);
const baseCwd = cliOptions.cwd ?? process.cwd();
const app = serverFactory({
cwd: path.join(baseCwd, folder ?? ""),
config: options.config,
port,
hostname: options.hostname,
compression: options.compression,
gzip: options.gzip,
debug: options.debug,
env: env,
});
let httpServer: ReturnType<typeof app.listen> | undefined = undefined;
httpServer = app.listen(() => {
const address =
typeof httpServer?.address === "function"
? httpServer.address()
: null;
const actualPort =
address && typeof address === "object" ? address.port : port;
console.log(
`Superstatic started.\nVisit http://${options.hostname ?? HOSTNAME}:${actualPort} to view your app.`,
);
resolve();
});
});
app.listen(() => resolve());
console.log(
`Superstatic started.\nVisit http://${options.hostname ?? HOSTNAME}:${isValidPort ? parsedPort : PORT} to view your app.`,
);
});
});

return program;
}

const cli = createCli();

export default cli;
Loading
Loading