Skip to content

Commit 9d6b072

Browse files
committed
chore: fixup a lot of small issues
1 parent 1f40851 commit 9d6b072

27 files changed

+226
-229
lines changed

packages/cta-cli/src/cli.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ import { normalizeOptions, promptForOptions } from './options.js'
2020

2121
import { createUIEnvironment } from './ui-environment.js'
2222

23-
import type {
24-
Mode,
25-
PackageManager,
26-
TemplateOptions,
27-
ToolChain,
28-
} from '@tanstack/cta-core'
23+
import type { Mode, PackageManager, TemplateOptions } from '@tanstack/cta-core'
2924

3025
import type { CliOptions } from './types.js'
3126

@@ -164,18 +159,18 @@ export function cli({
164159
return value as PackageManager
165160
},
166161
)
167-
.option<ToolChain>(
162+
.option<string>(
168163
`--toolchain <${Array.from(toolchains).join('|')}>`,
169164
`Explicitly tell the CLI to use this toolchain`,
170165
(value) => {
171-
if (!toolchains.has(value as ToolChain)) {
166+
if (!toolchains.has(value)) {
172167
throw new InvalidArgumentError(
173168
`Invalid toolchain: ${value}. The following are allowed: ${Array.from(
174169
toolchains,
175170
).join(', ')}`,
176171
)
177172
}
178-
return value as ToolChain
173+
return value
179174
},
180175
)
181176
.option('--tailwind', 'add Tailwind CSS', false)

packages/cta-core/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
"ejs": "^3.1.10",
3333
"execa": "^9.5.2",
3434
"memfs": "^4.17.0",
35-
"prettier": "^3.5.0",
36-
"vitest": "^3.1.1"
35+
"prettier": "^3.5.0"
3736
},
3837
"devDependencies": {
3938
"@tanstack/config": "^0.16.2",
4039
"@types/ejs": "^3.1.5",
4140
"@types/node": "^22.13.4",
4241
"eslint": "^9.20.0",
43-
"typescript": "^5.6.3"
42+
"typescript": "^5.6.3",
43+
"vitest": "^3.1.1"
4444
}
4545
}

packages/cta-core/src/environment.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
unlink,
77
writeFile,
88
} from 'node:fs/promises'
9-
import { existsSync, readdirSync, statSync } from 'node:fs'
9+
import { existsSync } from 'node:fs'
1010
import { dirname } from 'node:path'
1111
import { execa } from 'execa'
1212
import { memfs } from 'memfs'
@@ -49,14 +49,7 @@ export function createDefaultEnvironment(): Environment {
4949
await unlink(path)
5050
},
5151

52-
readFile: (path: string, encoding?: BufferEncoding) =>
53-
readFile(path, { encoding: encoding || 'utf8' }),
5452
exists: (path: string) => existsSync(path),
55-
readdir: (path) => readdirSync(path),
56-
isDirectory: (path) => {
57-
const stat = statSync(path)
58-
return stat.isDirectory()
59-
},
6053

6154
intro: () => {},
6255
outro: () => {},
@@ -112,12 +105,6 @@ export function createMemoryEnvironment() {
112105
})
113106
return Promise.resolve()
114107
}
115-
environment.readFile = async (path: string, encoding?: BufferEncoding) => {
116-
if (isTemplatePath(path)) {
117-
return (await readFile(path, encoding)).toString()
118-
}
119-
return fs.readFileSync(path, 'utf8').toString()
120-
}
121108
environment.writeFile = async (path: string, contents: string) => {
122109
fs.mkdirSync(dirname(path), { recursive: true })
123110
await fs.writeFileSync(path, contents)
@@ -131,19 +118,6 @@ export function createMemoryEnvironment() {
131118
}
132119
return fs.existsSync(path)
133120
}
134-
environment.readdir = (path: string) => {
135-
if (isTemplatePath(path)) {
136-
return readdirSync(path)
137-
}
138-
return fs.readdirSync(path).map((file) => file.toString())
139-
}
140-
environment.isDirectory = (path) => {
141-
if (isTemplatePath(path)) {
142-
const stat = statSync(path)
143-
return stat.isDirectory()
144-
}
145-
return fs.statSync(path).isDirectory()
146-
}
147121
environment.finishRun = () => {
148122
output.files = vol.toJSON() as Record<string, string>
149123
}

