@serpent/common-cli
Version:
通用的 cli 相关的函数
402 lines (395 loc) • 14.8 kB
JavaScript
import { h as existsFile, c as findupPackageRoot } from './context-96dcd180.mjs';
import { J as JsonFile } from './Dir-b98fddc7.mjs';
import 'module';
import path from 'path';
import 'assert';
import { r as readJsonFile } from './readJsonFile-255f65c0.mjs';
import 'fs';
import { _ as __awaiter, a as __generator, d as __extends } from './tslib.es6-636c4f06.mjs';
import { silentRunOutput, silentRun, run } from './run.mjs';
import './_commonjsHelpers-7d1333e8.mjs';
import './exists-d3d14d46.mjs';
import 'os';
import 'node:buffer';
import 'node:path';
import 'node:child_process';
import 'node:process';
import 'child_process';
import 'node:url';
import 'node:os';
import './index-3b2c9171.mjs';
import 'events';
import 'buffer';
import 'stream';
import 'util';
import 'url';
import './info-fd7bb244.mjs';
import './warn-1994a28d.mjs';
import './supportColor-2e661db8.mjs';
import './helper-1331d234.mjs';
import 'crypto';
var PackageManager = /** @class */ (function () {
function PackageManager() {
var root = findupPackageRoot();
this.root = root;
}
Object.defineProperty(PackageManager.prototype, "lockfilePath", {
get: function () {
return path.join(this.root, this.lockfile);
},
enumerable: false,
configurable: true
});
Object.defineProperty(PackageManager.prototype, "packagePath", {
/** package.json 文件路径 */
get: function () {
return path.join(this.root, 'package.json');
},
enumerable: false,
configurable: true
});
PackageManager.prototype.isLockFileExists = function () {
return existsFile(this.lockfilePath);
};
/** 获取当前项目的 registry 配置 */
PackageManager.prototype.registry = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = silentRunOutput;
return [4 /*yield*/, this.registryArgs()];
case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])];
}
});
});
};
PackageManager.prototype.tagVersionPrefix = function () {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = silentRunOutput;
return [4 /*yield*/, this.tagVersionPrefixArgs()];
case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])];
}
});
});
};
/** 初始化安装依赖 */
PackageManager.prototype.initInstall = function (options) {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = ((options === null || options === void 0 ? void 0 : options.silent) ? silentRun : run);
return [4 /*yield*/, this.initInstallArgs()];
case 1: return [4 /*yield*/, _a.apply(void 0, [_b.sent()])];
case 2:
_b.sent();
return [2 /*return*/];
}
});
});
};
/** ci 安装依赖 */
PackageManager.prototype.ciInstall = function (options) {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = ((options === null || options === void 0 ? void 0 : options.silent) ? silentRun : run);
return [4 /*yield*/, this.initInstallArgs()];
case 1: return [4 /*yield*/, _a.apply(void 0, [_b.sent()])];
case 2:
_b.sent();
return [2 /*return*/];
}
});
});
};
/** 运行 package.json 中的 scripts 脚本 */
PackageManager.prototype.runScript = function (name, options) {
return __awaiter(this, void 0, void 0, function () {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = ((options === null || options === void 0 ? void 0 : options.silent) ? silentRun : run);
return [4 /*yield*/, this.runScriptArgs(name)];
case 1: return [4 /*yield*/, _a.apply(void 0, [_b.sent()])];
case 2:
_b.sent();
return [2 /*return*/];
}
});
});
};
/**
* 更新 package.json 中的版本号(如果对应的 lock 文件有需要的话,也会同步更新)
*/
PackageManager.prototype.updateVersion = function (version) {
return __awaiter(this, void 0, void 0, function () {
var jf;
return __generator(this, function (_a) {
jf = new JsonFile(path.join(this.root, 'package.json'));
jf.set('version', version);
jf.save();
return [2 /*return*/];
});
});
};
return PackageManager;
}());
var Npm = /** @class */ (function (_super) {
__extends(Npm, _super);
function Npm() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = 'npm';
_this.lockfile = 'package-lock.json';
return _this;
}
/** 获取当前系统安装的 npm 版本号 */
Npm.prototype.version = function () {
return silentRunOutput('npm --version');
};
/** 获取 registry 的命令 */
Npm.prototype.registryArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['npm', 'config', 'get', 'registry']];
});
});
};
/** 获取读取 tag 的前缀配置的命令 */
Npm.prototype.tagVersionPrefixArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['npm', 'config', 'get', 'tag-version-prefix']];
});
});
};
/** 获取初始化安装依赖的命令 */
Npm.prototype.initInstallArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['npm', 'install']];
});
});
};
/** 获取 ci 中安装依赖的命令 */
Npm.prototype.ciInstallArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this.isLockFileExists()) {
return [2 /*return*/, ['npm', 'ci', '--engine-strict']];
}
else {
return [2 /*return*/, ['npm', 'install', '--no-package-lock', '--no-production', '--engine-strict']];
}
});
});
};
/** 运行运行 scripts 中脚本的命令 */
Npm.prototype.runScriptArgs = function (name) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['npm', 'run', name]];
});
});
};
/**
* 更新版本号
*/
Npm.prototype.updateVersion = function (version) {
return __awaiter(this, void 0, void 0, function () {
var jf;
return __generator(this, function (_a) {
_super.prototype.updateVersion.call(this, version);
// 更新 lock 文件中的版本号
if (this.isLockFileExists()) {
jf = new JsonFile(this.lockfilePath);
if (jf.has('version')) {
jf.set('version', version);
}
if (jf.has('packages..version')) {
jf.set('packages..version', version);
}
jf.save();
}
return [2 /*return*/];
});
});
};
return Npm;
}(PackageManager));
var Pnpm = /** @class */ (function (_super) {
__extends(Pnpm, _super);
function Pnpm() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = 'pnpm';
_this.lockfile = 'pnpm-lock.yaml';
return _this;
}
/** 获取当前系统安装的 pnpm 版本号 */
Pnpm.prototype.version = function () {
return silentRunOutput('pnpm --version');
};
/** 获取 registry 的命令 */
Pnpm.prototype.registryArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['pnpm', 'config', 'get', 'registry']];
});
});
};
/** 获取读取 tag 的前缀配置的命令 */
Pnpm.prototype.tagVersionPrefixArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['pnpm', 'config', 'get', 'tag-version-prefix']];
});
});
};
/** 获取初始化安装依赖的命令 */
Pnpm.prototype.initInstallArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['pnpm', 'install']];
});
});
};
/** 获取 ci 中安装依赖的命令 */
Pnpm.prototype.ciInstallArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this.isLockFileExists()) {
return [2 /*return*/, ['pnpm', 'install', '--frozen-lockfile']];
}
else {
return [2 /*return*/, ['pnpm', 'install', '--no-lockfile']];
}
});
});
};
/** 运行运行 scripts 中脚本的命令 */
Pnpm.prototype.runScriptArgs = function (name) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['pnpm', 'run', name]];
});
});
};
return Pnpm;
}(PackageManager));
var Yarn = /** @class */ (function (_super) {
__extends(Yarn, _super);
function Yarn() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = 'yarn';
_this.lockfile = 'yarn.lock';
return _this;
}
/** 获取当前系统安装的 yarn 版本号 */
Yarn.prototype.version = function () {
return silentRunOutput('yarn --version');
};
/** 获取 registry 的命令 */
Yarn.prototype.registryArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['yarn', 'config', 'get', 'registry']];
});
});
};
/** 获取读取 tag 的前缀配置的命令 */
Yarn.prototype.tagVersionPrefixArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['yarn', 'config', 'get', 'version-tag-prefix']];
});
});
};
/** 获取初始化安装依赖的命令 */
Yarn.prototype.initInstallArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['yarn', 'install']];
});
});
};
/** 获取 ci 中安装依赖的命令 */
Yarn.prototype.ciInstallArgs = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['yarn', 'install', '--frozen-lockfile', '--production=false']];
});
});
};
/** 运行运行 scripts 中脚本的命令 */
Yarn.prototype.runScriptArgs = function (name) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, ['yarn', name]];
});
});
};
return Yarn;
}(PackageManager));
var CLIENT_KEY_MAP = {};
var CLIENT_INSTANCE_MAP = {};
function getPackageManager(npmClientKey) {
var key = npmClientKey || tryGetNpmClient();
if (CLIENT_INSTANCE_MAP[key])
return CLIENT_INSTANCE_MAP[key];
var instance;
if (key === 'npm') {
instance = new Npm();
}
else if (key === 'pnpm') {
instance = new Pnpm();
}
else if (key === 'yarn') {
instance = new Yarn();
}
else {
throw new Error("".concat(key, " is not a valid package manager name"));
}
CLIENT_INSTANCE_MAP[key] = instance;
return instance;
}
function tryGetNpmClient(workDirectory, defaultClient) {
var _a;
if (defaultClient === void 0) { defaultClient = 'npm'; }
var refFile = workDirectory || process.cwd();
if (CLIENT_KEY_MAP[refFile])
return CLIENT_KEY_MAP[refFile];
var returnClient = defaultClient;
var root = findupPackageRoot(refFile);
var pkg = readJsonFile(path.join(root, 'package.json'));
var configClient = (_a = pkg.durka) === null || _a === void 0 ? void 0 : _a.npmClient;
if (configClient) {
returnClient = configClient;
}
else {
var lockFiles = [
['yarn', 'yarn.lock'],
['pnpm', 'pnpm-lock.yaml'],
['npm', 'package-lock.json'],
];
for (var _i = 0, lockFiles_1 = lockFiles; _i < lockFiles_1.length; _i++) {
var _b = lockFiles_1[_i], client = _b[0], filename = _b[1];
if (existsFile(path.join(root, filename))) {
returnClient = client;
break;
}
}
}
CLIENT_KEY_MAP[refFile] = returnClient;
return returnClient;
}
export { PackageManager, getPackageManager, tryGetNpmClient };