compose-as-code
Version:
Provides a module to express docker compose files as code
100 lines (99 loc) • 3.83 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeFile = exports.compileList = exports.compileObjectListWithId = exports.compileObject = exports.indent = exports.compileKeyValuePair = exports.createDirIfNotExisting = void 0;
const fs_1 = __importDefault(require("fs"));
const INDENTATION_CHARACTER = ' ';
const createDirIfNotExisting = (dirname) => {
if (!fs_1.default.existsSync(dirname)) {
fs_1.default.mkdirSync(dirname);
}
};
exports.createDirIfNotExisting = createDirIfNotExisting;
const compileKeyValuePair = (key, value, indentationDepth, options) => {
let result = '';
const quote = (options === null || options === void 0 ? void 0 : options.noQuotes) ? '' : '"';
result += (0, exports.indent)(indentationDepth);
result += `${key}: ${value ? `${quote}${value}${quote}` : ''}\n`;
return result;
};
exports.compileKeyValuePair = compileKeyValuePair;
const indent = (indentationDepth) => {
let indentationResult = '';
for (let i = 0; i < indentationDepth; i++) {
indentationResult += INDENTATION_CHARACTER;
}
return indentationResult;
};
exports.indent = indent;
const compileObject = (obj, baseIndentationDepth) => {
let result = '';
Object.keys(obj).forEach(key => {
const value = obj[key];
if (typeof value === 'number') {
result += `${(0, exports.indent)(baseIndentationDepth)}${key}: ${value}\n`;
}
else if (Array.isArray(value)) {
result += `${(0, exports.indent)(baseIndentationDepth)}${key}: ${(0, exports.compileList)(value, baseIndentationDepth + 1, { asSingleLineList: true })}\n`;
}
else if (typeof value === 'string') {
if (value.includes('\n')) {
const lines = value.split('\n');
lines.forEach((line, index) => {
if (index === 0) {
result += `${(0, exports.indent)(baseIndentationDepth)}${key}: ${line}\n`;
}
else {
result += ` ${(0, exports.indent)(baseIndentationDepth)}${line}\n`;
}
});
}
else {
result += `${(0, exports.indent)(baseIndentationDepth)}${key}: ${value}\n`;
}
}
else {
result += `${(0, exports.indent)(baseIndentationDepth)}${key}: ${value}\n`;
}
});
return result;
};
exports.compileObject = compileObject;
const compileObjectListWithId = (list, baseIndentationDepth) => {
let result = '';
list.forEach(entry => {
result += (0, exports.indent)(baseIndentationDepth);
result += `- ${entry.id}\n`;
});
return result;
};
exports.compileObjectListWithId = compileObjectListWithId;
const compileList = (list, baseIndentationDepth, options) => {
let result = '';
if (options === null || options === void 0 ? void 0 : options.asSingleLineList) {
result += '[';
list.forEach((entry, index) => {
result += `"${entry}"`;
if (index < list.length - 1) {
result += ', ';
}
});
result += ']';
}
else {
list.forEach(entry => {
result += (0, exports.indent)(baseIndentationDepth);
result += `- ${entry}\n`;
});
}
return result;
};
exports.compileList = compileList;
const writeFile = (config) => {
(0, exports.createDirIfNotExisting)(config.outputDir);
const resultFileName = `${config.outputDir}/${config.fileName}.yaml`;
fs_1.default.writeFileSync(resultFileName, config.content);
};
exports.writeFile = writeFile;