Skip to content

Commit

Permalink
Merge pull request #48 from syfxlin/develop
Browse files Browse the repository at this point in the history
refactor(depker): Adjust handling
  • Loading branch information
syfxlin authored Jan 13, 2024
2 parents b05f089 + cad5623 commit bc6ec81
Show file tree
Hide file tree
Showing 51 changed files with 3,918 additions and 954 deletions.
18 changes: 0 additions & 18 deletions .eslintrc

This file was deleted.

8 changes: 8 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ jobs:
with:
registry-url: "https://registry.npmjs.org"
cache: "pnpm"
- name: Build
run: |
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o bin/depker.win.amd64.exe bin/depker.go
CGO_ENABLED=0 GOOS=windows GOARCH=arm64 go build -o bin/depker.win.arm64.exe bin/depker.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/depker.win.amd64 bin/depker.go
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -o bin/depker.win.arm64 bin/depker.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o bin/depker.mac.amd64 bin/depker.go
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o bin/depker.mac.arm64 bin/depker.go
- name: Publish
run: pnpm --package=@semantic-release/git --package=semantic-release-replace-plugin --package=semantic-release dlx semantic-release --branches master
env:
Expand Down
3 changes: 0 additions & 3 deletions .prettierrc

This file was deleted.

14 changes: 13 additions & 1 deletion .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@
"assets": ["src/depker.ts"]
}
],
"@semantic-release/github"
[
"@semantic-release/github",
{
"assets": [
{ "label": "depker.win.amd64.exe", "path": "bin/depker.win.amd64.exe" },
{ "label": "depker.win.arm64.exe", "path": "bin/depker.win.arm64.exe" },
{ "label": "depker.linux.amd64", "path": "bin/depker.linux.amd64" },
{ "label": "depker.linux.arm64", "path": "bin/depker.linux.arm64" },
{ "label": "depker.mac.amd64", "path": "bin/depker.mac.amd64" },
{ "label": "depker.mac.arm64", "path": "bin/depker.mac.arm64" }
]
}
]
]
}
173 changes: 173 additions & 0 deletions bin/depker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package main

import (
"errors"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)

func main() {
deno := deno()
path := lookup()

if len(os.Args) > 1 && os.Args[1] == "create" {
create(deno, path, os.Args[2:]...)
return
}
if len(os.Args) > 1 && os.Args[1] == "reload" {
reload(deno, path, os.Args[2:]...)
return
}

depker(deno, path, os.Args[1:]...)
}

func deno() string {
deno, err := exec.LookPath("deno")
if err == nil {
return deno
}
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
if runtime.GOOS == "windows" {
deno = filepath.Join(home, ".deno", "bin", "deno.exe")
look, err := exec.LookPath(deno)
if err == nil {
return look
}
execute("powershell.exe", "-Command", "irm https://deno.land/install.ps1 | iex")
return deno
} else {
deno = filepath.Join(home, ".deno", "bin", "deno")
look, err := exec.LookPath(deno)
if err == nil {
return look
}
execute("sh", "-c", "curl -fsSL https://deno.land/install.sh | sh")
return deno
}
}

func lookup() string {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
paths := []string{
filepath.Join(dir, "depker.config.ts"),
filepath.Join(dir, "depker.config.js"),
filepath.Join(dir, ".depker/depker.config.ts"),
filepath.Join(dir, ".depker/depker.config.js"),
filepath.Join(dir, ".depker/depker.ts"),
filepath.Join(dir, ".depker/depker.js"),
filepath.Join(dir, ".depker/config.ts"),
filepath.Join(dir, ".depker/config.js"),
}
for _, path := range paths {
if _, err := os.Stat(path); !errors.Is(err, os.ErrNotExist) {
// Ref: https://github.com/sayjun0505/Golangvuln/blob/0c3dd31c3533348628b74edaa5230501afb69e29/internal/web/url.go
if !filepath.IsAbs(path) {
panic(errors.New("path is not absolute"))
}
u := url.URL{Scheme: "file"}
if vol := filepath.VolumeName(path); vol != "" {
if strings.HasPrefix(vol, `\\`) {
path = filepath.ToSlash(path[2:])
i := strings.IndexByte(path, '/')

if i < 0 {
u.Host = path
u.Path = "/"
} else {
u.Host = path[:i]
u.Path = filepath.ToSlash(path[i:])
}
} else {
u.Path = "/" + filepath.ToSlash(path)
}
} else {
u.Path = filepath.ToSlash(path)
}
return u.String()
}
}
return "https://raw.githubusercontent.com/syfxlin/depker/master/mod.ts"
}

