rucken
Version:
Console tools and scripts for nx and not only that I (EndyKaufman) use to automate the workflow and speed up the development process
192 lines • 7.97 kB
JavaScript
;
var UtilsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UtilsService = exports.TRANSLOCO_CONFIG_JS = exports.TRANSLOCO_CONFIG_JSON = exports.RUCKEN_JSON = exports.PACKAGE_JSON = exports.PROJECT_JSON = exports.TSCONFIG_JSON = exports.TSCONFIG_BASE_JSON = exports.WORKSPACE_JSON = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const case_anything_1 = require("case-anything");
const fs_1 = require("fs");
const glob_1 = require("glob");
// @ts-expect-error - No type definitions available for lodash.mergewith
const lodash_mergewith_1 = tslib_1.__importDefault(require("lodash.mergewith"));
const log4js_1 = require("log4js");
const path_1 = require("path");
exports.WORKSPACE_JSON = 'workspace.json';
exports.TSCONFIG_BASE_JSON = 'tsconfig.base.json';
exports.TSCONFIG_JSON = 'tsconfig.json';
exports.PROJECT_JSON = 'project.json';
exports.PACKAGE_JSON = 'package.json';
exports.RUCKEN_JSON = 'rucken.json';
exports.TRANSLOCO_CONFIG_JSON = 'transloco.config.json';
exports.TRANSLOCO_CONFIG_JS = 'transloco.config.js';
let UtilsService = class UtilsService {
static { UtilsService_1 = this; }
logger;
static logLevel = () => (process.env['DEBUG'] === '*'
? 'all'
: process.env['RUCKEN_LOG_LEVEL'] || process.env['DEBUG']) || 'info';
constructor() {
this.logger = (0, log4js_1.getLogger)(UtilsService_1.name);
this.logger.level = UtilsService_1.logLevel();
}
getLogger() {
return this.logger;
}
resolveFilePath(filename, dirname) {
if (!dirname && (0, fs_1.existsSync)(filename)) {
return (0, path_1.resolve)(filename);
}
if (!dirname && (0, fs_1.existsSync)((0, path_1.join)(process.cwd(), filename))) {
return (0, path_1.resolve)((0, path_1.join)(process.cwd(), filename));
}
if (dirname && (0, fs_1.existsSync)((0, path_1.join)(dirname, filename))) {
return (0, path_1.resolve)((0, path_1.join)(dirname, filename));
}
return dirname ? (0, path_1.resolve)((0, path_1.join)(dirname, filename)) : (0, path_1.resolve)(filename);
}
getWorkspaceProjects(workspaceFile) {
let workspaceJson;
const resolvedWorkspaceFile = workspaceFile
? this.resolveFilePath(workspaceFile)
: this.resolveFilePath(exports.WORKSPACE_JSON);
if ((0, fs_1.existsSync)(resolvedWorkspaceFile)) {
workspaceJson = JSON.parse((0, fs_1.readFileSync)(resolvedWorkspaceFile).toString());
}
else {
const tsconfigBaseJson = this.resolveFilePath(exports.TSCONFIG_BASE_JSON);
if ((0, fs_1.existsSync)(tsconfigBaseJson)) {
const projects = this.collectProjectsFromTsConfig(tsconfigBaseJson);
workspaceJson = { projects };
}
else {
const tsconfigJson = this.resolveFilePath(exports.TSCONFIG_JSON);
if ((0, fs_1.existsSync)(tsconfigJson)) {
const projects = this.collectProjectsFromTsConfig(tsconfigJson);
workspaceJson = { projects };
}
else {
workspaceJson = { projects: {} };
}
}
}
const projectFiles = (0, glob_1.globSync)(`./**/**/${exports.PROJECT_JSON}`);
const projectsFromFiles = {};
for (const projectFile of projectFiles) {
const project = JSON.parse((0, fs_1.readFileSync)(projectFile).toString());
if (project.name &&
project.sourceRoot &&
typeof project.name === 'string') {
projectsFromFiles[project.name] = project;
}
}
const ruckenConfig = this.getRuckenConfig({
projects: {},
});
const mergedWorkspace = {
projects: {
...(workspaceJson?.projects || {}),
...(ruckenConfig?.projects || {}),
...(projectsFromFiles || {}),
},
};
return Object.entries(mergedWorkspace.projects).reduce((acc, [projectName, projectData]) => {
let project;
try {
project =
typeof projectData === 'string'
? JSON.parse((0, fs_1.readFileSync)(`${projectData}/project.json`).toString())
: { ...projectData };
}
catch (_err) {
project = {
root: typeof projectData === 'string' ? projectData : projectData.root,
};
}
project.root =
project.root ||
(project.sourceRoot || '')
.split('/')
.filter((_, i, arr) => i < arr.length - 1)
.join('/');
acc[(0, case_anything_1.kebabCase)(projectName)] = project;
return acc;
}, {});
}
collectProjectsFromTsConfig(tsconfigFile) {
const json = JSON.parse((0, fs_1.readFileSync)(tsconfigFile).toString());
const projects = {};
const paths = json.compilerOptions?.paths || {};
Object.entries(paths)
.filter(([key]) => !key.includes('*'))
.forEach(([key, pathArray]) => {
try {
let path = pathArray[0].replace('/src/index.ts', '');
const projectName = (0, case_anything_1.kebabCase)(key);
if ((0, fs_1.existsSync)((0, path_1.join)(path, exports.PROJECT_JSON))) {
projects[projectName] = path;
}
else {
path = pathArray[0].replace('/index.ts', '');
projects[projectName] = path;
}
}
catch (err) {
this.logger.error(JSON.stringify({ json, key }));
this.logger.error(err.message, err.stack);
throw err;
}
});
return projects;
}
getRuckenConfig(defaultValue, configFile) {
const resolvedConfigFile = configFile
? this.resolveFilePath(configFile)
: this.resolveFilePath(exports.RUCKEN_JSON);
if (!(0, fs_1.existsSync)(resolvedConfigFile)) {
return defaultValue;
}
try {
const config = JSON.parse((0, fs_1.readFileSync)(resolvedConfigFile).toString());
return (0, lodash_mergewith_1.default)({}, defaultValue, config);
}
catch (error) {
this.logger.warn(error);
return defaultValue;
}
}
getExtractAppName(nxAppName) {
return nxAppName
.split('-')
.join('_')
.replace('-server', '')
.replace('-ms', '');
}
replaceEnv(command, envReplacerKeyPattern = '$key', depth = 10) {
if (!command) {
return command || '';
}
let newCommand = command;
for (const key of Object.keys(process.env)) {
const envValue = process.env[key] || '';
newCommand = newCommand
.split('%space%')
.join(' ')
.split('%br%')
.join('<br/>')
.split(`\${${key}}`)
.join(envValue)
.split(envReplacerKeyPattern.replace('key', key))
.join(envValue);
}
if (command !== newCommand && newCommand.includes('$') && depth > 0) {
newCommand = this.replaceEnv(newCommand, envReplacerKeyPattern, depth - 1);
}
return newCommand;
}
};
exports.UtilsService = UtilsService;
exports.UtilsService = UtilsService = UtilsService_1 = tslib_1.__decorate([
(0, common_1.Injectable)(),
tslib_1.__metadata("design:paramtypes", [])
], UtilsService);
//# sourceMappingURL=utils.service.js.map