UNPKG

1.79 kBJavaScriptView Raw
1/**
2 * Copyright IBM Corp. 2019, 2019
3 *
4 * This source code is licensed under the Apache-2.0 license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10const execa = require('execa');
11const fs = require('fs-extra');
12const path = require('path');
13const packageJson = require('../../../package.json');
14
15const denylist = new Set(['.DS_Store']);
16const PACKAGES_DIR = path.resolve(__dirname, '../..');
17const packagePaths = fs
18 .readdirSync(PACKAGES_DIR)
19 .filter(basename => {
20 const filename = path.join(PACKAGES_DIR, basename);
21 if (!denylist.has(filename)) {
22 return false;
23 }
24
25 const stats = fs.lstatSync(filename);
26 if (!stats.isDirectory()) {
27 return false;
28 }
29
30 return true;
31 })
32 .map(pkg => {
33 const packageJsonPath = path.join(PACKAGES_DIR, pkg, 'package.json');
34 return {
35 basename: pkg,
36 packageJsonPath,
37 packageJson: fs.readJsonSync(packageJsonPath),
38 packagePath: path.join(PACKAGES_DIR, pkg),
39 };
40 });
41
42const env = {
43 root: {
44 directory: path.resolve(__dirname, '../../..'),
45 packageJson,
46 },
47 packagesDir: PACKAGES_DIR,
48 packagePaths,
49};
50
51function workspace(fn) {
52 return (...args) => fn(...args, env);
53}
54
55/**
56 * Lists the packages for the current project using the `lerna list` command
57 * @returns {Array<PackageInfo>}
58 */
59async function getPackages() {
60 const { stdout: lernaListOutput } = await execa('yarn', [
61 'lerna',
62 'list',
63 '--json',
64 ]);
65 return JSON.parse(
66 // Clean-up output by stripping out `yarn` information related to the
67 // command and how long it took to run
68 lernaListOutput
69 .split('\n')
70 .slice(2, -1)
71 .join('\n')
72 ).filter(pkg => !pkg.private);
73}
74
75module.exports = {
76 workspace,
77 getPackages,
78};