@node-kit/pnpm-workspace-info
Version:
A simple utility to get the pnpm workspace information
161 lines (157 loc) • 4.69 kB
JavaScript
import { join } from 'path';
import fg from 'fast-glob';
import { loadYmlSync, loadYml } from 'load-yml';
import { dirname } from '@node-kit/extra.path';
import { pnpmWorkspaceRootSync, pnpmWorkspaceRoot } from '@node-kit/pnpm-workspace-root';
function _arrayLikeToArray(r, a) {
(null == a || a > r.length) && (a = r.length);
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
return n;
}
function asyncGeneratorStep(n, t, e, r, o, a, c) {
try {
var i = n[a](c),
u = i.value;
} catch (n) {
return void e(n);
}
i.done ? t(u) : Promise.resolve(u).then(r, o);
}
function _asyncToGenerator(n) {
return function () {
var t = this,
e = arguments;
return new Promise(function (r, o) {
var a = n.apply(t, e);
function _next(n) {
asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
}
function _throw(n) {
asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
}
_next(void 0);
});
};
}
function _createForOfIteratorHelper(r, e) {
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
if (!t) {
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) {
t && (r = t);
var n = 0,
F = function () {};
return {
s: F,
n: function () {
return n >= r.length ? {
done: !0
} : {
done: !1,
value: r[n++]
};
},
e: function (r) {
throw r;
},
f: F
};
}
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
var o,
a = !0,
u = !1;
return {
s: function () {
t = t.call(r);
},
n: function () {
var r = t.next();
return a = r.done, r;
},
e: function (r) {
u = !0, o = r;
},
f: function () {
try {
a || null == t.return || t.return();
} finally {
if (u) throw o;
}
}
};
}
function _unsupportedIterableToArray(r, a) {
if (r) {
if ("string" == typeof r) return _arrayLikeToArray(r, a);
var t = {}.toString.call(r).slice(8, -1);
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
}
}
var DEFAULT_IGNORE_PATHS = ["**/node_modules/**", "**/bower_components/**", "**/.*/**", "**/__test__/**", "**/__tests__/**", "**/test/**", "**/tests/**"];
var WORKSPACE_MANIFEST_FILENAME = "pnpm-workspace.yaml";
function pnpmWorkspaceInfo() {
return _pnpmWorkspaceInfo.apply(this, arguments);
}
function _pnpmWorkspaceInfo() {
_pnpmWorkspaceInfo = _asyncToGenerator(function* () {
var cwd = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : process.cwd();
var root = yield pnpmWorkspaceRoot(cwd);
if (!root) {
console.error("not a pnpm workspace project");
return null;
}
var manifest = yield loadYml(join(root, WORKSPACE_MANIFEST_FILENAME));
var projects = yield fg([].concat(manifest.packages), {
cwd: root,
ignore: DEFAULT_IGNORE_PATHS,
onlyDirectories: true
});
var workspaceInfo = {};
var _iterator2 = _createForOfIteratorHelper(projects), _step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done; ) {
var projectPath = _step2.value;
workspaceInfo[dirname(projectPath)] = {
path: projectPath
};
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return workspaceInfo;
});
return _pnpmWorkspaceInfo.apply(this, arguments);
}
function pnpmWorkspaceInfoSync() {
var cwd = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : process.cwd();
var root = pnpmWorkspaceRootSync(cwd);
if (!root) {
console.error("not a pnpm workspace project");
return null;
}
var manifest = loadYmlSync(join(root, WORKSPACE_MANIFEST_FILENAME));
var projects = fg.sync([].concat(manifest.packages), {
cwd: root,
ignore: DEFAULT_IGNORE_PATHS,
onlyDirectories: true
});
var workspaceInfo = {};
var _iterator = _createForOfIteratorHelper(projects), _step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done; ) {
var projectPath = _step.value;
workspaceInfo[dirname(projectPath)] = {
path: projectPath
};
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return workspaceInfo;
}
export { pnpmWorkspaceInfo, pnpmWorkspaceInfoSync };