Skip to content

Commit

Permalink
feat(config): enable choice of practices supplementally and exclusively
Browse files Browse the repository at this point in the history
  • Loading branch information
uladkasach committed Sep 3, 2024
1 parent 58781d4 commit d141ec3
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .husky/post-checkout
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
. "$(dirname -- "$0")/_/husky.sh"

. "$(dirname -- "$0")/check.nvm.sh"
# . "$(dirname -- "$0")/check.nvm.sh"
. "$(dirname -- "$0")/check.lockfile.sh"
105 changes: 44 additions & 61 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"postinstall": "[ -d .git ] && npx husky install || exit 0"
},
"dependencies": {
"@ehmpathy/error-fns": "1.0.2",
"@ehmpathy/error-fns": "1.3.2",
"@oclif/core": "2.0.11",
"@oclif/plugin-help": "3.3.1",
"chalk": "2.4.2",
Expand Down
11 changes: 9 additions & 2 deletions src/domain/objects/ActionUsePracticesConfig.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { DomainObject } from 'domain-objects';
import Joi from 'joi';
import { PickAny } from 'type-fns';

import { ProjectVariablesImplementation } from '../constants';
import { DeclaredPractices } from './DeclaredPractices';

const schema = Joi.object().keys({
rootDir: Joi.string().required(), // dir of config file, to which all config paths are relative
declared: DeclaredPractices.schema.required(), // the declared practices to use
useCase: Joi.string().required(), // specifies which use case to use
scope: Joi.object().keys({
usecase: Joi.string().required().allow(null),
practices: Joi.array().items(Joi.string().required()),
}),
variables: Joi.object().required(), // specifies which variables to use
});

export interface ActionUsePracticesConfig {
rootDir: string;
declared: DeclaredPractices;
useCase: string;
scope: PickAny<{
usecase: string | null;
practices: string[];
}>;
variables: ProjectVariablesImplementation;
}
export class ActionUsePracticesConfig
Expand Down
15 changes: 13 additions & 2 deletions src/domain/objects/ActionUsePracticesConfigInput.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import { DomainObject } from 'domain-objects';
import Joi from 'joi';
import { PickAny } from 'type-fns';

const schema = Joi.object().keys({
declarations: Joi.string().required(), // either an ssh path to a git repo - or a file path to a local directory
useCase: Joi.string().required(), // specifies which use case to use
useCase: Joi.string().required().optional(), // specifies which use case to use
scope: Joi.object()
.keys({
usecase: Joi.string().required().optional(),
practices: Joi.array().items(Joi.string().required()),
})
.optional(),
variables: Joi.object().optional(), // specifies which variables to use
});

export interface ActionUsePracticesConfigInput {
declarations: string;
useCase: string;
useCase?: string;
scope?: PickAny<{
usecase: string;
practices: string[];
}>;
variables?: Record<string, any>;
}
export class ActionUsePracticesConfigInput
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declarations: ../example-best-practices-repo
scope:
usecase: lambda-service
practices:
- cicd-common
- conventional-commits
- husky
variables:
organizationName: 'awesome-org'
serviceName: 'svc-awesome-thing'
infrastructureNamespaceId: 'abcde12345'
slackReleaseWebHook: 'https://...'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"best-practices-typescript": "0.1.7"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declarations: ../example-best-practices-repo
scope:
practices:
- cicd-common
- conventional-commits
- husky
variables:
organizationName: 'awesome-org'
serviceName: 'svc-awesome-thing'
infrastructureNamespaceId: 'abcde12345'
slackReleaseWebHook: 'https://...'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"best-practices-typescript": "0.1.7"
}
}
19 changes: 4 additions & 15 deletions src/logic/commands/apply.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RequiredAction } from '../../domain';
import { UnexpectedCodePathError } from '../UnexpectedCodePathError';
import { applyPlans } from '../usage/plan/apply/applyPlans';
import { filterPracticeEvaluationsFromPlans } from '../usage/plan/filterPracticeEvaluationsFromPlans';
import { getPlansForProject } from '../usage/plan/getPlansForProject';
import { readUsePracticesConfig } from '../usage/readUsePracticesConfig';
import { getDesiredPractices } from './getScopedPractices';

export const apply = async ({
usePracticesConfigPath,
Expand All @@ -21,24 +21,13 @@ export const apply = async ({
configPath: usePracticesConfigPath,
});

// grab the selected use case's practices
const useCase = config.declared.useCases.find(
(thisUseCase) => thisUseCase.name === config.useCase,
);
if (!useCase)
throw new UnexpectedCodePathError(
'requested use case was not defined on config. should have thrown an error when processing the config by now',
);
// grab the desired practices
const practices = getDesiredPractices({ config, filter });

// get plans for this project
console.log('🔬️ evaluating project...'); // tslint:disable-line: no-console
const plans = await getPlansForProject({
practices: useCase.practices.filter(
(practice) =>
filter?.practiceNames
? filter?.practiceNames.includes(practice.name) // if practice.name filter was defined, ensure practice.name is included
: true, // otherwise, all are included
),
practices,
projectRootDirectory: config.rootDir,
projectVariables: config.variables,
});
Expand Down
51 changes: 51 additions & 0 deletions src/logic/commands/getScopedPractices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { UnexpectedCodePathError } from '@ehmpathy/error-fns';

import { ActionUsePracticesConfig } from '../../domain/objects/ActionUsePracticesConfig';

export const getDesiredPractices = ({
config,
filter,
}: {
config: ActionUsePracticesConfig;
filter?: {
practiceNames?: string[];
filePaths?: string[];
};
}) => {
// grab the selected use case's practices
const usecase =
config.declared.useCases.find(
(thisUseCase) => thisUseCase.name === config.scope.usecase,
) ?? null;
if (!usecase && config.scope.usecase)
throw new UnexpectedCodePathError(
'requested usecase was not declared on config',
{ usecase: config.scope.usecase },
);

// declare the practices
const practicesChosen = [
...(usecase?.practices ?? []),
...(config.scope.practices ?? []).map(
(practiceName) =>
config.declared.practices.find(
(practice) => practice.name === practiceName,
) ??
UnexpectedCodePathError.throw(
'requested practice was not declared on config',
{ practiceName },
),
),
];

// filter the practices
const practicesFiltered = practicesChosen.filter(
(practice) =>
filter?.practiceNames
? filter?.practiceNames.includes(practice.name) // if practice.name filter was defined, ensure practice.name is included
: true, // otherwise, all are included
);

// return those
return practicesFiltered;
};
Loading

0 comments on commit d141ec3

Please sign in to comment.