@routineless/nx-aws-cdk
Version:
Nx plugin for AWS CDK
106 lines • 3.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRunning = exports.TARGET_NAME = void 0;
const tslib_1 = require("tslib");
const events_1 = tslib_1.__importDefault(require("events"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const executors_1 = require("../../utils/executors");
const logger_1 = require("../../utils/logger");
exports.TARGET_NAME = 'localstack';
const isRunning = async (context) => {
const command = buildCommand({ command: 'ps' }, context, false);
const tmpLogsPath = `${context.root}/tmp/docker-ps-log.json`;
if (fs_1.default.existsSync(tmpLogsPath)) {
fs_1.default.rmSync(tmpLogsPath);
}
const logStream = fs_1.default.createWriteStream(tmpLogsPath);
await events_1.default.once(logStream, 'open');
await (0, executors_1.runCommand)(command, { stdio: [process.stdin, logStream, process.stderr] });
logStream.close();
const logsContent = fs_1.default.readFileSync(tmpLogsPath).toString();
if (!logsContent) {
return false;
}
try {
const dockerPsOutput = logsContent.split(/\r?\n/).map((line) => JSON.parse(line));
for (const container of dockerPsOutput) {
if (container.Image.includes('localstack') && container.State === 'running') {
return true;
}
}
}
catch (e) {
logger_1.logger.error(`Failed to parse docker ps output: ${e}`);
return false;
}
return false;
};
exports.isRunning = isRunning;
const buildCommand = (options, context, compose = true) => {
const pluginPath = `${context.root}/node_modules/@routineless/nx-aws-cdk`;
const executorPath = `${pluginPath}/src/executors/localstack`;
const routinelessDockerComposeFilePath = `${executorPath}/docker/docker-compose.yaml`;
let command = `docker`;
if (compose) {
command = `${command} compose`;
if (options.composeFile) {
command = `${command} -f ${context.root}/${options.composeFile}`;
}
else {
command = `${command} -f ${routinelessDockerComposeFilePath}`;
}
}
switch (options.command) {
case 'start':
command = `${command} up --wait`;
break;
case 'stop':
if (!options.preserveVolumes) {
command = `${command} down -v`;
}
else {
command = `${command} stop`;
}
break;
case 'ps':
command = `${command} ps --format json`;
break;
}
if (options.debug) {
command = `DEBUG=1 ${command}`;
}
if (options.containerName) {
command = `LOCALSTACK_DOCKER_NAME=${options.containerName} ${command}`;
}
if (options.volumeMountPath) {
command = `LOCALSTACK_VOLUME_DIR=${options.volumeMountPath} ${command}`;
}
return { command: command };
};
const localstackExecutor = async (options, context) => {
logger_1.logger.debug('Localstack executor options %s', JSON.stringify(options));
if (options.command === 'start') {
if (await (0, exports.isRunning)(context)) {
logger_1.logger.info('Localstack is already running');
return {
success: true,
};
}
logger_1.logger.info('Localstack is not running. Starting localstack');
}
const command = buildCommand(options, context);
try {
await (0, executors_1.runCommand)(command);
return {
success: true,
};
}
catch (e) {
logger_1.logger.error(`Failed to execute command: ${e}`);
return {
success: false,
};
}
};
exports.default = localstackExecutor;
//# sourceMappingURL=index.js.map