Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Rolon committed Oct 2, 2017
0 parents commit e91c463
Show file tree
Hide file tree
Showing 15 changed files with 7,789 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# @slater/cli

```bash
npm i barrel-cli -g
```

## Usage
A slater theme needs two things:
1. standard `config.yml`
2. a `webpack.config.js` in your root

Files should follow the default directory structure of [Slate](https://github.com/Shopify/slate). We recommend [@slater/theme](https://github.com/the-couch/slater-theme).

During development, replace your Shopify CDN url with a path to your local bundle to enable HMR. This is not currently configurable.

```html
<body>
...
{% comment %} <script src="{{ 'index.js' | asset_url }}" defer="defer"></script> {% endcomment %}
<script src="https://unpkg.com/@slater/reload"></script>
<script src="https://localhost:3000/index.js"></script>
</body>
```

Then just run the commands:

```bash
# watch theme files and
# hot reload javascript
slater -w # or --watch

# build javascript and upload entire theme
slater -d # or --deploy

# specify an environment, default: development
slater -e production # or --env
```

You'll also want the [@slater/reload](https://github.com/the-couch/slater-reload) package to listen for file changes and live-reload the remote page. That will look like this:

```html
<body>
...
{% comment %} <script src="{{ 'index.js' | asset_url }}" defer="defer"></script> {% endcomment %}
<script src="https://unpkg.com/@slater/reload"></script>
<script src="https://localhost:3000/index.js"></script>
</body>
```

MIT
44 changes: 44 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#! /usr/bin/env node
'use strict'

const path = require('path')
const fs = require('fs')
const program = require('commander')
const colors = require('colors')

const pkg = require('./package.json')
const bus = require('./lib/emitter')
const Err = require('./lib/error')

program
.version(pkg.version)
.option('-w --watch', 'recursively watch src directory')
.option('-e --env [env]', 'specify an environment')
.option('-b, --build [env]', 'deploy a theme')
.option('--debug', 'enable available debugging')
.parse(process.argv)

if (program.debug) {
process.env.DEBUG = '*'
}

process.env.ENV = program.env || 'development'

const configure = require('./lib/configure')
const watcher = require('./lib/watcher')
const builder = require('./lib/builder')
const gulp = require('./lib/gulp')


/**
* Clear terminal bc it's prettier
*/
process.stdout.write('\x1B[2J\x1B[0f')

try {
configure.setup().then(() => {
program.watch ? watcher() : builder()
})
} catch (e) {
new Err(e)
}
1 change: 1 addition & 0 deletions lib/browsersync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('browser-sync').create();
45 changes: 45 additions & 0 deletions lib/builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const colors = require('colors')
const config = require('./configure')
const Webpack = require('./webpack')
const Err = require('./error')
const Gulp = require('./Gulp')

class Builder {
constructor () {
this.compiler = Webpack(false).compiler
}

run () {
this.compiler.run((err, stats) => {
this.handleErrors(err, stats)
console.log('successfully compiled javascript...'.green)
})

Gulp(false)
}

handleErrors (err, stats) {
if (err) {
throw new Error(err)
}

if (stats.hasErrors()) {
let {errors = []} = stats.compilation
errors.map(e => e.message)
throw new Error(errors)
}
}
}

module.exports = () => {
let builder

try {
builder = new Builder()
builder.run()
} catch (e) {
new Err(e)
}

return builder
}
21 changes: 21 additions & 0 deletions lib/cert/s.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDijCCAnICCQClrA9v9Wg39zANBgkqhkiG9w0BAQsFADCBhjELMAkGA1UEBhMC
VVMxETAPBgNVBAgTCE5ldyBZb3JrMREwDwYDVQQHEwhOZXcgWW9yazESMBAGA1UE
ChMJVGhlIENvdWNoMQwwCgYDVQQLEwNEZXYxDTALBgNVBAMTBEVyaWMxIDAeBgkq
hkiG9w0BCQEWEWVyaWNAdGhlY291Y2gubnljMB4XDTE3MDkwODE3NTEyMloXDTE4
MDkwODE3NTEyMlowgYYxCzAJBgNVBAYTAlVTMREwDwYDVQQIEwhOZXcgWW9yazER
MA8GA1UEBxMITmV3IFlvcmsxEjAQBgNVBAoTCVRoZSBDb3VjaDEMMAoGA1UECxMD
RGV2MQ0wCwYDVQQDEwRFcmljMSAwHgYJKoZIhvcNAQkBFhFlcmljQHRoZWNvdWNo
Lm55YzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKZdJj6U0QtT+vkO
UmAoeC+P1FZaiMKyiHTmJj38Atf8TTUdRTEuIXY0mpyPCOom0gxwGks/q49EIgNR
568iZZuxm34KkmWYkRdChovI+mnczPL+8Z779OiHTs1UMod+FesNRAl6pZ97E+4P
dTkxK6DxmOaAE3XqB78rkHOniqLOL7k6T/1XSqYedGkStmZHdzI7nUpjp/lo+n1X
4BDsydQGgYNdJjLbOUwcBUV6Zz1wme6pC+s005Lfd4KuIk4DcqHJObicCzTbZkTe
cY8/yKxtY1xFNvxyzh5aYz1EgsscyiGqrKsp0bcj48awv+cfu7GH2VRix+s8uj8K
j9wwdUMCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEARR99CSaY1fKXkjDu5Ss47Nxk
TkMORwfsHlecTXSGy9t0EKWtsUSRA1r6FqPE4mPefwTPFtEAo4BgrYobrp8PuijN
qKhyOPRoa38aStq6eaXNWNZbjjuWhMMgcODcrAKAwYa6j5jIGAN7nFT0Wr5yWzve
XNqDyojEi8+7MMjz4o6SDWPrmGqY8j/oYPHhMvK78Iv7SDbdneugLU8oNEmdzYr7
EeysK3fm86Dr8/kN2Y5QTJPY2Hut+RUzHp4xQZX5grQ9+rzTMyjIiFvS+wN1FEOc
GHAsGFUwAQ7uGMmJlQCtx874sEKR4+iAZoL806ixpHKMnb4e70Wv6HM7GycbpA==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions lib/cert/s.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEApl0mPpTRC1P6+Q5SYCh4L4/UVlqIwrKIdOYmPfwC1/xNNR1F
MS4hdjSanI8I6ibSDHAaSz+rj0QiA1HnryJlm7GbfgqSZZiRF0KGi8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w1HJTPSMdqzjvbpYF6Wv9afVrMo0EyZoliQp044zkVB8SswWwm
uBNXHZ1eqgZESQBxDxGJT3UCgYEAqq+w8l0mwt5L0MeHWzKzfW3lyKlXl+/+dIFE
vVtjYv3fF6iNb8w0bBxULVCzdOJXYJ5oY9yE5wMufIh+4c7CLQpRfIpoirdS++r1
tmI8UpiD7FWOs/VAsug0wsi1e5hmufpJQ8n5jmc/xp+BpU/xMK9v8eu3BIypow3E
b9FxCCkCgYEAt4E+WnAhC1S6UNow/g0zxHOex2xjJK5KQPxzG21Vb5ZL5Wv3Mgve
ze1dIizFyox4s7F1WY2Ee/PACPeSy/9Mf8K5YDvrAyI0bro4BYTiRNRdSZ+jk/1s
/G0sSvKd6uf5z5qblvytFr99wZ0DpQ/XR9BunZruhGO76xwA2urXyJQ=
-----END RSA PRIVATE KEY-----
136 changes: 136 additions & 0 deletions lib/configure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
const path = require('path')
const fs = require('fs')
const yaml = require('js-yaml')
const bus = require('./emitter')
const Err = require('./error')

class Configure {

setDefaults () {
this.defaults = {
publicPath: '/dev',
proxy: '//localhost:3000',
dist: `${this.cwd}/dist`,
theme_id: '',
api_key: '',
password: '',
store: '',
}
}

constructor(props) {
this.cwd = process.cwd()
this.env = process.env.ENV
this.path1 = `${this.cwd}/config.yml`
this.path2 = `${this.cwd}/dist/config.yml`
this.webpackPath = `${this.cwd}/webpack.config.js`
}

start () {
this.setDefaults()
this.findConfig()
this.getWebpackConfig()
this.fillInBlanks()
bus.emit('configured')
}

findConfig () {
[this.path1, this.path2].every(path => {
if (fs.existsSync(path)) {
this.configPath = path
this.loadConfig(path)
return false
} else {
return true
}
})
}

loadConfig (path) {
const config = yaml.safeLoad(
fs.readFileSync(path)
)

if (!config[this.env]) {
throw new Error(`Environment ${this.env} in ${path} not set`)
}

const envConfig = config[this.env]

Object.keys(envConfig).forEach(key => {
this[key] = envConfig[key]
})
}

getWebpackConfig () {
this.webpack = require(this.webpackPath)
}

fillInBlanks () {
if (!this['theme_id']) {
this.shopify = false
console.log('No theme_id found so assuming this is not a Shopify project')
} else {
this.shopify = true
}

this.checkProxyTarget()
this.checkPublicPath()
this.checkDist()
}

checkProxyTarget () {
if (this.shopify) {
this.target = `https://${this.store}`
console.log(`Proxy target set at ${this.target}`)
} else {
if (!this.target) {
throw new Error('Proxy target not found')
}
}
}

checkPublicPath () {
if (this.shopify) {
this.publicPath = this.defaults.publicPath
} else {
const uri = this.webpack.output.path.split('/wp-content/')[1]
this.publicPath = `/wp-content/${uri}`
}
}

checkDist () {
if (this.shopify) {
this.dist = this.defaults.dist
} else {
this.dist = false
}
}

setup () {
if (this.hasRun) {
return new Promise(resolve => {
resolve()
})
}

const promise = new Promise(resolve => {
bus.on('configured', () => resolve())
})

this.start()

return promise
}

get (prop) {
return this[prop]
}

set (prop, val) {
this[prop] = val
}

}

module.exports = new Configure
5 changes: 5 additions & 0 deletions lib/emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const EventEmitter = require('events');

class Emitter extends EventEmitter {}

module.exports = new Emitter()
11 changes: 11 additions & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const colors = require('colors')

class CustomError {
constructor (err) {
console.log(`😭 ❕❕❕❕ >>>> ${err.message}`.white)
console.log(`${err.stack}`.blue)
process.exit(1)
}
}

module.exports = CustomError
Loading

0 comments on commit e91c463

Please sign in to comment.