Skip to content

Commit

Permalink
Add feature flag with comma, update fixtures & readme
Browse files Browse the repository at this point in the history
  • Loading branch information
muhibbudins committed May 24, 2020
1 parent 9e27948 commit f62afd7
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 49 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ $ curl -LJO https://raw.githubusercontent.com/muhibbudins/denomon/master/denomon
Move the denomon binary into **/usr/bin** or **/usr/local/bin**

```bash
$ mv denomon /usr/local/bin
$ sudo mv denomon /usr/local/bin
```

Make it executable
Expand All @@ -40,10 +40,13 @@ $ denomon <options> <file>
Example

```bash
$ denomon --dir fixtures --allow net express.ts
or
$ denomon --allow net fixtures/express.ts
or
# spawn folder but direct to files with permission of net & read
$ denomon --dir fixtures --allow net,read server.ts
# spawn folder with permission of net & read
$ denomon --dir fixtures --allow net,read
# spawn file with permission of net
$ denomon --allow net fixtures/server.ts
# spawn file without any permission
$ denomon fixtures/mod.ts
```

Expand All @@ -55,7 +58,7 @@ To set target directory

#### --allow

To set allowed permission
To set allowed permission, comma sepearated

### Features

Expand All @@ -71,4 +74,4 @@ Refer to this [issue](https://github.com/fsnotify/fsnotify/issues/18), currently

### License

This project under MIT License
This project is under MIT License
Binary file modified denomon
Binary file not shown.
17 changes: 0 additions & 17 deletions fixtures/express.ts

This file was deleted.

82 changes: 82 additions & 0 deletions fixtures/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {
green,
cyan,
bold,
yellow,
red,
} from "https://deno.land/std@0.53.0/fmt/colors.ts";

import { Application, HttpError, send, Status } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

// Error handler middleware
app.use(async (context, next) => {
try {
await next();
} catch (e) {
if (e instanceof HttpError) {
context.response.status = e.status as any;
if (e.expose) {
context.response.body = `<!DOCTYPE html>
<html>
<body>
<h1>${e.status} - ${e.message}</h1>
</body>
</html>`;
} else {
context.response.body = `<!DOCTYPE html>
<html>
<body>
<h1>${e.status} - ${Status[e.status]}</h1>
</body>
</html>`;
}
} else if (e instanceof Error) {
context.response.status = 500;
context.response.body = `<!DOCTYPE html>
<html>
<body>
<h1>500 - Internal Server Error</h1>
</body>
</html>`;
console.log("Unhandled Error:", red(bold(e.message)));
console.log(e.stack);
}
}
});

// Logger
app.use(async (context, next) => {
await next();
const rt = context.response.headers.get("X-Response-Time");
console.log(
`${green(context.request.method)} ${cyan(context.request.url.pathname)} - ${
bold(
String(rt),
)
}`,
);
});

// Response Time
app.use(async (context, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
context.response.headers.set("X-Response-Time", `${ms}ms`);
});

// Send static content
app.use(async (context) => {
await context.send({
root: `${Deno.cwd()}/fixtures/static`,
index: "index.html",
});
});

const options = { hostname: "127.0.0.1", port: 3000 };
console.log(
bold("Start listening on ") + yellow(`${options.hostname}:${options.port}`),
);
await app.listen(options);
11 changes: 11 additions & 0 deletions fixtures/static/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>A static page</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<h1>Hello from a static page</h1>
</body>
</html>
1 change: 1 addition & 0 deletions fixtures/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body {color: #c0392b}
50 changes: 25 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"os/exec"
"strings"

"github.com/fatih/color"
"github.com/fsnotify/fsnotify"
Expand All @@ -16,8 +17,8 @@ var pid int = 0
var command exec.Cmd
var args []string
var file string = ""
var directory string = ""
var permission permissionFlag
var path string = ""
var permission []string

func help() {
fmt.Printf("[DENOMON HELP]\n\n")
Expand All @@ -29,23 +30,23 @@ func help() {
fmt.Printf("--allow Assign permission for the files\n\n")
}

func build(path string) bool {
color.Green("[denomon] build: %s", path)
func build(file string) bool {
color.Green("[denomon] build: %s", file)

if pid > 0 {
command.Process.Kill()
}

var stderr bytes.Buffer

options := []string{"run", path}
options := []string{"run", file}

if len(permission) > 0 {
options = []string{"run"}
for i := 0; i < len(permission); i++ {
options = append(options, "--allow-"+permission[i])
}
options = append(options, path)
options = append(options, file)
}

cmd := exec.Command("deno", options...)
Expand All @@ -66,30 +67,29 @@ func build(path string) bool {
return false
}

type permissionFlag []string

func (i *permissionFlag) String() string {
return "my string representation"
}

func (i *permissionFlag) Set(value string) error {
*i = append(*i, value)
return nil
}

func main() {
dir, err := os.Getwd()

if err != nil {
log.Fatal(err)
}

flag.Var(&permission, "allow", "Some description for this param.")
dirPtr := flag.String("dir", ".", "Directory assignment")
helpPtr := flag.Bool("help", false, "Help message")
dirPtr := flag.String("dir", ".", "Assign directory to watch")
helpPtr := flag.Bool("help", false, "Print this help message")
allowPtr := flag.String("allow", "", "Assign permission for the files")

flag.Parse()

if *allowPtr != "" {
allowSlice := strings.Split(*allowPtr, ",")

for i := range allowSlice {
allowSlice[i] = strings.TrimSpace(allowSlice[i])
}

permission = allowSlice
}

args = flag.Args()

if (len(args) == 0 && *dirPtr == ".") || *helpPtr == true {
Expand All @@ -102,13 +102,13 @@ func main() {
}

if file != "" {
directory = dir + "/" + *dirPtr + "/" + file
build(directory)
path = dir + "/" + *dirPtr + "/" + file
build(path)
} else {
directory = dir + "/" + *dirPtr
path = dir + "/" + *dirPtr
}

color.Cyan("[denomon] watching: %s", directory)
color.Cyan("[denomon] watching: %s", path)

watcher, err := fsnotify.NewWatcher()

Expand Down Expand Up @@ -144,7 +144,7 @@ func main() {
}
}()

if err = watcher.Add(directory); err != nil {
if err = watcher.Add(path); err != nil {
color.Red("[denomon] error: %s", err.Error())
os.Exit(1)
}
Expand Down

0 comments on commit f62afd7

Please sign in to comment.