Skip to content

Commit

Permalink
refactor(): initial changes for use in sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
jwerle committed Apr 26, 2024
1 parent 55cbb93 commit 470170e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
4 changes: 3 additions & 1 deletion socket.ini
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ src/css = css
node_modules/@socketsupply/components = node_modules/@socketsupply/components
node_modules/@socketsupply/indexed = node_modules/@socketsupply/indexed
node_modules/@socketsupply/tonic = node_modules/@socketsupply/tonic
node_modules/@socketsupply/socket = node_modules/@socketsupply/socket
node_modules/@socketsupply/socket-darwin-arm64 = node_modules/@socketsupply/socket-darwin-arm64


[build.script]
Expand All @@ -97,7 +99,7 @@ forward_arguments = false
sources[] = "src"

[webview]
importmap = "/importmap.json"
importmap = "importmap.json"

; Make root open index.html
; default value: "/"
Expand Down
2 changes: 1 addition & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http-equiv="Content-Security-Policy"
content="
connect-src socket: npm: https: http: blob: ipc: wss: ws: ws://localhost:*;
script-src socket: npm: https: http: blob: http://localhost:* 'unsafe-eval' 'unsafe-inline';
script-src socket: npm: node: https: http: blob: http://localhost:* 'unsafe-eval' 'unsafe-inline';
worker-src socket: https: http: blob: 'unsafe-eval' 'unsafe-inline';
frame-src socket: https: http: blob: http://localhost:*;
img-src socket: https: http: blob: data: http://localhost:*;
Expand Down
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { network, Encryption } from 'socket:network'
import vm from 'socket:vm'
import { inspect, format } from 'socket:util'
import { spawn, exec } from 'socket:child_process'
import os from 'socket:os'

import Tonic from '@socketsupply/tonic'
import components from '@socketsupply/components'
Expand Down Expand Up @@ -338,9 +339,18 @@ class AppView extends Tonic {
await this.db.projects.put(bundleId, project)
await fs.promises.mkdir(project.path, { recursive: true })

const runtime = await import(`npm:@socketsupply/socket-${os.platform()}-${os.arch()}`)
const ssc = path.join(process.cwd(), runtime.bin.ssc.replace(globalThis.location.origin, ''))
console.log({ ssc })
try {
await fs.promises.access('/Applications/Xcode.app/Contents/Developer/usr/bin/git', fs.constants.X_OK)
} catch (err) {
console.error(err)
}
try {
await exec('ssc init', { cwd: project.path })
await exec('git init', { cwd: project.path })
await exec(`${ssc} init`, { cwd: project.path })

console.log(await exec('/Applications/Xcode.app/Contents/Developer/usr/bin/git init', { cwd: project.path }))
} catch (err) {
console.error(err)
}
Expand Down
20 changes: 11 additions & 9 deletions src/lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import fs from 'socket:fs'
import path from 'socket:path'

export async function rm (directory) {
const files = await fs.promises.readdir(directory, { withFileTypes: true })
try {
const files = await fs.promises.readdir(directory, { withFileTypes: true })

for (const file of files) {
const filePath = path.join(directory, file.name)
for (const file of files) {
const filePath = path.join(directory, file.name)

if (file.isDirectory()) {
await rm(filePath)
} else {
await fs.promises.unlink(filePath)
if (file.isDirectory()) {
await rm(filePath)
} else {
await fs.promises.unlink(filePath)
}
}
}

await fs.promises.rmdir(directory)
await fs.promises.rmdir(directory)
} catch {}
}

export async function cp (srcDir, destDir) {
Expand Down
14 changes: 5 additions & 9 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import path from 'socket:path'
import { lookup } from 'socket:mime'
import application from 'socket:application'

const mount = '/user/home'
const navigatorPath = path.DATA.replace(path.HOME, mount)
import fs from 'socket:fs/promises'

export default async function (req, env, ctx) {
const url = new URL(req.url)
Expand All @@ -12,7 +10,7 @@ export default async function (req, env, ctx) {

if (!route) return

const p = path.join(navigatorPath, route.pathname.groups[0])
const p = path.join(path.DATA, route.pathname.groups[0])
const params = url.searchParams

const types = await lookup(path.extname(url.pathname).slice(1))
Expand All @@ -27,11 +25,9 @@ export default async function (req, env, ctx) {
let data = ''

try {
const res = await fetch(p)

if (res.ok && res.status === 200) {
data = await res.text()
} else if (!res.ok || res.status === 404) {
if (await fs.access(p, fs.constants.R_OK)) {
data = await fs.readFile(p, 'utf8')
} else {
data = '<h1>Not Found</h1>'
headers['Runtime-Preload-Injection'] = 'disabled'
}
Expand Down

0 comments on commit 470170e

Please sign in to comment.