UNPKG

@node-kit/yarn-workspace-root

Version:
81 lines (77 loc) 2.55 kB
import { normalize, join, relative, dirname } from 'path'; import { readJSONSync, getRealPathSync, readJSON, getRealPath } from '@node-kit/extra.fs'; import micromatch from 'micromatch'; 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 yarnWorkspaceRoot() { return _yarnWorkspaceRoot.apply(this, arguments); } function _yarnWorkspaceRoot() { _yarnWorkspaceRoot = _asyncToGenerator(function* () { var cwd = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : process.cwd(); var previous = null, current = normalize(cwd); do { var manifest = yield readJSON(join(current, "package.json")); var workspaces = extractWorkspaces(manifest); if (workspaces) { var relativePath = relative(current, yield getRealPath(cwd)); if (relativePath === "" || micromatch([relativePath], workspaces).length > 0) { return current; } else { return null; } } previous = current; current = dirname(current); } while (current !== previous); return null; }); return _yarnWorkspaceRoot.apply(this, arguments); } function yarnWorkspaceRootSync() { var cwd = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : process.cwd(); var previous = null, current = normalize(cwd); do { var manifest = readJSONSync(join(current, "package.json")); var workspaces = extractWorkspaces(manifest); if (workspaces) { var relativePath = relative(current, getRealPathSync(cwd)); if (relativePath === "" || micromatch([relativePath], workspaces).length > 0) { return current; } else { return null; } } previous = current; current = dirname(current); } while (current !== previous); return null; } function extractWorkspaces(manifest) { var workspaces = (manifest || {}).workspaces; return workspaces && workspaces.packages || (Array.isArray(workspaces) ? workspaces : null); } export { yarnWorkspaceRoot, yarnWorkspaceRootSync };