-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-remote-ios-bindings.js
83 lines (71 loc) · 2.71 KB
/
setup-remote-ios-bindings.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
const fs = require('fs').promises;
const path = require('path');
const simpleGit = require('simple-git');
// Configuration
const repoOwner = 'pubky';
const repoName = 'pubky-core-ffi';
const branch = 'main';
const frameworkPath = 'bindings/ios/PubkyCore.xcframework';
const frameworkDestinationPath = 'ios/Frameworks';
const swiftFilePath = 'bindings/ios/pubkycore.swift';
const swiftDestinationPath = 'ios/';
const tempDir = 'temp';
async function runSetup() {
try {
console.log('Removing existing files...');
// Remove destination directories if they exist & Clean up any lingering temporary directory
await Promise.all([
fs.rm(frameworkDestinationPath, { recursive: true, force: true }),
fs.rm('ios/pubkycore.swift', { recursive: true, force: true }),
fs.rm(tempDir, { recursive: true, force: true }),
]);
console.log('Creating directories...');
// Create destination directories if they don't exist
await Promise.all([
fs.mkdir(frameworkDestinationPath, { recursive: true }),
fs.mkdir(swiftDestinationPath, { recursive: true }),
]);
// Initialize Git
const git = simpleGit();
// Clone the repository sparsely
await git.clone(
`https://github.com/${repoOwner}/${repoName}.git`,
tempDir,
['--depth', '1', '--filter=blob:none', '--sparse', `--branch=${branch}`]
);
// Change directory to the cloned repository
const tempGit = simpleGit(tempDir);
// Set sparse-checkout to include only the required directory
await tempGit.raw(['sparse-checkout', 'set', 'bindings/ios']);
// Copy framework to destination
const frameworkSourcePath = path.join(tempDir, frameworkPath);
const frameworkTargetPath = path.join(
frameworkDestinationPath,
path.basename(frameworkPath)
);
await fs.cp(frameworkSourcePath, frameworkTargetPath, { recursive: true });
// Copy Swift file to destination
const swiftSourcePath = path.join(tempDir, swiftFilePath);
const swiftTargetPath = path.join(
swiftDestinationPath,
path.basename(swiftFilePath)
);
await fs.copyFile(swiftSourcePath, swiftTargetPath);
// Clean up temporary directory
await fs.rm(tempDir, { recursive: true, force: true });
console.log('Framework and Swift file downloaded and copied successfully!');
} catch (error) {
console.error('Error during setup:', error);
// Try to clean up temp directory if it exists
try {
await fs.rm(tempDir, { recursive: true, force: true });
} catch (cleanupError) {
console.error('Failed to clean up temporary directory:', cleanupError);
}
process.exit(1);
}
}
runSetup().catch((error) => {
console.error('Unhandled error:', error);
process.exit(1);
});