first pass
This commit is contained in:
174
cli/utils/config-manager.js
Normal file
174
cli/utils/config-manager.js
Normal file
@@ -0,0 +1,174 @@
|
||||
import { readFileSync, writeFileSync, existsSync } from 'fs';
|
||||
import { join, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
// Default configuration values
|
||||
const DEFAULT_CONFIG = {
|
||||
version: '1.0',
|
||||
project: {
|
||||
name: '',
|
||||
type: '',
|
||||
template: ''
|
||||
},
|
||||
build: {
|
||||
platform: 'linux/amd64',
|
||||
nodeVersion: '20',
|
||||
pythonVersion: '3.11',
|
||||
dotnetVersion: '9.0',
|
||||
entryPoint: '',
|
||||
buildCommand: null
|
||||
},
|
||||
runtime: {
|
||||
port: 3000,
|
||||
envFile: true,
|
||||
volumes: [],
|
||||
extraHosts: false
|
||||
},
|
||||
deployment: {
|
||||
sshHost: '',
|
||||
sshUser: '',
|
||||
sshKeyPath: '',
|
||||
targetPath: '',
|
||||
autoLoad: true,
|
||||
autoStart: true
|
||||
}
|
||||
};
|
||||
|
||||
// Global defaults (can be overridden per project)
|
||||
const GLOBAL_DEFAULTS = {
|
||||
deployment: {
|
||||
sshHost: '192.168.8.178',
|
||||
sshUser: 'deployer',
|
||||
targetRoot: '~/containers'
|
||||
},
|
||||
build: {
|
||||
platform: 'linux/amd64',
|
||||
nodeVersion: '20',
|
||||
pythonVersion: '3.11',
|
||||
dotnetVersion: '9.0'
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Load project configuration from docker-deployment.json
|
||||
*/
|
||||
export function loadProjectConfig(projectPath) {
|
||||
const configPath = join(projectPath, 'docker-deployment.json');
|
||||
|
||||
if (!existsSync(configPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const content = readFileSync(configPath, 'utf-8');
|
||||
return JSON.parse(content);
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to parse config at ${configPath}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save project configuration to docker-deployment.json
|
||||
*/
|
||||
export function saveProjectConfig(projectPath, config) {
|
||||
const configPath = join(projectPath, 'docker-deployment.json');
|
||||
const content = JSON.stringify(config, null, 2);
|
||||
writeFileSync(configPath, content, 'utf-8');
|
||||
return configPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load global configuration from the deployment tool directory
|
||||
*/
|
||||
export function loadGlobalConfig() {
|
||||
const globalConfigPath = join(__dirname, '..', '..', 'global-deployment-config.json');
|
||||
|
||||
if (!existsSync(globalConfigPath)) {
|
||||
return GLOBAL_DEFAULTS;
|
||||
}
|
||||
|
||||
try {
|
||||
const content = readFileSync(globalConfigPath, 'utf-8');
|
||||
return { ...GLOBAL_DEFAULTS, ...JSON.parse(content) };
|
||||
} catch (error) {
|
||||
return GLOBAL_DEFAULTS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save global configuration
|
||||
*/
|
||||
export function saveGlobalConfig(config) {
|
||||
const globalConfigPath = join(__dirname, '..', '..', 'global-deployment-config.json');
|
||||
const content = JSON.stringify(config, null, 2);
|
||||
writeFileSync(globalConfigPath, content, 'utf-8');
|
||||
return globalConfigPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new project configuration with defaults
|
||||
*/
|
||||
export function createProjectConfig(projectName, detection, overrides = {}) {
|
||||
const globalConfig = loadGlobalConfig();
|
||||
|
||||
const config = {
|
||||
...DEFAULT_CONFIG,
|
||||
project: {
|
||||
name: projectName,
|
||||
type: detection.type,
|
||||
template: detection.template
|
||||
},
|
||||
build: {
|
||||
...DEFAULT_CONFIG.build,
|
||||
...globalConfig.build,
|
||||
entryPoint: detection.entryPoint || '',
|
||||
buildCommand: detection.buildCommand || null
|
||||
},
|
||||
runtime: {
|
||||
...DEFAULT_CONFIG.runtime,
|
||||
port: detection.port || 3000
|
||||
},
|
||||
deployment: {
|
||||
...DEFAULT_CONFIG.deployment,
|
||||
sshHost: globalConfig.deployment?.sshHost || '',
|
||||
sshUser: globalConfig.deployment?.sshUser || '',
|
||||
targetPath: `${globalConfig.deployment?.targetRoot || '~/containers'}/${projectName}/files`
|
||||
}
|
||||
};
|
||||
|
||||
// Apply overrides
|
||||
if (overrides.port) config.runtime.port = overrides.port;
|
||||
if (overrides.name) config.project.name = overrides.name;
|
||||
if (overrides.sshHost) config.deployment.sshHost = overrides.sshHost;
|
||||
if (overrides.sshUser) config.deployment.sshUser = overrides.sshUser;
|
||||
if (overrides.volumes) config.runtime.volumes = overrides.volumes;
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the target path for deployment
|
||||
*/
|
||||
export function getTargetPath(config) {
|
||||
return config.deployment?.targetPath ||
|
||||
`~/containers/${config.project.name}/files`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge configs with priority: overrides > project > global > defaults
|
||||
*/
|
||||
export function mergeConfigs(projectConfig, overrides = {}) {
|
||||
const globalConfig = loadGlobalConfig();
|
||||
|
||||
return {
|
||||
...DEFAULT_CONFIG,
|
||||
...globalConfig,
|
||||
...projectConfig,
|
||||
...overrides
|
||||
};
|
||||
}
|
||||
|
||||
export { DEFAULT_CONFIG, GLOBAL_DEFAULTS };
|
||||
158
cli/utils/template-engine.js
Normal file
158
cli/utils/template-engine.js
Normal file
@@ -0,0 +1,158 @@
|
||||
import Handlebars from 'handlebars';
|
||||
import { readFileSync, existsSync } from 'fs';
|
||||
import { join, dirname } from 'path';
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
|
||||
// Templates directory
|
||||
const TEMPLATES_DIR = join(__dirname, '..', '..', 'templates');
|
||||
|
||||
// Register Handlebars helpers
|
||||
Handlebars.registerHelper('if_eq', function(a, b, options) {
|
||||
return a === b ? options.fn(this) : options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('if_includes', function(arr, value, options) {
|
||||
if (Array.isArray(arr) && arr.includes(value)) {
|
||||
return options.fn(this);
|
||||
}
|
||||
return options.inverse(this);
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('join', function(arr, separator) {
|
||||
if (Array.isArray(arr)) {
|
||||
return arr.join(separator);
|
||||
}
|
||||
return '';
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('lowercase', function(str) {
|
||||
return str ? str.toLowerCase() : '';
|
||||
});
|
||||
|
||||
Handlebars.registerHelper('sanitize', function(str) {
|
||||
// Convert to lowercase and replace invalid chars for Docker
|
||||
return str ? str.toLowerCase().replace(/[^a-z0-9-]/g, '-') : '';
|
||||
});
|
||||
|
||||
/**
|
||||
* Load a template file from the templates directory
|
||||
*/
|
||||
export function loadTemplate(templatePath) {
|
||||
const fullPath = join(TEMPLATES_DIR, templatePath);
|
||||
|
||||
if (!existsSync(fullPath)) {
|
||||
throw new Error(`Template not found: ${templatePath}`);
|
||||
}
|
||||
|
||||
return readFileSync(fullPath, 'utf-8');
|
||||
}
|
||||
|
||||
/**
|
||||
* Render a template with the given context
|
||||
*/
|
||||
export function renderTemplate(templateContent, context) {
|
||||
const template = Handlebars.compile(templateContent, { noEscape: true });
|
||||
return template(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load and render a template file
|
||||
*/
|
||||
export function processTemplate(templatePath, context) {
|
||||
const content = loadTemplate(templatePath);
|
||||
return renderTemplate(content, context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the template directory path for a given project type
|
||||
*/
|
||||
export function getTemplateDir(projectType) {
|
||||
// Map project types to template directories
|
||||
const typeMap = {
|
||||
'nodejs-express': 'nodejs/express',
|
||||
'nodejs-vite-react': 'nodejs/vite-react',
|
||||
'nodejs-vite-react-ssr': 'nodejs/vite-react-ssr',
|
||||
'nodejs-generic': 'nodejs/express',
|
||||
'python-standard': 'python/standard',
|
||||
'python-ml-pytorch': 'python/ml-pytorch',
|
||||
'dotnet-blazor': 'dotnet/blazor',
|
||||
'dotnet-webapi': 'dotnet/webapi',
|
||||
'static-nginx': 'static/nginx',
|
||||
'flutter-web': 'static/nginx'
|
||||
};
|
||||
|
||||
return typeMap[projectType] || 'nodejs/express';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all template files for a project type
|
||||
*/
|
||||
export function getTemplateFiles(projectType) {
|
||||
const templateDir = getTemplateDir(projectType);
|
||||
|
||||
const files = [
|
||||
{ template: `${templateDir}/Dockerfile.template`, output: 'Dockerfile' },
|
||||
{ template: `${templateDir}/docker-compose.yml.template`, output: 'docker-compose.yml' },
|
||||
{ template: `${templateDir}/.dockerignore.template`, output: '.dockerignore' }
|
||||
];
|
||||
|
||||
// Add nginx.conf for static sites
|
||||
if (projectType === 'static-nginx' || projectType === 'flutter-web') {
|
||||
files.push({ template: `${templateDir}/nginx.conf.template`, output: 'nginx.conf' });
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build template context from config
|
||||
*/
|
||||
export function buildTemplateContext(config, detection) {
|
||||
const projectName = config.project.name;
|
||||
const sanitizedName = projectName.toLowerCase().replace(/[^a-z0-9-]/g, '-');
|
||||
|
||||
return {
|
||||
// Project info
|
||||
PROJECT_NAME: sanitizedName,
|
||||
PROJECT_NAME_RAW: projectName,
|
||||
PROJECT_TYPE: config.project.type,
|
||||
|
||||
// Build settings
|
||||
NODE_VERSION: config.build.nodeVersion || '20',
|
||||
PYTHON_VERSION: config.build.pythonVersion || '3.11',
|
||||
DOTNET_VERSION: config.build.dotnetVersion || '9.0',
|
||||
PLATFORM: config.build.platform || 'linux/amd64',
|
||||
ENTRY_POINT: config.build.entryPoint || detection?.entryPoint || 'index.js',
|
||||
BUILD_COMMAND: config.build.buildCommand,
|
||||
|
||||
// Runtime settings
|
||||
PORT: config.runtime.port || 3000,
|
||||
USE_ENV_FILE: config.runtime.envFile !== false,
|
||||
VOLUMES: config.runtime.volumes || [],
|
||||
HAS_VOLUMES: (config.runtime.volumes || []).length > 0,
|
||||
EXTRA_HOSTS: config.runtime.extraHosts || false,
|
||||
|
||||
// Deployment settings
|
||||
SSH_HOST: config.deployment.sshHost || '',
|
||||
SSH_USER: config.deployment.sshUser || '',
|
||||
TARGET_PATH: config.deployment.targetPath || `~/containers/${sanitizedName}/files`,
|
||||
HAS_SSH: !!(config.deployment.sshHost && config.deployment.sshUser),
|
||||
|
||||
// Detection info
|
||||
HAS_BUILD_COMMAND: !!detection?.buildCommand,
|
||||
IS_SSR: config.project.type === 'nodejs-vite-react-ssr',
|
||||
IS_STATIC: config.project.type === 'static-nginx' || config.project.type === 'flutter-web',
|
||||
|
||||
// .NET specific
|
||||
CSPROJ_FILE: detection?.csprojFile || '',
|
||||
DLL_NAME: detection?.dllName || '',
|
||||
|
||||
// Data directory (for projects that need persistence)
|
||||
DATA_DIR: detection?.dataDir || 'data'
|
||||
};
|
||||
}
|
||||
|
||||
export { TEMPLATES_DIR };
|
||||
Reference in New Issue
Block a user