chore: cleanup

This commit is contained in:
Nathan Walker
2025-07-14 08:45:36 -07:00
parent 4218cb8134
commit d9aa930ef8

View File

@ -1,4 +1,4 @@
import * as fs from '../file-system'; import { knownFolders, File, path, Folder } from '../file-system';
import { Trace } from '../trace'; import { Trace } from '../trace';
const cache = new Set<string>(); const cache = new Set<string>();
@ -19,13 +19,13 @@ function register(name: string, loader: (name?: string) => void) {
} }
} }
function processFile(file: fs.File) { function processFile(file: File) {
const filePathRelativeToApp = file.path.substr(fs.knownFolders.currentApp().path.length + 1); const filePathRelativeToApp = file.path.substring(knownFolders.currentApp().path.length + 1);
const loadContent = () => file.readTextSync(); const loadContent = () => file.readTextSync();
switch (file.extension.toLocaleLowerCase()) { switch (file.extension.toLocaleLowerCase()) {
case '.js': { case 'js': {
const noExtPath = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - '.js'.length); const noExtPath = filePathRelativeToApp.substring(0, filePathRelativeToApp.length - '.js'.length);
register(filePathRelativeToApp, function () { register(filePathRelativeToApp, function () {
return global.require(file.path); return global.require(file.path);
@ -36,11 +36,23 @@ function processFile(file: fs.File) {
break; break;
} }
case '.css': case 'mjs': {
const noExtPath = filePathRelativeToApp.substring(0, filePathRelativeToApp.length - '.mjs'.length);
register(filePathRelativeToApp, function () {
return global.require(file.path);
});
register(noExtPath, function () {
return global.require(file.path);
});
break;
}
case 'css':
register(filePathRelativeToApp, loadContent); register(filePathRelativeToApp, loadContent);
break; break;
case '.xml': case 'xml':
register(filePathRelativeToApp, loadContent); register(filePathRelativeToApp, loadContent);
break; break;
} }
@ -48,8 +60,8 @@ function processFile(file: fs.File) {
if (file.name === 'package.json') { if (file.name === 'package.json') {
const json = global.require(file.path); const json = global.require(file.path);
if (json.main) { if (json.main) {
const name = filePathRelativeToApp.substr(0, filePathRelativeToApp.length - 'package.json'.length - 1); const name = filePathRelativeToApp.substring(0, filePathRelativeToApp.length - 'package.json'.length - 1);
const requirePath = fs.path.join(file.parent.path, json.main); const requirePath = path.join(file.parent.path, json.main);
register(name, () => global.require(requirePath)); register(name, () => global.require(requirePath));
} }
@ -72,11 +84,11 @@ function processFolder(path: string): boolean {
let folderEmpty = true; let folderEmpty = true;
if (fs.Folder.exists(path)) { if (Folder.exists(path)) {
const folder = fs.Folder.fromPath(path); const folder = Folder.fromPath(path);
folder.eachEntity((file) => { folder.eachEntity((file) => {
if (file instanceof fs.File) { if (file instanceof File) {
processFile(file); processFile(file);
folderEmpty = false; folderEmpty = false;
} }
@ -99,15 +111,15 @@ export function registerModulesFromFileSystem(moduleName: string) {
let folderProcessed = false; let folderProcessed = false;
let parentFolderProcessed = false; let parentFolderProcessed = false;
// moduleName is a folder with package.json // moduleName is a folder with package.json
const path = fs.path.join(fs.knownFolders.currentApp().path, moduleName); const filePath = path.join(knownFolders.currentApp().path, moduleName);
if (fs.Folder.exists(path)) { if (Folder.exists(filePath)) {
folderProcessed = processFolder(path); folderProcessed = processFolder(filePath);
} }
// moduleName is file - load all files in its parent folder // moduleName is file - load all files in its parent folder
const parentName = moduleName.substr(0, moduleName.lastIndexOf(fs.path.separator)); const parentName = moduleName.substring(0, moduleName.lastIndexOf(path.separator));
const parentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, parentName); const parentFolderPath = path.join(knownFolders.currentApp().path, parentName);
if (fs.Folder.exists(parentFolderPath)) { if (Folder.exists(parentFolderPath)) {
parentFolderProcessed = processFolder(parentFolderPath); parentFolderProcessed = processFolder(parentFolderPath);
} }
@ -119,15 +131,15 @@ export function registerModulesFromFileSystem(moduleName: string) {
} }
// moduleName is a folder in tns_modules ex. "nativescript-ui-chart" // moduleName is a folder in tns_modules ex. "nativescript-ui-chart"
const tnsModulesPath = fs.path.join(fs.knownFolders.currentApp().path, 'tns_modules', moduleName); const tnsModulesPath = path.join(knownFolders.currentApp().path, 'tns_modules', moduleName);
if (fs.Folder.exists(tnsModulesPath)) { if (Folder.exists(tnsModulesPath)) {
processFolder(tnsModulesPath); processFolder(tnsModulesPath);
} }
// moduleName a file in tns_modules/plugin. Avoid traversing the whole tns_modules folder if parentName is empty // moduleName a file in tns_modules/plugin. Avoid traversing the whole tns_modules folder if parentName is empty
if (parentName) { if (parentName) {
const tnsParentFolderPath = fs.path.join(fs.knownFolders.currentApp().path, 'tns_modules', parentName); const tnsParentFolderPath = path.join(knownFolders.currentApp().path, 'tns_modules', parentName);
if (fs.Folder.exists(tnsParentFolderPath)) { if (Folder.exists(tnsParentFolderPath)) {
processFolder(tnsParentFolderPath); processFolder(tnsParentFolderPath);
} }
} }