Skip to content
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
8 changes: 4 additions & 4 deletions src/components/Form/Text/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { HTMLAttributes, HTMLInputAutoCompleteAttribute, HTMLInputTypeAttribute, ReactElement } from 'react';
import { HTMLInputAutoCompleteAttribute, HTMLInputTypeAttribute, InputHTMLAttributes, ReactElement } from 'react';
import { RegisterOptions, useFormContext } from 'react-hook-form';

import FormError from '@/components/Form/Error';

import styles from '../form.module.css';

type TextInputProps = {
title: string;
title?: string;
name: string;
options?: RegisterOptions;
type?: HTMLInputTypeAttribute;
autoComplete?: HTMLInputAutoCompleteAttribute;
} & HTMLAttributes<HTMLInputElement>;
} & InputHTMLAttributes<HTMLInputElement>;

const TextInput = ({
name,
Expand All @@ -25,7 +25,7 @@ const TextInput = ({

return (
<label className={styles.label}>
<span className={styles.title}>{title}</span>{' '}
{title && <span className={styles.title}>{title}</span>}{' '}
<input
className={styles.input}
{...form.register(name, options)}
Expand Down
3 changes: 3 additions & 0 deletions src/components/Generator/Generator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type GeneratorSettings = {
font: Font;
runtime: 'local' | 'docker-compose';
developmentProxy: boolean;
localHosts: boolean;
localDomains: { domain: string }[];
checkoutGitRequest: 'none' | 'github' | 'gitlab';
configureGitHooks: boolean;
subTaskfile: boolean;
Expand All @@ -30,6 +32,7 @@ const Generator = (): ReactElement => {
project: 'Taskfile',
font: 'Shadow',
runtime: 'local',
localDomains: [{ domain: '' }],
checkoutGitRequest: 'none',
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const lineRenderers: Record<RendererType, LineRenderer> = {
},
},
[RendererType.Conditionals]: {
test: (line) => /^if+|^then+|^else+|^fi/.test(line.trim()),
test: (line) => /^if+|^then+|^for+|^done+|^else+|^fi/.test(line.trim()),
render: (line, i) => (
<div key={i} className={styles['text-pink']}>
{line}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Generator/GeneredTaskfile/addons/addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TaskfileAddons } from '@/components/Generator/GeneredTaskfile/taskfile'

import runtime from './runtime';
import git from './git';
import localHosts from './localHosts';
import subTaskfile from './subTaskfile';
import fileUtilities from './fileUtilities';
import appUtilities from '@/components/Generator/GeneredTaskfile/addons/appUtilities';
Expand All @@ -16,6 +17,7 @@ import appUtilities from '@/components/Generator/GeneredTaskfile/addons/appUtili
const renderAddons = (settings: GeneratorSettings, addons: TaskfileAddons): void => {
runtime(settings, addons);
git(settings, addons);
localHosts(settings, addons);
fileUtilities(settings, addons);
appUtilities(settings, addons);
subTaskfile(settings, addons);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use client';

import { ReactElement, useEffect } from 'react';
import { useFieldArray, useFormContext } from 'react-hook-form';

import TextInput from '@/components/Form/Text';
import { GeneratorSettings } from '@/components/Generator';

import styles from './local-domains.module.scss';

const LocalDomains = (): ReactElement => {
const form = useFormContext<GeneratorSettings>();
const { fields, append, remove } = useFieldArray<GeneratorSettings, 'localDomains'>({ name: 'localDomains' });
const domains = form.watch('localDomains');
const lastDomain = domains?.length ? domains[domains.length - 1]?.domain : undefined;
const emptyIndex = domains ? domains.slice(0, -1).findIndex((entry) => !entry?.domain) : -1;

// Remove emptied textboxes and make sure there is always one empty textbox to add a new domain
useEffect(() => {
if (emptyIndex !== -1) {
remove(emptyIndex);
return;
}

if (lastDomain !== '') {
append({ domain: '' }, { shouldFocus: false });
}
}, [emptyIndex, lastDomain, append, remove]);

return (
<div className={styles.container}>
{fields.map((field, index) => (
<TextInput key={field.id} name={`localDomains.${index}.domain`} placeholder="example.local" />
))}
</div>
);
};

export default LocalDomains;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './localHosts';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.container {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GeneratorSettings } from '@/components/Generator';
import { TaskfileAddons } from '@/components/Generator/GeneredTaskfile/taskfile';
import loadTemplate from '@/helpers/loadTemplate';
import setLocalHostsSh from './set-local-hosts.sh';

const localHosts = (settings: GeneratorSettings, addon: TaskfileAddons): void => {
if (!settings.localHosts) {
return;
}

const domains = (settings.localDomains ?? []).map((entry) => entry.domain.trim()).filter(Boolean);

if (domains.length === 0) {
return;
}

addon.projectFunctions.push(
loadTemplate(setLocalHostsSh, {
project: settings.project || 'Taskfile',
domains: domains.map((domain) => `\t\t"${domain}"`).join('\n'),
})
);

addon.initCheckCommands.push('task:set-local-hosts');
};

export default localHosts;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function task:set-local-hosts { ## Add required local hosts to /etc/hosts
title "Checking local hosts"
local domains=(
[[domains]]
)
local domain pattern
for domain in "${domains[@]}"; do
pattern="^127\.0\.0\.1[[:space:]]+${domain//./\\.}([[:space:]]|\$)"
if grep -qE "$pattern" /etc/hosts; then
echo -e "${GREEN}✓${RESET} host ${GREEN}${domain}${RESET} is present."
else
echo -e "Adding ${YELLOW}${domain}${RESET} to ${YELLOW}/etc/hosts${RESET} (${RED}sudo required${RESET})..."
echo "127.0.0.1 ${domain} # project: [[project]]" | sudo tee -a /etc/hosts > /dev/null
echo -e "${GREEN}✓${RESET} added host ${GREEN}${domain}${RESET}."
fi
done
}
4 changes: 4 additions & 0 deletions src/components/Generator/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import RadioInput from '@/components/Form/Radio';
import { useFormContext } from 'react-hook-form';
import { GeneratorSettings } from '@/components/Generator';
import Checkbox from '@/components/Form/Checkbox';
import LocalDomains from '@/components/Generator/GeneredTaskfile/addons/localHosts/LocalDomains';

const Settings = (): ReactElement => {
const form = useFormContext<GeneratorSettings>();
Expand Down Expand Up @@ -80,6 +81,9 @@ const Settings = (): ReactElement => {
]}
/>
<Checkbox name="configureGitHooks">Configure git hooks</Checkbox>
<h2>Local hosts</h2>
<Checkbox name="localHosts">Check and add local domains</Checkbox>
{settings.localHosts && <LocalDomains />}
<h2>SubTaskfile</h2>
<Checkbox name="subTaskfile">Include SubTaskfile example</Checkbox>
<h2>Utilities</h2>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Generator/Settings/settings.module.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.container {
display: flex;
flex-direction: column;
gap: 1rem;
gap: 0.5rem;

h2 {
font-size: 1rem;
Expand Down
Loading