@chuva.io/less-cli
Advanced tools
Comparing version 1.0.0-beta.3 to 1.0.0-beta.4
@@ -15,152 +15,145 @@ import AdmZip from 'adm-zip'; | ||
function loadEnvironmentVariables(configFile) { | ||
if (!fs.existsSync(configFile)) { | ||
throw `Config file not found: ${configFile}`; | ||
} | ||
if (!fs.existsSync(configFile)) { | ||
console.error(chalk.redBright(`Config file not found: ${configFile}`)); | ||
process.exit(1); | ||
} | ||
const configFileContent = fs.readFileSync(configFile, 'utf8'); | ||
const config = yaml.load(configFileContent); | ||
const configFileContent = fs.readFileSync(configFile, 'utf8'); | ||
const config = yaml.load(configFileContent); | ||
if (!config.hasOwnProperty('env_vars')) { | ||
throw `Key 'env_vars' not found in the config.less file`; | ||
} | ||
if (!config.hasOwnProperty('env_vars')) { | ||
console.error(chalk.redBright("Key 'env_vars' not found in the config.less file")); | ||
process.exit(1); | ||
} | ||
const keys = config.env_vars; | ||
const env_vars = {}; | ||
const keys = config.env_vars; | ||
const envVars = {}; | ||
for (const key of keys) { | ||
const value = process.env[key]; | ||
if (value === undefined) { | ||
throw `Environment variable '${key}' must be defined`; | ||
} | ||
env_vars[key] = value; | ||
for (const key of keys) { | ||
const value = process.env[key]; | ||
if (value === undefined) { | ||
console.error(chalk.redBright(`Environment variable '${key}' must be defined`)); | ||
process.exit(1); | ||
} | ||
envVars[key] = value; | ||
} | ||
return env_vars; | ||
return envVars; | ||
} | ||
async function deployProject(projectPath, projectName, envVars) { | ||
let connection_id; | ||
const tempZipFilename = 'temp_project.zip'; | ||
const zip = new AdmZip(); | ||
let connectionId; | ||
const tempZipFilename = 'temp_project.zip'; | ||
const zip = new AdmZip(); | ||
// Get specific files and directories | ||
const itemsToZip = glob.sync('{src,requirements.txt,yarn.lock,package.lock,config.less,package.json}', { | ||
cwd: projectPath | ||
}); | ||
const itemsToZip = glob.sync('{src,requirements.txt,yarn.lock,package.lock,config.less,package.json}', { | ||
cwd: projectPath, | ||
}); | ||
for (const item of itemsToZip) { | ||
const itemPath = path.join(projectPath, item); | ||
for (const item of itemsToZip) { | ||
const itemPath = path.join(projectPath, item); | ||
// Check if the item is a file or a directory | ||
if (fs.statSync(itemPath).isDirectory()) { | ||
zip.addLocalFolder(itemPath, item); // Add specific directory to zip | ||
} else { | ||
zip.addLocalFile(itemPath); // Add specific file to zip | ||
} | ||
if (fs.statSync(itemPath).isDirectory()) { | ||
zip.addLocalFolder(itemPath, item); | ||
} else { | ||
zip.addLocalFile(itemPath); | ||
} | ||
} | ||
await zip.writeZipPromise(tempZipFilename); | ||
await zip.writeZipPromise(tempZipFilename); | ||
const serverUrl = 'http://ec2-54-220-76-209.eu-west-1.compute.amazonaws.com:3000/v1/deploys'; | ||
const socket = new WebSocket('ws://ec2-54-220-76-209.eu-west-1.compute.amazonaws.com:3000'); | ||
const serverUrl = 'http://ec2-54-220-76-209.eu-west-1.compute.amazonaws.com:3000/v1/deploys'; | ||
const socket = new WebSocket('ws://ec2-54-220-76-209.eu-west-1.compute.amazonaws.com:3000'); | ||
// Open socket connection | ||
socket.on('open', async () => {}); | ||
socket.on('open', async () => { }); | ||
// Inside the 'message' event handler | ||
socket.on('message', async (data) => { | ||
const message = JSON.parse(data); | ||
socket.on('message', async (data) => { | ||
const message = JSON.parse(data); | ||
if (message.event === 'deploymentStatus') { | ||
const data = message.data; | ||
if (data?.status?.includes('Building...')) { | ||
spinner.stop(); | ||
} | ||
if (message.event === 'deploymentStatus') { | ||
const statusData = message.data; | ||
const { status, resources, error } = statusData; | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright(data?.status)); | ||
if (status?.includes('Building...')) { | ||
spinner.stop(); | ||
} | ||
if (data?.status?.includes('Deploy completed')) { | ||
socket.close(); | ||
console.log(chalk.yellowBright('[less-cli]'), '🇨🇻'); | ||
} | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright(status)); | ||
if (data?.status?.includes('Resources')) { | ||
const { apis, websockets } = data?.resources | ||
if (status?.includes('Deploy completed')) { | ||
console.log(chalk.yellowBright('[less-cli]'), '🇨🇻'); | ||
} | ||
if (apis?.length) { | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright('\t- API URLs')); | ||
apis.forEach(api => { | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright(`\t\t- ${api.api_name}: ${api.url}`)); | ||
}); | ||
} | ||
if (resources) { | ||
const { apis, websockets } = resources; | ||
if (websockets?.length) { | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright('\t- WEBSOCKET URLs')); | ||
websockets.forEach(websocket => { | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright(`\t\t- ${websocket.api_name}: ${websocket.url}`)); | ||
}); | ||
} | ||
if (apis?.length) { | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright('\t- API URLs')); | ||
apis.forEach(api => { | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright(`\t\t- ${api.api_name}: ${api.url}`)); | ||
}); | ||
} | ||
socket.close(); | ||
process.exit(1); | ||
} | ||
if (data?.status?.includes('Deploy failed') || data?.status?.includes('Build Failed')) { | ||
socket.close(); | ||
process.exit(1); | ||
} | ||
if (websockets?.length) { | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright('\t- WEBSOCKET URLs')); | ||
websockets.forEach(websocket => { | ||
console.log(chalk.yellowBright('[less-cli]'), chalk.greenBright(`\t\t- ${websocket.api_name}: ${websocket.url}`)); | ||
}); | ||
} | ||
if (message.event === 'contectionInfo') { | ||
connection_id = message.data?.connectionId; | ||
try { | ||
const formData = new FormData(); | ||
formData.append('zipFile', fs.createReadStream(tempZipFilename)); | ||
formData.append('env_vars', JSON.stringify(envVars)); | ||
socket.close(); | ||
process.exit(0); | ||
} | ||
const headers = { | ||
Authorization: `Bearer ${process.env.LESS_TOKEN}`, | ||
'Project-Name': projectName, | ||
connection_id, | ||
...formData.getHeaders() | ||
}; | ||
const response = await axios.post(serverUrl, formData, { headers }); | ||
if (response.status === 202) { | ||
return response.data; | ||
} | ||
} catch (error) { | ||
if(error?.response?.status === 401) { | ||
spinner.stop(); | ||
console.log(chalk.redBright('Error:'), 'Deployment failed with wrong credentials please check your Less token'); | ||
socket.close(); | ||
process.exit(1); | ||
} | ||
spinner.stop(); | ||
console.log(chalk.redBright('Error:'), 'Deployment failed'); | ||
socket.close(); | ||
process.exit(1); | ||
} finally { | ||
fs.unlinkSync(tempZipFilename); | ||
} | ||
if (error) { | ||
socket.close(); | ||
process.exit(1); // Non-success exit code for failure | ||
} | ||
} | ||
if (message.event === 'conectionInfo') { | ||
connectionId = message.data?.connectionId; | ||
try { | ||
const formData = new FormData(); | ||
formData.append('zipFile', fs.createReadStream(tempZipFilename)); | ||
formData.append('env_vars', JSON.stringify(envVars)); | ||
formData.append('project_name', JSON.stringify(projectName)); | ||
const headers = { | ||
Authorization: `Bearer ${process.env.LESS_TOKEN}`, | ||
'connection_id': connectionId, | ||
...formData.getHeaders(), | ||
}; | ||
const response = await axios.post(serverUrl, formData, { headers }); | ||
if (response.status === 202) { | ||
return response.data; | ||
} | ||
}); | ||
} catch (error) { | ||
spinner.stop(); | ||
console.error(chalk.redBright('Error:'), error?.response?.data || 'Deployment failed'); | ||
socket.close(); | ||
process.exit(1); // Non-success exit code for failure | ||
} finally { | ||
fs.unlinkSync(tempZipFilename); | ||
} | ||
} | ||
}); | ||
} | ||
export default async function deploy(projectName) { | ||
spinner.start('[less-cli] Connecting to the Less Server... ⚙️'); | ||
spinner.start(); | ||
try { | ||
const currentWorkingDirectory = process.cwd(); | ||
const configFile = path.join(currentWorkingDirectory, 'config.less'); | ||
const envVars = loadEnvironmentVariables(configFile); | ||
await deployProject(currentWorkingDirectory, projectName, envVars); | ||
} catch (error) { | ||
spinner.stop(); | ||
console.log(chalk.redBright('Error: '), error); | ||
process.exit(1); | ||
} | ||
}; | ||
spinner.start('[less-cli] Connecting to the Less Server... ⚙️'); | ||
spinner.start(); | ||
try { | ||
const currentWorkingDirectory = process.cwd(); | ||
const configFile = path.join(currentWorkingDirectory, 'config.less'); | ||
const envVars = loadEnvironmentVariables(configFile); | ||
await deployProject(currentWorkingDirectory, projectName, envVars); | ||
} catch (error) { | ||
spinner.stop(); | ||
console.error(chalk.redBright('Error: '), error.message || 'An error occurred'); | ||
process.exit(1); // Non-success exit code for any error | ||
} | ||
} |
{ | ||
"name": "@chuva.io/less-cli", | ||
"version": "1.0.0-beta.3", | ||
"version": "1.0.0-beta.4", | ||
"description": "Less is a framework that allows you to build distributed, infinitely scalable, event-driven, realtime, fault tolerant, and serverless REST APIs, Websockets, and Microservices with no DevOps and no infrastructure. This CLI allows you to deploy and monitor your Less projects.", | ||
@@ -5,0 +5,0 @@ "author": "Chuva, LLC", |
20197
155