@viewdo/dxp-story-cli
Version:
DXP Story Management CLI
217 lines • 9.15 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileService = void 0;
const ConsoleService_1 = require("./ConsoleService");
const typedi_1 = require("typedi");
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const trash_1 = __importDefault(require("trash"));
const prettier_1 = __importDefault(require("prettier"));
const getAllFiles = (dirPath, arrayOfFiles = []) => {
let files = fs_1.default.readdirSync(dirPath);
files.forEach((file) => {
if (fs_1.default.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles);
}
else {
arrayOfFiles.push(path_1.default.join(dirPath, "/", file));
}
});
return arrayOfFiles;
};
let FileService = class FileService {
constructor(console) {
this.console = console;
}
// File API -------------------------------
exists(file_path) {
let full_path = this._getFullPath(file_path);
this.console.debug(`Checking file path: ${full_path}`.grey);
return fs_1.default.existsSync(full_path);
}
read(file_path) {
let full_path = this._getFullPath(file_path);
this.console.debug(`Reading file path: ${full_path} (rel: ${file_path})`.grey);
if (this.exists(file_path))
return fs_1.default.readFileSync(full_path, 'utf8');
}
hasContent(file_path) {
let full_path = this._getFullPath(file_path);
this.console.debug(`Checking content for file path: ${full_path}`);
if (this.exists(file_path)) {
const content = fs_1.default.readFileSync(full_path, 'utf8');
return content.length > 0;
}
return false;
}
ensure(file_path) {
if (!this.exists(file_path))
this.write(file_path);
}
rename(file_path, new_file_path) {
if (!this.exists(file_path))
throw new Error(`File does not exist at ${file_path}`);
fs_1.default.renameSync(file_path, new_file_path);
}
write(file_path, content = '') {
let full_path = this._getFullPath(file_path);
this.console.debug(`Writing file path: ${full_path}`.grey);
let directory = path_1.default.dirname(full_path);
this.console.debug(`-> directory: ${directory}`.grey);
try {
fs_1.default.mkdirSync(directory, {
recursive: true
});
fs_1.default.writeFileSync(full_path, content, 'utf8');
}
catch (e) {
this.console.error(e);
}
}
listFiles(file_path) {
return getAllFiles(file_path, []);
}
listDirectories(dirPath) {
let files = fs_1.default.readdirSync(dirPath);
let arrayOfDirectories = [];
files.forEach((file) => {
if (fs_1.default.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfDirectories.push(file);
}
});
return arrayOfDirectories;
}
copyFile(source, target, overwrite) {
var targetFile = target;
// If target is a directory, a new file with the same name will be created
if (fs_1.default.existsSync(target)) {
if (fs_1.default.lstatSync(target).isDirectory()) {
targetFile = path_1.default.join(target, path_1.default.basename(source));
}
}
if (fs_1.default.existsSync(targetFile) && !overwrite)
return;
fs_1.default.copyFileSync(source, targetFile);
}
syncDirectories(source, target, overwrite) {
try {
// test if source exists
fs_1.default.accessSync(source);
// Check if folder needs to be created or integrated
if (target != process.cwd()) {
if (!fs_1.default.existsSync(target)) {
fs_1.default.mkdirSync(target, {
recursive: true
});
}
}
// Copy
if (fs_1.default.lstatSync(source).isDirectory()) {
let files = fs_1.default.readdirSync(source);
files.forEach((file) => {
var curSource = path_1.default.join(source, file);
var targetFolder = path_1.default.resolve(target, path_1.default.basename(curSource));
if (fs_1.default.lstatSync(curSource).isDirectory()) {
this.syncDirectories(curSource, targetFolder, overwrite);
}
else {
this.copyFile(curSource, targetFolder, overwrite);
}
});
}
}
catch (err) {
this.console.debug('sync directories err: ' + err);
}
}
delete(file_path) {
let full_path = this._getFullPath(file_path);
if (this.exists(file_path)) {
this.console.debug('Deleting file path: ' + full_path);
return fs_1.default.unlinkSync(full_path);
}
}
removeDir(file_path) {
return __awaiter(this, void 0, void 0, function* () {
let full_path = this._getFullPath(file_path);
if (fs_1.default.existsSync(file_path)) {
this.console.debug('Deleting director: ' + full_path);
return (0, trash_1.default)(full_path)
.catch(e => this.console.error(e));
}
});
}
_getFullPath(file_path) {
if (path_1.default.isAbsolute(file_path))
return file_path;
if (file_path.startsWith('~'))
return path_1.default.join(os_1.default.homedir(), path_1.default.normalize(file_path.replace('~/', '')));
if (file_path.startsWith('./'))
return path_1.default.join(process.cwd(), file_path.replace('./', '/'));
return file_path;
}
_clear() {
// do nothing, used during testing
}
getFilesInDirectory(dir, baseDir = dir) {
let fileList = [];
try {
fs_1.default.readdirSync(dir).forEach(file => {
const filePath = path_1.default.join(dir, file);
const stat = fs_1.default.statSync(filePath);
if (stat.isDirectory()) {
fileList = fileList.concat(this.getFilesInDirectory(filePath, baseDir));
}
else {
fileList.push({
file: file,
path: path_1.default.relative(baseDir, dir)
});
}
});
}
catch (err) {
this.console.debug(`Unable to read directory ${dir} : ${err}`);
}
return fileList;
}
parseKeyToPathAndFile(key) {
const segments = key.split('--');
const file = segments.pop(); // Remove and store the last segment as the file
const path = segments.join('/'); // Join the remaining segments with '/'
return { file, path };
}
formatHtml(content) {
return __awaiter(this, void 0, void 0, function* () {
return yield prettier_1.default.format(content, { parser: "html" });
});
}
};
exports.FileService = FileService;
exports.FileService = FileService = __decorate([
(0, typedi_1.Service)(),
__metadata("design:paramtypes", [ConsoleService_1.ConsoleService])
], FileService);
//# sourceMappingURL=FileService.js.map