-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathaction.impl.ts
48 lines (40 loc) · 1.55 KB
/
action.impl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { formatFiles, Tree } from '@nx/devkit';
import { insertImport } from '../../utils/insert-import';
import { insertStatementAfterImports } from '../../utils/insert-statement-after-imports';
import { insertStatementInDefaultFunction } from '../../utils/insert-statement-in-default-function';
import { resolveRemixRouteFile } from '../../utils/remix-route-utils';
import { LoaderSchema } from './schema';
export default async function (tree: Tree, schema: LoaderSchema) {
const routeFilePath =
schema.nameAndDirectoryFormat === 'as-provided'
? schema.path
: await resolveRemixRouteFile(tree, schema.path, schema.project);
if (!tree.exists(routeFilePath)) {
throw new Error(
`Route path does not exist: ${routeFilePath}. Please generate a Remix route first.`
);
}
insertImport(tree, routeFilePath, 'ActionFunctionArgs', '@remix-run/node', {
typeOnly: true,
});
insertImport(tree, routeFilePath, 'json', '@remix-run/node');
insertImport(tree, routeFilePath, 'useActionData', '@remix-run/react');
insertStatementAfterImports(
tree,
routeFilePath,
`
export const action = async ({ request }: ActionFunctionArgs) => {
let formData = await request.formData();
return json({message: formData.toString()}, { status: 200 });
};
`
);
const statement = `\nconst actionMessage = useActionData<typeof action>();`;
try {
insertStatementInDefaultFunction(tree, routeFilePath, statement);
} catch (err) {
// eslint-disable-next-line no-empty
} finally {
await formatFiles(tree);
}
}