mongoose-management
Version:
Mongoose schemas management tool
110 lines (109 loc) • 3.86 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const helper_1 = require("../helper");
/**
*
*/
class File {
/**
*
* @param pathSource
* @param pathDestination
*/
constructor(pathSource, pathDestination) {
this.pathSource = pathSource;
this.pathDestination = pathDestination;
this.encoding = 'utf8';
this.fileAccess = 0o755;
this.folderAccess = 0o755;
this.types = {
index: { path: '', template: 'index' },
documents: { path: 'documents', template: 'document' },
interfaces: { path: 'interfaces', template: 'interface' },
models: { path: 'models', template: 'model' },
repositories: { path: 'repositories', template: 'repository' },
};
this.staticFiles = ['helper.ts', 'types.ts'];
}
/**
*
*/
createFolders() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield helper_1.exists(this.pathDestination);
}
catch (err1) {
yield helper_1.writable(path_1.resolve(this.pathDestination, '..'));
yield helper_1.mkdir(this.pathDestination, { mode: this.folderAccess });
}
yield helper_1.writable(this.pathDestination);
yield Promise.all(Object.entries(this.types)
.filter(([, { path }]) => path !== '')
.map(([, { path }]) => helper_1.mkdir(path_1.resolve(this.pathDestination, path), { recursive: true, mode: this.folderAccess })));
});
}
/**
*
*/
copyStaticFiles() {
return __awaiter(this, void 0, void 0, function* () {
yield Promise.all(this.staticFiles.map((file) => helper_1.copy(path_1.resolve(this.pathSource, 'statics', file), path_1.resolve(this.pathDestination, file))));
});
}
/**
*
* @param type
*/
read(type) {
return __awaiter(this, void 0, void 0, function* () {
const template = yield helper_1.readFile(path_1.resolve(this.pathSource, `${this.types[type].template}.mst`), {
encoding: this.encoding,
});
return template;
});
}
/**
*
* @param folder
* @param name
* @param data
*/
write(type, name, data) {
return __awaiter(this, void 0, void 0, function* () {
const path = path_1.resolve(this.pathDestination, this.types[type].path);
yield helper_1.writable(path);
yield helper_1.writeFile(path_1.resolve(path, `${name}.ts`), data, {
encoding: this.encoding,
});
});
}
/**
*
* @param type
* @param name
*/
exists(type, name) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield helper_1.exists(path_1.resolve(this.pathDestination, this.types[type].path, `${name}.ts`));
}
catch (e) {
// file does not exist
return false;
}
// file exists
return true;
});
}
}
exports.default = File;