Skip to content

Instantly share code, notes, and snippets.

@radekwarisch
radekwarisch / ComponentWithDecisions.tsx
Last active June 26, 2022 18:20
React component example with comments marking decisions required from developer during creation process
import * as React from 'react';
import {navigate, trackEvent} from '@common';
// decision 1: use style guide component or custom grid
import {Flex, Link} from 'brainly-style-guide';
// decision 2: use Readonly in type or not
type PropsType = Readonly<{
href: string;
@radekwarisch
radekwarisch / NewComponent.ts
Created January 31, 2021 15:11
Newly generated component
import * as React from 'react';
type PropsType = Readonly<{
data: Record<string, unknown>,
type: number,
isActive: boolean,
items: Array<unknown>,
}>;
const NewComponent = ({data, type, isActive, items}: PropsType) => {
@radekwarisch
radekwarisch / schematicUtils.ts
Last active February 9, 2021 17:35
Merge files utils
export const mergeFilesAnd = <TInputType>(
...plugins: (ChainableType<TInputType>)[]
) => (input: TInputType) => () => {
const {templates = empty()} = input;
return chain([
mergeWith(templates, MergeStrategy.Overwrite),
...plugins.map(compose(input)),
]);
};
@radekwarisch
radekwarisch / schematicUtils.ts
Last active February 9, 2021 17:36
Copy templates util
export const copyTemplatesAnd = <TInputType>(
...plugins: (ChainableType<TInputType>)[]
) => (input: TInputType) => () => {
const {templatesPath, movePath, templateVariables, tree} = input;
const newTemplates = apply(url(templatesPath), [
template(templateVariables),
move(movePath),
...plugins.map(compose(input)),
]);
@radekwarisch
radekwarisch / schematicUtils.ts
Created January 31, 2021 14:08
Chainify gather options util
export const chainifyGatherOptions = <TInputType>(
gatherOptionsFn: (
tree: Tree,
context: SchematicContext
) => Observable<TInputType> | Promise<TInputType> | TInputType
) => {
const reassignInputKeys = (input: TInputType) => (newInput: TInputType) => {
Object.entries(newInput).forEach(([key, newValue]: [string, unknown]) => {
Object.assign(input, {
[key]: newValue,
@radekwarisch
radekwarisch / interface.ts
Created January 31, 2021 13:41
Schematics Rule interface
export type Rule = (tree: Tree, context: SchematicContext) =>
Tree | Observable<Tree> | Rule | Promise<void | Rule> | void;
@radekwarisch
radekwarisch / gatherOptions.ts
Last active January 31, 2021 12:59
Gather options function
export const gatherOptions = <TInputType>(schema: ComponentSchemaType) => async (
tree: Tree,
context: SchematicContext
): TInputType => {
const {classify} = strings;
const {name, 'parent-file-name': parentFileName} = schema;
const fileName = classify(name);
const config = loadConfigFromFile({tree});
@radekwarisch
radekwarisch / mergeFilesPlugins.ts
Last active February 9, 2021 17:36
Merge files plugins
import {
noop,
externalSchematic,
} from '@angular-devkit/schematics';
import {compose, toPairs} from 'ramda'
export const lint = <TInputType>({
config,
}: TInputType) => compose(lint(config));
@radekwarisch
radekwarisch / copyTemplatesPlugins.ts
Last active February 9, 2021 17:37
Copy template plugins
import {
forEach,
rename,
FileEntry
} from '@angular-devkit/schematics';
export const maybeExcludeTemplates = <TInputType>({
options,
}: TInputType) => forEach(maybeExcludeFile(options));
@radekwarisch
radekwarisch / componentSchematic.ts
Last active February 9, 2021 17:37
Schematic execute file
import {
schematicPipe,
chainifyGatherOptions,
copyTemplatesAnd,
mergeFilesAnd
} from './schematicUtils';
import {gatherOptions} from './gatherOptions';
import {
maybeExcludeTemplates,
overwriteEJSExtension