Skip to content

Commit 385bbb5

Browse files
committed
refactor: extract package fetching logic into shared utility
1 parent fc7359b commit 385bbb5

3 files changed

Lines changed: 94 additions & 83 deletions

File tree

src/commands/build.js

Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const path = require('path');
21
const webpack = require('webpack');
32
const { terminal } = require('terminal-kit');
43
// eslint-disable-next-line import/no-extraneous-dependencies
@@ -7,11 +6,9 @@ const webpackConfig = require('./build/webpack');
76

87
const spinner = ora();
98

10-
const { findAllProjectPaths, validateProject } = require('./../utils/projectpaths');
11-
const { getPackage } = require('./../utils/get-package');
129
const { getFilteredEntryPoints } = require('./../utils/get-filtered-entrypoints');
13-
const dirsExist = require('../utils/dirs-exist');
1410
const getProjectConfig = require('../utils/get-project-config');
11+
const getPackagesForCommand = require('../utils/get-packages-for-command');
1512

1613
global.buildCount = 0;
1714

@@ -56,66 +53,16 @@ exports.handler = async ({
5653
once = false,
5754
quiet = false,
5855
}) => {
59-
const currentLocation = path.basename(process.cwd());
60-
6156
const mode = production ? 'production' : 'development';
62-
// Use env variables if working on Webpack >=5.
63-
const projectsList = projects.split(',').map((item) => item.split('@')[0]).filter((item) => item.length > 0);
64-
const hasTargetDirs = dirsExist(targetDirs);
65-
const isAllProjects = (site || hasTargetDirs) && (!projects || projectsList.length === 0);
66-
67-
let packages = [];
68-
69-
try {
70-
if (projectsList.length === 0 && !isAllProjects) {
71-
// Is project root - a standalone build.
72-
terminal(`\x1b[1mCompiling \x1b[4msingle\x1b[0m\x1b[1m project in ${mode} mode.\x1b[0m\n`);
73-
packages.push(getPackage(path.resolve('./')));
74-
} else if (isAllProjects) {
75-
// Find all projects through-out the site.
76-
terminal(`\x1b[1mCompiling \x1b[4mall\x1b[0m\x1b[1m projects in ${mode} mode.\x1b[0m\n`);
77-
packages = findAllProjectPaths(targetDirs).map((path) => getPackage(path));
78-
} else {
79-
// List of projects.
80-
terminal(`\x1b[1mCompiling \x1b[4mlist\x1b[0m\x1b[1m of projects in ${mode} mode.\x1b[0m\n`);
81-
packages = findAllProjectPaths(targetDirs, projectsList).map((path) => getPackage(path));
82-
83-
const packageNames = packages.map((pkg) => pkg.name);
84-
projectsList.map((projectName) => {
85-
if (!packageNames.includes(projectName)) {
86-
terminal.red(`Error: Project ${projectName} does not exist.\n`);
87-
}
88-
packageNames.includes(projectName);
89-
});
90-
}
91-
} catch (e) {
92-
terminal.red(e);
93-
process.exit(1);
94-
}
95-
96-
const validProjects = packages.filter((pkg) => validateProject(pkg));
97-
98-
if (!quiet) {
99-
const invalidProjects = packages.filter((pkg) => !validateProject(pkg));
100-
invalidProjects.map((invalidProject) =>
101-
terminal.red(`[${invalidProject.relativePath}] no entrypoints\n`),
102-
);
103-
}
104-
105-
if (validProjects.length === 0) {
106-
terminal.red(`Error: No projects found\n`);
107-
process.exit(1);
108-
}
10957

110-
terminal('Processing the following projects:\n');
111-
validProjects.forEach((pkg) => {
112-
terminal.defaultColor(` * %s `, pkg.name).dim(`[%s]\n`, pkg.relativePath);
58+
const packages = await getPackagesForCommand({
59+
projects,
60+
site,
11361
});
114-
terminal('\n');
11562

11663
spinner.start('Building webpack configs.\n');
11764

118-
const configMap = validProjects.map((packageObject) => {
65+
const configMap = packages.map((packageObject) => {
11966
// Empty array means all entrypoints.
12067
let filteredEntrypoints = [];
12168

src/commands/installer/installer.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,16 @@ const { execSync } = require('child_process');
33
const yargs = require('yargs');
44
const args = yargs.argv._;
55

6-
const { findAllProjectPaths } = require('./../../utils/projectpaths');
7-
const { getPackage } = require('./../../utils/get-package');
8-
const dirsExist = require('./../../utils/dirs-exist');
6+
const getPackagesForCommand = require('./../../utils/get-packages-for-command');
97

10-
module.exports = (commandType) => {
8+
module.exports = async (commandType) => {
119
if (!['ci', 'install'].includes(commandType)) {
1210
throw new Error('Not a valid comment type.');
1311
}
1412

15-
const hasTargetDirs = dirsExist(targetDirs);
16-
17-
if (!hasTargetDirs) {
18-
throw new Error('Recursive install only works from the site root directory.');
19-
}
20-
21-
let paths = findAllProjectPaths(targetDirs);
22-
23-
let packages = [];
24-
25-
try {
26-
packages = paths.map((item) => getPackage(item, false)).filter((item) => item);
27-
} catch (e) {
28-
terminal.red(e);
29-
process.exit(1);
30-
}
31-
32-
terminal('Installing packages for the following projects:\n');
33-
packages.forEach((pkg) => {
34-
terminal.defaultColor(` * %s `, pkg.name).dim(`[%s]\n`, pkg.relativePath);
13+
const packages = await getPackagesForCommand({
14+
requireSiteRoot: true,
3515
});
36-
terminal('\n');
3716

3817
const gluedArgs = args.join(' ');
3918

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const { terminal } = require('terminal-kit');
2+
const { findAllProjectPaths, validateProject } = require('./projectpaths');
3+
const { getPackage } = require('./get-package');
4+
const dirsExist = require('./dirs-exist');
5+
const path = require('path');
6+
7+
/**
8+
* Get valid packages to run a command against.
9+
*
10+
* Bases results on the following logic:
11+
* - If the `site` flag is set, or the command was ran from within wp-content, all projects are targeted.
12+
* - If the `projects` flag is set, only those projects are targeted.
13+
* - If neither the `site` or `projects` flags are set, only the current project is targeted.
14+
*
15+
* @param {Object} args
16+
* @param {string} [args.projects] Comma separated list of projects to target.
17+
* @param {boolean} [args.site] Run the process from the root of a site, such as from wp-content.
18+
* @param {boolean} [args.requireSiteRoot] Require the process to be run from the site
19+
*
20+
* @return {Array} An array of valid projects to run the command against.
21+
*/
22+
const getPackagesForCommand = ({ projects = '', site = false, requireSiteRoot = true }) => {
23+
const projectsList = projects
24+
.split(',')
25+
.map((item) => item.split('@')[0])
26+
.filter((item) => item.length > 0);
27+
28+
const hasTargetDirs = dirsExist(targetDirs);
29+
30+
if (requireSiteRoot && !hasTargetDirs) {
31+
terminal.red(`Command only works from the site root directory.\n`);
32+
}
33+
34+
const isAllProjects = (site || hasTargetDirs) && (!projects || projectsList.length === 0);
35+
36+
let validProjects = [];
37+
38+
try {
39+
let packages = [];
40+
41+
if (projectsList.length === 0 && !isAllProjects) {
42+
// Single project
43+
packages.push(getPackage(path.resolve('./')));
44+
} else if (isAllProjects) {
45+
// All projects
46+
packages = findAllProjectPaths(targetDirs).map((path) => getPackage(path));
47+
} else {
48+
// Specific projects
49+
packages = findAllProjectPaths(targetDirs, projectsList).map((path) => getPackage(path));
50+
const packageNames = packages.map((pkg) => pkg.name);
51+
projectsList.map((projectName) => {
52+
if (!packageNames.includes(projectName)) {
53+
terminal.red(`Error: Project ${projectName} does not exist.\n`);
54+
}
55+
packageNames.includes(projectName);
56+
});
57+
}
58+
59+
packages.map((pkg) => {
60+
if (validateProject(pkg)) {
61+
validProjects.push(pkg);
62+
} else {
63+
terminal.yellow(`Warning: Project [${pkg.relativePath}] has no entrypoints\n`);
64+
}
65+
});
66+
67+
if (validProjects.length === 0) {
68+
terminal.red(`Error: No projects found\n`);
69+
process.exit(1);
70+
}
71+
72+
terminal('Processing the following projects:\n');
73+
validProjects.forEach((pkg) => {
74+
terminal.defaultColor(` * %s `, pkg.name).dim(`[%s]\n`, pkg.relativePath);
75+
});
76+
terminal('\n');
77+
} catch (e) {
78+
terminal.red(e);
79+
process.exit(1);
80+
}
81+
82+
return validProjects;
83+
};
84+
85+
module.exports = getPackagesForCommand;

0 commit comments

Comments
 (0)