@magidoc/cli
Advanced tools
Comparing version 0.12.0 to 0.12.1
@@ -6,2 +6,3 @@ import { Option } from 'commander'; | ||
import { loadFileConfiguration } from '../utils/loadConfigFile.js'; | ||
import { templates } from '@magidoc/plugin-starter-variables'; | ||
@@ -22,5 +23,7 @@ function buildPreviewCommand(program) { | ||
await withStacktrace(stacktrace, () => { | ||
const root = fileConfiguration.website.options[templates.SITE_ROOT.name]; | ||
preview({ | ||
websiteLocation: path.resolve(fileConfiguration.website.output), | ||
port, | ||
siteRoot: root ? String(root) : undefined, | ||
}); | ||
@@ -27,0 +30,0 @@ }); |
export declare type PreviewConfig = { | ||
websiteLocation: string; | ||
port?: number; | ||
siteRoot?: string; | ||
}; |
@@ -5,3 +5,5 @@ import http from 'http'; | ||
const DEFAULT_PORT = 4000; | ||
function preview(config) { | ||
console.log(`⚠️ ${chalk.yellow('Preview command is not meant to be used for static file serving in production.')}`); | ||
const assets = sirv(config.websiteLocation, { | ||
@@ -12,11 +14,45 @@ maxAge: 31536000, | ||
const server = http.createServer((req, res) => { | ||
assets(req, res, () => { | ||
if (!config.siteRoot || req.url?.startsWith(config.siteRoot)) { | ||
req.url = req.url?.replace(config.siteRoot || '', ''); | ||
assets(req, res, () => { | ||
res.end(); | ||
}); | ||
} | ||
else { | ||
res.statusCode = 302; | ||
res.setHeader('Location', config.siteRoot); | ||
res.end(); | ||
}); | ||
} | ||
}); | ||
const port = config.port || 0; | ||
server.on('error', (error) => { | ||
if (error.message.includes('EADDRINUSE')) { | ||
if (config.port) { | ||
logError(`Could not start server... port ${chalk.cyan(config.port)} already in use.`); | ||
} | ||
else { | ||
// Use a random port since the default hardcoded port isn't free | ||
console.log(); | ||
console.log(`Port ${chalk.cyan(DEFAULT_PORT)} is occupied. Falling back to random port.`); | ||
server.close(); | ||
server.listen(0, 'localhost'); | ||
} | ||
} | ||
else { | ||
logError(`Could not start server... ${error.message}`); | ||
} | ||
}); | ||
if (config.port) { | ||
startApp(server, config.port); | ||
} | ||
else { | ||
startApp(server, 4000); | ||
} | ||
} | ||
function logError(message) { | ||
console.log(`${chalk.red('Error: ')} ${message}`); | ||
} | ||
function startApp(server, port) { | ||
server.listen(port, 'localhost', () => { | ||
const address = server.address(); | ||
const host = address.address === '127.0.0.1' ? 'localhost' : address.address; | ||
console.log(`⚠️ ${chalk.yellow('Preview command is not meant to be used for static file serving in production.')}`); | ||
console.log(); | ||
@@ -23,0 +59,0 @@ console.log(`Server listening on ${chalk.cyan(`http://${host}:${address.port}`)}`); |
@@ -5,3 +5,3 @@ { | ||
"private": false, | ||
"version": "0.12.0", | ||
"version": "0.12.1", | ||
"type": "module", | ||
@@ -20,4 +20,4 @@ "license": "MIT", | ||
"dependencies": { | ||
"@magidoc/plugin-starter-variables": "^0.12.0", | ||
"@magidoc/rollup-plugin-fetch-gql-schema": "^0.12.0", | ||
"@magidoc/plugin-starter-variables": "^0.12.1", | ||
"@magidoc/rollup-plugin-fetch-gql-schema": "^0.12.1", | ||
"axios": "^0.27.2", | ||
@@ -24,0 +24,0 @@ "chalk": "^5.0.1", |
@@ -13,2 +13,3 @@ import { Command, Option } from 'commander' | ||
import { loadFileConfiguration } from '../utils/loadConfigFile' | ||
import { templates } from '@magidoc/plugin-starter-variables' | ||
@@ -47,5 +48,7 @@ export default function buildPreviewCommand(program: Command) { | ||
await withStacktrace(stacktrace, () => { | ||
const root = fileConfiguration.website.options[templates.SITE_ROOT.name] | ||
preview({ | ||
websiteLocation: path.resolve(fileConfiguration.website.output), | ||
port, | ||
siteRoot: root ? String(root) : undefined, | ||
}) | ||
@@ -52,0 +55,0 @@ }) |
export type PreviewConfig = { | ||
websiteLocation: string | ||
port?: number | ||
siteRoot?: string | ||
} |
@@ -6,3 +6,12 @@ import type { PreviewConfig } from './config' | ||
import sirv from 'sirv' | ||
const DEFAULT_PORT = 4000 | ||
export default function preview(config: PreviewConfig) { | ||
console.log( | ||
`⚠️ ${chalk.yellow( | ||
'Preview command is not meant to be used for static file serving in production.', | ||
)}`, | ||
) | ||
const assets = sirv(config.websiteLocation, { | ||
@@ -14,8 +23,50 @@ maxAge: 31536000, // 1Y | ||
const server = http.createServer((req, res) => { | ||
assets(req, res, () => { | ||
if (!config.siteRoot || req.url?.startsWith(config.siteRoot)) { | ||
req.url = req.url?.replace(config.siteRoot || '', '') | ||
assets(req, res, () => { | ||
res.end() | ||
}) | ||
} else { | ||
res.statusCode = 302 | ||
res.setHeader('Location', config.siteRoot) | ||
res.end() | ||
}) | ||
} | ||
}) | ||
const port = config.port || 0 | ||
server.on('error', (error) => { | ||
if (error.message.includes('EADDRINUSE')) { | ||
if (config.port) { | ||
logError( | ||
`Could not start server... port ${chalk.cyan( | ||
config.port, | ||
)} already in use.`, | ||
) | ||
} else { | ||
// Use a random port since the default hardcoded port isn't free | ||
console.log() | ||
console.log( | ||
`Port ${chalk.cyan( | ||
DEFAULT_PORT, | ||
)} is occupied. Falling back to random port.`, | ||
) | ||
server.close() | ||
server.listen(0, 'localhost') | ||
} | ||
} else { | ||
logError(`Could not start server... ${error.message}`) | ||
} | ||
}) | ||
if (config.port) { | ||
startApp(server, config.port) | ||
} else { | ||
startApp(server, 4000) | ||
} | ||
} | ||
function logError(message: string) { | ||
console.log(`${chalk.red('Error: ')} ${message}`) | ||
} | ||
function startApp(server: http.Server, port: number) { | ||
server.listen(port, 'localhost', () => { | ||
@@ -25,7 +76,2 @@ const address = server.address() as AddressInfo | ||
console.log( | ||
`⚠️ ${chalk.yellow( | ||
'Preview command is not meant to be used for static file serving in production.', | ||
)}`, | ||
) | ||
console.log() | ||
@@ -32,0 +78,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
129211
2587