Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Move builder functions to a common helper #12

Merged
merged 1 commit into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 2 additions & 24 deletions src/commands/componentPaths.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,20 @@ import chalk from 'chalk';
import path from 'path';
import fromEntries from 'object.fromentries';
import getComponentName from 'airbnb-prop-types/build/helpers/getComponentName';
import has from 'has';

import forEachProject from '../traversal/forEachProject';
import forEachDescriptor from '../traversal/forEachDescriptor';
import getProjectRootConfig from '../helpers/getProjectRootConfig';
import requireFiles from '../helpers/requireFiles';
import getComponentPaths from '../helpers/getComponentPaths';
import normalizeConfig from '../helpers/normalizeConfig';
import validateProjects from '../helpers/validateProjects';
import validateProject from '../helpers/validateProject';
import stripMatchingPrefix from '../helpers/stripMatchingPrefix';
import validateCommand from '../helpers/validateCommand';

export const command = 'componentPaths';
export const desc = 'print out component paths for the given project';

export const builder = (yargs) => {
const config = normalizeConfig(yargs.argv);
const { project, projects, all } = config;
const allProjectNames = projects ? Object.keys(projects) : [];

if (all && allProjectNames.length <= 0) {
throw chalk.red('`--all` requires a non-empty “projects” config');
}
if (all && project) {
throw chalk.red('`--all` and `--project` are mutually exclusive');
}
if (project && !has(projects, project)) {
throw chalk.red(`Project "${project}" missing from “projects” config`);
}

if (projects) {
validateProjects(projects, allProjectNames, 'in the “projects” config');
} else {
validateProject(config);
}
};
export const builder = validateCommand();

export const handler = (config) => {
const projectRoot = process.cwd();
Expand Down
26 changes: 2 additions & 24 deletions src/commands/validate.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import chalk from 'chalk';
import has from 'has';

import getComponents from '../helpers/getComponents';
import getVariationProviders from '../helpers/getVariationProviders';
import getValidationErrors from '../helpers/getValidationErrors';
import requireFiles from '../helpers/requireFiles';
import forEachProject from '../traversal/forEachProject';
import validateProjects from '../helpers/validateProjects';
import validateProject from '../helpers/validateProject';
import normalizeConfig from '../helpers/normalizeConfig';
import validateCommand from '../helpers/validateCommand';

function getOverallErrors({
variations = {},
Expand Down Expand Up @@ -55,27 +53,7 @@ function getOverallErrors({

export const command = 'validate [variations]';
export const desc = 'validate Variation Providers';
export const builder = (yargs) => {
const config = normalizeConfig(yargs.argv);
const { project, projects, all } = config;
const allProjectNames = projects ? Object.keys(projects) : [];

if (all && allProjectNames.length <= 0) {
throw chalk.red('`--all` requires a non-empty “projects” config');
}
if (all && project) {
throw chalk.red('`--all` and `--project` are mutually exclusive');
}
if (project && !has(projects, project)) {
throw chalk.red(`Project "${project}" missing from “projects” config`);
}

if (projects) {
validateProjects(projects, allProjectNames, 'in the “projects” config');
} else {
validateProject(config);
}
};
export const builder = validateCommand;

export const handler = (config) => {
const projectRoot = process.cwd();
Expand Down
28 changes: 28 additions & 0 deletions src/helpers/validateCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import chalk from 'chalk';
import has from 'has';

import normalizeConfig from './normalizeConfig';
import validateProjects from './validateProjects';
import validateProject from './validateProject';

export default function validateCommand(yargs) {
const config = normalizeConfig(yargs.argv);
const { project, projects, all } = config;
const allProjectNames = projects ? Object.keys(projects) : [];

if (all && allProjectNames.length <= 0) {
throw chalk.red('`--all` requires a non-empty “projects” config');
}
if (all && project) {
throw chalk.red('`--all` and `--project` are mutually exclusive');
}
if (project && !has(projects, project)) {
throw chalk.red(`Project "${project}" missing from “projects” config`);
}

if (projects) {
validateProjects(projects, allProjectNames, 'in the “projects” config');
} else {
validateProject(config);
}
}