|
| 1 | +import { dirname } from 'node:path' |
| 2 | +import { fileURLToPath, pathToFileURL } from 'node:url' |
| 3 | + |
| 4 | +import { Folder } from '#src/index' |
| 5 | + |
| 6 | +export class Module { |
| 7 | + /** |
| 8 | + * Get the module first export match or default. |
| 9 | + * |
| 10 | + * @param {any|Promise<any>} module |
| 11 | + * @return {Promise<any>} |
| 12 | + */ |
| 13 | + static async get(module) { |
| 14 | + module = await module |
| 15 | + |
| 16 | + if (module.default) { |
| 17 | + return module.default |
| 18 | + } |
| 19 | + |
| 20 | + return module[Object.keys(module)[0]] |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Get the module first export match or default with alias. |
| 25 | + * |
| 26 | + * @param {any|Promise<any>} module |
| 27 | + * @param {string} subAlias |
| 28 | + * @return {Promise<{ alias: string, module: any }>} |
| 29 | + */ |
| 30 | + static async getWithAlias(module, subAlias) { |
| 31 | + module = await Module.get(module) |
| 32 | + |
| 33 | + if (!subAlias.endsWith('/')) { |
| 34 | + subAlias = subAlias.concat('/') |
| 35 | + } |
| 36 | + |
| 37 | + const alias = subAlias.concat(module.name) |
| 38 | + |
| 39 | + return { alias, module } |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Get all modules first export match or default and return |
| 44 | + * as array. |
| 45 | + * |
| 46 | + * @param {any[]|Promise<any[]>} modules |
| 47 | + * @return {Promise<any[]>} |
| 48 | + */ |
| 49 | + static async getAll(modules) { |
| 50 | + const promises = modules.map(m => Module.get(m)) |
| 51 | + |
| 52 | + return Promise.all(promises) |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Get all modules first export match or default with alias and return |
| 57 | + * as array. |
| 58 | + * |
| 59 | + * @param {any[]|Promise<any[]>} modules |
| 60 | + * @param {string} subAlias |
| 61 | + * @return {Promise<any[]>} |
| 62 | + */ |
| 63 | + static async getAllWithAlias(modules, subAlias) { |
| 64 | + const promises = modules.map(m => Module.getWithAlias(m, subAlias)) |
| 65 | + |
| 66 | + return Promise.all(promises) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Same as get method, but import the path directly. |
| 71 | + * |
| 72 | + * @param {string} path |
| 73 | + * @return {Promise<any>} |
| 74 | + */ |
| 75 | + static async getFrom(path) { |
| 76 | + const module = await Module.import(path) |
| 77 | + |
| 78 | + return Module.get(module) |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Same as getWithAlias method, but import the path directly. |
| 83 | + * |
| 84 | + * @param {string} path |
| 85 | + * @param {string} subAlias |
| 86 | + * @return {Promise<{ alias: string, module: any }>} |
| 87 | + */ |
| 88 | + static async getFromWithAlias(path, subAlias) { |
| 89 | + const module = await Module.import(path) |
| 90 | + |
| 91 | + return Module.getWithAlias(module, subAlias) |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Same as getAll method but import everything in the path directly. |
| 96 | + * |
| 97 | + * @param {string} path |
| 98 | + * @return {Promise<any[]>} |
| 99 | + */ |
| 100 | + static async getAllFrom(path) { |
| 101 | + const files = await Module.getAllJSFilesFrom(path) |
| 102 | + |
| 103 | + const promises = files.map(file => Module.getFrom(file.path)) |
| 104 | + |
| 105 | + return Promise.all(promises) |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Same as getAllWithAlias method but import everything in the path directly. |
| 110 | + * |
| 111 | + * @param {string} path |
| 112 | + * @param {string} subAlias |
| 113 | + * @return {Promise<{ alias: string, module: any }[]>} |
| 114 | + */ |
| 115 | + static async getAllFromWithAlias(path, subAlias) { |
| 116 | + const files = await Module.getAllJSFilesFrom(path) |
| 117 | + |
| 118 | + const promises = files.map(f => Module.getFromWithAlias(f.path, subAlias)) |
| 119 | + |
| 120 | + return Promise.all(promises) |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Verify if folder exists and get all .js files inside. |
| 125 | + * |
| 126 | + * @param {string} path |
| 127 | + * @return {Promise<File[]>} |
| 128 | + */ |
| 129 | + static async getAllJSFilesFrom(path) { |
| 130 | + if (!(await Folder.exists(path))) { |
| 131 | + return [] |
| 132 | + } |
| 133 | + |
| 134 | + if (!(await Folder.isFolder(path))) { |
| 135 | + return [] |
| 136 | + } |
| 137 | + |
| 138 | + const folder = await new Folder(path).load() |
| 139 | + |
| 140 | + // FIXME Why glob pattern *.js is retrieving .d.ts and .js.map files? |
| 141 | + return folder |
| 142 | + .getFilesByPattern('*/**/*.js', true) |
| 143 | + .filter(file => file.extension.endsWith('.js')) |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Import a full path using the path href to ensure compatibility |
| 148 | + * between OS's. |
| 149 | + * |
| 150 | + * @param {string} path |
| 151 | + * @return {Promise<any>} |
| 152 | + */ |
| 153 | + static async import(path) { |
| 154 | + return import(pathToFileURL(path).href) |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Create the __dirname property. Set in global if necessary. |
| 159 | + * |
| 160 | + * @param {boolean} setInGlobal |
| 161 | + * @return {string} |
| 162 | + */ |
| 163 | + static createDirname(setInGlobal = false) { |
| 164 | + const __dirname = dirname(Module.createFilename(false)) |
| 165 | + |
| 166 | + if (setInGlobal) { |
| 167 | + global.__dirname = __dirname |
| 168 | + } |
| 169 | + |
| 170 | + return __dirname |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Create the __filename property. Set in global if necessary. |
| 175 | + * |
| 176 | + * @param {boolean} setInGlobal |
| 177 | + * @return {string} |
| 178 | + */ |
| 179 | + static createFilename(setInGlobal = false) { |
| 180 | + const __filename = fileURLToPath(import.meta.url) |
| 181 | + |
| 182 | + if (setInGlobal) { |
| 183 | + global.__filename = __filename |
| 184 | + } |
| 185 | + |
| 186 | + return __filename |
| 187 | + } |
| 188 | +} |
0 commit comments