1 | 'use strict'
|
2 |
|
3 | const minimist = require('minimist')
|
4 | const path = require('path')
|
5 | const fs = require('fs')
|
6 | const watch = require('simple-watcher')
|
7 | const defaults = require('./src/defaults')
|
8 | const log = require('./src/log')
|
9 | const Pipeline = require('./src/pipeline')
|
10 | const { version } = require('./package.json')
|
11 |
|
12 | const MSG_HELP = `
|
13 | The code and content synchronization for Sling / AEM; version ${version}.
|
14 |
|
15 | Usage:
|
16 | aemsync [OPTIONS]
|
17 |
|
18 | Options:
|
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 |
|
37 | Website:
|
38 | https://github.com/gavoja/aemsync
|
39 | `
|
40 |
|
41 | function 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 |
|
50 | async function push (pathToPush, { targets, exclude, packmgrPath, checkBeforePush }) {
|
51 | const pipeline = new Pipeline({ targets, exclude, packmgrPath, checkBeforePush })
|
52 | return pipeline.push(pathToPush)
|
53 | }
|
54 |
|
55 | function main () {
|
56 | const args = minimist(process.argv.slice(2))
|
57 |
|
58 |
|
59 | if (args.h) {
|
60 | return log.info(MSG_HELP)
|
61 | }
|
62 |
|
63 |
|
64 | args.d && log.enableDebug()
|
65 |
|
66 |
|
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 |
|
77 |
|
78 |
|
79 | if (pathToPush) {
|
80 |
|
81 |
|
82 | return push(pathToPush, { targets })
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | if (!fs.existsSync(workingDir)) {
|
90 | return log.info('Invalid path:', log.gray(workingDir))
|
91 | }
|
92 |
|
93 |
|
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 |
|
105 | if (require.main === module) {
|
106 | main()
|
107 | }
|
108 |
|
109 | aemsync.Pipeline = Pipeline
|
110 | aemsync.main = main
|
111 | aemsync.push = push
|
112 | module.exports = aemsync
|