@coko/server
Version:
Reusable server for use by Coko's projects
107 lines • 4.66 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tempFolderPath = exports.ensureTempFolderExists = exports.writeFileToTemp = exports.emptyTemp = exports.deleteFileFromTemp = exports.readConfigurationFile = exports.findConfigurationFile = void 0;
const path_1 = __importDefault(require("path"));
const consumers_1 = require("stream/consumers");
const fs_extra_1 = __importDefault(require("fs-extra"));
const env_1 = require("./env");
const tempFolderPath = path_1.default.join(process.cwd(), 'tmp');
exports.tempFolderPath = tempFolderPath;
// returns path
const findConfigurationFile = (filename, options = {}) => {
const extensions = options.extensions || ['js', 'ts'];
const root = (0, env_1.env)('NODE_ENV') === 'production'
? path_1.default.join(process.cwd(), 'dist')
: process.cwd();
const basePath = options.basePath ? path_1.default.join(root, options.basePath) : root;
const foundFiles = extensions.reduce((found, ext) => {
const searchPath = path_1.default.join(basePath, `${filename}.${ext}`);
const exists = fs_extra_1.default.existsSync(searchPath) && fs_extra_1.default.statSync(searchPath).isFile();
if (exists)
found.push(searchPath);
return found;
}, []);
if (foundFiles.length === 0)
return null;
if (foundFiles.length > 1) {
throw new Error(`More than one files named ${filename} found at ${basePath}.`);
}
return foundFiles[0];
};
exports.findConfigurationFile = findConfigurationFile;
// returns component
const readConfigurationFile = async (filename, options = {}) => {
const filePath = findConfigurationFile(filename, options);
if (!filePath)
return null;
return await Promise.resolve(`${filePath}`).then(s => __importStar(require(s)));
};
exports.readConfigurationFile = readConfigurationFile;
const writeFileToTemp = async (readStream, filePath) => {
const outputPath = path_1.default.join(tempFolderPath, filePath);
await fs_extra_1.default.ensureFile(outputPath);
const isString = typeof readStream === 'string';
const isBase64 = isString && readStream.match(/[^:]\w+\/[\w\-+.]+(?=;base64,)/);
let dataBuffer;
if (isBase64) {
dataBuffer = Buffer.from(readStream.replace(/^data:.*;base64,/, ''), 'base64');
}
else if (isString) {
dataBuffer = Buffer.from(readStream, 'utf8');
}
else {
dataBuffer = await (0, consumers_1.buffer)(readStream);
}
await fs_extra_1.default.outputFile(outputPath, dataBuffer);
};
exports.writeFileToTemp = writeFileToTemp;
const deleteFileFromTemp = async (filePath) => {
const deletePath = path_1.default.join(tempFolderPath, filePath);
await fs_extra_1.default.remove(deletePath);
};
exports.deleteFileFromTemp = deleteFileFromTemp;
const emptyTemp = async () => {
await fs_extra_1.default.emptyDir(tempFolderPath);
};
exports.emptyTemp = emptyTemp;
const ensureTempFolderExists = async () => {
await fs_extra_1.default.ensureDir(tempFolderPath);
};
exports.ensureTempFolderExists = ensureTempFolderExists;
//# sourceMappingURL=filesystem.js.map