Skip to content

Commit 52234cb

Browse files
committed
chore: package-json cleanup
1 parent 5532010 commit 52234cb

19 files changed

+236
-303
lines changed

packages/cta-core/src/add-ons.ts

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { readFile } from 'node:fs/promises'
22
import { existsSync, readdirSync } from 'node:fs'
3-
import { basename, dirname, resolve } from 'node:path'
3+
import { resolve } from 'node:path'
44

5-
import { getBinaryFile } from './file-helper.js'
5+
import { readFileHelper } from './file-helper.js'
66
import { findFilesRecursively, isDirectory } from './utils.js'
77

8-
import type { AddOn, Environment, FrameworkDefinition } from './types.js'
8+
import type { AddOn, FrameworkDefinition } from './types.js'
99

1010
export async function getAllAddOns(
1111
framework: FrameworkDefinition,
@@ -50,7 +50,14 @@ export async function getAllAddOns(
5050
}
5151
const files: Record<string, string> = {}
5252
for (const file of Object.keys(absoluteFiles)) {
53-
files[file.replace(assetsDir, '.')] = absoluteFiles[file]
53+
files[file.replace(assetsDir, '.')] = await readFileHelper(file)
54+
}
55+
56+
const getFiles = () => {
57+
return Promise.resolve(Object.keys(files))
58+
}
59+
const getFileContents = (path: string) => {
60+
return Promise.resolve(files[path])
5461
}
5562

5663
addOns.push({
@@ -61,6 +68,8 @@ export async function getAllAddOns(
6168
readme,
6269
files,
6370
deletedFiles: [],
71+
getFiles,
72+
getFileContents,
6473
})
6574
}
6675
}
@@ -113,46 +122,9 @@ export async function loadRemoteAddOn(url: string): Promise<AddOn> {
113122
const response = await fetch(url)
114123
const fileContent = await response.json()
115124
fileContent.id = url
116-
return fileContent
117-
}
118-
119-
export async function copyAddOnFile(
120-
environment: Environment,
121-
content: string,
122-
target: string,
123-
targetPath: string,
124-
templateFile: (content: string, targetFileName: string) => Promise<void>,
125-
) {
126-
let targetFile = basename(target).replace(/^_dot_/, '.')
127-
let isTemplate = false
128-
if (targetFile.endsWith('.ejs')) {
129-
targetFile = targetFile.replace('.ejs', '')
130-
isTemplate = true
131-
}
132-
let isAppend = false
133-
if (targetFile.endsWith('.append')) {
134-
targetFile = targetFile.replace('.append', '')
135-
isAppend = true
136-
}
137-
138-
const finalTargetPath = resolve(dirname(targetPath), targetFile)
139-
140-
const binaryContent = getBinaryFile(content)
141-
if (binaryContent) {
142-
await environment.writeFile(
143-
finalTargetPath,
144-
binaryContent as unknown as string,
145-
)
146-
return
147-
}
148-
149-
if (isTemplate) {
150-
await templateFile(content, finalTargetPath)
151-
} else {
152-
if (isAppend) {
153-
await environment.appendFile(finalTargetPath, content)
154-
} else {
155-
await environment.writeFile(finalTargetPath, content)
156-
}
125+
return {
126+
...fileContent,
127+
getFiles: () => Promise.resolve(Object.keys(fileContent.files)),
128+
getFileContents: (path: string) => Promise.resolve(fileContent.files[path]),
157129
}
158130
}

packages/cta-core/src/frameworks.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { readFileSync } from 'node:fs'
12
import { resolve } from 'node:path'
23

34
import { findFilesRecursively } from './utils.js'
@@ -9,6 +10,15 @@ const frameworks: Array<Framework> = []
910

1011
export function registerFramework(framework: FrameworkDefinition) {
1112
const baseAssetsDirectory = resolve(framework.baseDirectory, 'base')
13+
14+
const basePackageJSON = JSON.parse(
15+
readFileSync(resolve(baseAssetsDirectory, 'package.json'), 'utf8'),
16+
)
17+
const optionalPackages = JSON.parse(
18+
readFileSync(resolve(framework.baseDirectory, 'packages.json'), 'utf8'),
19+
)
20+
console.log(optionalPackages)
21+
1222
const frameworkWithBundler: Framework = {
1323
...framework,
1424
getFiles: () => {
@@ -23,6 +33,8 @@ export function registerFramework(framework: FrameworkDefinition) {
2333
getFileContents: (path: string) => {
2434
return Promise.resolve(readFileHelper(resolve(baseAssetsDirectory, path)))
2535
},
36+
basePackageJSON,
37+
optionalPackages,
2638
}
2739

2840
frameworks.push(frameworkWithBundler)

packages/cta-core/src/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
export {
2-
copyAddOnFile,
3-
finalizeAddOns,
4-
getAllAddOns,
5-
loadRemoteAddOn,
6-
} from './add-ons.js'
1+
export { finalizeAddOns, getAllAddOns, loadRemoteAddOn } from './add-ons.js'
72
export {
83
createMemoryEnvironment,
94
createDefaultEnvironment,

packages/cta-core/src/types.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ export type FrameworkDefinition = {
2222
examplesDirectory: string
2323
}
2424

25-
export type Framework = FrameworkDefinition & FileBundleHandler
25+
export type Framework = FrameworkDefinition &
26+
FileBundleHandler & {
27+
basePackageJSON: Record<string, any>
28+
optionalPackages: Record<string, any>
29+
}
2630

2731
export interface Options {
2832
framework: Framework
@@ -98,7 +102,7 @@ type StringVariable = {
98102

99103
export type Variable = BooleanVariable | NumberVariable | StringVariable
100104

101-
export type AddOn = {
105+
export type AddOnDefinition = {
102106
id: string
103107
name: string
104108
description: string
@@ -136,7 +140,7 @@ export type AddOn = {
136140
deletedFiles?: Array<string>
137141
}
138142

139-
export type Starter = AddOn & {
143+
export type StarterDefinition = AddOnDefinition & {
140144
type: 'starter'
141145
version: string
142146
author: string
@@ -147,3 +151,6 @@ export type Starter = AddOn & {
147151
typescript: boolean
148152
tailwind: boolean
149153
}
154+
155+
export type AddOn = AddOnDefinition & FileBundleHandler
156+
export type Starter = StarterDefinition & FileBundleHandler

0 commit comments

Comments
 (0)