Skip to content

Commit f758e2a

Browse files
committed
Sanitize app name
1 parent 02fe955 commit f758e2a

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

packages/create-react-admin/src/ProjectState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export type ProjectConfiguration = {
22
name: string;
33
step:
4+
| 'name'
45
| 'data-provider'
56
| 'auth-provider'
67
| 'resources'
@@ -16,7 +17,7 @@ export type ProjectConfiguration = {
1617

1718
export const InitialProjectConfiguration: ProjectConfiguration = {
1819
name: '',
19-
step: 'data-provider',
20+
step: 'name',
2021
dataProvider: '',
2122
authProvider: '',
2223
resources: [],
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import * as React from 'react';
2+
import { useState } from 'react';
3+
import TextInput from 'ink-text-input';
4+
import { Text } from 'ink';
5+
import { Stack } from './Stack.js';
6+
7+
export const StepName = ({
8+
onSubmit,
9+
initialValue,
10+
}: {
11+
initialValue: string;
12+
onSubmit: (value: string) => void;
13+
}) => {
14+
const [value, setValue] = useState(initialValue || '');
15+
16+
const handleSubmit = (value: string) => {
17+
if (value !== '') {
18+
onSubmit(value);
19+
}
20+
};
21+
return (
22+
<Stack>
23+
<Text>
24+
Enter the name of your application and validate with Enter:
25+
</Text>
26+
<TextInput
27+
value={value}
28+
onChange={v => setValue(v)}
29+
onSubmit={handleSubmit}
30+
/>
31+
</Stack>
32+
);
33+
};

packages/create-react-admin/src/app.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { StepAuthProvider } from './StepAuthProvider.js';
1010
import { StepResources } from './StepResources.js';
1111
import { StepInstall } from './StepInstall.js';
1212
import { useInstallDeps } from './useInstallDeps.js';
13+
import { StepName } from './StepName.js';
1314

1415
type Props = {
1516
name: string | undefined;
@@ -20,6 +21,12 @@ const stepReducer = (
2021
action
2122
): ProjectConfiguration => {
2223
switch (state.step) {
24+
case 'name':
25+
return {
26+
...state,
27+
name: action.value,
28+
step: 'data-provider',
29+
};
2330
case 'data-provider':
2431
return {
2532
...state,
@@ -67,16 +74,21 @@ const stepReducer = (
6774
};
6875

6976
export default function App({ name = 'my-admin' }: Props) {
77+
const sanitizedName = sanitizeName(name);
7078
const [state, dispatch] = useReducer(stepReducer, {
7179
...InitialProjectConfiguration,
72-
name,
80+
name: sanitizedName,
81+
step: sanitizedName === name ? 'data-provider' : 'name',
7382
});
7483
const helpMessages = useRef([]);
7584
const installDeps = useInstallDeps();
7685
const handleSubmit = (value: any) => {
7786
dispatch({ value });
7887
};
7988

89+
if (state.step === 'name') {
90+
return <StepName initialValue={state.name} onSubmit={handleSubmit} />;
91+
}
8092
if (state.step === 'data-provider') {
8193
return <StepDataProvider onSubmit={handleSubmit} />;
8294
}
@@ -125,3 +137,12 @@ export default function App({ name = 'my-admin' }: Props) {
125137
</>
126138
);
127139
}
140+
141+
const sanitizeName = (name: string) => {
142+
return name
143+
.trim()
144+
.toLowerCase()
145+
.replace(/\s+/g, '-')
146+
.replace(/^[._]/, '')
147+
.replace(/[^a-z\d\-~]+/g, '-');
148+
};

0 commit comments

Comments
 (0)