@tobiastengler/create-relay-app
Version:
Easy configuration of Relay for existing projects
51 lines (47 loc) • 1.82 kB
JavaScript
import path from "path";
import { NEXT_SRC_PATH } from "../../consts.js";
import { prettifyCode } from "../../utils/ast.js";
import { bold } from "../../utils/cli.js";
import { TaskBase } from "../TaskBase.js";
const code = `
import type { GetServerSideProps, GetStaticProps, PreviewData } from "next";
import type { ParsedUrlQuery } from "querystring";
import type { RecordMap } from "relay-runtime/lib/store/RelayStoreTypes";
export type RelayPageProps = {
initialRecords?: RecordMap;
};
export type GetRelayServerSideProps<
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData
> = GetServerSideProps<P & Required<RelayPageProps>, Q, D>;
export type GetRelayStaticProps<
P extends { [key: string]: any } = { [key: string]: any },
Q extends ParsedUrlQuery = ParsedUrlQuery,
D extends PreviewData = PreviewData
> = GetStaticProps<P & Required<RelayPageProps>, Q, D>;
`;
export class Next_AddTypeHelpers extends TaskBase {
constructor(context) {
super();
this.context = context;
this.message = "Add type helpers";
}
isEnabled() {
return this.context.is("next") && this.context.args.typescript;
}
async run() {
const filepath = Next_AddTypeHelpers.getRelayTypesPath(this.context);
this.updateMessage(this.message + " " + bold(filepath.rel));
if (this.context.fs.exists(filepath.abs)) {
this.skip("File exists");
return;
}
const prettifiedCode = prettifyCode(code);
await this.context.fs.writeToFile(filepath.abs, prettifiedCode);
}
static getRelayTypesPath(context) {
const filepath = path.join(NEXT_SRC_PATH, "relay-types.ts");
return context.env.rel(filepath);
}
}