Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kennedyrose committed May 9, 2020
0 parents commit 8ceb957
Show file tree
Hide file tree
Showing 17 changed files with 7,511 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
public
node_modules
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@smarterlabs/eslint-config"
}
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# System generated files
.DS_Store

# Logs
logs
*.log
npm-debug.log*

# Dependency directories
node_modules

# Development/Environment
.env
.env.*
test-env.txt
!.env.example
dist
dist-*
build
public
storybook-static
.cache
.serverless
_optimize
/netlify.toml
.npmrc

# Temporary
temp
temp-*

# Local Netlify folder
.netlify
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v12.10.0
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Director
8 changes: 8 additions & 0 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"packages": [
"packages/*"
],
"version": "independent",
"npmClient": "yarn",
"useWorkspaces": true
}
37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "director",
"private": true,
"author": "https://smarterlabs.com",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"bootstrap": "yarn run clean && cd packages/data && yarn build",
"build": "cd packages/sandbox-director && yarn build",
"clean": "npx lerna run clean --stream --no-sort --concurrency 999",
"dev": "npx lerna run dev --stream --no-sort --concurrency 999",
"lint": "npx eslint ./",
"lint:fix": "npx eslint ./ --fix",
"stop": "sudo killall -9 node"
},
"workspaces": [
"packages/*"
],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"lint-staged": {
"**/*.js": [
"eslint --fix"
]
},
"devDependencies": {
"@smarterlabs/eslint-config": "^1.0.9",
"eslint": "^6.8.0",
"husky": "^4.2.5",
"lerna": "^3.20.2",
"lint-staged": "^10.2.2",
"open-cli": "^6.0.1"
}
}
13 changes: 13 additions & 0 deletions packages/director/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@smarterlabs/director",
"version": "0.0.0",
"main": "src/director.js",
"dependencies": {
"del": "^5.1.0",
"express": "^4.17.1",
"fs-extra": "^9.0.0",
"http-proxy-middleware": "^1.0.3",
"open": "^7.0.3",
"yargs": "^15.3.1"
}
}
14 changes: 14 additions & 0 deletions packages/director/src/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { move } = require(`fs-extra`)
const { join } = require(`path`)

module.exports = async function build(options){
console.log(`Assembling build...`)
let promises = []
options.paths.forEach(path => {
let src = join(options.src, path.src)
let dist = join(options.dist, path.url || path.dist)
promises.push(move(src, dist, { overwrite: true }))
})
await Promise.all(promises)
console.log(`Done assembling`)
}
5 changes: 5 additions & 0 deletions packages/director/src/clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const del = require(`del`)

module.exports = async function clean(options){
await del([options.dist])
}
59 changes: 59 additions & 0 deletions packages/director/src/dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const express = require(`express`)
const { createProxyMiddleware } = require(`http-proxy-middleware`)
const open = require(`open`)

const app = express()
const proxy = createProxyMiddleware
const defaultOptions = {
paths: [],
port: 5555,
}

module.exports = function startServer(options){
options = {
...defaultOptions,
...options,
}
let paths = getPaths(options)

paths.forEach(path => {
app.use(path.url, proxy({
target: `http://localhost:${path.port}`,
changeOrigin: true,
pathRewrite: {
[path.url]: ``,
},
...options.proxyOptions,
...path.proxyOptions,
}))
})

app.listen(options.port)
console.log(`Director started at ${options.port}`)
if(options.open){
open(`http://localhost:${options.port}/`)
}
}

// Build an array of objects containing path information
function getPaths(options) {
let paths = []

if (Array.isArray(options.paths)) {
paths.push(...options.paths)
}
else {
let keys = Object.keys(options.paths)
keys.forEach(url => {
paths.push({
url,
...options.paths[url],
})
})
}
if (options.sort) {
paths.sort(options.sort)
}

return paths
}
40 changes: 40 additions & 0 deletions packages/director/src/director.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const argv = require(`yargs`).argv
const dev = require(`./dev`)
const build = require(`./build`)
const clean = require(`./clean`)

const cmd = (label) => argv._.includes(label)

module.exports = function Director(options){
options = {
...defaultOptions,
...options,
}
const director = {
dev: () => dev(options),
build: () => build(options),
clean: () => clean(options),
}
if (cmd(`develop`)) {
director.dev()
}
else if (cmd(`build`)){
director.build()
}
else if (cmd(`clean`)){
director.clean()
}
return director
}

const defaultOptions = {
sort: (a, b) => {
if(a.url == `/`) return 1
if(b.url == `/`) return -1
let aDepth = a.url.split(`/`).length
let bDepth = b.url.split(`/`).length
if(aDepth > bDepth) return -1
else if(aDepth < bDepth) return 1
return 0
},
}
10 changes: 10 additions & 0 deletions packages/sandbox-a/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@app/sandbox-a",
"version": "0.0.0",
"scripts": {
"dev": "serve www -l 5000"
},
"dependencies": {
"serve": "^11.3.0"
}
}
4 changes: 4 additions & 0 deletions packages/sandbox-a/www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!DOCTYPE html>
<html>
<body>Success!</body>
</html>
14 changes: 14 additions & 0 deletions packages/sandbox-director/director.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Director = require(`@smarterlabs/director`)

Director({
paths: {
'/': {
port: 5000,
src: `sandbox-a/www`,
},
},
src: `../`,
dist: `dist`,
port: 5555,
open: true,
})
12 changes: 12 additions & 0 deletions packages/sandbox-director/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@app/sandbox-director",
"version": "0.0.0",
"scripts": {
"build": "node director build",
"clean": "node director clean",
"dev": "node director develop"
},
"dependencies": {
"@smarterlabs/director": "*"
}
}
Loading

0 comments on commit 8ceb957

Please sign in to comment.