Skip to content

Commit

Permalink
Merge tag '1.0.0' into develop
Browse files Browse the repository at this point in the history
1.0.0
  • Loading branch information
Max Rolon committed Jul 27, 2018
2 parents dc4248d + fcd9904 commit 3b58ba5
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 176 deletions.
1 change: 0 additions & 1 deletion .deps.json

This file was deleted.

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

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

Expand All @@ -27,7 +25,9 @@ if (program.debug) {

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

if (process.env.ENV !== 'development') {
if (process.env.ENV === 'development') {
process.env.NODE_ENV = 'development'
} else {
process.env.NODE_ENV = 'production'
}

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

const deps = require('./lib/deps.json')
const {exec} = require('child_process')

class Installer {
run () {
const dependencies = Object.keys(deps)
.map(key => `${key}@${deps[key]}`)
.join(' ')

this.install(dependencies)
.then(msg => {
console.log(msg)
})
.catch(msg => {
console.log(msg)
})
}

exec ( command = "ls" ) {
return new Promise( (resolve, reject) => {
exec(command, (err, stdout) => {
if (err) {
console.error(err)
reject(err.message)
return
}
resolve(stdout)
})
})
}

install (dependencies) {
// Last npm i is to recreate full .bin folder
return this.exec(`cd ${process.cwd()} && npm i -D ${dependencies} && npm i`)
}
}

const installer = new Installer()
installer.run()
2 changes: 1 addition & 1 deletion lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Configure {
setDefaults () {
this.defaults = {
publicPath: '/dev',
proxy: 'localhost',
local: 'localhost',
dist: `${this.cwd}/dist`,
theme_id: '',
api_key: '',
Expand Down
230 changes: 140 additions & 90 deletions lib/deployer.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const colors = require('colors')
const fs = require('fs');
const path = require('path')
const {exec, spawn} = require('child_process')
const {exec} = require('child_process')
const shopifyAPI = require('shopify-node-api')
const Moment = require('moment-timezone')
const readline = require('readline')
const config = require('./configure')
const Err = require('./error')
const Builder = require('./builder')
const locales = require('./locales')

class Deployer {
constructor () {
Expand All @@ -17,107 +17,160 @@ class Deployer {
access_token: config.get('password'),
verbose: false
});
this.files = []
this.questioner = false
this.deployAll = false

this.askQuestion = this.askQuestion.bind(this)
this.interpretLastDeploymentFlag = this.interpretLastDeploymentFlag.bind(this)
this.guessLastDeploymentFlag = this.guessLastDeploymentFlag.bind(this)
this.getLastVersionTag = this.getLastVersionTag.bind(this)
this.getBranchBaseCommit = this.getBranchBaseCommit.bind(this)
this.getChangedFiles = this.getChangedFiles.bind(this)
this.filterFiles = this.filterFiles.bind(this)
this.confirmFiles = this.confirmFiles.bind(this)
this.buildAndDeploy = this.buildAndDeploy.bind(this)
this.finishDeployment = this.finishDeployment.bind(this)
}

run () {
const questioner = readline.createInterface({
this.questioner = readline.createInterface({
input: process.stdin,
output: process.stdout
})
let question1 = '\n\nWhat\'s your deployment breakpoint (commit, tag or branch)? \n\n'.bgBlack.yellow
question1 += 'eg. commithash, v1.0.1, develop or.. "all" \n'.bgBlack.yellow
question1 += 'Default: Last version tag \n\n'.bgBlack.yellow
question1 += '[write your answer now..] \n'.bgBlack.yellow

this.askQuestion(questioner, question1)
.then(answer => {
let bp;
if (!answer) {
bp = this.getLastVersionTag()
.then(t => Promise.resolve(t.replace(/[\r\n]+/, '')))
.then(t => {
if (!t) {
let comment = 'No version tag found\n'.underline.bgWhite.black
comment += 'Getting branch merge-base..\n'.underline.bgWhite.black
console.log(comment)

return this.getBranchName()
.then(b => this.getBranchBaseCommit(b))
} else {
return Promise.resolve(t)
}
})
} else if (/^all/.test(answer)){
bp = Promise.resolve(null)
} else {
bp = Promise.resolve(answer.replace(/ /, ''))
this.askQuestion(locales.deployer.question1)
.then(this.interpretLastDeploymentFlag)
.then(this.getChangedFiles)
.then(this.confirmFiles)
.then(this.buildAndDeploy)
.then(this.finishDeployment)
.catch(err => {
console.log(`${err}`.red)
this.questioner.close()
})
}

askQuestion (question) {
question = this.attachColor(question)
return new Promise((resolve, reject) => {
this.questioner.question(question, answer => {
resolve(answer)
})
})
}

interpretLastDeploymentFlag (answer) {
answer = this.trimString(answer)
if (!answer) {
return this.guessLastDeploymentFlag()
}
if (/^all/.test(answer)) {
this.deployAll = true
}
return Promise.resolve(answer)
}

guessLastDeploymentFlag () {
return this.getLastVersionTag()
.then(t => {
t = this.trimString(t)
if (t) {
return Promise.resolve(t)
}
return bp.then(str => {
if (str) {
return this.getChangedFiles(str)
} else {
// Deploy all files
return Promise.resolve(null)
}
}).then(str => {
if (str === null) {
// Deploy all files
return Promise.resolve(str)
}
this.talk(locales.deployer.noVersionTagFound)
return this.getBranchName().then(this.getBranchBaseCommit)
})
}

if (!str) {
return Promise.reject('No files to upload :('.red)
}

const files = str.split(/\n/)
.filter(f => f)
.filter(f => /^src/.test(f))
return Promise.resolve(files)
})
}).then(files => {
if (!files) {
// Deploy all files
return Promise.resolve({answer: 'Y'})
getChangedFiles (breakpoint) {
if (this.deployAll) {
return Promise.resolve(this.files)
}
return this.getChangedFilesFromDiff(breakpoint)
.then(this.filterFiles)
.then(files => {
if (!files || !files.length) {
throw new Error('No files to deploy')
}
this.files = files
return Promise.resolve(this.files)
})
}

let question2 = '\n\nDo these files look correct? \n'.underline.bgWhite.black
question2 += '* JS and CSS will always be uploaded *\n\n'.underline.bgWhite.black
question2 += files.join('\n').bgBlack.white
question2 += '\n\n[Y|n]\n'.bgBlack.white

return this.askQuestion(questioner, question2)
.then(answer => {
return Promise.resolve({answer, files})
})

}).then(({answer, files = null}) => {
if (!answer || /^[Yy]+/.test(answer)) {
console.log('Starting deployment..'.green)
return Promise.resolve(files)
getChangedFilesFromDiff (breakpoint) {
return this.exec(`git diff --name-status ${this.trimString(breakpoint)}..HEAD`)
}

filterFiles (files) {
const cleansed = files.split(/\n/)
.filter(f => f)
.map(line => {
return line.split(/\t/)
})
// Forget about files that have been deleted
// Unless they are a compiled file (config, css, js)
.filter(line => {
if (!/D/.test(line[0])) {
return true
}
return Promise.reject('Files do not look good :('.red)
}).then(files => {
if (files === null) {
return Builder(true)
if (/(?:config|[.]js|[.]s?css)/i.test(line[1])) {
return true
}
return false
})
// If a file has been replaced, get the new location
.map(line => {
if (/R/.test(line[0])) {
return line[2]
} else {
return Builder(true, files)
return line[1]
}
}).then(() => {
this.renameThemeFile()
questioner.close()
console.log('🏆 Deployment completed!'.green)
}).catch(err => {
console.log(`${err}`.red)
questioner.close()
})
.filter(f => /^src/.test(f))

return Promise.resolve(cleansed)
}

askQuestion (process, question) {
return new Promise((resolve, reject) => {
process.question(question, answer => {
resolve(answer)
confirmFiles () {
if (this.deployAll) {
return Promise.resolve(true)
}
let question2 = locales.deployer.question2
.replace('{{files}}', this.files.join('\n'))

return this.askQuestion(question2)
.then(answer => {
if (!answer || /^[Yy]+/.test(answer)) {
this.talk('Starting deployment..')
return Promise.resolve(true)
}
throw new Error('Files do not look good :(')
})
})
}

buildAndDeploy () {
if (this.deployAll) {
return Builder(true)
}
return Builder(true, this.files)
}

finishDeployment () {
this.renameThemeFile()
this.questioner.close()
this.talk('🏆 Deployment completed!')
}

talk (msg) {
console.log(this.attachColor(msg))
}

attachColor (str) {
return `${str}`.bgBlack.yellow
}

trimString (str) {
return str.replace(/[\r\n]+/, '')
}

exec ( command = "ls" ) {
Expand All @@ -133,17 +186,14 @@ class Deployer {
})
}

getChangedFiles (breakpoint) {
return this.exec(`git diff --name-only HEAD..${breakpoint}`)
}

getBranchBaseCommit (branchName) {
let cleansed = branchName.replace(/[\r\n]+/, '')
if (cleansed === 'develop') {
return this.exec(`git merge-base ${cleansed} master`)
}
return this.exec(`git merge-base ${cleansed} develop`)
}

getBranchName () {
return this.exec('git rev-parse --abbrev-ref HEAD')
}
Expand Down
28 changes: 28 additions & 0 deletions lib/deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"autoprefixer": "^7.1.4",
"babel-core": "^6.25.0",
"babel-eslint": "^7.2.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-assign": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^0.28.7",
"eslint-loader": "^2.0.0",
"file-loader": "^1.1.11",
"globby": "^5.0.0",
"gulp": "^3.9.1",
"postcss-automath": "^1.0.1",
"postcss-color-function": "^4.0.0",
"postcss-extend": "^1.0.5",
"postcss-fontpath": "^1.0.0",
"postcss-hexrgba": "^1.0.0",
"postcss-import": "^10.0.0",
"postcss-inline-svg": "^3.0.0",
"postcss-loader": "^2.0.6",
"precss": "^2.0.0",
"standard": "^10.0.2",
"style-loader": "^0.18.2",
"url-loader": "^1.0.1",
"webpack": "^4.16.2"
}
Loading

0 comments on commit 3b58ba5

Please sign in to comment.