ph-dev-tools
Version:
Development Tools for PHibernate
56 lines • 2.37 kB
JavaScript
/**
* Created by Papa on 4/28/2016.
*/
"use strict";
const fs = require('fs');
const pathResolver_1 = require("../resolve/pathResolver");
class PathBuilder {
constructor(configuration) {
this.configuration = configuration;
this.dirExistanceMap = {};
this.workingDirPath = pathResolver_1.normalizePath(process.cwd());
this.sourceDirPath = this.workingDirPath + '/' + pathResolver_1.normalizePath(configuration.source.dir);
this.generatedDirPath = pathResolver_1.normalizePath(configuration.generated.client.dir);
this.usePathCache = configuration.cacheGeneratedPaths ? true : false;
}
getGenerationPathForFile(sourcePath) {
sourcePath = pathResolver_1.normalizePath(sourcePath);
let indexOfSourceDirInPath = sourcePath.indexOf(this.sourceDirPath);
if (indexOfSourceDirInPath !== 0) {
throw `Cannot generate file from source outside of root source dir`;
}
let sourceRelativePath = sourcePath.substr(this.sourceDirPath.length + 1);
return this.generatedDirPath + '/' + sourceRelativePath;
}
getFullPathToGeneratedSource(//
sourcePath //
) {
let generatedPath = this.getGenerationPathForFile(sourcePath);
return this.workingDirPath + '/' + generatedPath;
}
setupFileForGeneration(sourcePath) {
let generatedPath = this.getGenerationPathForFile(sourcePath);
let genPathFragments = generatedPath.split('/');
let currentPath = this.workingDirPath;
for (let i = 0; i < genPathFragments.length - 1; i++) {
currentPath += '/' + genPathFragments[i];
if (this.usePathCache && this.dirExistanceMap[currentPath]) {
continue;
}
let pathExists = fs.existsSync(currentPath);
if (!pathExists) {
fs.mkdirSync(currentPath);
}
else {
let pathStat = fs.statSync(currentPath);
if (!pathStat.isDirectory()) {
throw `'${currentPath}' is not a directory`;
}
}
this.dirExistanceMap[currentPath] = true;
}
return './' + generatedPath;
}
}
exports.PathBuilder = PathBuilder;
//# sourceMappingURL=PathBuilder.js.map