20
20
fs = require ( 'fs' ) ,
21
21
path = require ( 'path' ) ,
22
22
__ = appc . i18n ( __dirname ) . __ ,
23
- defaultProfileDir = '~/Library/MobileDevice/Provisioning Profiles' ;
23
+ provisioningProfilesDirectories = [
24
+ '~/Library/Developer/Xcode/UserData/Provisioning Profiles' ,
25
+ '~/Library/MobileDevice/Provisioning Profiles'
26
+ ]
24
27
25
28
var cache = null ,
26
29
watchers = { } ;
@@ -37,7 +40,6 @@ var cache = null,
37
40
* @type {Error }
38
41
*/
39
42
40
- exports . defaultProfileDir = defaultProfileDir ;
41
43
exports . detect = detect ;
42
44
exports . find = find ;
43
45
exports . watch = watch ;
@@ -48,7 +50,7 @@ exports.unwatch = unwatch;
48
50
*
49
51
* @param {Object } [options] - An object containing various settings.
50
52
* @param {Boolean } [options.bypassCache=false] - When true, re-detects all provisioning profiles.
51
- * @param {String } [options.profileDir=~/Library/MobileDevice /Provisioning Profiles] - The path to search for provisioning profiles.
53
+ * @param {String } [options.profileDir=~/Library/Developer/Xcode/UserData /Provisioning Profiles] - The path to search for provisioning profiles.
52
54
* @param {Boolean } [options.unmanaged] - When true, excludes managed provisioning profiles.
53
55
* @param {Boolean } [options.validOnly=true] - When true, only returns non-expired, valid provisioning profiles.
54
56
* @param {Boolean } [options.watch=false] - If true, watches the specified provisioning profile directory for updates.
@@ -63,10 +65,10 @@ function detect(options, callback) {
63
65
return magik ( options , callback , function ( emitter , options , callback ) {
64
66
var files = { } ,
65
67
validOnly = options . validOnly === undefined || options . validOnly === true ,
66
- profileDir = appc . fs . resolvePath ( options . profileDir || defaultProfileDir ) ,
68
+ profileDirs = getExistingProvisioningProfileDirectories ( options . profileDir ) ,
67
69
results = {
68
70
provisioning : {
69
- profileDir : profileDir ,
71
+ profileDir : profileDirs [ 0 ] ,
70
72
development : [ ] ,
71
73
adhoc : [ ] ,
72
74
enterprise : [ ] ,
@@ -82,51 +84,53 @@ function detect(options, callback) {
82
84
} ,
83
85
84
86
ppRegExp = / .* \. ( m o b i l e p r o v i s i o n | p r o v i s i o n p r o f i l e ) $ / ;
85
-
87
+
86
88
87
89
if ( options . watch ) {
88
90
var throttleTimer = null ;
89
91
90
- if ( ! watchers [ profileDir ] ) {
91
- watchers [ profileDir ] = {
92
- handle : fs . watch ( profileDir , { persistent : false } , function ( event , filename ) {
93
- if ( ! ppRegExp . test ( filename ) ) {
94
- // if it's not a provisioning profile, we don't care about it
95
- return ;
96
- }
97
-
98
- var file = path . join ( profileDir , filename ) ;
99
-
100
- if ( event === 'rename' ) {
101
- if ( files [ file ] ) {
102
- if ( fs . existsSync ( file ) ) {
103
- // change, reload the provisioning profile
104
- parseProfile ( file ) ;
92
+ for ( const profileDir of profileDirs ) {
93
+ if ( ! watchers [ profileDir ] ) {
94
+ watchers [ profileDir ] = {
95
+ handle : fs . watch ( profileDir , { persistent : false } , function ( event , filename ) {
96
+ if ( ! ppRegExp . test ( filename ) ) {
97
+ // if it's not a provisioning profile, we don't care about it
98
+ return ;
99
+ }
100
+
101
+ var file = path . join ( profileDir , filename ) ;
102
+
103
+ if ( event === 'rename' ) {
104
+ if ( files [ file ] ) {
105
+ if ( fs . existsSync ( file ) ) {
106
+ // change, reload the provisioning profile
107
+ parseProfile ( file ) ;
108
+ } else {
109
+ // delete
110
+ removeProfile ( file ) ;
111
+ }
105
112
} else {
106
- // delete
107
- removeProfile ( file ) ;
113
+ // add
114
+ parseProfile ( file ) ;
108
115
}
109
- } else {
110
- // add
116
+ } else if ( event === 'change' ) {
117
+ // updated
111
118
parseProfile ( file ) ;
112
119
}
113
- } else if ( event === 'change' ) {
114
- // updated
115
- parseProfile ( file ) ;
116
- }
117
-
118
- clearTimeout ( throttleTimer ) ;
119
-
120
- throttleTimer = setTimeout ( function ( ) {
121
- detectIssues ( ) ;
122
- emitter . emit ( 'detected' , results ) ;
123
- } , 250 ) ;
124
- } ) ,
125
- count : 0
126
- } ;
120
+
121
+ clearTimeout ( throttleTimer ) ;
122
+
123
+ throttleTimer = setTimeout ( function ( ) {
124
+ detectIssues ( ) ;
125
+ emitter . emit ( 'detected' , results ) ;
126
+ } , 250 ) ;
127
+ } ) ,
128
+ count : 0
129
+ } ;
130
+ }
131
+
132
+ watchers [ profileDir ] . count ++ ;
127
133
}
128
-
129
- watchers [ profileDir ] . count ++ ;
130
134
}
131
135
132
136
if ( cache && ! options . bypassCache ) {
@@ -137,7 +141,7 @@ function detect(options, callback) {
137
141
function detectIssues ( ) {
138
142
results . issues = [ ] ;
139
143
140
- if ( ! results . provisioning . development . length || ! valid . development ) {
144
+ if ( results . provisioning . development . length > 0 && ! valid . development ) {
141
145
results . issues . push ( {
142
146
id : 'IOS_NO_VALID_DEVELOPMENT_PROVISIONING_PROFILES' ,
143
147
type : 'warning' ,
@@ -146,7 +150,7 @@ function detect(options, callback) {
146
150
} ) ;
147
151
}
148
152
149
- if ( ! results . provisioning . adhoc . length || ! valid . adhoc ) {
153
+ if ( results . provisioning . adhoc . length > 0 && ! valid . adhoc ) {
150
154
results . issues . push ( {
151
155
id : 'IOS_NO_VALID_ADHOC_PROVISIONING_PROFILES' ,
152
156
type : 'warning' ,
@@ -155,7 +159,7 @@ function detect(options, callback) {
155
159
} ) ;
156
160
}
157
161
158
- if ( ! results . provisioning . distribution . length || ! valid . distribution ) {
162
+ if ( results . provisioning . distribution . length > 0 && ! valid . distribution ) {
159
163
results . issues . push ( {
160
164
id : 'IOS_NO_VALID_DISTRIBUTION_PROVISIONING_PROFILES' ,
161
165
type : 'warning' ,
@@ -245,16 +249,16 @@ function detect(options, callback) {
245
249
}
246
250
}
247
251
248
- fs . exists ( profileDir , function ( exists ) {
249
- exists && fs . readdirSync ( profileDir ) . forEach ( function ( name ) {
252
+ for ( const profileDir of profileDirs ) {
253
+ fs . readdirSync ( profileDir ) . forEach ( function ( name ) {
250
254
ppRegExp . test ( name ) && parseProfile ( path . join ( profileDir , name ) ) ;
251
255
} ) ;
256
+ }
252
257
253
- detectIssues ( ) ;
254
- cache = results ;
255
- emitter . emit ( 'detected' , results ) ;
256
- return callback ( null , results ) ;
257
- } ) ;
258
+ detectIssues ( ) ;
259
+ cache = results ;
260
+ emitter . emit ( 'detected' , results ) ;
261
+ return callback ( null , results ) ;
258
262
} ) ;
259
263
} ;
260
264
@@ -332,7 +336,7 @@ function find(options, callback) {
332
336
* Watches a provisioning profile directory for file changes.
333
337
*
334
338
* @param {Object } [options] - An object containing various settings.
335
- * @param {String } [options.profileDir=~/Library/MobileDevice /Provisioning Profiles] - The path to search for provisioning profiles.
339
+ * @param {String } [options.profileDir=~/Library/Developer/Xcode/UserData /Provisioning Profiles] - The path to search for provisioning profiles.
336
340
* @param {Function } [callback(err, results)] - A function to call with the provisioning profile information.
337
341
*
338
342
* @returns {Function } A function that unwatches changes.
@@ -358,19 +362,46 @@ function watch(options, callback) {
358
362
/**
359
363
* Stops watching the specified provisioning profile directory.
360
364
*
361
- * @param {String } [profileDir=~/Library/MobileDevice /Provisioning Profiles] - The path to the provisioning profile directory.
365
+ * @param {String } [profileDir=~/Library/Developer/Xcode/UserData /Provisioning Profiles] - The path to the provisioning profile directory.
362
366
*/
363
367
function unwatch ( profileDir ) {
364
- var profileDir = appc . fs . resolvePath ( profileDir || defaultProfileDir ) ;
368
+ var profileDirs = getExistingProvisioningProfileDirectories ( profileDir ) ;
365
369
366
- if ( ! watchers [ profileDir ] ) return ;
370
+ for ( const profileDir of profileDirs ) {
371
+ if ( ! watchers [ profileDir ] ) continue ;
367
372
368
- if ( -- watchers [ profileDir ] . count <= 0 ) {
369
- watchers [ profileDir ] . handle . close ( ) ;
370
- delete watchers [ profileDir ] ;
373
+ if ( -- watchers [ profileDir ] . count <= 0 ) {
374
+ watchers [ profileDir ] . handle . close ( ) ;
375
+ delete watchers [ profileDir ] ;
376
+ }
371
377
}
372
378
} ;
373
379
380
+ /**
381
+ * Searches for existing provisioning profile directories.
382
+ *
383
+ * @throws
384
+ * @param {string | undefined } profileDir A custom directory set by the developer.
385
+ * @returns {string[] } The directories that exist on the filesystem.
386
+ */
387
+ function getExistingProvisioningProfileDirectories ( profileDir ) {
388
+ const profileDirectories = [ ] ;
389
+
390
+ for ( const directory of [ profileDir , ...provisioningProfilesDirectories ] ) {
391
+ if ( ! directory ) {
392
+ continue ;
393
+ }
394
+
395
+ const resolvedDirectory = appc . fs . resolvePath ( directory ) ;
396
+
397
+ if ( fs . existsSync ( resolvedDirectory ) ) {
398
+ profileDirectories . push ( resolvedDirectory ) ;
399
+ }
400
+ }
401
+
402
+ return profileDirectories ;
403
+ }
404
+
374
405
/*
375
406
* If the app exits, close all filesystem watchers.
376
407
*/
0 commit comments