packages/cta-core/src/file-helper.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/cta-core/src/file-helpers.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { readFileSync, readdirSync, statSync } from 'node:fs'
2+
import { basename, extname, resolve } from 'node:path'
3+
4+
const BINARY_EXTENSIONS = ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.ico']
5+
6+
export function readFileHelper(path: string): string {
7+
if (BINARY_EXTENSIONS.includes(extname(path))) {
8+
return `base64::${readFileSync(path).toString('base64')}`
9+
} else {
10+
return readFileSync(path, 'utf-8').toString()
11+
}
12+
}
13+
14+
export function getBinaryFile(content: string): string | null {
15+
if (content.startsWith('base64::')) {
16+
const binaryContent = Buffer.from(content.replace('base64::', ''), 'base64')
17+
return binaryContent as unknown as string
18+
}
19+
return null
20+
}
21+
22+
export function relativePath(from: string, to: string) {
23+
const cleanedFrom = from.startsWith('./') ? from.slice(2) : from
24+
const cleanedTo = to.startsWith('./') ? to.slice(2) : to
25+
26+
const fromSegments = cleanedFrom.split('/')
27+
const toSegments = cleanedTo.split('/')
28+
29+
fromSegments.pop()
30+
toSegments.pop()
31+
32+
let commonIndex = 0
33+
while (
34+
commonIndex < fromSegments.length &&
35+
commonIndex < toSegments.length &&
36+
fromSegments[commonIndex] === toSegments[commonIndex]
37+
) {
38+
commonIndex++
39+
}
40+
41+
const upLevels = fromSegments.length - commonIndex
42+
const downLevels = toSegments.slice(commonIndex)
43+
44+
if (upLevels === 0 && downLevels.length === 0) {
45+
return `./${basename(to)}`
46+
} else if (upLevels === 0 && downLevels.length > 0) {
47+
return `./${downLevels.join('/')}/${basename(to)}`
48+
} else {
49+
const relativePath = [...Array(upLevels).fill('..'), ...downLevels].join(
50+
'/',
51+
)
52+
return `${relativePath}/${basename(to)}`
53+
}
54+
}
55+
56+
export function isDirectory(path: string): boolean {
57+
return statSync(path).isDirectory()
58+
}
59+
60+
export function findFilesRecursively(
61+
path: string,
62+
files: Record<string, string>,
63+
) {
64+
const dirFiles = readdirSync(path)
65+
for (const file of dirFiles) {
66+
const filePath = resolve(path, file)
67+
if (isDirectory(filePath)) {
68+
findFilesRecursively(filePath, files)
69+
} else {
70+
files[filePath] = readFileHelper(filePath)
71+
}
72+
}
73+
}

packages/cta-core/src/frameworks.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { existsSync, readFileSync, readdirSync } from 'node:fs'
22
import { resolve } from 'node:path'
33

4-
import { findFilesRecursively, isDirectory } from './utils.js'
5-
import { readFileHelper } from './file-helper.js'
4+
import {
5+
findFilesRecursively,
6+
isDirectory,
7+
readFileHelper,
8+
} from './file-helpers.js'
69

710
import type { AddOn, Framework, FrameworkDefinition } from './types.js'
811

