2020 fs = require ( 'fs' ) ,
2121 path = require ( 'path' ) ,
2222 __ = appc . i18n ( __dirname ) . __ ,
23- defaultProfileDir = '~/Library/MobileDevice/Provisioning Profiles' ;
23+ provisioningProfilesDirectories = [
24+ '~/Library/Developer/Xcode/UserData/Provisioning Profiles' ,
25+ '~/Library/MobileDevice/Provisioning Profiles'
26+ ]
2427
2528var cache = null ,
2629 watchers = { } ;
@@ -37,7 +40,6 @@ var cache = null,
3740 * @type {Error }
3841 */
3942
40- exports . defaultProfileDir = defaultProfileDir ;
4143exports . detect = detect ;
4244exports . find = find ;
4345exports . watch = watch ;
@@ -48,7 +50,7 @@ exports.unwatch = unwatch;
4850 *
4951 * @param {Object } [options] - An object containing various settings.
5052 * @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.
5254 * @param {Boolean } [options.unmanaged] - When true, excludes managed provisioning profiles.
5355 * @param {Boolean } [options.validOnly=true] - When true, only returns non-expired, valid provisioning profiles.
5456 * @param {Boolean } [options.watch=false] - If true, watches the specified provisioning profile directory for updates.
@@ -63,10 +65,10 @@ function detect(options, callback) {
6365 return magik ( options , callback , function ( emitter , options , callback ) {
6466 var files = { } ,
6567 validOnly = options . validOnly === undefined || options . validOnly === true ,
66- profileDir = appc . fs . resolvePath ( options . profileDir || defaultProfileDir ) ,
68+ profileDirs = getExistingProvisioningProfileDirectories ( options . profileDir ) ,
6769 results = {
6870 provisioning : {
69- profileDir : profileDir ,
71+ profileDir : profileDirs [ 0 ] ,
7072 development : [ ] ,
7173 adhoc : [ ] ,
7274 enterprise : [ ] ,
@@ -82,51 +84,53 @@ function detect(options, callback) {
8284 } ,
8385
8486 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+
8688
8789 if ( options . watch ) {
8890 var throttleTimer = null ;
8991
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+ }
105112 } else {
106- // delete
107- removeProfile ( file ) ;
113+ // add
114+ parseProfile ( file ) ;
108115 }
109- } else {
110- // add
116+ } else if ( event === 'change' ) {
117+ // updated
111118 parseProfile ( file ) ;
112119 }
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 ++ ;
127133 }
128-
129- watchers [ profileDir ] . count ++ ;
130134 }
131135
132136 if ( cache && ! options . bypassCache ) {
@@ -137,7 +141,7 @@ function detect(options, callback) {
137141 function detectIssues ( ) {
138142 results . issues = [ ] ;
139143
140- if ( ! results . provisioning . development . length || ! valid . development ) {
144+ if ( results . provisioning . development . length > 0 && ! valid . development ) {
141145 results . issues . push ( {
142146 id : 'IOS_NO_VALID_DEVELOPMENT_PROVISIONING_PROFILES' ,
143147 type : 'warning' ,
@@ -146,7 +150,7 @@ function detect(options, callback) {
146150 } ) ;
147151 }
148152
149- if ( ! results . provisioning . adhoc . length || ! valid . adhoc ) {
153+ if ( results . provisioning . adhoc . length > 0 && ! valid . adhoc ) {
150154 results . issues . push ( {
151155 id : 'IOS_NO_VALID_ADHOC_PROVISIONING_PROFILES' ,
152156 type : 'warning' ,
@@ -155,7 +159,7 @@ function detect(options, callback) {
155159 } ) ;
156160 }
157161
158- if ( ! results . provisioning . distribution . length || ! valid . distribution ) {
162+ if ( results . provisioning . distribution . length > 0 && ! valid . distribution ) {
159163 results . issues . push ( {
160164 id : 'IOS_NO_VALID_DISTRIBUTION_PROVISIONING_PROFILES' ,
161165 type : 'warning' ,
@@ -245,16 +249,16 @@ function detect(options, callback) {
245249 }
246250 }
247251
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 ) {
250254 ppRegExp . test ( name ) && parseProfile ( path . join ( profileDir , name ) ) ;
251255 } ) ;
256+ }
252257
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 ) ;
258262 } ) ;
259263} ;
260264
@@ -332,7 +336,7 @@ function find(options, callback) {
332336 * Watches a provisioning profile directory for file changes.
333337 *
334338 * @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.
336340 * @param {Function } [callback(err, results)] - A function to call with the provisioning profile information.
337341 *
338342 * @returns {Function } A function that unwatches changes.
@@ -358,19 +362,46 @@ function watch(options, callback) {
358362/**
359363 * Stops watching the specified provisioning profile directory.
360364 *
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.
362366 */
363367function unwatch ( profileDir ) {
364- var profileDir = appc . fs . resolvePath ( profileDir || defaultProfileDir ) ;
368+ var profileDirs = getExistingProvisioningProfileDirectories ( profileDir ) ;
365369
366- if ( ! watchers [ profileDir ] ) return ;
370+ for ( const profileDir of profileDirs ) {
371+ if ( ! watchers [ profileDir ] ) continue ;
367372
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+ }
371377 }
372378} ;
373379
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+
374405/*
375406 * If the app exits, close all filesystem watchers.
376407 */
0 commit comments