backend/lib/seed/devops/deploy-v2.js
2025-05-14 21:45:16 +02:00

99 lines
3.3 KiB
JavaScript

/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/explicit-function-return-type */
/* eslint-disable @typescript-eslint/no-var-requires */
const execa = require('execa');
const Listr = require('listr');
const package = require('../../../package.json');
const fs = require('fs');
if (!package.devops) throw `No devops configuration found in package.json`;
if (!package.devops.services) throw `No devops services found in package.json`;
if (!package.devops.awsProfile) throw `No devops awsProfile found in package.json`;
if (!package.devops.awsRegion) throw 'No devops awsRegion found in package.json';
// Config input
const paths = package.devops.services;
const awsProfile = package.devops.awsProfile;
const awsRegion = package.devops.awsRegion;
// Arguments checks
const stage = process.argv[2] || 'dev';
const sName = process.argv[3] || 'all';
const deployBase = function(basePath) {
const baseDirectory = basePath[0];
const serverlessFile = baseDirectory + '/' + basePath[1];
let handlerFile;
if (basePath[2]) handlerFile = baseDirectory + '/' + basePath[2];
else handlerFile = baseDirectory + '/' + 'handler.ts';
return new Listr(
[
{
title: 'Remove handler file',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
task: () => execa('rm', ['-f', 'handler.ts']).then((result) => {}),
},
{
title: 'Copy handler file',
task: () => {
console.log('handler file here', handlerFile);
execa('cp', [handlerFile, 'handler.ts']).then((result) => {});
},
},
{
title: 'Deploy serverless file',
task: async () => {
console.log('serverless file here', serverlessFile);
const subprocess = execa(
'npx',
['sls', 'deploy', '--config', serverlessFile, '--stage', stage, '--aws-profile', awsProfile, '--region', awsRegion],
{ env: { 'SLS_DEBUG=*': '*' } },
);
console.log('deploying logs here', '.slslogs.txt');
subprocess.stdout.pipe(fs.createWriteStream('.slslogs.txt', { flags: 'a' }));
const { stdout } = await subprocess;
},
},
{
title: 'Remove handler file',
task: () => execa('rm', ['-f', 'handler.ts']).then((result) => {}),
},
],
{ concurrent: false },
);
};
let functions = Object.keys(paths);
if (sName != 'all') {
functions = sName.split(',');
for (let index = 0; index < functions.length; index++) {
if (!paths.hasOwnProperty(functions[index])) throw `Bad functions name for ${functions[index]}`;
}
}
const tasksList = [];
for (let index = 0; index < functions.length; index++) {
const element = functions[index];
tasksList.push({
title: 'Deploying' + ' ' + element,
task: () => {
return deployBase(paths[element]);
},
});
}
const tasks = new Listr(tasksList, { concurrent: false });
tasks.run().catch((err) => {
console.error(err);
});