codess
Version:
vscode代码片段管理器,通过 本地包(本地某个文件夹) 或官网的 远程包(代码片段集合) 生成本地项目的 vscode 代码片段配置。
85 lines (84 loc) • 3.34 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parsePackageNameStr = parsePackageNameStr;
exports.comparePackageStr = comparePackageStr;
const path_1 = __importDefault(require("path"));
const getAbsolute_1 = require("./utils/getAbsolute");
const url_1 = require("url");
function parsePackageNameStr(str) {
str = str.trim();
let packageConfig = {
type: undefined,
package: '',
op: {},
};
if (path_1.default.isAbsolute(str) || /^(\.\.|~|\.)?\/|file:/.test(str)) {
str = str.replace(/^file:/, '');
packageConfig.type = 'file';
packageConfig.package = `file:${path_1.default.relative(process.cwd(), (0, getAbsolute_1.getAbsolute)(str))}`;
packageConfig.op.file = {
path: (0, getAbsolute_1.getAbsolute)(str),
};
}
else if (/^(github:|https?:\/\/github\.com)/.test(str)) {
if (/^github:/.test(str)) {
let match = str.match(/^(github:)([^/]*)\/([^/]*)(?:\/tree\/([^/]*)(\/.*)?)?/);
if (match) {
match[4] = match[4] || 'master';
match[5] = match[5] || '/';
packageConfig.type = 'github';
packageConfig.package = `${packageConfig.type}:${match[2]}/${match[3]}/tree/${match[4]}${match[5]}`;
packageConfig.op.repository = {
owner: match[2],
repo: match[3],
ref: match[4],
path: match[5] || '',
};
}
}
else {
let url = new url_1.URL(str);
let match = url.pathname.match(/^\/([^/]*)\/([^/]*)(?:\/tree\/([^/]*)(\/.*)?)?/);
if (match) {
match[3] = match[3] || 'master';
match[4] = match[4] || '/';
packageConfig.type = 'github';
packageConfig.package = `${packageConfig.type}:${match[1]}/${match[2]}/tree/${match[3]}${match[4]}`;
packageConfig.op.repository = {
owner: match[1],
repo: match[2],
ref: match[3],
path: match[4] || '',
};
}
}
}
else if (/^[a-zA-Z][\w-]*(@[0-9]+(?:\.[0-9]+)*)?$/.test(str)) {
let match = str.match(/^([a-zA-Z][\w-]*)(@[0-9]+(?:\.[0-9]+)*)?$/);
packageConfig.type = 'web';
packageConfig.package = str;
packageConfig.op.web = {
name: match[1],
version: (match[2] || '').replace(/^@/, ''),
};
}
return packageConfig.type ? packageConfig : undefined;
}
function comparePackageStr(a, b) {
let ac = parsePackageNameStr(a);
let bc = parsePackageNameStr(b);
if ((ac === null || ac === void 0 ? void 0 : ac.type) && (bc === null || bc === void 0 ? void 0 : bc.type) && ac.type == bc.type) {
switch (ac.type) {
case 'file':
return ac.op.file.path == bc.op.file.path;
case 'github':
return ac.package == bc.package;
case 'web':
return ac.op.web.name == bc.op.web.name;
}
}
return false;
}