func create(deno string, path string, args ...string) {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
file, err1 := os.OpenFile(filepath.Join(dir, "depker.config.ts"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
if err1 != nil {
panic(err1)
}
_, err2 := file.WriteString(strings.Join(
[]string{
"import { depker } from \"https://raw.githubusercontent.com/syfxlin/depker/master/mod.ts\";",
"",
"const app = depker();",
"",
"export default app;",
"",
},
"\n",
))
if err2 != nil {
panic(err2)
}
err3 := file.Close()
if err3 != nil {
panic(err3)
}
}

func reload(deno string, path string, args ...string) {
execute(deno, append([]string{"cache", "-r", path}, args...)...)
}

func depker(deno string, path string, args ...string) {
file, err1 := os.CreateTemp("", "depker-cli-")
if err1 != nil {
panic(err1)
}
_, err2 := file.WriteString(strings.Join(
[]string{
"const depker = await import('" + path + "').then(mod => mod?.default ?? mod);",
"if (typeof depker.execute === 'function') {",
" await depker.execute();",
"} else {",
" throw new ReferenceError('Missing depker instance! Ensure your config file does export the Site instance as default.');",
"}",
},
"\n",
))
if err2 != nil {
panic(err2)
}
err3 := file.Close()
if err3 != nil {
panic(err3)
}
execute(deno, append([]string{"run", "-A", file.Name()}, args...)...)
}

func execute(name string, args ...string) {
cmd := exec.Command(name, args...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if code, ok := err.(*exec.ExitError); ok {
os.Exit(code.ExitCode())
}
panic(err)
}
}
39 changes: 0 additions & 39 deletions depker.ts

This file was deleted.

17 changes: 17 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import config from "@syfxlin/eslint-config";

export default config({
stylistic: {
quotes: "double",
indent: 2,
semi: true,
},
rules: {
"curly": ["error", "multi-line", "consistent"],
"no-console": ["off"],
"style/brace-style": ["error", "1tbs"],
"style/member-delimiter-style": ["error", { multiline: { delimiter: "semi" }, singleline: { delimiter: "semi" } }],
"ts/ban-ts-comment": ["off"],
"ts/consistent-type-imports": ["off"],
},
});
9 changes: 7 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { depker } from "./src/depker.ts";

// dependencies
export * as deps from "./src/deps.ts";

// services
export * from "./src/depker.ts";
export * from "./src/services/docker/index.ts";
export * from "./src/services/run/index.ts";

// modules
export * from "./src/modules/proxy/proxy.module.ts";
Expand All @@ -11,11 +14,13 @@ export * from "./src/modules/service/service.module.ts";
export * from "./src/modules/service/service.type.ts";

// packs
export * from "./src/modules/service/pack.context.ts";
export * from "./src/modules/service/packs/dockerfile/dockerfile.pack.ts";
export * from "./src/modules/service/packs/image/image.pack.ts";
export * from "./src/modules/service/packs/nextjs/nextjs.pack.ts";
export * from "./src/modules/service/packs/nginx/nginx.pack.ts";
export * from "./src/modules/service/packs/nodejs/nodejs.pack.ts";
export * from "./src/modules/service/packs/nextjs/nextjs.pack.ts";
export * from "./src/modules/service/packs/coline/coline.pack.ts";

// default
export default depker();
9 changes: 1 addition & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,14 @@
"license": "Apache-2.0",
"private": true,
"type": "module",
"scripts": {
"dev": "deno run -A depker.ts"
},
"engines": {
"node": ">=16.0.0",
"npm": ">=8.0.0"
},
"devDependencies": {
"@syfxlin/eslint-config": "^1.0.1",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.2",
"prettier": "^3.1.1",
"typescript": "^5.3.3",
"typescript-deno-plugin": "^1.31.0"
}
Expand Down
Loading

0 comments on commit bc6ec81

Please sign in to comment.