Skip to content

Commit 78f0442

Browse files
committed
chore: removing cwd
1 parent 02161b4 commit 78f0442

27 files changed

+284
-471
lines changed

packages/cta-cli/src/cli.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolve } from 'node:path'
12
import { Command, InvalidArgumentError } from 'commander'
23
import { intro, log } from '@clack/prompts'
34
import chalk from 'chalk'
@@ -250,9 +251,14 @@ export function cli({
250251
})
251252
}
252253

253-
await createApp(environment, finalOptions!, {
254-
cwd: options.targetDir || undefined,
255-
})
254+
if (!finalOptions) {
255+
throw new Error('No options were provided')
256+
}
257+
258+
finalOptions.targetDir =
259+
options.targetDir || resolve(process.cwd(), finalOptions!.projectName)
260+
261+
await createApp(environment, finalOptions!)
256262
} catch (error) {
257263
log.error(
258264
error instanceof Error ? error.message : 'An unknown error occurred',

packages/cta-cli/src/options.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { resolve } from 'node:path'
12
import {
23
cancel,
34
confirm,
@@ -110,6 +111,7 @@ export async function normalizeOptions(
110111
return {
111112
// TODO: This is a bit to fix the default framework
112113
projectName: cliOptions.projectName,
114+
targetDir: resolve(process.cwd(), cliOptions.projectName),
113115
framework: getFrameworkById(cliOptions.framework || 'react-cra')!,
114116
mode,
115117
typescript,
@@ -229,7 +231,7 @@ export async function promptForOptions(
229231
cancel('Operation cancelled.')
230232
process.exit(0)
231233
}
232-
options.mode = routerType as typeof CODE_ROUTER | typeof FILE_ROUTER
234+
options.mode = routerType as Mode
233235
} else if (forcedMode) {
234236
options.mode = forcedMode === 'file-router' ? FILE_ROUTER : CODE_ROUTER
235237
options.typescript = options.mode === FILE_ROUTER

packages/cta-custom-add-on/src/custom-add-on.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ export async function createAppOptionsFromPersisted(
123123

124124
async function runCreateApp(options: Required<Options>) {
125125
const { environment, output } = createMemoryEnvironment()
126-
await createApp(environment, options, {
127-
cwd: process.cwd(),
126+
await createApp(environment, {
127+
...options,
128+
targetDir: process.cwd(),
128129
})
129130
return output
130131
}

packages/cta-engine/src/add-to-app.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ async function createOptions(
4646

4747
async function runCreateApp(options: Required<Options>) {
4848
const { environment, output } = createMemoryEnvironment()
49-
await createApp(environment, options, {
50-
cwd: process.cwd(),
49+
await createApp(environment, {
50+
...options,
51+
targetDir: process.cwd(),
5152
})
5253
return output
5354
}

packages/cta-engine/src/create-app.ts

+24-41
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,19 @@ import { setupGit } from './integrations/git.js'
1414

1515
import type { Environment, FileBundleHandler, Options } from './types.js'
1616

17-
async function writeFiles(
18-
environment: Environment,
19-
targetDir: string,
20-
options: Options,
21-
) {
22-
const templateFileFromContent = createTemplateFile(
23-
environment,
24-
options,
25-
targetDir,
26-
)
17+
async function writeFiles(environment: Environment, options: Options) {
18+
const templateFileFromContent = createTemplateFile(environment, options)
2719

2820
async function writeFileBundle(bundle: FileBundleHandler) {
2921
const files = await bundle.getFiles()
3022
for (const file of files) {
3123
const contents = await bundle.getFileContents(file)
3224
const binaryFile = getBinaryFile(contents)
3325
if (binaryFile) {
34-
await environment.writeFile(resolve(targetDir, file), binaryFile)
26+
await environment.writeFile(
27+
resolve(options.targetDir, file),
28+
binaryFile,
29+
)
3530
} else {
3631
await templateFileFromContent(file, contents)
3732
}
@@ -55,32 +50,31 @@ async function writeFiles(
5550
}
5651

5752
await environment.writeFile(
58-
resolve(targetDir, './package.json'),
53+
resolve(options.targetDir, './package.json'),
5954
JSON.stringify(createPackageJSON(options), null, 2),
6055
)
6156

62-
await writeConfigFile(environment, targetDir, options)
57+
await writeConfigFile(environment, options.targetDir, options)
6358
}
6459

6560
async function runCommandsAndInstallDependencies(
6661
environment: Environment,
67-
targetDir: string,
6862
options: Options,
6963
) {
7064
const s = environment.spinner()
7165

7266
// Setup git
7367
if (options.git) {
7468
s.start(`Initializing git repository...`)
75-
await setupGit(environment, targetDir)
69+
await setupGit(environment, options.targetDir)
7670
s.stop(`Initialized git repository`)
7771
}
7872

7973
// Install dependencies
8074
s.start(`Installing dependencies via ${options.packageManager}...`)
8175
await packageManagerInstall(
8276
environment,
83-
resolve(targetDir),
77+
options.targetDir,
8478
options.packageManager,
8579
)
8680
s.stop(`Installed dependencies`)
@@ -94,7 +88,7 @@ async function runCommandsAndInstallDependencies(
9488
await environment.execute(
9589
addOn.command!.command,
9690
addOn.command!.args || [],
97-
resolve(targetDir),
91+
options.targetDir,
9892
)
9993
s.stop(`${addOn.name} setup complete`)
10094
}
@@ -110,15 +104,15 @@ async function runCommandsAndInstallDependencies(
110104
await environment.execute(
111105
options.starter.command.command,
112106
options.starter.command.args || [],
113-
resolve(targetDir),
107+
options.targetDir,
114108
)
115109
s.stop(`Starter ${options.starter.name} setup complete`)
116110
}
117111

118-
await installShadcnComponents(environment, targetDir, options)
112+
await installShadcnComponents(environment, options.targetDir, options)
119113
}
120114

121-
function report(environment: Environment, options: Options, targetDir: string) {
115+
function report(environment: Environment, options: Options) {
122116
const warnings: Array<string> = []
123117
for (const addOn of options.chosenAddOns) {
124118
if (addOn.warning) {
@@ -140,35 +134,24 @@ Errors were encountered during this process:
140134
${environment.getErrors().join('\n')}`
141135
}
142136

143-
environment.outro(`Your ${environment.appName} app is ready in '${basename(targetDir)}'.
137+
environment.outro(
138+
`Your ${environment.appName} app is ready in '${basename(options.targetDir)}'.
144139
145140
Use the following commands to start your app:
146141
% cd ${options.projectName}
147142
% ${formatCommand(
148-
getPackageManagerScriptCommand(options.packageManager, ['dev']),
149-
)}
143+
getPackageManagerScriptCommand(options.packageManager, ['dev']),
144+
)}
150145
151-
Please check the README.md for more information on testing, styling, adding routes, etc.${errorStatement}`)
146+
Please check the README.md for more information on testing, styling, adding routes, etc.${errorStatement}`,
147+
)
152148
}
153149

154-
export async function createApp(
155-
environment: Environment,
156-
options: Options,
157-
{
158-
cwd,
159-
}: {
160-
cwd?: string
161-
} = {},
162-
) {
150+
export async function createApp(environment: Environment, options: Options) {
163151
environment.startRun()
164-
165-
const targetDir: string = cwd || resolve(process.cwd(), options.projectName)
166-
167-
await writeFiles(environment, targetDir, options)
168-
169-
await runCommandsAndInstallDependencies(environment, targetDir, options)
170-
152+
await writeFiles(environment, options)
153+
await runCommandsAndInstallDependencies(environment, options)
171154
environment.finishRun()
172155

173-
report(environment, options, targetDir)
156+
report(environment, options)
174157
}

packages/cta-engine/src/template-file.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ function convertDotFilesAndPaths(path: string) {
1919
.join('/')
2020
}
2121

22-
export function createTemplateFile(
23-
environment: Environment,
24-
options: Options,
25-
targetDir: string,
26-
) {
22+
export function createTemplateFile(environment: Environment, options: Options) {
2723
function getPackageManagerAddScript(
2824
packageName: string,
2925
isDev: boolean = false,
@@ -157,9 +153,9 @@ export function createTemplateFile(
157153
}
158154

159155
if (append) {
160-
await environment.appendFile(resolve(targetDir, target), content)
156+
await environment.appendFile(resolve(options.targetDir, target), content)
161157
} else {
162-
await environment.writeFile(resolve(targetDir, target), content)
158+
await environment.writeFile(resolve(options.targetDir, target), content)
163159
}
164160
}
165161
}

packages/cta-engine/src/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export type Framework = FrameworkDefinition &
8686

8787
export interface Options {
8888
projectName: string
89+
targetDir: string
8990

9091
framework: Framework
9192
mode: Mode

packages/cta-engine/tests/create-app.test.ts

+37-49
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { AddOn, Options } from '../src/types.js'
1010

1111
const simpleOptions = {
1212
projectName: 'test',
13+
targetDir: '/',
1314
framework: {
1415
id: 'test',
1516
name: 'Test',
@@ -49,17 +50,16 @@ const simpleOptions = {
4950
describe('createApp', () => {
5051
it('should create an app', async () => {
5152
const { environment, output } = createMemoryEnvironment()
52-
await createApp(environment, simpleOptions, {
53-
cwd: '/',
54-
})
53+
await createApp(environment, simpleOptions)
5554

5655
expect(output.files['/src/test.txt']).toEqual('Hello')
5756
})
5857

5958
it('should create an app - not silent', async () => {
6059
const { environment, output } = createMemoryEnvironment()
61-
await createApp(environment, simpleOptions, {
62-
cwd: '/foo/bar/baz',
60+
await createApp(environment, {
61+
...simpleOptions,
62+
targetDir: '/foo/bar/baz',
6363
})
6464

6565
const cwd = process.cwd()
@@ -71,57 +71,45 @@ describe('createApp', () => {
7171

7272
it('should create an app - with a starter', async () => {
7373
const { environment, output } = createMemoryEnvironment()
74-
await createApp(
75-
environment,
76-
{
77-
...simpleOptions,
78-
starter: {
79-
command: {
80-
command: 'echo',
81-
args: ['Hello'],
82-
},
83-
getFiles: () => ['./src/test2.txt'],
84-
getFileContents: () => 'Hello-2',
85-
} as unknown as AddOn,
86-
},
87-
{
88-
cwd: '/',
89-
},
90-
)
74+
await createApp(environment, {
75+
...simpleOptions,
76+
starter: {
77+
command: {
78+
command: 'echo',
79+
args: ['Hello'],
80+
},
81+
getFiles: () => ['./src/test2.txt'],
82+
getFileContents: () => 'Hello-2',
83+
} as unknown as AddOn,
84+
})
9185

9286
expect(output.files['/src/test2.txt']).toEqual('Hello-2')
9387
expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)
9488
})
9589

9690
it('should create an app - with a add-on', async () => {
9791
const { environment, output } = createMemoryEnvironment()
98-
await createApp(
99-
environment,
100-
{
101-
...simpleOptions,
102-
git: true,
103-
chosenAddOns: [
104-
{
105-
type: 'add-on',
106-
phase: 'add-on',
107-
warning: 'This is a warning',
108-
command: {
109-
command: 'echo',
110-
args: ['Hello'],
111-
},
112-
packageAdditions: {
113-
dependencies: {},
114-
devDependencies: {},
115-
},
116-
getFiles: () => ['./src/test2.txt', './public/foo.jpg'],
117-
getFileContents: () => 'base64::aGVsbG8=',
118-
} as unknown as AddOn,
119-
],
120-
},
121-
{
122-
cwd: '/',
123-
},
124-
)
92+
await createApp(environment, {
93+
...simpleOptions,
94+
git: true,
95+
chosenAddOns: [
96+
{
97+
type: 'add-on',
98+
phase: 'add-on',
99+
warning: 'This is a warning',
100+
command: {
101+
command: 'echo',
102+
args: ['Hello'],
103+
},
104+
packageAdditions: {
105+
dependencies: {},
106+
devDependencies: {},
107+
},
108+
getFiles: () => ['./src/test2.txt', './public/foo.jpg'],
109+
getFileContents: () => 'base64::aGVsbG8=',
110+
} as unknown as AddOn,
111+
],
112+
})
125113

126114
expect(output.files['/src/test2.txt']).toEqual('hello')
127115
expect(output.commands.some(({ command }) => command === 'echo')).toBe(true)

packages/cta-engine/tests/template-file.test.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { AddOn, Integration, Options } from '../src/types.js'
88

99
const simpleOptions = {
1010
projectName: 'test',
11+
targetDir: '/test',
1112
framework: {
1213
id: 'test',
1314
name: 'Test',
@@ -35,16 +36,12 @@ describe('createTemplateFile', () => {
3536

3637
it('should template a simple file with ejs', async () => {
3738
const { environment, output } = createMemoryEnvironment()
38-
const templateFile = createTemplateFile(
39-
environment,
40-
{
41-
...simpleOptions,
42-
variableValues: {
43-
a: 'foo',
44-
},
45-
} as unknown as Options,
46-
'/test',
47-
)
39+
const templateFile = createTemplateFile(environment, {
40+
...simpleOptions,
41+
variableValues: {
42+
a: 'foo',
43+
},
44+
} as unknown as Options)
4845
environment.startRun()
4946
await templateFile('./test.ts.ejs', "let a = '<%= variables.a %>'")
5047
environment.finishRun()

0 commit comments

Comments
 (0)