Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add import path alias. #247

Merged
merged 11 commits into from
Feb 14, 2022
10 changes: 5 additions & 5 deletions packages/create-bison-app/template/README.md.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Because Nexus is strongly typed, all of the `t.` operations should autocomplete
```ts
import { objectType, inputObjectType, queryField, mutationField, arg, list, nonNull } from 'nexus';

import { isAdmin } from '../../services/permissions';
import { isAdmin } from '@/services/permissions';

// Organization Type
export const Organization = objectType({
Expand Down Expand Up @@ -272,8 +272,8 @@ export const OrganizationFactory = {
Here we use inline snapshots to confirm the error message content, but you can also manually assert the content.

```ts
import { graphQLRequest, graphQLRequestAsUser, resetDB, disconnect } from '../../helpers';
import { OrganizationFactory } from '../factories/organization';
import { graphQLRequest, graphQLRequestAsUser, resetDB, disconnect } from '@/tests/helpers';
import { OrganizationFactory } from '@/tests/factories/organization';

beforeEach(async () => resetDB());
afterAll(async () => disconnect());
Expand Down Expand Up @@ -515,7 +515,7 @@ import React from 'react';
import gql from 'graphql-tag';
import { Spinner, Text } from '@chakra-ui/react';

import { OrganizationQuery, useOrganizationQuery } from '../types';
import { OrganizationQuery, useOrganizationQuery } from '@/types';

