-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·130 lines (113 loc) · 5.74 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
#!/usr/bin/node
const chokidar = require('chokidar')
const path = require('path')
const colors = require('colors/safe')
const readline = require('readline')
const updateResources = require('./lib/update-resource-files')
const updatePolicies = require('./lib/update-policies-files')
const uploadBundle = require('./lib/upload-bundle')
const downloadBundle = require('./lib/download-bundle')
const obtainLatestRevision = require('./lib/obtain-latest-revision')
const args = require('./lib/arguments')
const log = console.log.bind(console)
const configurer = require('./lib/create-config.js')
const config = configurer(args)
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
colors.setTheme({
verbose: 'cyan',
info: 'green',
error: 'red',
warn: 'yellow'
})
// chokidar options
const options = {
ignoreInitial: true,
ignored: /(^|[\/\\])\../
}
downloadBundle.observable.on('ready', function () {
log(colors.green("==================================================================================="))
log(colors.green("Starting Watcher for"))
log(colors.green("===================================================================================\n"))
log(`Proxy: ${config.api_name} \nRevision: ${config.api_revision} \nOrganization: ${config.api_organization}\nUser: ${config.apigee_username}\n`)
try {
process.chdir(`${config.api_name}`);
log(colors.verbose(`Moving to directory: ${process.cwd()}`))
startWatcher()
} catch (err) {
console.error(`chdir: ${err}`);
}
})
const isFolderChanged = ( pathChanged, folder ) => !!~pathChanged.indexOf(folder)
const startWatcher = () => {
chokidar.watch(args.dir, options)
.on(args.event, wPath => {
let dirname = path.dirname(wPath)
if (isFolderChanged(wPath, '/jsc')) {
log(colors.warn("\n==================================================================================="))
log(colors.warn("Updating js file..."))
log(colors.warn("===================================================================================\n"))
let jsFile = path.basename(wPath)
log(colors.verbose(`${dirname}/${jsFile}`))
updateResources(config, dirname, jsFile)
}
if (isFolderChanged(wPath, '/policies')) {
log(colors.warn("\n==================================================================================="))
log(colors.warn("Updating policy..."))
log(colors.warn("===================================================================================\n"))
// we specify a file extension to extract because Apigee API needs the file name in the url
let xmlFile = path.basename(wPath, '.xml')
log(colors.verbose(wPath))
updatePolicies(config, dirname, xmlFile)
}
const updateCurrentRevision = () => {
log(colors.warn("\n==================================================================================="))
log(colors.warn("Uploading directory..."))
log(colors.warn("===================================================================================\n"))
let dirToUpload = dirname.replace('/proxies', "")
log(colors.verbose(dirToUpload))
uploadBundle(config, dirToUpload)
}
if (isFolderChanged(wPath, '/proxies') || isFolderChanged(wPath, `/${config.api_name}.xml`)) {
if (args.autoUpdate) {
updateCurrentRevision()
} else {
rl.question('Are you sure you want to upload the bundle to Apigee? (Y/n): ', answer => {
if (/y(?:es)?|1/i.test(answer)) {
updateCurrentRevision()
} else {
log(colors.error("\nAPI Proxy not updated"))
}
})
}
}
})
}
if (args.downloadBundle) {
if (!config.api_revision) {
console.log("Getting latest API Proxy revision...")
obtainLatestRevision(config, function (error, rev) {
if (error) throw new Error(error);
config.api_revision = rev
log(colors.cyan("==================================================================================="))
log(colors.cyan("Starting Download for"))
log(colors.cyan("===================================================================================\n"))
log(`Proxy: ${config.api_name} \nRevision: ${config.api_revision} \nOrganization: ${config.api_organization}\nUser: ${config.apigee_username}\n`)
downloadBundle.makeRequest(config)
})
} else {
log(colors.cyan("==================================================================================="))
log(colors.cyan("Starting Download for"))
log(colors.cyan("===================================================================================\n"))
log(`Proxy: ${config.api_name} \nRevision: ${config.api_revision} \nOrganization: ${config.api_organization}\nUser: ${config.apigee_username}\n`)
downloadBundle.makeRequest(config)
}
} else {
log(colors.green("==================================================================================="))
log(colors.green("Starting Watcher for"))
log(colors.green("===================================================================================\n"))
log(`Proxy: ${config.api_name} \nRevision: ${config.api_revision} \nOrganization: ${config.api_organization}\nUser: ${config.apigee_username}\n`)
startWatcher()
}