fast-deployment
Version:
A lightweight Node.js package for rapid deployment of Vue.js, Next.js, and Nuxt.js applications
76 lines (74 loc) • 3.05 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadConfig = loadConfig;
exports.getHtaccessContent = getHtaccessContent;
const dotenv_1 = __importDefault(require("dotenv"));
const fs_1 = __importDefault(require("fs"));
// Load .env file from the project where this package is installed
dotenv_1.default.config();
function loadConfig() {
const { DEPLOYMENT_SERVER_HOST, DEPLOYMENT_SERVER_USER, DEPLOYMENT_SERVER_DIR, DEPLOYMENT_FOLDER_NAME, DEPLOYMENT_APP_TYPE, DEPLOYMENT_HTACCESS_TEMPLATE, DEPLOYMENT_HEALTH_CHECK_URL, DEPLOYMENT_HEALTH_CHECK_STATUS, DEPLOYMENT_PM2_APP_NAME } = process.env;
// Validate required environment variables
if (!DEPLOYMENT_SERVER_HOST) {
throw new Error('Missing required environment variable: DEPLOYMENT_SERVER_HOST');
}
if (!DEPLOYMENT_SERVER_USER) {
throw new Error('Missing required environment variable: DEPLOYMENT_SERVER_USER');
}
if (!DEPLOYMENT_SERVER_DIR) {
throw new Error('Missing required environment variable: DEPLOYMENT_SERVER_DIR');
}
if (!DEPLOYMENT_FOLDER_NAME) {
throw new Error('Missing required environment variable: DEPLOYMENT_FOLDER_NAME');
}
// Validate application type
const appType = DEPLOYMENT_APP_TYPE?.toLowerCase();
if (!appType || !['vue', 'next', 'nuxt'].includes(appType)) {
throw new Error('Invalid or missing DEPLOYMENT_APP_TYPE. Must be one of: vue, next, nuxt');
}
// Parse health check status if provided
let healthCheckStatus;
if (DEPLOYMENT_HEALTH_CHECK_STATUS) {
healthCheckStatus = parseInt(DEPLOYMENT_HEALTH_CHECK_STATUS, 10);
if (isNaN(healthCheckStatus)) {
throw new Error('DEPLOYMENT_HEALTH_CHECK_STATUS must be a valid number');
}
}
// Return the configuration object
return {
serverHost: DEPLOYMENT_SERVER_HOST,
serverUser: DEPLOYMENT_SERVER_USER,
serverDir: DEPLOYMENT_SERVER_DIR,
folderName: DEPLOYMENT_FOLDER_NAME,
appType: appType,
htaccessTemplate: DEPLOYMENT_HTACCESS_TEMPLATE,
healthCheckUrl: DEPLOYMENT_HEALTH_CHECK_URL,
healthCheckStatus: healthCheckStatus,
pm2AppName: DEPLOYMENT_PM2_APP_NAME
};
}
function getHtaccessContent(htaccessTemplate) {
if (htaccessTemplate && fs_1.default.existsSync(htaccessTemplate)) {
return fs_1.default.readFileSync(htaccessTemplate, 'utf-8');
}
// Default .htaccess content if no template is provided
return `
# Enable URI navigation
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Redirect all requests to index.html
RewriteRule ^index\\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
# Deny access to files starting with a dot
<FilesMatch "^\\."
Require all denied
</FilesMatch>
`;
}