@tsclean/scaffold
Version:
This CLI creates an initial structure of a project based on clean architecture.
167 lines • 5.55 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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommandUtils = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const mkdirp_1 = require("mkdirp");
const messages_1 = require("../utils/messages");
const constants_1 = require("../utils/constants");
class CommandUtils {
/**
* Creates directories recursively
*
* @param directory
* @returns
*/
static createDirectories(directory) {
return (0, mkdirp_1.mkdirp)(directory, 0o777);
}
/**
* Creates a file with the given content in the given path
*
* @param filePath
* @param content
* @param override
* @returns
*/
static async createFile(filePath, content, override = true) {
await CommandUtils.createDirectories(path.dirname(filePath));
return new Promise((resolve, reject) => {
if (override === false && fs.existsSync(filePath))
return resolve();
fs.writeFile(filePath, content, err => err ? reject(err) : resolve());
});
}
/**
* Reads everything from a given file and returns its content as a string
*
* @param filePath
* @returns
*/
static async readFile(filePath) {
return new Promise((resolve, reject) => {
fs.readFile(filePath, (err, data) => err ? reject(err) : resolve(data.toString()));
});
}
/**
* @param filePath
* @returns
*/
static async fileExists(filePath) {
return fs.existsSync(filePath);
}
/**
* @param filePath
*/
static async deleteFile(filePath) {
return fs.unlink(filePath, (err) => err);
}
/**
* Capitalize string
* @param param
*/
static capitalizeString(param) {
let capitalize = param.replace(/(\b\w)/g, (str) => str.toUpperCase());
return capitalize.replace(/-/g, "");
}
/**
* @param param
*/
static transformStringToUpperCase(param) {
return param.toUpperCase();
}
/**
* Transforms the initial letter into lowercase
* @param param
*/
static transformInitialString(param) {
let stringInitial = param.replace(/(\b\w)/g, (str) => str.toUpperCase());
let lowerCaseString = stringInitial[0].toLowerCase() + stringInitial.substring(1);
return lowerCaseString.replace(/-/g, "");
}
/**
* Reads the files in the service implementation directory
* @param directory
*/
static injectServiceAdapter(directory) {
return new Promise((resolve, reject) => {
fs.readdir(directory, function (error, files) {
if (error)
return error;
resolve(files);
});
});
}
/**
*
* @param directory
* @param name
*/
static readModelFiles(directory, name) {
fs.readdir(directory, function (error, files) {
let fileExist;
const result = files.find(item => item.slice(0, -3) === name);
fileExist = (result === null || result === void 0 ? void 0 : result.slice(0, -3)) === name;
if (!fileExist) {
console.log(messages_1.MESSAGES.ERROR_MODEL(name));
process.exit(1);
}
});
}
/**
*
* @param directory
* @param manager
*/
static readManagerFiles(directory, manager) {
if (manager === constants_1.CONSTANTS.MYSQL || manager === constants_1.CONSTANTS.POSTGRES) {
fs.readdir(directory, function (error, files) {
if (!files === undefined) {
let flag;
const searchManager = files.slice(0)[0].split("-")[1];
flag = searchManager === manager;
if (!flag) {
console.log(messages_1.MESSAGES.ERROR_MANAGER(manager, searchManager));
process.exit(1);
}
}
return;
});
}
}
}
exports.CommandUtils = CommandUtils;
//# sourceMappingURL=CommandUtils.js.map