This repository was archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy patheach-service.js
163 lines (146 loc) · 4.67 KB
/
each-service.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
/**
* Copyright (c) Microsoft. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// Helper script that can read the set of services included
// in the azure SDK dynamically.
var fs = require('fs');
var path = require('path');
var util = require('util');
var sdkRoot = path.join(__dirname, '..');
var servicesRoot = path.join(sdkRoot, 'lib/services');
//
// Can't use readable-stream, have to only use core modules
// since this is called by ci before npm install runs.
//
//
// Implementation of async versions of fundamental higher-order functions
// over arrays.
//
var autorestgenServices = {
'authorizationManagement': 'authorizationManagement',
'batch': 'batch',
'batchManagement': 'batchManagement',
'cdnManagement': 'cdnManagement',
'computeManagement2': 'computeManagement2',
'dataLake.Analytics': 'dataLake.Analytics',
'dataLake.Store': 'dataLake.Store',
'graphManagement': 'graphManagement',
'intune': 'intune',
'networkManagement2': 'networkManagement2',
'rediscachemanagement': 'rediscachemanagement',
'resourceManagement': 'resourceManagement',
'serviceFabric': 'serviceFabric',
'storageManagement2': 'storageManagement2',
'storeManagement': 'storeManagement',
'webSiteManagement2': 'webSiteManagement2',
};
function reduceAsync(items, asyncIterator, done, initialValue) {
if (!initialValue) { initialValue = []; }
function doReduce(accumulator, remainingItems, index) {
if (remainingItems.length === 0) {
return done(null, accumulator);
}
asyncIterator(accumulator, remainingItems[0],
function (err, newAccumulator) {
if (err) { return done(err); }
doReduce(newAccumulator, remainingItems.slice(1), index + 1);
}, index, items);
}
doReduce(initialValue, items, 0);
}
function filterAsync(items, asyncPredicate, done) {
reduceAsync(items,
function(acc, item, next, index, array) {
asyncPredicate(item,
function(err, passed) {
if (err) { return next(err); }
if (passed) {
acc.push(item);
}
return next(null, acc);
},
index, array);
}, done);
}
function mapAsync(items, asyncIterator, done) {
reduceAsync(items,
function (acc, item, next, index, array) {
asyncIterator(item,
function (err, mappedItem) {
if (err) { return next(err); }
return next(null, acc.concat(mappedItem));
},
index, array);
}, done);
}
function forEachAsync(items, asyncIterator, done) {
reduceAsync(items,
function(acc, item, next, index, array) {
asyncIterator(item,
function (err) { return next(err, acc); },
index, array);
},
function (err, result) {
done(err);
}
);
}
//
// Find the list of potential service directories, including common
//
function readServiceDirs(done) {
fs.readdir(servicesRoot, function (err, files) {
if (err) { return done(err); }
function isNotAutorestgenService(item) {
if (!autorestgenServices[item]) {
return item;
}
}
var filteredFiles = files.filter(isNotAutorestgenService);
done(null, [path.join(sdkRoot, 'lib/common')].concat(
filteredFiles.map(function (f) {
return path.join(servicesRoot, f);
})));
});
}
function hasPackageJson(servicePath, done) {
fs.exists(path.join(servicePath, 'package.json'), function (exists) {
return done(null, exists);
})
}
function readPackageJson(servicePath, done) {
var packagejsonpath = path.join(servicePath, 'package.json');
fs.readFile(packagejsonpath, function (err, buffer) {
if (err) { return done(err); }
var json = JSON.parse(buffer.toString().trim());
return done(null, {
path: servicePath,
packageJson: json
});
});
}
function forEachService(iterator, done) {
readServiceDirs(function (err, paths) {
if (err) { return done(err); }
filterAsync(paths, hasPackageJson, function (err, filteredPaths) {
if (err) { return done(err); }
mapAsync(filteredPaths, readPackageJson, function (err, serviceData) {
if (err) { return done(err); }
forEachAsync(serviceData, iterator, done);
});
});
});
}
module.exports = forEachService;