Skip to content

Commit

Permalink
feat: add typescript workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusted committed May 17, 2021
1 parent 5fc70ce commit 12593be
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 30 deletions.
5 changes: 5 additions & 0 deletions air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = "."
tmp_dir = "tmp"

[build]
exclude_dir = ["typescript"]
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/davidbyttow/govips/v2 v2.6.0
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
github.com/emersion/go-smtp v0.15.0
github.com/evanw/esbuild v0.11.23
github.com/form3tech-oss/jwt-go v3.2.2+incompatible
github.com/go-co-op/gocron v1.5.0
github.com/go-playground/validator/v10 v10.6.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1X
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.15.0 h1:3+hMGMGrqP/lqd7qoxZc1hTU8LY8gHV9RFGWlqSDmP8=
github.com/emersion/go-smtp v0.15.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
github.com/evanw/esbuild v0.11.23 h1:Y5I6OTABHBmOmt2cbRKrByW5sLdTrpaWhV2MihqLLiw=
github.com/evanw/esbuild v0.11.23/go.mod h1:y2AFBAGVelPqPodpdtxWWqe6n2jYf5FrsJbligmRmuw=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/flosch/pongo2/v4 v4.0.2/go.mod h1:B5ObFANs/36VwxxlgKpdchIJHMvHB562PW+BWPhwZD8=
Expand Down Expand Up @@ -518,6 +520,7 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
2 changes: 2 additions & 0 deletions static/js/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion tools/run
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dev() {
env DB_DEBUG="${2:-silent}" \
DB_DROP="${3:-false}" \
DB_COLOR="true" \
air
air -c air.toml
}

# This option builds USw executable for use in production.
Expand All @@ -82,11 +82,20 @@ build() {
-buildmode pie -tags 'osusergo netgo static_build'
}

ts() {
log "Compiling typescript"

env DEBUG="${2:-false}" \
WATCH="${3:-false}" \
go run ./typescript/run.go
}

case "$1" in
build) build "$@" ;;
dev) dev "$@" ;;
drop) drop "$@" ;;
prod) prod "$@" ;;
sass) sass "$@" ;;
ts) ts "$@" ;;
*) invalid "$@" ;;
esac
17 changes: 17 additions & 0 deletions typescript/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {ShareButton} from './share-button';
import {BroadcastReady} from './third-party';
import {addDOMReadyListener, isDOMReady} from './utils/dom';

const setup = () => {
ShareButton();
BroadcastReady();
}

// Setup contains code that only should be handle
// when the DOM is ready to go.
// Any other code shouldn't depend on this setup function.
if (isDOMReady()) {
setup();
} else {
addDOMReadyListener(setup);
}
77 changes: 77 additions & 0 deletions typescript/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// +build ignore

package main

import (
"log"
"os"

"github.com/evanw/esbuild/pkg/api"
)

var (
isDebug = getEnv("DEBUG", "false") == "true"
isProduction = !isDebug
shouldWatch = getEnv("WATCH", "false") == "true"
)

func getEnv(name, fallback string) string {
if val, set := os.LookupEnv(name); set {
return val
}
return fallback
}

func main() {
sourceMap := api.SourceMapInline
var watch *api.WatchMode
if isProduction {
sourceMap = api.SourceMapNone
}
if shouldWatch {
watch = &api.WatchMode{
OnRebuild: func(result api.BuildResult) {
if len(result.Errors) > 0 {
log.Printf("watch build failed: %d errors\n", len(result.Errors))
} else {
log.Printf("watch build succeeded: %d warnings\n", len(result.Warnings))
}
},
}
}
buildResult := api.Build(api.BuildOptions{
EntryPoints: []string{"./typescript/main.ts"},
Outfile: "./static/js/main.js",
Bundle: true,
Write: true,
LogLevel: api.LogLevelInfo,
Platform: api.PlatformBrowser,
MinifySyntax: isProduction,
MinifyWhitespace: isProduction,
MinifyIdentifiers: isProduction,
Sourcemap: sourceMap,
Target: api.ES2017,
Charset: api.CharsetUTF8,
Format: api.FormatIIFE,
Watch: watch,
Banner: map[string]string{
"js": "\"use strict\";",
},
})

if len(buildResult.Errors) > 0 {
os.Exit(1)
}

// When we are watching we shouldn't exit program.
// So a quick and dirty hack to let an never end loop run.
// From Go 1.15 this section will be optimized away into (amd64).
// any_label:
// XCHGL AX, AX
// JMP any_label
// Whereby XCHGL acts as a "write block".
if shouldWatch {
for {
}
}
}
10 changes: 10 additions & 0 deletions typescript/share-button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function ShareButton() {
const i = document.getElementById('share') as HTMLInputElement;
const shareButton = document.getElementById('btn-share') as HTMLButtonElement;
shareButton.addEventListener("click", () => {
i.select();
document.execCommand('copy');
i.blur();
shareButton.classList.add('copied');
});
}
19 changes: 19 additions & 0 deletions typescript/third-party.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const onMessage = (ev: MessageEvent<any>) => {
if (!ev.data || !ev.data.type) {
return;
}
switch (ev.data.type) {
case "usw-remove-stylus-button": {
document.querySelector('a#stylus').remove();
}
}
}

window.addEventListener("message", onMessage)

export function BroadcastReady() {
window.dispatchEvent(new MessageEvent('message', {
data: {type: "usw-ready"},
origin: "https://userstyles.world"
}));
}
21 changes: 21 additions & 0 deletions typescript/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"target": "es2017",
"baseUrl": ".",
"module": "es2015",
"moduleResolution": "node",
"lib": [
"es2015",
"es2017",
"dom",
"dom.iterable"
],
"downlevelIteration": true,
"noEmit": true,
"incremental": true,
"isolatedModules": true
},
"exclude": [
"node_modules"
]
}
15 changes: 15 additions & 0 deletions typescript/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const readyStateListeners = new Set<() => void>();

export const isDOMReady = () => document.readyState === 'complete' || document.readyState === 'interactive';
export const addDOMReadyListener = (listener: () => void) => readyStateListeners.add(listener);

if (!isDOMReady()) {
const onReadyStateChange = () => {
if (isDOMReady()) {
document.removeEventListener('readystatechange', onReadyStateChange);
readyStateListeners.forEach((listener) => listener());
readyStateListeners.clear();
}
};
document.addEventListener('readystatechange', onReadyStateChange);
}
10 changes: 0 additions & 10 deletions views/js/share-button.html

This file was deleted.

19 changes: 0 additions & 19 deletions views/js/third-party-integration.html

This file was deleted.

1 change: 1 addition & 0 deletions views/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
<meta property="og:description" content="{{ .Style.Description }}" />
<meta property="og:image" content="{{ .Style.Preview }}" />
{{ end }}
<script src="/js/main.js"></script>

0 comments on commit 12593be

Please sign in to comment.