UNPKG

3.49 kBJavaScriptView Raw
1'use strict'
2
3const minimist = require('minimist')
4const path = require('path')
5const fs = require('fs')
6const watch = require('simple-watcher')
7const defaults = require('./src/defaults')
8const log = require('./src/log')
9const Pipeline = require('./src/pipeline')
10const { version } = require('./package.json')
11
12const MSG_HELP = `
13The code and content synchronization for Sling / AEM; version ${version}.
14
15Usage:
16 aemsync [OPTIONS]
17
18Options:
19 -t <target> URL to AEM instance; multiple can be set.
20 Default: ${defaults.targets}
21 -w <path_to_watch> Watch over folder.
22 Default: CWD
23 -p <path_to_push> Push specific file or folder.
24 -e <exclude_filter> Extended glob filter; multiple can be set.
25 Default:
26 **/jcr_root/*
27 **/@(.git|.svn|.hg|target)
28 **/@(.git|.svn|.hg|target)/**
29 -i <sync_interval> Update interval.
30 Default: ${defaults.interval} ms
31 -u <packmgr_path> Package manager path.
32 Default: ${defaults.packmgrPath}
33 -c Check if AEM is up and running before pushing.
34 -d Enable debug mode.
35 -h Display this screen.
36
37Website:
38 https://github.com/gavoja/aemsync
39`
40
41function aemsync (workingDir, { targets, interval, exclude, packmgrPath, onPushEnd, checkBeforePush }) {
42 const pipeline = new Pipeline({ targets, interval, exclude, packmgrPath, onPushEnd, checkBeforePush })
43
44 pipeline.start()
45 watch(workingDir, localPath => {
46 pipeline.enqueue(localPath)
47 })
48}
49
50async function push (pathToPush, { targets, exclude, packmgrPath, checkBeforePush }) {
51 const pipeline = new Pipeline({ targets, exclude, packmgrPath, checkBeforePush })
52 return pipeline.push(pathToPush)
53}
54
55function main () {
56 const args = minimist(process.argv.slice(2))
57
58 // Show help.
59 if (args.h) {
60 return log.info(MSG_HELP)
61 }
62
63 // Print additional debug information.
64 args.d && log.enableDebug()
65
66 // Get the args.
67 const pathToPush = args.p ? path.resolve(args.p) : null
68 const workingDir = path.resolve(args.w || defaults.workingDir)
69 const targets = args.t ? (typeof args.t === 'string' ? [args.t] : args.t) : defaults.targets
70 const exclude = args.e ? (typeof args.e === 'string' ? [args.e] : args.e) : defaults.exclude
71 const interval = args.i || defaults.interval
72 const checkBeforePush = args.c
73 const packmgrPath = args.u || defaults.packmgrPath
74
75 //
76 // Just the push.
77 //
78
79 if (pathToPush) {
80 // Path to push does not have to exist.
81 // Non-existing path can be used for deletion.
82 return push(pathToPush, { targets })
83 }
84
85 //
86 // Watch mode.
87 //
88
89 if (!fs.existsSync(workingDir)) {
90 return log.info('Invalid path:', log.gray(workingDir))
91 }
92
93 // Start aemsync
94 log.info(`aemsync version ${version}
95
96 Watch over: ${log.gray(workingDir)}
97 Targets: ${targets.map(t => log.gray(t)).join('\n'.padEnd(17, ' '))}
98 Exclude: ${exclude.map(x => log.gray(x)).join('\n'.padEnd(17, ' '))}
99 Interval: ${log.gray(interval)}
100 `)
101
102 aemsync(workingDir, { targets, interval, exclude, packmgrPath, checkBeforePush })
103}
104
105if (require.main === module) {
106 main()
107}
108
109aemsync.Pipeline = Pipeline
110aemsync.main = main
111aemsync.push = push
112module.exports = aemsync