osr-mono/packages/osr-tasks/library.js
2025-01-29 17:48:22 +01:00

67 lines
2.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.unique_by = exports.file_path_with_ext_ex = exports.file_path_with_ext = exports.forward_slash = exports.files = exports.MainAssembly = exports.CAD_MAIN_PATTERN = exports.option = exports.gruntOptions = void 0;
const gruntOptions = (grunt) => {
const ret = {};
grunt.option.keys().forEach((k) => {
ret[k] = grunt.option(k);
});
delete ret['verbose'];
delete ret['help'];
delete ret['gruntfile'];
return ret;
};
exports.gruntOptions = gruntOptions;
const option = (option, taskOptions, grunt, _default) => {
let ret = taskOptions[option] !== undefined ? taskOptions[option] : _default;
if (grunt.option(option) !== undefined) {
ret = grunt.option(option);
}
return ret;
};
exports.option = option;
const path = require("path");
const exists_1 = require("@plastichub/fs/exists");
const fg = require('fast-glob');
exports.CAD_MAIN_PATTERN = '**/cad/**/*Global*.+(SLDASM)';
const MainAssembly = (dir, filePattern = exports.CAD_MAIN_PATTERN) => (0, exports.files)(dir, filePattern)[0];
exports.MainAssembly = MainAssembly;
const files = (dir, glob) => fg.sync(glob, {
dot: true,
cwd: dir,
absolute: true,
caseSensitiveMatch: false
});
exports.files = files;
const forward_slash = (path) => {
const isExtendedLengthPath = /^\\\\\?\\/.test(path);
const hasNonAscii = /[^\u0000-\u0080]+/.test(path); // eslint-disable-line no-control-regex
if (isExtendedLengthPath || hasNonAscii) {
return path;
}
return path.replace(/\\/g, '/');
};
exports.forward_slash = forward_slash;
const file_path_with_ext = (file, ext) => {
const parts = path.parse(file);
return path.join(parts.dir, parts.name + '.' + ext);
};
exports.file_path_with_ext = file_path_with_ext;
const file_path_with_ext_ex = (file, ext) => {
let parts = path.parse(file);
let _path = path.join(parts.dir, parts.name.toLowerCase() + '.' + ext.toLowerCase());
if ((0, exists_1.sync)(_path)) {
return _path;
}
_path = (0, exports.file_path_with_ext)(file, ext);
if ((0, exists_1.sync)(_path)) {
return _path;
}
return '';
};
exports.file_path_with_ext_ex = file_path_with_ext_ex;
const unique_by = (arr, key) => {
return [...new Map(arr.map(item => [item[key], item])).values()];
};
exports.unique_by = unique_by;
//# sourceMappingURL=library.js.map