ucbuilder
Version:
:Shree Ganeshay Namah: new way app design
167 lines • 8.2 kB
JavaScript
import { ucUtil } from "../global/ucUtil.js";
import { IpcRendererHelper } from "../renderer/ipc/IpcRendererHelper.js";
import { nodeFn } from "../renderer/nodeFn.js";
import { UcDependencyScanner } from "./fileStates.js";
export class fileWatcher {
constructor(main) { this.main = main; }
main;
static renderer = IpcRendererHelper.Group('ucbuilder/src/build/fileWatcher.ts');
WATCH_LIST = {
removed: [],
modified: [],
moved: [],
};
clear() {
this.WATCH_LIST.modified.length =
this.WATCH_LIST.removed.length =
this.WATCH_LIST.moved.length = 0;
}
init() {
const _this = this;
fileWatcher.renderer.on("updates", (e, update) => {
this.doRecursion(update);
});
}
depScanner = new UcDependencyScanner(nodeFn.path, nodeFn.fs);
static getImportTypeRelativePath = (pth) => {
if (pth.startsWith('./') || pth.startsWith('../'))
return pth;
else
return `./${pth}`;
};
GET_REL_PATH = (update, InIndex, findable_filepath, _path) => {
const rtrn = {
isChanged: false,
path: undefined,
};
if (InIndex == -1) {
let fpath = nodeFn.path.resolveFilePath(findable_filepath, _path);
let findex = update.moved.findIndex(s => {
return nodeFn.path.isSamePath(s.from, fpath);
});
let REL_PATH = '';
if (findex != -1) {
REL_PATH = nodeFn.path.relativeFilePath(findable_filepath, update.moved[findex].to);
rtrn.isChanged = true;
rtrn.path = fileWatcher.getImportTypeRelativePath(REL_PATH);
}
}
else {
let isJsfile = _path.endsWith('.js');
//if (isJsfile && !_path.startsWith('./') && !_path.startsWith('../')) return rtrn;
let oldFullPath = nodeFn.path.resolveFilePath(findable_filepath, _path);
let OutIndex = update.moved.findIndex(s => {
return nodeFn.path.isSamePath(s.from, oldFullPath);
});
let IS_ALSO_IMPORT_POINT_TO_OLD_FILE = OutIndex != -1;
let REL_PATH = IS_ALSO_IMPORT_POINT_TO_OLD_FILE ?
nodeFn.path.relativeFilePath(update.moved[InIndex].to, update.moved[OutIndex].to)
:
nodeFn.path.relativeFilePath(update.moved[InIndex].to, oldFullPath);
rtrn.isChanged = true;
rtrn.path = isJsfile ? fileWatcher.getImportTypeRelativePath(REL_PATH) : REL_PATH;
}
return rtrn;
};
static PATTERN = /import\s+(.*?)\s+from\s+["'](\.{1,2}\/[^"']+\.js)["']|import\s*\(\s*["'](\.{1,2}\/[^"']+\.js)["']\s*\)|\s+x-from\s*=\s*"([^"]+\.html)"\s*|@import\s+["']([^"']+(?:\.css|\.scss))["']\s*;|@use\s+["']([^"']+(?:\.css|\.scss))["']\s*;|\{:\s*([^}]+)\}/g;
///(x-from)\s*=\s*"(.*?)"|(import\s+.*?)\s+from\s+["'](.*?)["'];|(@use|@import)\s["'](.*?)["'];|({:)(.*?)}/gi;
doRecursion = (updateStr) => {
const _builder = this.main;
const update = JSON.parse(updateStr);
const _this = this;
const changedFiles = new Map();
const pref = this.main.project.config.preference;
let bpath = nodeFn.path.join(_this.main.project.projectPath, pref.dirDeclaration[pref.srcDir].dirPath);
//console.log(update);
_builder.recursive(bpath, undefined, async (recursive_filepath) => {
if (fileWatcher.isValidFileForPathReplacer(recursive_filepath)) {
let ext = recursive_filepath.slice(recursive_filepath.lastIndexOf('.'));
if (!nodeFn.fs.existsSync(recursive_filepath)) {
console.log(recursive_filepath);
return;
}
let data = nodeFn.fs.readFileSync(recursive_filepath, 'binary');
let isChanged = false;
recursive_filepath = ucUtil.changeExtension(recursive_filepath, '.ts', '.js');
let InIndex = update.moved.findIndex(s => {
return nodeFn.path.isSamePath(s.to, recursive_filepath);
});
let IS_CURRENT_FILE_MOVED = InIndex != -1;
let findable_filepath = IS_CURRENT_FILE_MOVED ? update.moved[InIndex].from : recursive_filepath;
data = data.replace(fileWatcher.PATTERN, (fullMatch, tsfromModule, tsImport, // group 1
tsAsyncImport, // group 1
htmlXFrom, // group 2
scssImport, // group 3
scssUse, // group 4
customPath // group 5
) => {
if (tsImport !== undefined) {
let stts = this.GET_REL_PATH(update, InIndex, findable_filepath, tsImport);
if (stts.isChanged) {
isChanged = true;
return `import ${tsfromModule} from "${stts.path}"`;
}
}
if (tsAsyncImport !== undefined) {
let stts = this.GET_REL_PATH(update, InIndex, findable_filepath, tsAsyncImport);
if (stts.isChanged) {
isChanged = true;
return `import ("${stts.path}")`;
}
}
if (htmlXFrom !== undefined) {
let stts = this.GET_REL_PATH(update, InIndex, findable_filepath, htmlXFrom);
if (stts.isChanged) {
isChanged = true;
return ` x-from="${stts.path}" `;
}
}
if (scssImport !== undefined) {
let stts = this.GET_REL_PATH(update, InIndex, findable_filepath, scssImport);
if (stts.isChanged) {
isChanged = true;
return `@import "${stts.path}"; `;
}
}
if (scssUse !== undefined) {
let stts = this.GET_REL_PATH(update, InIndex, findable_filepath, scssUse);
if (stts.isChanged) {
isChanged = true;
return `@use "${stts.path}"; `;
}
}
if (customPath !== undefined) {
let stts = this.GET_REL_PATH(update, InIndex, findable_filepath, customPath);
if (stts.isChanged) {
isChanged = true;
return `{:${stts.path}}`;
}
}
return fullMatch;
});
recursive_filepath = ucUtil.changeExtension(recursive_filepath, '.js', '.ts');
if (isChanged)
changedFiles.set(recursive_filepath, data);
}
});
console.log(Array.from(changedFiles.keys()).join('\n') + '\nFiles Modified');
fileWatcher.renderer.sendSync('writeContents', [Object.fromEntries(changedFiles.entries())]);
_builder.buildALL();
};
isGenerating = false;
isCollectiong = false;
static isTSFile(filePath) { return filePath.match(/\.ts$/i) != null; }
static isHTMLFile(filePath) { return filePath.match(/\.uc\.html$|\.tpt\.html$/i) != null; }
static isUcHTMLFile(filePath) { return filePath.match(/\.uc\.html$/i) != null; }
static isSCSSFile(filePath) { return filePath.match(/\.scss$/i) != null; }
static isValidFileForPathReplacer(filePath) { return filePath.match(/\.ts$|\.scss$|\.html$/i) != null; }
startWatch() {
const pref = this.main.project.config.preference;
let bpath = nodeFn.path.join(this.main.project.projectPath, pref.dirDeclaration[pref.srcDir].dirPath);
fileWatcher.renderer.send("startWatch", [bpath]);
}
async stopWatch() {
return await fileWatcher.renderer.Invoke("stopWatch", []);
}
}
//# sourceMappingURL=fileWatcher.js.map