forked from fredericksilva/rem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PodfileLoader.js
82 lines (72 loc) · 2.13 KB
/
PodfileLoader.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
'use strict';
require('instapromise');
const child_process = require('child_process');
const path = require('path');
const fs = require('fs');
class PodfileLoader {
constructor(settings: Settings) {
this.settings = settings;
}
/**
* Reads the Podfile in the given directory, creating one if it doesn't exist,
* and returns its contents
*/
async readEnsuredAsync(): string {
let directory = this.settings.xcodeProjectDirectory;
var contents = await this.readAsync(directory);
if (contents) {
return contents;
}
await this.createAsync(directory);
contents = this.readAsync(directory);
if (!contents) {
throw new Error('Created a Podfile but was unable to read its contents');
}
return contents;
}
/**
* Reads the Podfile in the given directory and returns its contents, or null
* if it doesn't exist
*/
async readAsync(): ?string {
let directory = this.settings.xcodeProjectDirectory;
let podfilePath = path.join(directory, 'Podfile');
try {
return await fs.promise.readFile(podfilePath, 'utf8');
} catch (error) {
if (error.code === 'ENOENT') {
return null;
}
throw error;
}
}
/**
* Writes the Podfile in the given directory.
*/
async writeAsync(contents: string) {
let directory = this.settings.xcodeProjectDirectory;
let podfilePath = path.join(directory, 'Podfile');
await fs.promise.writeFile(podfilePath, contents, 'utf8');
}
/**
* Creates a Podfile in the given directory with the help of `pod`
*/
createAsync(): ?string {
return new Promise((resolve, reject) => {
let directory = this.settings.xcodeProjectDirectory;
let options = {cwd: directory};
child_process.exec('pod init', options, (processError, stdout, stderr) => {
if (processError) {
let error = new Error('Could not create Podfile:\n' + stdout);
error.cause = processError;
error.stdout = stdout;
error.stderr = stderr;
reject(error);
} else {
resolve({stdout, stderr});
}
});
});
}
}
export default PodfileLoader;