jsm-core
Version:
Core library for JSM project
105 lines (104 loc) • 5.06 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveFiles = resolveFiles;
exports.resolveModulePath = resolveModulePath;
exports.resolveModuleEntryFile = resolveModuleEntryFile;
const glob_1 = require("glob");
const node_path_1 = __importDefault(require("node:path"));
const node_fs_1 = __importDefault(require("node:fs"));
const context_1 = require("../../context");
const jsm_logger_1 = __importStar(require("jsm-logger"));
const logger = (0, jsm_logger_1.default)(jsm_logger_1.LoggerContext.UTILITY, "ModuleResolver");
/**
* Resolve scheduler files based on the current environment.
* - Looks for `.scheduler.ts` in development and `.scheduler.js` in production.
* - The pattern should be relative to the `src` directory.
*/
function resolveFiles(pattern) {
if (pattern.endsWith(".ts") || pattern.endsWith(".js"))
pattern = pattern.substring(0, pattern.length - 3);
if (pattern.startsWith('/')) {
pattern = pattern.substring(1);
}
const tsPattern = node_path_1.default.join((0, context_1.getRegistry)().cwd, 'src/', `${pattern}.ts`);
const jsPattern = node_path_1.default.join((0, context_1.getRegistry)().cwd, 'dist/', `${pattern}.js`);
const selectedPattern = ((0, context_1.getRegistry)().getConfig(true).isDev) ? tsPattern : jsPattern;
logger.debug("FN[resolveFiles]: Selected pattern:", selectedPattern);
const files = (0, glob_1.globSync)(selectedPattern);
if (files.length === 0) {
logger.warn("No files found with this pattern:", pattern);
}
return files;
}
/**
* Resolve module path based on the current environment.
* - Looks for `.ts` in development and `.js` in production.
* - The path should be relative to the `src` directory.
*/
function resolveModulePath(modulePath) {
if (modulePath.endsWith(".ts") || modulePath.endsWith(".js")) {
modulePath = modulePath.substring(0, modulePath.length - 3);
}
if (modulePath.startsWith('/')) {
modulePath = modulePath.substring(1);
}
const tsPath = node_path_1.default.join((0, context_1.getRegistry)().cwd, 'src/', `${modulePath}.ts`);
const jsPath = node_path_1.default.join((0, context_1.getRegistry)().cwd, 'dist/', `${modulePath}.js`);
let selectedPath = ((0, context_1.getRegistry)().getConfig(true).isDev && node_fs_1.default.existsSync(tsPath)) ? tsPath : node_fs_1.default.existsSync(jsPath) ? jsPath : undefined;
if (!selectedPath) {
logger.warn(`FN[resolveModulePath]: Module path not found for: ${modulePath}`, selectedPath);
}
logger.debug("FN[resolveModulePath]: Selected path:", selectedPath);
return selectedPath;
}
/**
* Resolve module entry file based on the current environment.
* - Looks for `main.ts` in development and `main.js` in production.
* - The path should be relative to the `src/modules` directory.
*/
function resolveModuleEntryFile(module) {
const tsPath = node_path_1.default.join((0, context_1.getRegistry)().cwd, 'src/modules', `${module}/main.ts`);
const jsPath = node_path_1.default.join((0, context_1.getRegistry)().cwd, 'dist/modules', `${module}/main.js`);
let selectedPath = ((0, context_1.getRegistry)().getConfig(true).isDev && node_fs_1.default.existsSync(tsPath)) ? tsPath : node_fs_1.default.existsSync(jsPath) ? jsPath : undefined;
if (!selectedPath) {
logger.warn(`FN[resolveModulePath]: Module path not found for: ${module}`, selectedPath);
}
logger.debug("FN[resolveModulePath]: Selected path:", selectedPath);
return selectedPath;
}