packages/cta-core/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export {
88
DEFAULT_PACKAGE_MANAGER,
99
SUPPORTED_PACKAGE_MANAGERS,
1010
getPackageManager,
11+
getPackageManagerInstallCommand,
12+
getPackageManagerExecuteCommand,
13+
packageManagerInstall,
1114
packageManagerExecute,
1215
} from './package-manager.js'
1316
export {
@@ -16,9 +19,9 @@ export {
1619
getFrameworkByName,
1720
getFrameworks,
1821
} from './frameworks.js'
19-
export { jsSafeName, relativePath, sortObject } from './utils.js'
22+
export { formatCommand, jsSafeName, sortObject } from './utils.js'
2023
export { writeConfigFile, readConfigFile } from './config-file.js'
21-
export { readFileHelper, getBinaryFile } from './file-helper.js'
24+
export { readFileHelper, getBinaryFile, relativePath } from './file-helpers.js'
2225

2326
export type {
2427
AddOn,
@@ -33,4 +36,3 @@ export type {
3336
} from './types.js'
3437
export type { PersistedOptions } from './config-file.js'
3538
export type { PackageManager } from './package-manager.js'
36-
export type { ToolChain } from './toolchain.js'

packages/cta-core/src/package-manager.ts

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,76 @@ export function getPackageManager(): PackageManager | undefined {
2424
return packageManager
2525
}
2626

27-
export function packageManagerExecute(
28-
environment: Environment,
27+
export function getPackageManagerExecuteCommand(
2928
packagerManager: PackageManager,
3029
pkg: string,
31-
args: Array<string>,
32-
cwd: string,
30+
args: Array<string> = [],
3331
) {
3432
switch (packagerManager) {
3533
case 'yarn':
36-
return environment.execute('yarn', ['dlx', pkg, ...args], cwd)
34+
return { command: 'yarn', args: ['dlx', pkg, ...args] }
3735
case 'pnpm':
38-
return environment.execute('pnpx', [pkg, ...args], cwd)
36+
return { command: 'pnpx', args: [pkg, ...args] }
3937
case 'bun':
40-
return environment.execute('bunx', ['--bun', pkg, ...args], cwd)
38+
return { command: 'bunx', args: ['--bun', pkg, ...args] }
4139
case 'deno':
42-
return environment.execute('deno', ['run', `npm:${pkg}`, ...args], cwd)
40+
return { command: 'deno', args: ['run', `npm:${pkg}`, ...args] }
4341
default:
44-
return environment.execute('npx', [pkg, ...args], cwd)
42+
return { command: 'npx', args: [pkg, ...args] }
4543
}
4644
}
45+
46+
export function getPackageManagerInstallCommand(
47+
packagerManager: PackageManager,
48+
pkg?: string,
49+
isDev: boolean = false,
50+
) {
51+
if (pkg) {
52+
switch (packagerManager) {
53+
case 'yarn':
54+
case 'pnpm':
55+
return {
56+
command: packagerManager,
57+
args: ['add', pkg, isDev ? '--dev' : ''],
58+
}
59+
default:
60+
return {
61+
command: packagerManager,
62+
args: ['install', pkg, isDev ? '-D' : ''],
63+
}
64+
}
65+
} else {
66+
return {
67+
command: packagerManager,
68+
args: ['install'],
69+
}
70+
}
71+
}
72+
73+
export function packageManagerInstall(
74+
environment: Environment,
75+
cwd: string,
76+
packagerManager: PackageManager,
77+
pkg?: string,
78+
) {
79+
const { command, args: commandArgs } = getPackageManagerInstallCommand(
80+
packagerManager,
81+
pkg,
82+
)
83+
return environment.execute(command, commandArgs, cwd)
84+
}
85+
86+
export function packageManagerExecute(
87+
environment: Environment,
88+
cwd: string,
89+
packagerManager: PackageManager,
90+
pkg: string,
91+
args: Array<string>,
92+
) {
93+
const { command, args: commandArgs } = getPackageManagerExecuteCommand(
94+
packagerManager,
95+
pkg,
96+
args,
97+
)
98+
return environment.execute(command, commandArgs, cwd)
99+
}

packages/cta-core/src/types.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ type FileEnvironment = {
108108
writeFile: (path: string, contents: string) => Promise<void>
109109
execute: (command: string, args: Array<string>, cwd: string) => Promise<void>
110110
deleteFile: (path: string) => Promise<void>
111-
112-
readFile: (path: string, encoding?: BufferEncoding) => Promise<string>
113111
exists: (path: string) => boolean
114-
readdir: (path: string) => Array<string>
115-
isDirectory: (path: string) => boolean
116112
}
117113

118114
type UIEnvironment = {

0 commit comments

Comments
 (0)