Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroHryshyn committed Jan 17, 2024
1 parent 6b2e8d2 commit d7e376b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 42 deletions.
61 changes: 39 additions & 22 deletions codemods/next/13/app-directory-boilerplate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ type State = {
underscoreAppPath: string | null;
underscoreAppData: string | null;
underscoreDocumentPath: string | null;
underscoreDocumentData: string | null
}
underscoreDocumentData: string | null;
};

type FileAPI = Parameters<HandleFile<Dependencies, State>>[0];
type DataAPI = Parameters<HandleData<Dependencies, State>>[0];
Expand Down Expand Up @@ -745,11 +745,12 @@ const injectLayoutClientComponent = (sourceFile: SourceFile) => {
);
};

const handleFile: Filemod<
Dependencies,
State
>['handleFile'] = async (api, path, options, state) => {

const handleFile: Filemod<Dependencies, State>['handleFile'] = async (
api,
path,
options,
state,
) => {
const parsedPath = parse(path);
const directoryNames = parsedPath.dir.split(sep);
const endsWithPages =
Expand All @@ -759,31 +760,37 @@ const handleFile: Filemod<
const nameIsIndex = parsedPath.name === 'index';

// reads underscoreAppData & underscoreDocumentData files and stores the data to the state, because files can be deleted during codemod execution
if(state !== null && state.underscoreAppData === null && state.underscoreDocumentData === null) {
if (
state !== null &&
state.underscoreAppData === null &&
state.underscoreDocumentData === null
) {
const extensiolessUnderscoreDocumentPath = join(
parsedPath.dir,
'_document',
);

const underscoreDocumentPath = resolveExtensionlessFilePath(
extensiolessUnderscoreDocumentPath,
api,
);

const extensionlessUnderscoreAppPath = join(parsedPath.dir, '_app');

const underscoreAppPath = resolveExtensionlessFilePath(
extensionlessUnderscoreAppPath,
api,
);

if (underscoreAppPath !== null && state !== null) {
state.underscoreAppData = await api.readFile(underscoreAppPath);
state.underscoreAppPath = underscoreAppPath;
}

if(underscoreDocumentPath !== null && state !== null) {
state.underscoreDocumentData = await api.readFile(underscoreDocumentPath);

if (underscoreDocumentPath !== null && state !== null) {
state.underscoreDocumentData = await api.readFile(
underscoreDocumentPath,
);
state.underscoreAppPath = underscoreDocumentPath;
}
}
Expand Down Expand Up @@ -838,7 +845,6 @@ const handleFile: Filemod<

const rootNotFoundPathIncluded =
api.exists(jsxNotFoundPath) || api.exists(tsxNotFoundPath);


const oldData = await api.readFile(path);
const commands: FileCommand[] = [
Expand Down Expand Up @@ -877,9 +883,12 @@ const handleFile: Filemod<
path,
},
];

if (state !== null && state.underscoreAppPath !== null && state.underscoreAppData !== null) {

if (
state !== null &&
state.underscoreAppPath !== null &&
state.underscoreAppData !== null
) {
commands.unshift({
kind: 'upsertFile' as const,
path: format({
Expand Down Expand Up @@ -907,7 +916,8 @@ const handleFile: Filemod<
}),
options: {
...options,
...(state !== null && state.underscoreDocumentPath !== null &&
...(state !== null &&
state.underscoreDocumentPath !== null &&
state.underscoreDocumentData !== null && {
underscoreDocumentPath: state.underscoreDocumentPath,
underscoreDocumentData: state.underscoreDocumentData,
Expand Down Expand Up @@ -1122,9 +1132,16 @@ const handleData: HandleData<Dependencies, State> = async (
}
};

const initializeState: Filemod<Dependencies, State>['initializeState'] = async (
) => {
return { underscoreAppData: null, underscoreAppPath: null, underscoreDocumentData: null, underscoreDocumentPath: null }
const initializeState: Filemod<
Dependencies,
State
>['initializeState'] = async () => {
return {
underscoreAppData: null,
underscoreAppPath: null,
underscoreDocumentData: null,
underscoreDocumentPath: null,
};
};

export const repomod: Filemod<Dependencies, State> = {
Expand Down
41 changes: 21 additions & 20 deletions codemods/next/13/app-directory-boilerplate/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const getServerSideProps = () => {

const transform = async (json: DirectoryJSON) => {
const volume = Volume.fromJSON(json, 'C:\\');
console.log(volume.toJSON(), "???")
const fileSystemManager = new FileSystemManager(
volume.promises.readdir as any,
volume.promises.readFile as any,
Expand Down Expand Up @@ -299,13 +298,13 @@ describe('next 13 app-directory-boilerplate', function () {
// command.path === 'C:\\project\\app\\components.tsx' &&
// command.data.replace(/\W/gm, '') ===
// `
// 'use client';
// // This file has been sourced from: C\\project\\pages\\index.jsx
// export default function Index({}) {
// return null;
// }
// ;`.replace(/\W/gm, '')
// 'use client';
// // This file has been sourced from: C\\project\\pages\\index.jsx

// export default function Index({}) {
// return null;
// }
// ;`.replace(/\W/gm, '')
// );
// }),
// );
Expand All @@ -317,15 +316,15 @@ describe('next 13 app-directory-boilerplate', function () {
// command.path === 'C:\\project\\app\\[a]\\c\\page.tsx' &&
// command.data.replace(/\W/gm, '') ===
// `
// // This file has been sourced from: C"\\project\\pages\\[a]\\c.tsx
// import Components from "./components";
// // TODO reimplement getServerSideProps with custom logic
// const getServerSideProps = () => {
// };
// export default async function Page(props: any) {
// return <Components {...props}/>;
// }
// `.replace(/\W/gm, '')
// // This file has been sourced from: C"\\project\\pages\\[a]\\c.tsx
// import Components from "./components";
// // TODO reimplement getServerSideProps with custom logic
// const getServerSideProps = () => {
// };
// export default async function Page(props: any) {
// return <Components {...props}/>;
// }
// `.replace(/\W/gm, '')
// );
// }),
// );
Expand All @@ -338,9 +337,9 @@ describe('next 13 app-directory-boilerplate', function () {
// 'C:\\project\\app\\[a]\\[b]\\components.tsx' &&
// command.data.replace(/\W/gm, '') ===
// `
// 'use client';
// // This file has been sourced from: C:\\project\\pages\\[a]\\[b].tsx
// `.replace(/\W/gm, '')
// 'use client';
// // This file has been sourced from: C:\\project\\pages\\[a]\\[b].tsx
// `.replace(/\W/gm, '')
// );
// }),
// );
Expand Down Expand Up @@ -823,6 +822,8 @@ describe('next 13 app-directory-boilerplate', function () {
'/opt/project/pages/a/b/c.tsx': index,
});

console.log([upsertPageCommand, _, deleteFileCommand], '???');

deepStrictEqual(upsertPageCommand?.kind, 'upsertFile');
deepStrictEqual(
upsertPageCommand?.path,
Expand Down

0 comments on commit d7e376b

Please sign in to comment.