@lakutata/cli
Version:
Lakutata CLI tool
226 lines (225 loc) • 8.3 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Information = void 0;
const ansis_1 = __importDefault(require("ansis"));
const node_path_1 = require("node:path");
const promises_1 = require("node:fs/promises");
const lakutata_1 = require("lakutata");
const di_1 = require("lakutata/decorator/di");
const helper_1 = require("lakutata/helper");
class Information extends lakutata_1.Provider {
/**
* Initializer
* @protected
*/
async init() {
const { packageDirectory } = await import('pkg-dir');
this.packageDirectory = packageDirectory;
const installPath = await packageDirectory({ cwd: this.currentDirectory });
this.installPath = installPath ? installPath : 'UNKNOWN';
const projectRoot = await this.findProjectRoot(this.workingDirectory);
if (projectRoot) {
const packageJsonPath = (0, node_path_1.resolve)(projectRoot, './package.json');
try {
const rawPackageJsonBuffer = await (0, promises_1.readFile)(packageJsonPath);
const packageJson = JSON.parse(rawPackageJsonBuffer.toString());
const dependenciesKeyRegExp = new RegExp('dependencies'.toUpperCase());
Object.keys(packageJson).forEach((key) => {
if (dependenciesKeyRegExp.test(key.toUpperCase())) {
Object.keys(packageJson[key]).forEach((dependencyName) => {
if (dependencyName === this.name) {
this.projectRoot = projectRoot;
}
});
}
});
}
catch (e) {
(0, helper_1.DevNull)(e);
}
}
}
/**
* To find the root path of the project where the working directory is located
* @param path
* @private
*/
async findProjectRoot(path) {
let localRootPath = null;
while (true) {
const _localRootPath = await this.findLocalRoot(localRootPath ? (0, node_path_1.dirname)(localRootPath) : path);
if (!_localRootPath)
break;
else
localRootPath = _localRootPath;
}
return localRootPath;
}
/**
* To locate the root path containing the package.json file
* @param path
* @private
*/
async findLocalRoot(path) {
try {
const dir = await this.packageDirectory({ cwd: path });
return dir ? dir : null;
}
catch (e) {
return null;
}
}
/**
* Convert string first char to upper case
* @param str
* @private
*/
stringFirstCharUpperCase(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
/**
* Format framework ascii logo
* @protected
*/
formatFrameworkAsciiLogo() {
const asciiLogo = '' +
' _ _ _ \n' +
'| | _ | | | | \n' +
'| | __ _ | | _ _ _ | |_ __ _ | |_ __ _ \n' +
'| | / _` | | |/ / | | | | | __| / _` | | __| / _` | \n' +
'| |____ | (_| | | < | |_| | \\ |_ | (_| | \\ |_ | (_| | \n' +
'|______| \\__,_| |_|\\_\\ \\__,_| \\__| \\__,_| \\__| \\__,_| \n' +
' ';
return `${ansis_1.default.yellow(asciiLogo)}`;
}
/**
* Format framework description
* @protected
*/
formatFrameworkDescription() {
return `${ansis_1.default.bold(this.stringFirstCharUpperCase(this.name))} is ${ansis_1.default.blue(this.description)}`;
}
/**
* Format version text
* @protected
*/
formatVersionText() {
return `The current version is ${ansis_1.default.bold(this.version)}`;
}
/**
* Format framework installation path
* @protected
*/
formatInstallationPath() {
return `The installation directory path is ${ansis_1.default.underline(this.getInstallPath())}`;
}
/**
* Format current running level
* @protected
*/
formatRunningLevel() {
return `Currently running at ${ansis_1.default.bold(this.getLevel())} level`;
}
/**
* Format license
* @protected
*/
formatLicense() {
return `${this.stringFirstCharUpperCase(this.name)} is ${ansis_1.default.cyan(this.license)} licensed.`;
}
/**
* Format copyright
* @protected
*/
formatCopyright() {
return `Copyright (c) ${new lakutata_1.Time().format('YYYY')} ${ansis_1.default.bold(this.stringFirstCharUpperCase(this.name))}. All rights reserved.`;
}
/**
* To retrieve the hierarchy level of the project containing the package under the current command
*/
getLevel() {
return this.projectRoot ? 'PROJECT' : 'GLOBAL';
}
/**
* Locate the root path of the project
*/
getRoot() {
return this.projectRoot;
}
/**
* Locate the installation directory
*/
getInstallPath() {
return this.installPath;
}
/**
* Retrieve the framework installation version at the project level
*/
async getProjectLevelInstalledFrameworkVersion() {
const projectRoot = this.getRoot();
if (projectRoot) {
try {
const pkgJsonBuffer = await (0, promises_1.readFile)((0, node_path_1.resolve)(projectRoot, './node_modules', `./${this.name}/package.json`));
const pkgJson = JSON.parse(pkgJsonBuffer.toString());
const version = pkgJson.version;
return version ? version : null;
}
catch (e) {
return null;
}
}
return null;
}
/**
* Print infos
*/
async print() {
const texts = [
this.formatFrameworkAsciiLogo(),
this.formatFrameworkDescription(),
this.formatVersionText(),
this.formatInstallationPath(),
this.formatRunningLevel(),
this.formatLicense(),
this.formatCopyright()
];
console.info(texts.join('\n'));
}
}
exports.Information = Information;
__decorate([
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
__metadata("design:type", String)
], Information.prototype, "name", void 0);
__decorate([
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
__metadata("design:type", String)
], Information.prototype, "version", void 0);
__decorate([
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
__metadata("design:type", String)
], Information.prototype, "description", void 0);
__decorate([
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
__metadata("design:type", String)
], Information.prototype, "license", void 0);
__decorate([
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
__metadata("design:type", String)
], Information.prototype, "currentDirectory", void 0);
__decorate([
(0, di_1.Configurable)(lakutata_1.DTO.String().required()),
__metadata("design:type", String)
], Information.prototype, "workingDirectory", void 0);