|
| 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