@ordinarysh/docker-config
Version:
Docker configurations for Next.js applications with multi-stage builds and optimization
104 lines (93 loc) • 3.16 kB
JavaScript
import { readFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = dirname(fileURLToPath(import.meta.url))
/**
* Get the path to a Docker template file
* @param {string} template - Template name (nextjs, node)
* @returns {string} Absolute path to template
*/
export function getDockerfilePath(template) {
return join(__dirname, 'templates', `Dockerfile.${template}`)
}
/**
* Get the content of a Docker template
* @param {string} template - Template name (nextjs, node)
* @returns {string} Dockerfile content
* @throws {Error} When template doesn't exist or cannot be read
*/
export function getDockerfileContent(template) {
const validTemplates = ['nextjs', 'node']
if (!validTemplates.includes(template)) {
throw new Error(
`Invalid template "${template}". Available templates: ${validTemplates.join(', ')}`
)
}
const templatePath = getDockerfilePath(template)
try {
return readFileSync(templatePath, 'utf8')
} catch (error) {
throw new Error(
`Failed to read Docker template "${template}": ${error instanceof Error ? error.message : String(error)}`
)
}
}
/**
* Get the path to the .dockerignore template
* @returns {string} Absolute path to .dockerignore
*/
export function getDockerIgnorePath() {
return join(__dirname, 'templates', '.dockerignore')
}
/**
* Get the content of the .dockerignore template
* @returns {string} .dockerignore content
* @throws {Error} When template cannot be read
*/
export function getDockerIgnoreContent() {
const dockerIgnorePath = getDockerIgnorePath()
try {
return readFileSync(dockerIgnorePath, 'utf8')
} catch (error) {
throw new Error(
`Failed to read .dockerignore template: ${error instanceof Error ? error.message : String(error)}`
)
}
}
/**
* Get the path to a docker-compose template
* @param {string} template - Template name (test)
* @returns {string} Absolute path to template
*/
export function getDockerComposePath(template = 'test') {
return join(__dirname, 'templates', `docker-compose.${template}.yml`)
}
/**
* Get the content of a docker-compose template
* @param {string} template - Template name (test)
* @returns {string} Docker compose content
* @throws {Error} When template doesn't exist or cannot be read
*/
export function getDockerComposeContent(template = 'test') {
const validTemplates = ['test']
if (!validTemplates.includes(template)) {
throw new Error(
`Invalid docker-compose template "${template}". Available templates: ${validTemplates.join(', ')}`
)
}
const composePath = getDockerComposePath(template)
try {
return readFileSync(composePath, 'utf8')
} catch (error) {
throw new Error(
`Failed to read docker-compose template "${template}": ${error instanceof Error ? error.message : String(error)}`
)
}
}
// Convenience exports
export const templates = {
nextjs: () => getDockerfileContent('nextjs'),
node: () => getDockerfileContent('node'),
dockerignore: () => getDockerIgnoreContent(),
dockerCompose: () => getDockerComposeContent('test'),
}