Skip to content

Commit

Permalink
Fixes to #32 and #34. Code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
gavoja committed Feb 27, 2018
1 parent 02388d3 commit ccdecac
Show file tree
Hide file tree
Showing 9 changed files with 672 additions and 151 deletions.
55 changes: 43 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,70 @@ npm install aemsync -g

Commandline
```
aemsync -t targets -w path_to_watch
-t: Comma separated list of target hosts; default is http://admin:admin@localhost:4502.
-w: Folder to watch; default is current.
-i: Update interval; default is 300ms.
-e: Anymatch exclude filter; any file matching the pattern will be skipped.
-d: Enable debug mode.
Usage:
aemsync [OPTIONS]
Options:
-t <targets> Defult is http://admin:admin@localhost:4502
-w <path_to_watch> Default is current
-p <path_to_push> Path to push directly; used instead of above,
no watching takes place
-e <exclude_filter> Micromatch exclude filter; disabled by default
-i <sync_interval> Update interval; default is 300ms
-u <packmgr_path> Package manager path; default is
/crx/packmgr/service.jsp
-d Enable debug mode
-h Displays this screen
```
```
aemsync -t http://admin:admin@localhost:4502,http://admin:admin@localhost:4503 -w ~/workspace/my_project
```

JavaScript
JavaScript (full watch example):
```JavaScript
// Import aemsync.
const aemsync = require('aemsync')

// Set up the environment.
const workingDir = '~/workspace/my_project'
const targets = [
'http://admin:admin@localhost:4502',
'http://admin:admin@localhost:4503'
]
const exclude = '**/*.orig' // Skip merge files.
const interval = 300
const packmgrUrl = '/foo/crx/packmgr/service.jsp'
const onPushEnd = (err, host) => {
if (err) {
return console.log(`Error when pushing package to ${host}.`, err)
}
console.log(`Package pushed to ${host}.`)
}

// Will watch for changes on workingDir and push them.
aemsync({workingDir, targets, exclude, interval, packmgrUrl, onPushEnd})
```

JavaScript (direct push example):
```JavaScript
// Import aemsync.
const aemsync = require('aemsync')

// Set up the environment.
let workingDir = '~/workspace/my_project'
let path = '~/foo/bar/my-workspace/jcr_content/apps/my-app/components/my-component'
let targets = [
'http://admin:admin@localhost:4502',
'http://admin:admin@localhost:4503'
]
let exclude = '**/*.orig' // Skip merge files.
let pushInterval = 300
let onPushEnd = (err, host) => {
if (err) {
return console.log(`Error when pushing package to ${host}.`, err)
}
console.log(`Package pushed to ${host}.`)
}

aemsync({workingDir, targets, exclude, pushInterval, onPushEnd})
// Will push the path to AEM.
aemsync.push({path, targets, onPushEnd})
```

### Description
Expand Down
88 changes: 57 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,93 @@ const fs = require('graceful-fs')
const log = require('./src/log')
const chalk = require('chalk')
const Watcher = require('./src/watcher')
const Pusher = require('./src/pusher')
const Pipeline = require('./src/pipeline')

const MSG_HELP = `Usage: aemsync [OPTIONS]
const MSG_HELP = `
Usage:
aemsync [OPTIONS]
Options:
-t targets Defult is http://admin:admin@localhost:4502
-w path_to_watch Default is current
-e exclude_filter Micromatch exclude filter; disabled by default
-i sync_interval Update interval; default is 300ms
-d Enable debug mode
-h Displays this screen
-t <targets> Defult is http://admin:admin@localhost:4502
-w <path_to_watch> Default is current
-p <path_to_push> Path to push directly; used instead of above,
no watching takes place
-e <exclude_filter> Micromatch exclude filter; disabled by default
-i <sync_interval> Update interval; default is 300ms
-u <packmgr_path> Package manager path; default is
/crx/packmgr/service.jsp
-d Enable debug mode
-h Displays this screen
Website: https://github.com/gavoja/aemsync`
Website:
https://github.com/gavoja/aemsync
`

function aemsync (args) {
let pusher = new Pusher(args.targets.split(','), args.pushInterval, args.onPushEnd)
let watcher = new Watcher()
const pipeline = new Pipeline(args)
const watcher = new Watcher()

pusher.start()
watcher.watch(args.workingDir, args.exclude, (localPath) => {
pusher.enqueue(localPath)
})
pipeline.start()

args.callback = (localPath) => {
pipeline.enqueue(localPath)
}

watcher.watch(args)
}

function push (args) {
const pipeline = new Pipeline(args)
pipeline.push(args.pathToPush)
}

function main () {
let args = minimist(process.argv.slice(2))
const args = minimist(process.argv.slice(2))

// Show help.
if (args.h) {
console.log(MSG_HELP)
return
return console.log(MSG_HELP)
}

// Get other args.
log.isDebug = args.d
let workingDir = path.resolve(args.w ? args.w : '.')
const workingDir = path.resolve(args.w || '.')
const targets = (args.t || 'http://admin:admin@localhost:4502').split(',')
const interval = args.i || 300
const exclude = args.e || ''
const packmgrPath = args.u

if (!fs.existsSync(workingDir)) {
log.info('Invalid path:', chalk.yellow(workingDir))
return
// Just the push.
if (args.p) {
let pathToPush = path.resolve(args.p)
if (!fs.existsSync(pathToPush)) {
return log.info('Invalid path:', chalk.yellow(workingDir))
}

return push({pathToPush, targets})
}

let targets = args.t ? args.t : 'http://admin:admin@localhost:4502'
let pushInterval = args.i ? args.i : 300
let exclude = args.e ? args.e : ''
if (!fs.existsSync(workingDir)) {
return log.info('Invalid path:', chalk.yellow(workingDir))
}

// Start aemsync
log.info(`
Working dir: ${chalk.yellow(workingDir)}
Targets: ${chalk.yellow(targets)}
Interval: ${chalk.yellow(pushInterval)}
Exclude: ${chalk.yellow(exclude)}
Working dir: ${chalk.yellow(workingDir)}
Targets: ${chalk.yellow(targets)}
Interval: ${chalk.yellow(interval)}
Exclude: ${chalk.yellow(exclude)}
`)

aemsync({workingDir, targets, pushInterval, exclude})
aemsync({workingDir, targets, interval, exclude, packmgrPath})
}

if (require.main === module) {
main()
}

aemsync.Watcher = Watcher
aemsync.Pusher = Pusher
aemsync.Pipeline = Pipeline
aemsync.main = main
aemsync.push = push
module.exports = aemsync
Loading

0 comments on commit ccdecac

Please sign in to comment.