Skip to content

Commit

Permalink
Add 'create/sitefox-nbb/' from commit '07e61ee42f64979767c6dc68b6ff68…
Browse files Browse the repository at this point in the history
…1fddfc3fc8'

git-subtree-dir: create/sitefox-nbb
git-subtree-mainline: 38aa41f
git-subtree-split: 07e61ee
  • Loading branch information
chr15m committed Mar 8, 2024
2 parents 38aa41f + 07e61ee commit cc55fe1
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 0 deletions.
2 changes: 2 additions & 0 deletions create/sitefox-nbb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.*.swp
node_modules
20 changes: 20 additions & 0 deletions create/sitefox-nbb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Set up a ClojureScript web server in one command using
[sitefox](https://github.com/chr15m/sitefox)
and [nbb](https://github.com/borkdude/nbb).

```shell
npm create sitefox-nbb mywebsite
cd mywebsite
npm run serve
```

Then open `server.cljs` in your editor and start hacking.
When `server.cljs` is modified the server will automatically reload routes. 👍

To serve the live version without file watcher reloading:

```
npm run serve-live
```

See the [sitefox documentation](https://github.com/chr15m/sitefox#batteries-included) for details on what you can do next.
28 changes: 28 additions & 0 deletions create/sitefox-nbb/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node

const args = process.argv.slice(2);
const execSync = require('child_process').execSync;
const fs = require('fs-extra');
const replace = require('replace-in-file');

const name = args[0];
const dir = name && args[0].replace(/-/g, '_');

if (name) {
console.log("Creating", name);
fs.copySync(__dirname + "/template", name);
fs.moveSync(name + "/gitignore", name + "/.gitignore");
replace.sync({
"files": [
args[0] + "/**/**",
],
"from": "NAME",
"to": name,
"countMatches": true,
});
console.log("Running npm install");
execSync("npm install", {"cwd": name, "stdio": ["inherit", "inherit", "inherit"]});
console.log("\nOk, you are ready to roll:\n$ cd " + name + "\n$ npm run serve\n\nThen edit server.cljs\n");
} else {
console.log("Usage: " + process.argv[1] + " APP-NAME");
}
13 changes: 13 additions & 0 deletions create/sitefox-nbb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "create-sitefox-nbb",
"description": "Set up a ClojureScript web server in one command with sitefox and nbb.",
"repository": "https://github.com/chr15m/create-sitefox-nbb",
"version": "0.0.7",
"bin": {
"create-sitefox-nbb": "index.js"
},
"dependencies": {
"fs-extra": "^9.0.1",
"replace-in-file": "^6.1.0"
}
}
1 change: 1 addition & 0 deletions create/sitefox-nbb/template/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:lint-as {promesa.core/let clojure.core/let}}
4 changes: 4 additions & 0 deletions create/sitefox-nbb/template/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{;; this deps.edn enables clojure-lsp features
:paths ["."]
:deps
{io.github.chr15m/sitefox {:git/tag "v0.0.1" :git/sha "b5520678b9fbf5919878a75750bb4d792c9eb8e2"}}}
5 changes: 5 additions & 0 deletions create/sitefox-nbb/template/gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
.*.swp
logs
database.sqlite
.clj-kondo/.cache
20 changes: 20 additions & 0 deletions create/sitefox-nbb/template/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "NAME",
"version": "0.0.1",
"dependencies": {
"nbb": "0.3.9",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"sitefox": "chr15m/sitefox"
},
"devDependencies": {
"browser-sync": "^2.27.7",
"fast-glob": "^3.2.11",
"filewatcher": "^3.0.1"
},
"scripts": {
"serve": "DEV=1 nbb --classpath ${VIRTUAL_ENV:=.}/node_modules/sitefox/src/ server.cljs",
"serve-live": "nbb --classpath ${VIRTUAL_ENV:=.}/node_modules/sitefox/src/ server.cljs",
"repl": "DEV=1 nbb --classpath ${VIRTUAL_ENV:=.}/node_modules/sitefox/src/ nrepl-server :port 1338"
}
}
14 changes: 14 additions & 0 deletions create/sitefox-nbb/template/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Sitefox HTML template merge example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/style.css" >
</head>
<body>
<main>
<p><div class="spinner spin"></div></p>
</main>
</body>
</html>
32 changes: 32 additions & 0 deletions create/sitefox-nbb/template/public/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
html, body, #app, #loading {
height: 100%;
}

body {
max-width: 100%;
width: 800px;
font-family: Helvetica, Arial, sans-serif;
margin: auto;
}

#loading {
display: flex;
justify-content: center;
align-items: center;
}

#loading div {
animation: spin 0.33s linear infinite;
width: 48px;
height: 48px;
border-radius: 24px;
border: 3px solid transparent;
border-left: 3px solid silver;
border-right: 3px solid silver;
}

@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}

47 changes: 47 additions & 0 deletions create/sitefox-nbb/template/server.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(ns webserver
(:require
["fs" :as fs]
[promesa.core :as p]
[nbb.core :refer [*file*]]
["browser-sync" :as browser-sync]
["fast-glob$default" :as fg]
[sitefox.web :as web]
[sitefox.util :refer [env]]
[sitefox.html :refer [render-into]]
[sitefox.reloader :refer [nbb-reloader]]
[sitefox.tracebacks :refer [install-traceback-emailer]]))

(when-let [admin-email (env "ADMIN_EMAIL")]
(install-traceback-emailer admin-email))

(def template (fs/readFileSync "public/index.html"))

(defn component-main []
[:div
[:h1 "Your Sitefox site"]
[:p "Welcome to your new sitefox site.
The code for this site is in " [:code "server.cljs"] "."]
[:p "Check out "
[:a {:href "https://github.com/chr15m/sitefox#batteries-included"} "the documentation"]
" to start building."]])

(defn setup-routes [app]
(web/reset-routes app)
(.get app "/"
(fn [_req res]
(->> (render-into template "main" [component-main])
(.send res))))
(web/static-folder app "/" "public"))

(defonce init
(p/let [self *file*
[app host port] (web/start)
sync-options {:files ["public/**/**"]
:proxy (str host ":" port)}
watch-files (fg #js [self "src/**/*.cljs" "public/*"])
bs (when (env "DEV") (browser-sync/init nil (clj->js sync-options)))]
(setup-routes app)
(nbb-reloader watch-files (fn []
(setup-routes app)
(when bs (.reload bs))))
(print "Serving at" (str host ":" port))))

0 comments on commit cc55fe1

Please sign in to comment.