Skip to content

Commit cf9a08f

Browse files
committed
chore: wip
1 parent 37321c8 commit cf9a08f

File tree

8 files changed

+149
-126
lines changed

8 files changed

+149
-126
lines changed

storage/framework/core/Caddyfile renamed to storage/framework/Caddyfile

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@
22
// TODO: need to create a watcher
33
// TODO: need to create an action that can be run to add a new project
44
// TODO: need to an action that checks whether there are an env port clashes aross projects
5-
stacksjs.localhost {
6-
route {
7-
@libs {
8-
path /libs/*
9-
}
105

11-
handle @libs {
12-
reverse_proxy localhost:3003
13-
}
6+
// stacksjs.localhost {
7+
// route {
8+
// @
149

15-
@api {
16-
path /api/*
17-
}
10+
// @libs {
11+
// path /libs/*
12+
// }
1813

19-
handle @api {
20-
reverse_proxy localhost:3999
21-
}
14+
// handle @libs {
15+
// reverse_proxy localhost:3003
16+
// }
2217

18+
// @api {
19+
// path /api/*
20+
// }
2321

24-
handle {
25-
reverse_proxy localhost:3000
26-
}
27-
}
28-
}
22+
// handle @api {
23+
// reverse_proxy localhost:3999
24+
// }
25+
26+
27+
// handle {
28+
// reverse_proxy localhost:3000
29+
// }
30+
// }
31+
// }
Lines changed: 14 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,23 @@
1-
import fs from 'node:fs/promises'
2-
import path from 'node:path'
31
import os from 'node:os'
4-
import type { Dirent } from 'node:fs'
2+
import { findStacksProjects } from '@stacksjs/utils'
53
import { log } from '@stacksjs/logging'
6-
import { italic } from '@stacksjs/cli'
74

85
const startDirectory = os.homedir()
9-
const targetFileName = 'buddy' // The exact name of the target file
106

11-
// Assuming excludePatterns is defined somewhere in your script
12-
const excludePatterns = [
13-
'node_modules',
14-
'dist',
15-
/.*out.*/,
16-
`${os.homedir()}/Documents`,
17-
`${os.homedir()}/Pictures`,
18-
`${os.homedir()}/Library`,
19-
`${os.homedir()}/.Trash`,
20-
]
7+
try {
8+
// Usage example, assuming log is defined and works similarly to console
9+
const projects = await findStacksProjects(startDirectory)
2110

22-
async function findStacksProjects(dir: string): Promise<string[]> {
23-
log.info(`This may take a few moments, searching for Stacks projects in: ${italic(dir)}`)
24-
// eslint-disable-next-line no-console
25-
console.log(italic(' Please note, while Stacks is searching for projects on your machine, you may'))
26-
// eslint-disable-next-line no-console
27-
console.log(italic(' be asked for your permissions to search through certain directories.'))
11+
if (projects.length > 0) {
12+
log.info('Found Projects:')
13+
// eslint-disable-next-line no-console
14+
projects.forEach(project => console.log(` - ${project}`))
15+
}
16+
else {
2817
// eslint-disable-next-line no-console
29-
console.log('')
30-
log.debug(`Excluding directories: ${excludePatterns.join(', ')}`)
31-
32-
const foundProjects: string[] = []
33-
34-
async function searchDirectory(directory: string) {
35-
if (path.basename(directory).startsWith('.'))
36-
return
37-
38-
const isExcluded = excludePatterns.some(pattern => typeof pattern === 'string' ? directory.includes(pattern) : pattern.test(directory))
39-
if (isExcluded)
40-
return
41-
42-
let items: Dirent[]
43-
try {
44-
items = await fs.readdir(directory, { withFileTypes: true })
45-
}
46-
catch (error) {
47-
console.error(`Error reading directory ${directory}:`, error)
48-
return
49-
}
50-
51-
let buddyFileFound = false
52-
let storageDirFound = false
53-
54-
for (const item of items) {
55-
if (item.isFile() && item.name === targetFileName) {
56-
buddyFileFound = true
57-
}
58-
else if (item.isDirectory()) {
59-
// Recursively search in directories
60-
const fullPath = path.join(directory, item.name)
61-
if (item.name === 'storage') {
62-
// Check if the 'storage/framework/core/buddy/' structure exists within this directory
63-
try {
64-
const storagePath = path.join(fullPath, 'framework/core/buddy')
65-
await fs.access(storagePath)
66-
storageDirFound = true
67-
}
68-
catch (error) {
69-
// The specific directory structure does not exist
70-
}
71-
}
72-
await searchDirectory(fullPath)
73-
}
74-
}
75-
76-
if (buddyFileFound && storageDirFound)
77-
foundProjects.push(directory) // Both conditions are met
18+
console.log('No Stacks Projects found.')
7819
}
79-
80-
await searchDirectory(dir)
81-
return foundProjects
8220
}
83-
84-
// Usage example, assuming log is defined and works similarly to console
85-
findStacksProjects(startDirectory)
86-
.then((projects) => {
87-
if (projects.length > 0) {
88-
log.info('Found Projects:')
89-
// eslint-disable-next-line no-console
90-
projects.forEach(project => console.log(` - ${project}`))
91-
}
92-
else {
93-
// eslint-disable-next-line no-console
94-
console.log('No Stacks Projects found.')
95-
}
96-
})
97-
.catch((error) => {
98-
console.error('Error searching for Stacks projects:', error)
99-
})
21+
catch (error: any) {
22+
console.error('Error searching for Stacks projects:', error)
23+
}

storage/framework/core/actions/src/generate/caddyfile.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,37 @@ import { readdir } from 'node:fs/promises'
2929
import { path as p } from '@stacksjs/path'
3030
import { logger } from '@stacksjs/logging'
3131
import { storage } from '@stacksjs/storage'
32+
import { findProjects } from '@stacksjs/buddy'
3233

3334
logger.log('Generating Local Reverse Proxy...')
3435

35-
const files = await readdir(p.appPath('Actions'), { recursive: true })
36+
const projects = await findProjects()
3637

37-
const validActions = `// This file is auto-generated by Stacks. Do not edit this file manually.
38-
// If you wish to rename an action, please do so by editing the file name.
39-
//
40-
// For more information, please visit: https://stacksjs.org/docs
38+
const caddyfile = `stacksjs.localhost {
39+
route {
40+
@
4141
42-
export type ActionPath = ${
43-
files
44-
.filter(file => file.endsWith('.ts'))
45-
.map(value => `'Actions/${value.replace('.ts', '')}'`)
46-
.join(' | ')
47-
}
48-
`
42+
@libs {
43+
path /libs/*
44+
}
4945
50-
await storage.writeFile(p.frameworkPath('types/actions.d.ts'), validActions)
46+
handle @libs {
47+
reverse_proxy localhost:3003
48+
}
49+
50+
@api {
51+
path /api/*
52+
}
53+
54+
handle @api {
55+
reverse_proxy localhost:3999
56+
}
57+
58+
59+
handle {
60+
reverse_proxy localhost:3000
61+
}
62+
}
63+
}`
64+
65+
await storage.writeFile(p.frameworkPath('types/actions.d.ts'), caddyfile)

storage/framework/core/stacks

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

storage/framework/core/stx

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import os from 'node:os'
2+
import fs from 'node:fs/promises'
3+
import path from 'node:path'
4+
import type { Dirent } from 'node:fs'
5+
import { italic } from '@stacksjs/cli'
6+
import { log } from '@stacksjs/logging'
7+
8+
const targetFileName = 'buddy' // The exact name of the target file
9+
10+
// Assuming excludePatterns is defined somewhere in your script
11+
const excludePatterns = [
12+
'node_modules',
13+
'dist',
14+
/.*out.*/,
15+
`${os.homedir()}/Documents`,
16+
`${os.homedir()}/Pictures`,
17+
`${os.homedir()}/Library`,
18+
`${os.homedir()}/.Trash`,
19+
]
20+
21+
export async function findStacksProjects(dir: string): Promise<string[]> {
22+
log.info(`This may take a few moments, searching for Stacks projects in: ${italic(dir)}`)
23+
// eslint-disable-next-line no-console
24+
console.log('')
25+
// eslint-disable-next-line no-console
26+
console.log(italic(' Please note, while Stacks is searching for projects on your machine, you'))
27+
// eslint-disable-next-line no-console
28+
console.log(italic(' may be asked for your permissions to search through certain directories.'))
29+
// eslint-disable-next-line no-console
30+
console.log('')
31+
log.debug(`Excluding directories: ${excludePatterns.join(', ')}`)
32+
33+
const foundProjects: string[] = []
34+
35+
async function searchDirectory(directory: string) {
36+
if (path.basename(directory).startsWith('.'))
37+
return
38+
39+
const isExcluded = excludePatterns.some(pattern => typeof pattern === 'string' ? directory.includes(pattern) : pattern.test(directory))
40+
if (isExcluded)
41+
return
42+
43+
let items: Dirent[]
44+
try {
45+
items = await fs.readdir(directory, { withFileTypes: true })
46+
}
47+
catch (error) {
48+
console.error(`Error reading directory ${directory}:`, error)
49+
return
50+
}
51+
52+
let buddyFileFound = false
53+
let storageDirFound = false
54+
55+
for (const item of items) {
56+
if (item.isFile() && item.name === targetFileName) {
57+
buddyFileFound = true
58+
}
59+
else if (item.isDirectory()) {
60+
// Recursively search in directories
61+
const fullPath = path.join(directory, item.name)
62+
if (item.name === 'storage') {
63+
// Check if the 'storage/framework/core/buddy/' structure exists within this directory
64+
try {
65+
const storagePath = path.join(fullPath, 'framework/core/buddy')
66+
await fs.access(storagePath)
67+
storageDirFound = true
68+
}
69+
catch (error) {
70+
// The specific directory structure does not exist
71+
}
72+
}
73+
await searchDirectory(fullPath)
74+
}
75+
}
76+
77+
if (buddyFileFound && storageDirFound)
78+
foundProjects.push(directory) // Both conditions are met
79+
}
80+
81+
await searchDirectory(dir)
82+
return foundProjects
83+
}

storage/framework/core/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from './debounce'
66
export * from './delete'
77
export * from './equal'
88
export * from './export-size'
9+
export * from './find'
910
export * from './function'
1011
export * from './git'
1112
export * from './guards'

storage/framework/ide/dictionary.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bumpp
3131
bundleconfig
3232
bunx
3333
Bußmann
34+
caddyfile
3435
caddyserver
3536
cardano
3637
cback

0 commit comments

Comments
 (0)