Skip to content

Commit b414d74

Browse files
committed
Merge branch 'develop' into feature/fix-app-props-type
2 parents f2e18dc + 459838b commit b414d74

22 files changed

+212
-329
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const capitalizeFirstLetter = (string: string) => {
2+
return string.charAt(0).toUpperCase() + string.slice(1)
3+
}

src/setup/setup.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { ValidCNSInputs } from "../create-next-stack-types"
2+
import { capitalizeFirstLetter } from "../helpers/capitalize-first-letter"
3+
import { exitWithError } from "../helpers/exit-with-error"
4+
import { commandInstance } from "../instance"
25
import { Step } from "./step"
36
import { addBaseBabelConfigStep } from "./steps/add-base-babel-config"
47
import { addContentStep } from "./steps/add-content/add-content"
@@ -11,8 +14,7 @@ import { gitCommitStep } from "./steps/git-commit"
1114
import { installFormikStep } from "./steps/install-formik"
1215
import { installFramerMotionStep } from "./steps/install-framer-motion"
1316
import { installReactHookFormStep } from "./steps/install-react-hook-form"
14-
import { printGitInitializationWarningStep } from "./steps/print-git-initialization-warning"
15-
import { printSuccessMessageStep } from "./steps/print-success-message"
17+
import { printFinalMessagesStep } from "./steps/print-final-messages"
1618
import { removeOfficialCNAContentStep } from "./steps/remove-official-cna-content"
1719
import { setUpEmotionStep } from "./steps/set-up-emotion"
1820
import { setUpLintStagedStep } from "./steps/set-up-lint-staged"
@@ -23,6 +25,8 @@ import { updateYarnStep } from "./steps/update-yarn"
2325
export const performSetupSteps = async (
2426
inputs: ValidCNSInputs
2527
): Promise<void> => {
28+
const instance = commandInstance.get()
29+
2630
const steps: Step[] = [
2731
updateYarnStep,
2832
createNextAppStep,
@@ -49,13 +53,19 @@ export const performSetupSteps = async (
4953
addGitAttributesStep,
5054
gitCommitStep,
5155

52-
printGitInitializationWarningStep,
53-
printSuccessMessageStep,
56+
printFinalMessagesStep,
5457
]
5558

5659
for (const step of steps) {
5760
if (await step.shouldRun(inputs)) {
58-
await step.run(inputs)
61+
instance.log(`${capitalizeFirstLetter(step.description)}...`)
62+
63+
try {
64+
await step.run(inputs)
65+
} catch (error) {
66+
exitWithError(`An error occurred while ${step.description}.`, error)
67+
}
68+
5969
step.didRun = true
6070
}
6171
}

src/setup/step.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { ValidCNSInputs } from "../create-next-stack-types"
22

33
export type Step = {
4+
/**
5+
* `description` should be written in present continuous tense, without punctuation, and with a lowercase first letter unless the description starts with a name or similar.
6+
*/
7+
description: string
48
shouldRun: (inputs: ValidCNSInputs) => Promise<boolean>
59
run: (inputs: ValidCNSInputs) => Promise<void>
610
didRun: boolean
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
import { exitWithError } from "../../helpers/exit-with-error"
21
import { writeJsonFile } from "../../helpers/write-json-file"
3-
import { commandInstance } from "../../instance"
42
import { Step } from "../step"
53

64
export const addBaseBabelConfigStep: Step = {
5+
description: "adding custom Babel configuration",
6+
77
shouldRun: async () => true,
88

99
didRun: false,
1010

1111
run: async () => {
12-
const instance = commandInstance.get()
13-
instance.log("Adding custom Babel configuration...")
14-
1512
// The base Babel config is ready for custom preset configurations to be added onto the `next/babel` preset as per the Next.js docs: https://nextjs.org/docs/advanced-features/customizing-babel-config
1613
const baseBabelConfig = {
1714
presets: [["next/babel", {}]],
1815
plugins: [],
1916
}
2017

21-
try {
22-
await writeJsonFile(".babelrc", baseBabelConfig)
23-
} catch (error) {
24-
exitWithError(
25-
"An error occurred while adding custom Babel configuration.",
26-
error
27-
)
28-
}
18+
await writeJsonFile(".babelrc", baseBabelConfig)
2919
},
3020
}
Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { promises as fs } from "fs"
2-
import { exitWithError } from "../../../helpers/exit-with-error"
3-
import { commandInstance } from "../../../instance"
42
import { Step } from "../../step"
53
import { generatePage } from "./components/generate-page"
64
import { generateWithDefaultGlobalStyles } from "./components/generate-with-default-global-styles"
@@ -9,45 +7,40 @@ import { globalStyles } from "./global-styles"
97
import { generateIndexPage } from "./index-page/generate-index"
108

119
export const addContentStep: Step = {
10+
description: "adding content",
11+
1212
shouldRun: async () => true,
1313

1414
didRun: false,
1515

1616
run: async (inputs) => {
17-
const instance = commandInstance.get()
18-
instance.log("Adding content...")
19-
20-
try {
21-
await fs.mkdir("components")
17+
await fs.mkdir("components")
2218

23-
const promises = [
24-
fs.writeFile("components/Page.tsx", generatePage(inputs)),
25-
fs.writeFile("pages/index.tsx", generateIndexPage(inputs)),
26-
fs.writeFile("pages/_app.tsx", generateApp(inputs)),
27-
]
19+
const promises = [
20+
fs.writeFile("components/Page.tsx", generatePage(inputs)),
21+
fs.writeFile("pages/index.tsx", generateIndexPage(inputs)),
22+
fs.writeFile("pages/_app.tsx", generateApp(inputs)),
23+
]
2824

29-
if (
30-
inputs.flags.styling === "emotion" ||
31-
inputs.flags.styling === "styled-components"
32-
) {
33-
promises.push(
34-
fs.writeFile(
35-
"components/WithDefaultGlobalStyles.tsx",
36-
generateWithDefaultGlobalStyles(inputs)
37-
)
38-
)
39-
} else if (inputs.flags.styling === "css-modules") {
40-
await fs.mkdir("styles")
41-
promises.push(fs.writeFile("styles/global-styles.css", globalStyles))
42-
} else {
43-
throw new Error(
44-
"Unsupported styling technology found in addContentStep, or no styling technology was chosen."
25+
if (
26+
inputs.flags.styling === "emotion" ||
27+
inputs.flags.styling === "styled-components"
28+
) {
29+
promises.push(
30+
fs.writeFile(
31+
"components/WithDefaultGlobalStyles.tsx",
32+
generateWithDefaultGlobalStyles(inputs)
4533
)
46-
}
47-
48-
await Promise.all(promises)
49-
} catch (error) {
50-
exitWithError("An error occurred while adding content.", error)
34+
)
35+
} else if (inputs.flags.styling === "css-modules") {
36+
await fs.mkdir("styles")
37+
promises.push(fs.writeFile("styles/global-styles.css", globalStyles))
38+
} else {
39+
throw new Error(
40+
"Unsupported styling technology found in addContentStep, or no styling technology was chosen."
41+
)
5142
}
43+
44+
await Promise.all(promises)
5245
},
5346
}

src/setup/steps/add-git-attributes.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import endent from "endent"
22
import { promises as fs } from "fs"
3-
import { exitWithError } from "../../helpers/exit-with-error"
43
import { isGitInitialized } from "../../helpers/is-git-initialized"
54
import { commandInstance } from "../../instance"
65
import { Step } from "../step"
76

87
const filename = ".gitattributes"
98

109
export const addGitAttributesStep: Step = {
10+
description: `adding ${filename}`,
11+
1112
shouldRun: async () => {
1213
if (!(await isGitInitialized())) {
1314
const instance = commandInstance.get()
@@ -22,14 +23,7 @@ export const addGitAttributesStep: Step = {
2223
didRun: false,
2324

2425
run: async () => {
25-
try {
26-
const instance = commandInstance.get()
27-
instance.log(`Adding ${filename}...`)
28-
29-
await fs.writeFile(filename, generateGitAttributes())
30-
} catch (error) {
31-
exitWithError(`An error occurred while adding ${filename}.`, error)
32-
}
26+
await fs.writeFile(filename, generateGitAttributes())
3327
},
3428
}
3529

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
import { promises as fs } from "fs"
2-
import { exitWithError } from "../../../helpers/exit-with-error"
3-
import { commandInstance } from "../../../instance"
42
import { Step } from "../../step"
53
import { generateReadme } from "./generate-readme"
64

75
export const addReadmeStep: Step = {
6+
description: "adding Readme",
7+
88
shouldRun: async () => true,
99

1010
didRun: false,
1111

1212
run: async (inputs) => {
13-
const instance = commandInstance.get()
14-
instance.log("Adding Readme...")
15-
16-
try {
17-
const readmeString = await generateReadme(inputs)
18-
await fs.writeFile("README.md", readmeString)
19-
} catch (error) {
20-
exitWithError("An error occurred while adding Readme.", error)
21-
}
13+
const readmeString = await generateReadme(inputs)
14+
await fs.writeFile("README.md", readmeString)
2215
},
2316
}

src/setup/steps/copy-assets.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { getCreateNextStackDir } from "../../helpers/get-create-next-stack-dir"
44
import { Step } from "../step"
55

66
export const copyAssetsStep: Step = {
7+
description: "copying static assets",
8+
79
shouldRun: async () => true,
810

911
didRun: false,

src/setup/steps/create-next-app.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
import execa from "execa"
22
import { promises as fs } from "fs"
3-
import { exitWithError } from "../../helpers/exit-with-error"
4-
import { commandInstance } from "../../instance"
53
import { getNameVersionCombo, packages } from "../packages"
64
import { Step } from "../step"
75

86
export const createNextAppStep: Step = {
7+
description: "creating Next.js app",
8+
99
shouldRun: async () => true,
1010

1111
didRun: false,
1212

1313
run: async ({ args, flags }) => {
14-
const instance = commandInstance.get()
15-
instance.log("Creating Next.js app...")
16-
17-
try {
18-
// Make sure directory exists to avoid error from create-next-app
19-
await fs.mkdir(args.appName, { recursive: true })
14+
// Make sure directory exists to avoid error from create-next-app
15+
await fs.mkdir(args.appName, { recursive: true })
2016

21-
const createNextAppArgs = [args.appName, "--typescript"]
22-
if (flags["package-manager"] === "npm") {
23-
createNextAppArgs.push("--use-npm")
24-
}
25-
26-
await execa("npx", [
27-
getNameVersionCombo(packages["create-next-app"]),
28-
...createNextAppArgs,
29-
])
30-
} catch (error) {
31-
exitWithError("An error occurred while creating Next.js app.", error)
17+
const createNextAppArgs = [args.appName, "--typescript"]
18+
if (flags["package-manager"] === "npm") {
19+
createNextAppArgs.push("--use-npm")
3220
}
3321

22+
await execa("npx", [
23+
getNameVersionCombo(packages["create-next-app"]),
24+
...createNextAppArgs,
25+
])
26+
3427
process.chdir(args.appName)
3528
},
3629
}

src/setup/steps/format-project.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
import execa from "execa"
2-
import { exitWithError } from "../../helpers/exit-with-error"
3-
import { commandInstance } from "../../instance"
42
import { getNameVersionCombo, packages } from "../packages"
53
import { Step } from "../step"
64

75
export const formatProjectStep: Step = {
6+
description: "formatting project",
7+
88
shouldRun: async () => true,
99

1010
didRun: false,
1111

1212
run: async () => {
13-
const instance = commandInstance.get()
14-
instance.log("Formatting project...")
15-
16-
try {
17-
await execa("npx", [
18-
getNameVersionCombo(packages.prettier),
19-
"--write",
20-
".",
21-
])
22-
} catch (error) {
23-
exitWithError("An error occurred while formatting project.", error)
24-
}
13+
await execa("npx", [getNameVersionCombo(packages.prettier), "--write", "."])
2514
},
2615
}

0 commit comments

Comments
 (0)