@teambit/workspace
Version:
130 lines (125 loc) • 5.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PatternCommand = void 0;
function _chalk() {
const data = _interopRequireDefault(require("chalk"));
_chalk = function () {
return data;
};
return data;
}
function _bitError() {
const data = require("@teambit/bit-error");
_bitError = function () {
return data;
};
return data;
}
function _scope() {
const data = require("@teambit/scope.remotes");
_scope = function () {
return data;
};
return data;
}
function _filter() {
const data = require("./filter");
_filter = function () {
return data;
};
return data;
}
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
class PatternCommand {
constructor(workspace) {
this.workspace = workspace;
_defineProperty(this, "name", 'pattern <pattern>');
_defineProperty(this, "alias", '');
_defineProperty(this, "description", 'test and validate component patterns');
_defineProperty(this, "extendedDescription", `this command helps validating a pattern before using it in other commands.
NOTE: always wrap the pattern with quotes to avoid collision with shell commands. depending on your shell, it might be single or double quotes.
a pattern can be a simple component-id or component-name. e.g. 'ui/button'.
a pattern can be used with wildcards for multiple component ids, e.g. 'org.scope/utils/**' or '**/utils/**' to capture all org/scopes.
to enter multiple patterns, separate them by a comma, e.g. 'ui/*, lib/*'
to exclude, use '!'. e.g. 'ui/**, !ui/button'
the matching algorithm is from multimatch ( https://github.com/sindresorhus/multimatch).
to filter by a state or attribute, prefix the pattern with "$". e.g. '$deprecated', '$modified'.
list of supported states: [${_filter().statesFilter.join(', ')}].
to filter by multi-params state/attribute, separate the params with ":", e.g. '$env:teambit.react/react'.
list of supported multi-params states: [env].
to match a state and another criteria, use " AND " keyword. e.g. '$modified AND teambit.workspace/** AND $env:teambit.react/react'.
`);
_defineProperty(this, "examples", [{
cmd: "bit pattern '**'",
description: 'matches all components'
}, {
cmd: "bit pattern '*/ui/*'",
description: 'matches components with any scope-name and the "ui" namespace. e.g. "ui/button" but not "ui/elements/button"'
}, {
cmd: "bit pattern '*/ui/**'",
description: 'matches components whose namespace starts with "ui/" e.g. "ui/button", "ui/elements/button"'
}, {
cmd: "bit pattern 'bar, foo'",
description: 'matches two components: bar and foo'
}, {
cmd: "bit pattern 'my-scope.org/**'",
description: 'matches all components of the scope "my-scope.org"'
}, {
cmd: "bit pattern --remote 'teambit.workspace/**'",
description: 'matches all components from the remote scope "teambit.workspace"'
}]);
_defineProperty(this, "group", 'info-analysis');
_defineProperty(this, "private", false);
_defineProperty(this, "options", [['j', 'json', 'return the output as JSON'], ['r', 'remote', 'query a remote scope (the pattern must start with the scope name, e.g. "scope-name/**")']]);
_defineProperty(this, "remoteOp", true);
}
async report([pattern], flags) {
const ids = await this.json([pattern], flags);
const title = _chalk().default.green(`found ${_chalk().default.bold(ids.length.toString())} components matching the pattern`);
return `${title}\n${ids.join('\n')}`;
}
async json([pattern], flags) {
const {
remote
} = flags;
if (remote) {
const ids = await this.getRemoteIds(pattern);
return ids.map(id => id.toString());
}
const ids = await this.workspace.idsByPattern(pattern, false);
return ids.map(id => id.toString());
}
async getRemoteIds(pattern) {
const patterns = pattern.split(',').map(p => p.trim());
// Extract unique scope names from patterns (excluding negation patterns for fetching)
const scopeNames = this.extractScopeNames(patterns.filter(p => !p.startsWith('!')));
// Fetch all component IDs from all referenced remote scopes
const allIds = [];
for (const scopeName of scopeNames) {
const remoteObj = await (0, _scope().getRemoteByName)(scopeName, this.workspace.consumer);
const listResults = await remoteObj.list();
allIds.push(...listResults.map(r => r.id));
}
// Use the existing pattern filtering logic
const filteredIds = await this.workspace.scope.filterIdsFromPoolIdsByPattern(pattern, allIds, false);
return filteredIds;
}
extractScopeNames(patterns) {
const scopeNames = new Set();
for (const p of patterns) {
if (!p.includes('/')) {
throw new (_bitError().BitError)(`when using --remote, the pattern must include the scope name followed by "/", e.g. "scope-name/**". got "${p}"`);
}
const [scopeName] = p.split('/');
scopeNames.add(scopeName);
}
return Array.from(scopeNames);
}
}
exports.PatternCommand = PatternCommand;
//# sourceMappingURL=pattern.cmd.js.map