export const QUERY = gql`
query organization($where: OrganizationWhereUniqueInput!) {
Expand Down Expand Up @@ -556,7 +556,7 @@ import Head from 'next/head';
import { Flex } from '@chakra-ui/react';
import { useRouter } from 'next/router';

import { OrganizationCell } from '../../cells/Organization';
import { OrganizationCell } from '@/cells/Organization';

function OrganizationPage() {
const router = useRouter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React from 'react';
import gql from 'graphql-tag';
import { Spinner, Text } from '@chakra-ui/react';

import { useMyProfileQuery, MyProfileQuery } from '../types';
import { useMyProfileQuery, MyProfileQuery } from '@/types';

export const QUERY = gql`
query myProfile {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ to: graphql/modules/<%= name %>.ts
<% plural = h.inflection.pluralize(camelized) -%>
import { objectType, inputObjectType, queryField, mutationField, arg, list, nonNull } from 'nexus';

import { isAdmin } from '../../services/permissions';
import { isAdmin } from '@/services/permissions';

// <%= camelized %> Type
export const <%= camelized %> = objectType({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ to: tests/unit/components/<%= h.camelizedBaseName(name) %>.test.tsx
<% component = h.camelizedBaseName(name) -%>
import React from 'react';

import { render } from '../../../tests/utils';
import { <%= component %> } from '../../../components/<%= component %>';
import { render } from '@/tests/utils';
import { <%= component %> } from '@/components/<%= component %>';

describe('<%= component %>', () => {
it('loads', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ to: tests/factories/<%= name %>.ts
import { Prisma } from '@prisma/client';
// import Chance from 'chance';

import { buildPrismaIncludeFromAttrs } from '../helpers/buildPrismaIncludeFromAttrs';
import { prisma } from '../../lib/prisma'
import { buildPrismaIncludeFromAttrs } from '@/tests/helpers/buildPrismaIncludeFromAttrs';
import { prisma } from '@/lib/prisma'

// const chance = new Chance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ to: tests/requests/<%= name %>.test.ts
<% model = name.split('/')[0] %>
<% section = h.baseName(name) -%>
<% upper = h.inflection.camelize(model, false) -%>
import { graphQLRequest, graphQLRequestAsUser, resetDB, disconnect } from '../../helpers';
import { <%= upper %>Factory } from '../../factories/<%= model %>';
import { graphQLRequest, graphQLRequestAsUser, resetDB, disconnect } from '@/tests/helpers';
import { <%= upper %>Factory } from '@/tests/factories/<%= model %>';

beforeEach(async () => resetDB());
afterAll(async () => disconnect());
Expand Down Expand Up @@ -53,4 +53,4 @@ describe('<%= upper %> <%= section %> mutation', () => {
expect(response.body).toBe(true)
})
})
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { ApolloClient, ApolloProvider, NormalizedCacheObject } from '@apollo/cli
import { ChakraProvider, CSSReset } from '@chakra-ui/react';
import { Dict } from '@chakra-ui/utils';

import { AuthProvider } from '../context/auth';
import { createApolloClient } from '../lib/apolloClient';
import defaultTheme from '../chakra';
import { AuthProvider } from '@/context/auth';
import { createApolloClient } from '@/lib/apolloClient';
import defaultTheme from '@/chakra';

interface Props {
apolloClient?: ApolloClient<NormalizedCacheObject>;
Expand Down
12 changes: 6 additions & 6 deletions packages/create-bison-app/template/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Flex, Text, FormControl, FormLabel, Input, Stack, Button, Circle } from
import { useForm } from 'react-hook-form';
import { gql } from '@apollo/client';

import { EMAIL_REGEX } from '../constants';
import { useAuth } from '../context/auth';
import { ErrorText } from '../components/ErrorText';
import { Link } from './Link';
import { setErrorsFromGraphQLErrors } from '../utils/setErrors';
import { LoginMutationVariables, useLoginMutation } from '../types';
import { EMAIL_REGEX } from '@/constants';
import { useAuth } from '@/context/auth';
import { ErrorText } from '@/components/ErrorText';
import { Link } from '@/components/Link';
import { setErrorsFromGraphQLErrors } from '@/utils/setErrors';
import { LoginMutationVariables, useLoginMutation } from '@/types';

export const LOGIN_MUTATION = gql`
mutation login($email: String!, $password: String!) {
Expand Down
13 changes: 6 additions & 7 deletions packages/create-bison-app/template/components/SignupForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { gql } from '@apollo/client';
import { useForm } from 'react-hook-form';
import { useRouter } from 'next/router';

import { useAuth } from '../context/auth';
import { setErrorsFromGraphQLErrors } from '../utils/setErrors';
import { SignupMutationVariables, useSignupMutation } from '../types';
import { EMAIL_REGEX } from '../constants';

import { Link } from './Link';
import { ErrorText } from './ErrorText';
import { useAuth } from '@/context/auth';
import { setErrorsFromGraphQLErrors } from '@/utils/setErrors';
import { SignupMutationVariables, useSignupMutation } from '@/types';
import { EMAIL_REGEX } from '@/constants';
import { Link } from '@/components/Link';
import { ErrorText } from '@/components/ErrorText';

export const SIGNUP_MUTATION = gql`
mutation signup($data: SignupInput!) {
Expand Down
8 changes: 4 additions & 4 deletions packages/create-bison-app/template/context/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { createContext, useContext, ReactNode, useState, useEffect } from 'react';
import { gql } from '@apollo/client';

import { cookies } from '../lib/cookies';
import { useMeLazyQuery, User } from '../types';
import { FullPageSpinner } from '../components/FullPageSpinner';
import { LOGIN_TOKEN_KEY } from '../constants';
import { cookies } from '@/lib/cookies';
import { useMeLazyQuery, User } from '@/types';
import { FullPageSpinner } from '@/components/FullPageSpinner';
import { LOGIN_TOKEN_KEY } from '@/constants';

const now = new Date();
const timeValidInMs = 365 * 24 * 60 * 60 * 1000;
Expand Down
8 changes: 4 additions & 4 deletions packages/create-bison-app/template/cypress/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import path from 'path';

import { User } from '@prisma/client';

import { cookies } from '../../lib/cookies';
import { LOGIN_TOKEN_KEY } from '../../constants';
import { resetDB, disconnect, setupDB, graphQLRequest } from '../../tests/helpers';
import * as Factories from '../../tests/factories';
import { cookies } from '@/lib/cookies';
import { LOGIN_TOKEN_KEY } from '@/constants';
import { resetDB, disconnect, setupDB, graphQLRequest } from '@/tests/helpers';
import * as Factories from '@/tests/factories';

declare global {
// eslint-disable-next-line
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import '@testing-library/cypress/add-commands';

import { Prisma, User } from '@prisma/client';

import '@testing-library/cypress/add-commands';
import { LOGIN_TOKEN_KEY } from '../../constants';
import { LoginTaskObject } from '../plugins';
import { LOGIN_TOKEN_KEY } from '@/constants';
import { LoginTaskObject } from '@/cypress/plugins';

declare global {
// eslint-disable-next-line
Expand Down
4 changes: 2 additions & 2 deletions packages/create-bison-app/template/graphql/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { IncomingMessage } from 'http';
import { Context as ApolloContext } from 'apollo-server-core';
import { PrismaClient, User } from '@prisma/client';

import { prisma } from '../lib/prisma';
import { verifyAuthHeader } from '../services/auth';
import { prisma } from '@/lib/prisma';
import { verifyAuthHeader } from '@/services/auth';

/**
* Populates a context object for use in resolvers.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inputObjectType, objectType } from 'nexus';

import { NotFoundError } from '../errors';
import { NotFoundError } from '@/graphql/errors';

// Profile Type
export const Profile = objectType({
Expand Down
11 changes: 6 additions & 5 deletions packages/create-bison-app/template/graphql/modules/user.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
import { Role } from '@prisma/client';
import { UserInputError } from 'apollo-server-micro';

import { prismaArgObject } from '../helpers';
import { hashPassword, appJwtForUser, comparePasswords } from '../../services/auth';
import { canAccess, isAdmin } from '../../services/permissions';
import { hashPassword, appJwtForUser, comparePasswords } from '@/services/auth';
import { canAccess, isAdmin } from '@/services/permissions';
import { prismaArgObject } from '@/graphql/helpers';

// User Type
export const User = objectType({
Expand Down Expand Up @@ -257,8 +257,9 @@ export const UserCreateInput = inputObjectType({
t.field('roles', {
type: list('Role'),
});

t.field('profile', {
type: 'ProfileRelationalCreateInput'
})
type: 'ProfileRelationalCreateInput',
});
Comment on lines +262 to +263
Copy link
Contributor Author

@cullylarson cullylarson Feb 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are changes that will end up in the created app after the initial commit (potentially from something like an eslint --fix). So just made them here so there aren't uncommitted changes just after creating the app.

I.e. you create a bison app, it sets up your repo, builds the project, etc., you cd into the project folder and git status, you'll see some uncommitted changes.

},
});
4 changes: 2 additions & 2 deletions packages/create-bison-app/template/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import path from 'path';

import { declarativeWrappingPlugin, fieldAuthorizePlugin, makeSchema } from 'nexus';

import prettierConfig from '../prettier.config';

import * as types from './modules';

import prettierConfig from '@/prettier.config';

const currentDirectory = process.cwd();

export const schema = makeSchema({
Expand Down
11 changes: 11 additions & 0 deletions packages/create-bison-app/template/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
const { pathsToModuleNameMapper } = require('ts-jest/utils');

const { compilerOptions } = require('./tsconfig.json');

// this creates a module name map based on all the path aliases from tsconfig
// (so you only need to add path aliases in tsconfig, not here).
const moduleNameMapper = compilerOptions.paths
? pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/../' })
: {};

const testPathIgnorePatterns = [
'<rootDir>/node_modules',
'cypress',
Expand All @@ -21,4 +31,5 @@ module.exports = {
},
},
},
moduleNameMapper,
};
8 changes: 4 additions & 4 deletions packages/create-bison-app/template/layouts/LoggedIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'react';
import { useRouter } from 'next/router';
import { Box, Flex, Button } from '@chakra-ui/react';

import { Logo } from '../components/Logo';
import { Nav } from '../components/Nav';
import { useAuth } from '../context/auth';
import { Footer } from '../components/Footer';
import { Logo } from '@/components/Logo';
import { Nav } from '@/components/Nav';
import { useAuth } from '@/context/auth';
import { Footer } from '@/components/Footer';

interface Props {
children: React.ReactNode;
Expand Down
6 changes: 3 additions & 3 deletions packages/create-bison-app/template/layouts/LoggedOut.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import { Box, Flex } from '@chakra-ui/react';

import { ButtonLink } from '../components/Link';
import { Logo } from '../components/Logo';
import { Footer } from '../components/Footer';
import { ButtonLink } from '@/components/Link';
import { Logo } from '@/components/Logo';
import { Footer } from '@/components/Footer';

interface Props {
children: React.ReactNode;
Expand Down
4 changes: 2 additions & 2 deletions packages/create-bison-app/template/lib/apolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
import { setContext } from '@apollo/client/link/context';
import fetch from 'cross-fetch';

import { LOGIN_TOKEN_KEY } from '../constants';

import { cookies } from './cookies';

import { LOGIN_TOKEN_KEY } from '@/constants';

export function createApolloClient(ctx?: Record<string, any>) {
// Apollo needs an absolute URL when in SSR, so determine host
let host: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/create-bison-app/template/package.json.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"test:e2e": "cross-env DOTENV_CONFIG_PATH=.env.test ts-node -r dotenv/config $(yarn bin)/start-server-and-test 'yarn test:server' http://localhost:3001 'yarn cypress:run'",
"test:e2e:local": "cross-env CYPRESS_LOCAL=true CYPRESS_BASE_URL=http://localhost:3000 cypress open",
"test:server": "next start --port 3001",
"ts-node": "ts-node-dev --project tsconfig.cjs.json",
"ts-node": "ts-node-dev --project tsconfig.cjs.json -r tsconfig-paths/register",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what makes ts-node work with the path alias. Without it, none of the scripts run with ts-node would be able to use path aliases.

"withEnv:test": "cross-env DOTENV_CONFIG_PATH=.env.test yarn ts-node -r dotenv/config ./scripts/yarnWithEnv",
"watch:all": "concurrently -n \"NEXUS,GQLCODEGEN,TYPESCRIPT\" -c \"black.bgGreen.dim,black.bgBlue.dim,white.bgMagenta.dim\" \"yarn watch:nexus\" \"yarn watch:codegen\" \"yarn watch:ts\"",
"watch:codegen": "yarn codegen --watch",
Expand Down Expand Up @@ -117,6 +117,7 @@
"supertest": "^4.0.2",
"ts-jest": "^27.0.5",
"ts-node-dev": "^1.1.8",
"tsconfig-paths": "^3.12.0",
"typescript": "^4.4.3"
},
"bison": {
Expand Down
8 changes: 4 additions & 4 deletions packages/create-bison-app/template/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import React from 'react';
import type { AppProps } from 'next/app';
import dynamic from 'next/dynamic';

import { AllProviders } from '../components/AllProviders';
import { useAuth } from '../context/auth';
import { AllProviders } from '@/components/AllProviders';
import { useAuth } from '@/context/auth';

/**
* Dynamically load layouts. This codesplits and prevents code from the logged in layout from being
* included in the bundle if we're rendering the logged out layout.
*/
const LoggedInLayout = dynamic<{ children: React.ReactNode }>(() =>
import('../layouts/LoggedIn').then((mod) => mod.LoggedInLayout)
import('@/layouts/LoggedIn').then((mod) => mod.LoggedInLayout)
);

const LoggedOutLayout = dynamic<{ children: React.ReactNode }>(() =>
import('../layouts/LoggedOut').then((mod) => mod.LoggedOutLayout)
import('@/layouts/LoggedOut').then((mod) => mod.LoggedOutLayout)
);

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/create-bison-app/template/pages/api/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
import { ApolloServer } from 'apollo-server-micro';
import type { NextApiRequest, NextApiResponse } from 'next';

import { createContext } from '../../graphql/context';
import { schema } from '../../graphql/schema';
import { createContext } from '@/graphql/context';
import { schema } from '@/graphql/schema';

export const GRAPHQL_PATH = '/api/graphql';

Expand Down
4 changes: 2 additions & 2 deletions packages/create-bison-app/template/pages/login.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import Head from 'next/head';

import { CenteredBoxForm } from '../components/CenteredBoxForm';
import { LoginForm } from '../components/LoginForm';
import { CenteredBoxForm } from '@/components/CenteredBoxForm';
import { LoginForm } from '@/components/LoginForm';

function LoginPage() {
return (
Expand Down
4 changes: 2 additions & 2 deletions packages/create-bison-app/template/pages/signup.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import Head from 'next/head';

import { CenteredBoxForm } from '../components/CenteredBoxForm';
import { SignupForm } from '../components/SignupForm';
import { CenteredBoxForm } from '@/components/CenteredBoxForm';
import { SignupForm } from '@/components/SignupForm';

function SignupPage() {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { hashPassword } from '../../services/auth';
import { Role } from '../../types';

import { seedUsers } from '../seeds/users';
import { prisma } from '../../lib/prisma';
import { Prisma } from '@prisma/client';

import { hashPassword } from '@/services/auth';
import { Role } from '@/types';
import { seedUsers } from '@/prisma/seeds/users';
import { prisma } from '@/lib/prisma';

// HR: Hey, we've had a few more employees join -- can you create an account for them?!

const INITIAL_PASSWORD = 'test1234';
Expand Down
Loading