@allgemein/moduls
Version:
Commons-moduls handles and manages contextual moduls for complex and modular applications.
215 lines • 8.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Helper = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const lodash_1 = require("lodash");
const base_1 = require("@allgemein/base");
const micromatch_1 = require("micromatch");
const Constants_1 = require("../Constants");
class Helper {
static readFile(file) {
return new Promise((resolve, reject) => {
if ((0, fs_1.existsSync)(file)) {
(0, fs_1.readFile)(file, (err, buf) => {
if (err) {
reject(err);
}
else {
try {
resolve(buf);
}
catch (err) {
reject(err);
}
}
});
}
else {
reject(new Error('cant find file ' + file));
}
});
}
static getPackageJson(_path) {
if (!/package\.json$/.test(_path)) {
_path = (0, path_1.join)(_path, Constants_1.C_PACKAGE_JSON);
}
return this.readFile(_path).then(buf => {
let data = buf.toString('utf8');
let json = JSON.parse(data);
return json;
});
}
static ucfirst(word) {
return word[0].toUpperCase() + word.substring(1).toLowerCase();
}
static async readdir(dir) {
return new Promise((resolve, reject) => {
(0, fs_1.readdir)(dir, (err, files) => {
if (err) {
reject(err);
}
else {
resolve(files);
}
});
});
}
static async stat(dir) {
return new Promise((resolve, reject) => {
(0, fs_1.stat)(dir, (err, stats) => {
if (err) {
reject(err);
}
else {
resolve(stats);
}
});
});
}
static async npmls(node_modules_dir, options = { depth: 1, level: 0 }) {
let depth = options.depth;
let inc = options.level;
if (inc >= depth) {
return [];
}
options.level = inc + 1;
let modules = [];
let directories = await this.getValidDirectories(node_modules_dir, options);
await Promise.all((0, lodash_1.map)(directories, async (directory) => {
if (/^@/.test(directory)) {
// is grouped
let _grouped_node_modules_dir = base_1.PlatformUtils.join(node_modules_dir, directory);
let _directories = await this.getValidDirectories(_grouped_node_modules_dir, options);
return Promise.all((0, lodash_1.map)(_directories, async (_directory) => {
_directory = base_1.PlatformUtils.join(directory, _directory);
return Helper.lookupNpmInDirectory(node_modules_dir, _directory, modules, options);
}));
}
else {
return Helper.lookupNpmInDirectory(node_modules_dir, directory, modules, options);
}
}));
return modules;
}
static async getValidDirectories(node_modules_dir, options) {
let directories = await this.readdir(node_modules_dir);
if ((0, lodash_1.has)(options, 'exclude') || (0, lodash_1.has)(options, 'include')) {
if ((0, lodash_1.has)(options, 'include') && !(0, lodash_1.isEmpty)(options.include)) {
let includes = [];
for (const entry of options.include || []) {
includes = includes.concat(directories.filter((path => {
const _resolve = (0, path_1.join)(node_modules_dir, path);
return (0, micromatch_1.isMatch)(_resolve, entry, options.matcherOptions || { dot: true });
})));
}
directories = includes;
}
for (const entry of options.exclude || []) {
(0, lodash_1.remove)(directories, (path => {
const _resolve = (0, path_1.join)(node_modules_dir, path);
return (0, micromatch_1.isMatch)(_resolve, entry, options.matcherOptions || { dot: true });
}));
}
}
return directories;
}
static async lookupNpmInDirectory(node_modules_dir, directory, modules = [], options) {
let _path = (0, path_1.join)(node_modules_dir, directory);
if (!(0, fs_1.existsSync)((0, path_1.join)(_path, Constants_1.C_PACKAGE_JSON))) {
return;
}
let package_json = await this.getPackageJson(_path);
let modul_exists = (0, lodash_1.find)(modules, x => x.name == directory);
if (modul_exists) {
return;
}
package_json.path = base_1.PlatformUtils.pathResolve(_path);
package_json.child_modules = [];
package_json.sub_modules = {};
if (options && options.filter) {
if (!options.filter(package_json)) {
return;
}
}
modules.push(package_json);
if (options.subModulePaths) {
// look in subpath like "node_modules"
let results = await Promise.all((0, lodash_1.map)(options.subModulePaths, subpath => {
return this.look(_path, subpath, modules, options);
}));
for (let res of results) {
if (res.subpath === Constants_1.C_NODE_MODULES) {
package_json.has_node_modules = res.has_modules;
package_json.child_modules = res.child_modules;
}
else {
package_json.sub_modules[res.subpath] = {
has_modules: res.has_modules,
modules: res.child_modules
};
}
}
}
return modules;
}
static async look(_path, subpath, modules = [], options) {
// FIXME detect the node_modules path
let _new_node_module_dir = (0, path_1.join)(_path, subpath);
let info = {
subpath: subpath,
has_modules: false,
child_modules: []
};
if (base_1.PlatformUtils.fileExist(_new_node_module_dir)) {
try {
let stat = await this.stat(_new_node_module_dir);
if (stat && stat.isDirectory()) {
let _modules = await this.npmls(_new_node_module_dir, options);
info.has_modules = true;
for (let _x of _modules) {
info.child_modules.push(_x.name);
let _modul_exists = modules.find(function (_m) {
return _m.name == _x.name;
});
if (!_modul_exists) {
modules.push(_x);
}
}
}
}
catch (err) {
console.error(err);
}
}
return info;
}
static checkPaths(paths) {
let ret_paths = [];
for (let _path of paths) {
_path = (0, path_1.resolve)(_path);
let _try_path = (0, path_1.join)(_path, Constants_1.C_NODE_MODULES);
let _try_package = (0, path_1.join)(_path, Constants_1.C_PACKAGE_JSON);
if ((0, fs_1.existsSync)(_path)) {
if ((0, fs_1.existsSync)(_try_package) && (0, fs_1.existsSync)(_try_path)) {
_path = _try_path;
}
}
else {
throw new Error('checking path ' + _path + ' doesn\'t exists');
}
ret_paths.push(_path);
}
return ret_paths;
}
/**
* Checks if x is an glob pattern
*
* @param x
*/
static isGlobPattern(x) {
return /\+|\.|\(|\||\)|\*/.test(x);
}
}
exports.Helper = Helper;
//# sourceMappingURL=Helper.js.map