source: node_modules/find-yarn-workspace-root/index.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.3 KB
Line 
1'use strict';
2
3const fs = require('fs');
4const micromatch = require('micromatch');
5const path = require('path');
6
7module.exports = findWorkspaceRoot;
8
9/**
10 * Adapted from:
11 * https://github.com/yarnpkg/yarn/blob/ddf2f9ade211195372236c2f39a75b00fa18d4de/src/config.js#L612
12 * @param {string} [initial]
13 * @return {string|null}
14 */
15function findWorkspaceRoot(initial) {
16 if (!initial) {
17 initial = process.cwd();
18 }
19 let previous = null;
20 let current = path.normalize(initial);
21
22 do {
23 const manifest = readPackageJSON(current);
24 const workspaces = extractWorkspaces(manifest);
25
26 if (workspaces) {
27 const relativePath = path.relative(current, initial);
28 if (relativePath === '' || micromatch([relativePath], workspaces).length > 0) {
29 return current;
30 } else {
31 return null;
32 }
33 }
34
35 previous = current;
36 current = path.dirname(current);
37 } while (current !== previous);
38
39 return null;
40}
41
42function extractWorkspaces(manifest) {
43 const workspaces = (manifest || {}).workspaces;
44 return (workspaces && workspaces.packages) || (Array.isArray(workspaces) ? workspaces : null);
45}
46
47function readPackageJSON(dir) {
48 const file = path.join(dir, 'package.json');
49 if (fs.existsSync(file)) {
50 return JSON.parse(fs.readFileSync(file, 'utf8'));
51 }
52 return null;
53}
Note: See TracBrowser for help on using the repository browser.