Skip to content

Commit 4aab0cd

Browse files
committed
chore: end to end working, lots to be done
1 parent 9181463 commit 4aab0cd

File tree

6 files changed

+70
-34
lines changed

6 files changed

+70
-34
lines changed

src/add-ons.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { readFile } from 'node:fs/promises'
2-
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
2+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
33
import { resolve } from 'node:path'
44
import { fileURLToPath } from 'node:url'
55
import chalk from 'chalk'
@@ -103,6 +103,7 @@ export async function finalizeAddOns(
103103
addOn = loadAddOn(localAddOn)
104104
} else if (addOnID.startsWith('http')) {
105105
addOn = await loadRemoteAddOn(addOnID)
106+
addOns.push(addOn)
106107
} else {
107108
throw new Error(`Add-on ${addOnID} not found`)
108109
}
@@ -116,7 +117,10 @@ export async function finalizeAddOns(
116117
}
117118
}
118119

119-
return [...finalAddOnIDs].map((id) => addOns.find((a) => a.id === id)!)
120+
const finalAddOns = [...finalAddOnIDs].map(
121+
(id) => addOns.find((a) => a.id === id)!,
122+
)
123+
return finalAddOns
120124
}
121125

122126
export async function listAddOns(options: CliOptions) {
@@ -138,5 +142,6 @@ function loadAddOn(addOn: AddOn): AddOn {
138142
async function loadRemoteAddOn(url: string): Promise<AddOn> {
139143
const response = await fetch(url)
140144
const fileContent = await response.json()
141-
return JSON.parse(fileContent)
145+
fileContent.id = url
146+
return fileContent
142147
}

src/create-app.ts

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { basename, dirname, resolve } from 'node:path'
1+
import { basename, resolve } from 'node:path'
22
import { fileURLToPath } from 'node:url'
33
import { log, outro, spinner } from '@clack/prompts'
44
import { render } from 'ejs'
@@ -10,7 +10,7 @@ import { sortObject } from './utils.js'
1010
import { writeConfigFile } from './config-file.js'
1111
import { packageManagerExecute } from './package-manager.js'
1212

13-
import type { Environment, Options } from './types.js'
13+
import type { AddOn, Environment, Options } from './types.js'
1414

1515
function createCopyFiles(environment: Environment, targetDir: string) {
1616
return async function copyFiles(
@@ -342,6 +342,34 @@ export async function createApp(
342342
const isAddOnEnabled = (id: string) =>
343343
options.chosenAddOns.find((a) => a.id === id)
344344

345+
async function runAddOn(addOn: AddOn) {
346+
if (addOn.files) {
347+
for (const file of Object.keys(addOn.files)) {
348+
await copyAddOnFile(
349+
environment,
350+
addOn.files[file],
351+
file,
352+
resolve(targetDir, file),
353+
(content, targetFileName) =>
354+
templateFileFromContent(targetFileName, content),
355+
)
356+
}
357+
}
358+
if (addOn.deletedFiles) {
359+
for (const file of addOn.deletedFiles) {
360+
await environment.deleteFile(resolve(targetDir, file))
361+
}
362+
}
363+
364+
if (addOn.command && addOn.command.command) {
365+
await environment.execute(
366+
addOn.command.command,
367+
addOn.command.args || [],
368+
resolve(targetDir),
369+
)
370+
}
371+
}
372+
345373
// Setup the .vscode directory
346374
switch (options.toolchain) {
347375
case 'biome':
@@ -437,33 +465,15 @@ export async function createApp(
437465

438466
// Copy all the asset files from the addons
439467
const s = silent ? null : spinner()
440-
for (const phase of ['setup', 'add-on', 'example']) {
441-
for (const addOn of options.chosenAddOns.filter(
442-
(addOn) => addOn.phase === phase,
443-
)) {
444-
s?.start(`Setting up ${addOn.name}...`)
445-
if (addOn.files) {
446-
for (const file of Object.keys(addOn.files)) {
447-
await copyAddOnFile(
448-
environment,
449-
addOn.files[file],
450-
file,
451-
resolve(targetDir, file),
452-
(content, targetFileName) =>
453-
templateFileFromContent(targetFileName, content),
454-
)
455-
}
456-
}
457-
458-
if (addOn.command) {
459-
await environment.execute(
460-
addOn.command.command,
461-
addOn.command.args || [],
462-
resolve(targetDir),
463-
)
468+
for (const type of ['add-on', 'example']) {
469+
for (const phase of ['setup', 'add-on']) {
470+
for (const addOn of options.chosenAddOns.filter(
471+
(addOn) => addOn.phase === phase && addOn.type === type,
472+
)) {
473+
s?.start(`Setting up ${addOn.name}...`)
474+
await runAddOn(addOn)
475+
s?.stop(`${addOn.name} setup complete`)
464476
}
465-
466-
s?.stop(`${addOn.name} setup complete`)
467477
}
468478
}
469479

@@ -654,6 +664,15 @@ export async function createApp(
654664
// Create the README.md
655665
await templateFile(templateDirBase, 'README.md.ejs')
656666

667+
// Adding overlays
668+
for (const addOn of options.chosenAddOns.filter(
669+
(addOn) => addOn.type === 'overlay',
670+
)) {
671+
s?.start(`Setting up overlay ${addOn.name}...`)
672+
await runAddOn(addOn)
673+
s?.stop(`Overlay ${addOn.name} setup complete`)
674+
}
675+
657676
// Install dependencies
658677
s?.start(`Installing dependencies via ${options.packageManager}...`)
659678
await environment.execute(

src/custom-add-on.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ This is probably because this was created with an older version of create-tsrout
9191
author: 'John Doe',
9292
license: 'MIT',
9393
link: 'https://github.com/john-doe/custom-add-on',
94-
commands: [],
94+
command: {},
9595
shadcnComponents: [],
9696
templates: [persistedOptions.mode],
9797
routes: [],
9898
warning: '',
9999
variables: {},
100-
type: 'example',
101100
phase: 'add-on',
101+
type: 'overlay',
102102
},
103103
null,
104104
2,

src/environment.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
copyFile,
44
mkdir,
55
readFile,
6+
unlink,
67
writeFile,
78
} from 'node:fs/promises'
89
import { existsSync, readdirSync, statSync } from 'node:fs'
@@ -44,6 +45,9 @@ export function createDefaultEnvironment(): Environment {
4445
)
4546
}
4647
},
48+
deleteFile: async (path: string) => {
49+
await unlink(path)
50+
},
4751

4852
readFile: (path: string, encoding?: BufferEncoding) =>
4953
readFile(path, { encoding: encoding || 'utf8' }),
@@ -107,6 +111,9 @@ export function createMemoryEnvironment() {
107111
fs.mkdirSync(dirname(path), { recursive: true })
108112
await fs.writeFileSync(path, contents)
109113
}
114+
environment.deleteFile = async (path: string) => {
115+
await fs.unlinkSync(path)
116+
}
110117
environment.exists = (path: string) => {
111118
if (isTemplatePath(path)) {
112119
return existsSync(path)

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type Environment = {
4141
copyFile: (from: string, to: string) => Promise<void>
4242
writeFile: (path: string, contents: string) => Promise<void>
4343
execute: (command: string, args: Array<string>, cwd: string) => Promise<void>
44+
deleteFile: (path: string) => Promise<void>
4445

4546
readFile: (path: string, encoding?: BufferEncoding) => Promise<string>
4647
exists: (path: string) => boolean
@@ -73,9 +74,9 @@ export type Variable = BooleanVariable | NumberVariable | StringVariable
7374

7475
export type AddOn = {
7576
id: string
76-
type: 'add-on' | 'example'
7777
name: string
7878
description: string
79+
type: 'add-on' | 'example' | 'overlay'
7980
link: string
8081
templates: Array<string>
8182
routes: Array<{

tests/test-utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ export function createTestEnvironment(projectName: string) {
4343
args,
4444
})
4545
}
46+
environment.deleteFile = async (path: string) => {
47+
const relPath = trimProjectRelativePath(path)
48+
delete output.files[relPath]
49+
}
4650
environment.readFile = async (path: string, encoding?: BufferEncoding) => {
4751
if (isTemplatePath(path)) {
4852
return (await readFile(path, encoding)).toString()

0 commit comments

Comments
 (0)