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
194 lines (193 loc) • 8.29 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");
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 = UtilsService_1 = class UtilsService {
getLogger() {
const logger = (0, log4js_1.getLogger)(UtilsService_1.name);
logger.level = UtilsService_1.logLevel();
return 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));
}
if (dirname) {
return (0, path_1.resolve)((0, path_1.join)(dirname, filename));
}
else {
return (0, path_1.resolve)(filename);
}
}
getWorkspaceProjects(workspaceFile) {
var _a;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let workspaceJson;
if (!workspaceFile) {
workspaceFile = this.resolveFilePath(exports.WORKSPACE_JSON);
}
else {
workspaceFile = this.resolveFilePath(workspaceFile);
}
if ((0, fs_1.existsSync)(workspaceFile)) {
workspaceJson = JSON.parse((0, fs_1.readFileSync)(workspaceFile).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_BASE_JSON);
if ((0, fs_1.existsSync)(tsconfigJson)) {
const projects = this.collectProjectsFromTsConfig(tsconfigJson);
workspaceJson = { projects };
}
}
}
const files = (0, glob_1.globSync)(`./**/**/${exports.PROJECT_JSON}`);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const projects = {};
for (let index = 0; index < files.length; index++) {
const project = JSON.parse((0, fs_1.readFileSync)(files[index]).toString());
if (project.name && project.sourceRoot) {
projects[project.name] = project;
}
}
const ruckenWorkspaceJson = this.getRuckenConfig({
workspace: { projects: {} },
});
workspaceJson = {
projects: Object.assign(Object.assign(Object.assign({}, ((workspaceJson === null || workspaceJson === void 0 ? void 0 : workspaceJson.projects) || {})), (((_a = ruckenWorkspaceJson.workspace) === null || _a === void 0 ? void 0 : _a.projects) || {})), (projects || {})),
};
return Object.keys(workspaceJson === null || workspaceJson === void 0 ? void 0 : workspaceJson.projects)
.map((projectName) => {
let project = {};
try {
project =
typeof workspaceJson.projects[projectName] === 'string'
? {
[projectName]: JSON.parse((0, fs_1.readFileSync)(`${workspaceJson.projects[projectName]}/project.json`).toString()),
}
: { [projectName]: workspaceJson.projects[projectName] };
}
catch (err) {
project = {
[(0, case_anything_1.kebabCase)(projectName)]: {
root: workspaceJson.projects[projectName],
},
};
}
project[projectName].root =
project[projectName].root ||
(project[projectName].sourceRoot || '')
.split('/')
.filter((o, i, a) => i < a.length - 1)
.join('/');
return project;
})
.reduce((all, cur) => (Object.assign(Object.assign({}, all), cur)), {});
}
collectProjectsFromTsConfig(tsconfigFile) {
const json = JSON.parse((0, fs_1.readFileSync)(tsconfigFile).toString());
const projects = {};
Object.keys(json.compilerOptions.paths || {})
.filter((key) => !key.includes('*'))
.map((key) => {
try {
let path = json.compilerOptions.paths[key][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 = (Array.isArray(json.compilerOptions.paths[key])
? json.compilerOptions.paths[key][0]
: json.compilerOptions.paths[key]).replace('/index.ts', '');
projects[projectName] = path;
}
}
catch (err) {
this.getLogger().log(JSON.stringify({ json, key }));
this.getLogger().error(err, err.stack);
throw err;
}
});
return projects;
}
getRuckenConfig(defaultValue, configFile) {
if (!configFile) {
configFile = this.resolveFilePath(exports.RUCKEN_JSON);
}
else {
configFile = this.resolveFilePath(configFile);
}
if (!(0, fs_1.existsSync)(configFile)) {
return defaultValue;
}
try {
const config = JSON.parse((0, fs_1.readFileSync)(configFile).toString());
return (0, lodash_mergewith_1.default)(defaultValue, config);
}
catch (error) {
this.getLogger().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;
Object.keys(process.env).forEach((key) => (newCommand = (newCommand || '')
.split('%space%')
.join(' ')
.split('%br%')
.join('<br/>')
.split(`\${${key}}`)
.join(process.env[key])
.split(envReplacerKeyPattern.replace('key', key))
.join(process.env[key])));
if (command !== newCommand && newCommand.includes('$') && depth > 0) {
newCommand = this.replaceEnv(newCommand, envReplacerKeyPattern, depth - 1);
}
return newCommand || '';
}
};
UtilsService.logLevel = () => (process.env['DEBUG'] === '*'
? 'all'
: process.env['RUCKEN_LOG_LEVEL'] || process.env['DEBUG']) || 'info';
UtilsService = UtilsService_1 = tslib_1.__decorate([
(0, common_1.Injectable)()
], UtilsService);
exports.UtilsService = UtilsService;