-
Notifications
You must be signed in to change notification settings - Fork 56
/
index.js
304 lines (260 loc) · 9.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env node
import fs from 'fs'
import fetch, { FormData, File } from 'node-fetch'
import path from 'path'
import watch from 'simple-watcher'
import * as url from 'url'
import xmlToJson from 'xml-to-json-stream'
import Channel from './src/channel.js'
import * as log from './src/log.js'
import Package from './src/package.js'
const DIRNAME = url.fileURLToPath(new URL('.', import.meta.url))
const PACKAGE_JSON = path.join(DIRNAME, 'package.json')
const VERSION = JSON.parse(fs.readFileSync(PACKAGE_JSON, 'utf8')).version
const DEFAULTS = {
workingDir: '.',
exclude: ['**/jcr_root/*', '**/@(.git|.svn|.hg|target)', '**/@(.git|.svn|.hg|target)/**'],
packmgrPath: '/crx/packmgr/service.jsp',
targets: ['http://admin:admin@localhost:4502'],
delay: 300,
checkIfUp: false,
postHandler: post,
verbose: false
}
const HELP = `
The code and content synchronization for Sling / AEM; version ${VERSION}.
Usage:
aemsync [OPTIONS]
Options:
-t <target> URL to AEM instance; multiple can be set.
Default: ${DEFAULTS.targets}
-w <path_to_watch> Watch over folder.
Default: ${DEFAULTS.workingDir}
-p <path_to_push> Push specific file or folder.
-e <exclude_filter> Extended glob filter; multiple can be set.
Default:
**/jcr_root/*
**/@(.git|.svn|.hg|target)
**/@(.git|.svn|.hg|target)/**
-d <delay> Time to wait since the last change before push.
Default: ${DEFAULTS.interval} ms
-q <packmgr_path> Package manager path.
Default: ${DEFAULTS.packmgrPath}
-c Check if AEM is up and running before pushing.
-v Enable verbose mode.
-h Display this screen.
Examples:
Magic:
> aemsync
Custom targets:
> aemsync -t http://admin:admin@localhost:4502 -t http://admin:admin@localhost:4503 -w ~/workspace/my_project
Custom exclude rules:
> aemsync -e **/*.orig -e **/test -e -e **/test/**
Just push, don't watch:
> aemsync -p /foo/bar/my-workspace/jcr_content/apps/my-app/components/my-component
Push multiple:
> aemsync -p /foo/bar/my-workspace/jcr_content/apps/my-app/components/my-component -p /foo/bar/my-workspace/jcr_content/apps/my-app/components/my-other-component
Website:
https://github.com/gavoja/aemsync
`
// =============================================================================
// Posting to AEM.
// =============================================================================
async function post ({ archivePath, target, packmgrPath, checkIfUp }) {
const form = new FormData()
form.set('file', new File([fs.readFileSync(archivePath)], { type: 'text/plain' }))
form.set('force', 'true')
form.set('install', 'true')
// Check if AEM is up and runnig.
if (checkIfUp && !await check(target)) {
return { target, err: new Error('AEM not ready') }
}
const result = { target }
try {
const urlObj = new URL(target + packmgrPath)
const url = urlObj.origin + urlObj.pathname
const fetchArgs = { method: 'POST', body: form }
if (urlObj.password) {
const credentials = Buffer.from(`${urlObj.username}:${urlObj.password}`).toString('base64')
fetchArgs.headers = { Authorization: `Basic ${credentials}` }
}
const res = await fetch(url, fetchArgs)
if (res.ok) {
const text = await res.text()
// Handle errors with AEM response.
try {
const obj = await parseXml(text)
result.log = obj.crx.response.data.log
const errorLines = [...new Set(result.log.split('\n').filter(line => line.startsWith('E')))]
// Errors when installing selected nodes.
if (errorLines.length) {
result.err = new Error('Error installing nodes:\n' + errorLines.join('\n'))
// Error code in status.
} else if (obj.crx.response.status.code !== '200') {
result.err = new Error(obj.crx.response.status.textNode)
}
} catch (err) {
// Unexpected response format.
throw new Error('Unexpected response text format')
}
} else {
// Handle errors with the failed request.
result.err = new Error(res.statusText)
}
} catch (err) {
// Handle unexpeted errors.
result.err = err
}
return result
}
async function check (target) {
try {
const res = await fetch(target)
return res.status === 200
} catch (err) {
log.debug(err.message)
return false
}
}
function parseXml (xml) {
return new Promise(resolve => {
xmlToJson().xmlToJson(xml, (err, json) => err ? resolve({}) : resolve(json))
})
}
// =============================================================================
// Main API.
// =============================================================================
async function wait (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
export async function * push (args) {
const { payload, exclude, targets, packmgrPath, checkIfUp, postHandler, breakStuff } = { ...DEFAULTS, ...args }
// Get archive as many times as necessary.
let archive
while (true) {
const pack = new Package(exclude)
for (const localPath of payload) {
const item = pack.add(localPath)
item && log.info(item.exists ? '+' : '-', item.zipPath)
}
// Ability to break stuff when testing.
// This is to simulate changes between change reported and archive creation.
breakStuff && await breakStuff()
archive = pack.save()
if (archive.err) {
log.debug(archive.err)
await wait(3000)
log.info('Failed to create ZIP, retrying...')
} else {
break
}
}
// Archive may not be created if items added are on the exclude path.
if (archive.path) {
for (const target of targets) {
const response = await postHandler({ archivePath: archive.path, target, packmgrPath, checkIfUp })
log.info(log.gray(`${response.target} > ${response.err ? response.err.message : 'OK'}`))
yield { archive, response }
}
} else {
yield {}
}
}
export async function * aemsync (args) {
const { workingDir, delay } = { ...DEFAULTS, ...args }
const channel = new Channel()
const payload = []
let timeoutId
// Process file changes in the background.
;(async function () {
for await (const localPath of watch(workingDir)) {
payload.push(localPath)
// Graceful handling of bulk changes.
// Process only after a certain amount of time passes since the last change.
clearTimeout(timeoutId)
timeoutId = setTimeout(async () => {
// Make sure only current batch of payload is processed.
const batch = payload.splice(0, payload.length)
for await (const result of push({ ...args, payload: batch })) {
channel.put(result)
}
}, delay)
}
})()
// Yield results via channel.
while (true) {
yield await channel.take()
}
}
// =============================================================================
// CLI handling.
// =============================================================================
function debugResult (result) {
log.debug('Package contents:')
log.group()
log.debug(JSON.stringify(result?.archive?.contents, null, 2))
log.groupEnd()
log.debug('Response log:')
log.group()
log.debug(result?.response?.log)
log.groupEnd()
}
function getArgs () {
const args = [' ', ...process.argv.slice(2)].join(' ').split(' -').slice(1).reduce((obj, arg) => {
const [key, value] = arg.split(/ (.*)/s)
obj[key] = obj[key] ?? []
obj[key].push(value)
return obj
}, {})
return {
payload: args.p ? args.p.map(p => path.resolve(p)) : null,
workingDir: path.resolve(args?.w?.[0] ?? DEFAULTS.workingDir),
targets: args.t ?? DEFAULTS.targets,
exclude: args.e ?? DEFAULTS.exclude,
delay: Number(args?.d?.[0]) || DEFAULTS.delay,
checkIfUp: !!args.c,
packmgrPath: args?.q?.pop?.() ?? DEFAULTS.packmgrPath,
help: !!args.h,
verbose: !!args.v
}
}
export async function main () {
const args = getArgs()
// Show help.
if (args.help) {
log.info(HELP)
return
}
// Print additional debug information.
args.verbose && log.enableDebug()
//
// Just the push.
//
// Path to push does not have to exist.
// Non-existing path can be used for deletion.
if (args.payload) {
const result = (await push(args).next()).value
debugResult(result)
return
}
//
// Watch mode.
//
if (!fs.existsSync(args.workingDir)) {
log.info('Invalid path:', log.gray(args.workingDir))
return
}
// Start aemsync.
log.info(`aemsync version ${VERSION}
Watch over: ${log.gray(args.workingDir)}
Targets: ${args.targets.map(t => log.gray(t)).join('\n'.padEnd(17, ' '))}
Exclude: ${args.exclude.map(x => log.gray(x)).join('\n'.padEnd(17, ' '))}
Delay: ${log.gray(args.delay)}
`)
for await (const result of aemsync(args)) {
debugResult(result)
}
}
if (path.normalize(import.meta.url) === path.normalize(`file://${process.argv[1]}`)) {
main()
}