UNPKG

@node-kit/yarn-workspace-root

Version:
84 lines (79 loc) 2.62 kB
'use strict'; var path = require('path'); var extra_fs = require('@node-kit/extra.fs'); var micromatch = require('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 = path.normalize(cwd); do { var manifest = yield extra_fs.readJSON(path.join(current, "package.json")); var workspaces = extractWorkspaces(manifest); if (workspaces) { var relativePath = path.relative(current, yield extra_fs.getRealPath(cwd)); if (relativePath === "" || micromatch([relativePath], workspaces).length > 0) { return current; } else { return null; } } previous = current; current = path.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 = path.normalize(cwd); do { var manifest = extra_fs.readJSONSync(path.join(current, "package.json")); var workspaces = extractWorkspaces(manifest); if (workspaces) { var relativePath = path.relative(current, extra_fs.getRealPathSync(cwd)); if (relativePath === "" || micromatch([relativePath], workspaces).length > 0) { return current; } else { return null; } } previous = current; current = path.dirname(current); } while (current !== previous); return null; } function extractWorkspaces(manifest) { var workspaces = (manifest || {}).workspaces; return workspaces && workspaces.packages || (Array.isArray(workspaces) ? workspaces : null); } exports.yarnWorkspaceRoot = yarnWorkspaceRoot; exports.yarnWorkspaceRootSync = yarnWorkspaceRootSync;