Skip to content

Commit 77bc8cd

Browse files
authored
Merge pull request #130 from bigbite/fix/extract-package-fetching
Extract package fetching logic into shared utility
2 parents ad6e85b + 248127d commit 77bc8cd

3 files changed

Lines changed: 110 additions & 82 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 = 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: 3 additions & 24 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

108
module.exports = (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 = getPackagesForCommand({
14+
requireSiteRoot: true,
3515
});
36-
terminal('\n');
3716

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

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

0 commit comments

Comments
 (0)