Skip to content
This repository was archived by the owner on Oct 22, 2018. It is now read-only.

Commit ff597a5

Browse files
committed
[feat] initial draft
0 parents  commit ff597a5

7 files changed

Lines changed: 172 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fbarrel

.travis.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
language: go
2+
go:
3+
- master
4+
env:
5+
- "PATH=/home/travis/gopath/bin:$PATH"
6+
before_install:
7+
- go get github.com/tools/godep
8+
script:
9+
- go install
10+
- CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o goat-linux-amd64 .
11+
deploy:
12+
provider: releases
13+
api_key: $GITHUB_TOKEN
14+
file:
15+
- fbarrel-linux-amd64
16+
skip_cleanup: true
17+
on:
18+
tags: true
19+

Godeps/Godeps.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/Readme

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2+
Version 2, December 2004
3+
4+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
5+
6+
Everyone is permitted to copy and distribute verbatim or modified
7+
copies of this license document, and changing it is allowed as long
8+
as the name is changed.
9+
10+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
13+
0. You just DO WHAT THE FUCK YOU WANT TO.
14+

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# fbarrel
2+
3+
Given
4+
5+
```
6+
test/a.ts (export function fkbr(){})
7+
test/b.ts (export class Yolo{})
8+
```
9+
10+
then running `fbarrel -n cool -p test` will write
11+
12+
```
13+
test/barrel.ts
14+
test/cool.ts
15+
```
16+
17+
with `barrel.ts`:
18+
19+
```js
20+
export * from './a';
21+
export * from './b';
22+
```
23+
24+
and `cool.ts`:
25+
26+
```js
27+
import * as Cool from './barrel';
28+
export { Cool };
29+
```
30+
31+
So now you can do:
32+
33+
```js
34+
import {Cool} from './test/cool'
35+
36+
Cool.fkbr()
37+
```

fbarrel.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"github.com/jessevdk/go-flags"
7+
"io/ioutil"
8+
"bufio"
9+
"path"
10+
"strings"
11+
)
12+
13+
type opts struct {
14+
Path string `short:"p" long:"path" description:"Path to typescript folder where barrel should be created" required:"true"`
15+
Name string `short:"n" long:"name" description:"Name of barrel (omit .ts) - will be uppercased for namespace name" required:"true"`
16+
}
17+
18+
func fatal(err error){
19+
if err != nil {
20+
fmt.Fprintln(os.Stderr, err)
21+
os.Exit(1)
22+
}
23+
}
24+
25+
func main() {
26+
var opts = opts{}
27+
28+
_, err := flags.Parse(&opts)
29+
fatal(err)
30+
31+
files,err := listFiles(opts.Path)
32+
fatal(err)
33+
err = writeBarrel(opts.Path, files)
34+
fatal(err)
35+
36+
err = writeNamespace(opts.Name, opts.Path)
37+
38+
os.Exit(0)
39+
}
40+
41+
func writeNamespace(name string, ts_path string) error {
42+
fd, err := os.Create(path.Join(ts_path, fmt.Sprintf("%s.ts", name))); if err != nil {
43+
return err
44+
}
45+
namespace := strings.Title(name)
46+
fmt.Printf("Writing namespace { %s }\n", namespace)
47+
defer fd.Close()
48+
w := bufio.NewWriter(fd)
49+
50+
_, err = w.WriteString(fmt.Sprintf("import * as %s from './barrel';\nexport { %s };\n", namespace, namespace)); if err != nil {
51+
return err
52+
}
53+
w.Flush()
54+
return nil
55+
}
56+
57+
func writeBarrel(ts_path string, files []os.FileInfo) error {
58+
fd, err := os.Create(path.Join(ts_path, "barrel.ts")); if err != nil {
59+
return err
60+
}
61+
defer fd.Close()
62+
w := bufio.NewWriter(fd)
63+
64+
for _, f := range files {
65+
var name = f.Name()
66+
if(strings.HasPrefix(name, ".") || ! strings.HasSuffix(name, ".ts") || name == "barrel.ts"){ continue }
67+
name_without_ext := name[0:strings.LastIndex(name, ".ts")]
68+
fmt.Printf("Writing to barrel for %s (%s)\n", name_without_ext, name)
69+
_, err = w.WriteString(fmt.Sprintf("export * from './%s';\n", name_without_ext)); if err != nil {
70+
return err
71+
}
72+
}
73+
w.Flush()
74+
75+
return nil
76+
}
77+
78+
func listFiles(ts_path string) ([]os.FileInfo, error) {
79+
files, err := ioutil.ReadDir(ts_path)
80+
if err != nil {
81+
return nil,err
82+
}
83+
return files,nil
84+
}

0 commit comments

Comments
 (0)