Repository navigation
Expand file tree
/
Copy pathsetup.js
More file actions
151 lines (129 loc) · 6.8 KB
/
Copy pathsetup.js
File metadata and controls
151 lines (129 loc) · 6.8 KB
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
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import readline from 'node:readline';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const question = (query) => new Promise((resolve) => rl.question(query, resolve));
async function main() {
console.log('\n🚀 Calurcap Setup Wizard\n');
console.log('This script will update your project configuration.\n');
const appName = await question('Enter App Name (e.g., My App): ') || 'Calurcap';
const appId = await question('Enter App ID (e.g., com.example.app): ') || 'com.avarnic.calurcap';
const pocketbaseUrl = await question('Enter PocketBase URL (optional): ');
console.log('\nUpdating files...\n');
// 1. Update package.json
const packageJsonPath = path.join(process.cwd(), 'package.json');
if (fs.existsSync(packageJsonPath)) {
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
pkg.name = appName.toLowerCase().replace(/\s+/g, '-');
fs.writeFileSync(packageJsonPath, JSON.stringify(pkg, null, '\t'));
console.log('✅ Updated package.json');
}
// 2. Update capacitor.config.ts
const capConfigPath = path.join(process.cwd(), 'capacitor.config.ts');
if (fs.existsSync(capConfigPath)) {
let content = fs.readFileSync(capConfigPath, 'utf-8');
content = content.replace(/appId: '.*'/, `appId: '${appId}'`);
content = content.replace(/appName: '.*'/, `appName: '${appName}'`);
fs.writeFileSync(capConfigPath, content);
console.log('✅ Updated capacitor.config.ts');
}
// 3. Update android/app/build.gradle
const gradlePath = path.join(process.cwd(), 'android/app/build.gradle');
if (fs.existsSync(gradlePath)) {
let content = fs.readFileSync(gradlePath, 'utf-8');
content = content.replace(/namespace ".*"/, `namespace "${appId}"`);
content = content.replace(/applicationId ".*"/, `applicationId "${appId}"`);
fs.writeFileSync(gradlePath, content);
console.log('✅ Updated android/app/build.gradle');
}
if (pocketbaseUrl) {
const envExamplePath = path.join(process.cwd(), '.env.example');
const envPath = path.join(process.cwd(), '.env');
const target = fs.existsSync(envPath) ? envPath : envExamplePath;
if (fs.existsSync(target)) {
let content = fs.readFileSync(target, 'utf-8');
if (content.includes('VITE_POCKETBASE_URL=')) {
content = content.replace(/VITE_POCKETBASE_URL=.*/, `VITE_POCKETBASE_URL=${pocketbaseUrl}`);
} else {
content += `\nVITE_POCKETBASE_URL=${pocketbaseUrl}\n`;
}
fs.writeFileSync(target, content);
console.log(`✅ Updated ${path.basename(target)}`);
}
}
// 5. Update android/app/src/main/res/values/strings.xml
const stringsXmlPath = path.join(process.cwd(), 'android/app/src/main/res/values/strings.xml');
if (fs.existsSync(stringsXmlPath)) {
let content = fs.readFileSync(stringsXmlPath, 'utf-8');
content = content.replace(/<string name="app_name">.*<\/string>/, `<string name="app_name">${appName}</string>`);
content = content.replace(/<string name="title_activity_main">.*<\/string>/, `<string name="title_activity_main">${appName}</string>`);
content = content.replace(/<string name="package_name">.*<\/string>/, `<string name="package_name">${appId}</string>`);
content = content.replace(/<string name="custom_url_scheme">.*<\/string>/, `<string name="custom_url_scheme">${appId}</string>`);
fs.writeFileSync(stringsXmlPath, content);
console.log('✅ Updated android/app/src/main/res/values/strings.xml');
}
// 5.5 Update package-lock.json
const packageLockJsonPath = path.join(process.cwd(), 'package-lock.json');
if (fs.existsSync(packageLockJsonPath)) {
const pkgLock = JSON.parse(fs.readFileSync(packageLockJsonPath, 'utf-8'));
pkgLock.name = appName.toLowerCase().replace(/\s+/g, '-');
if (pkgLock.packages && pkgLock.packages[""]) {
pkgLock.packages[""].name = pkgLock.name;
}
fs.writeFileSync(packageLockJsonPath, JSON.stringify(pkgLock, null, '\t'));
console.log('✅ Updated package-lock.json');
}
// 6. Update src/routes/+page.svelte
const pageSveltePath = path.join(process.cwd(), 'src/routes/+page.svelte');
if (fs.existsSync(pageSveltePath)) {
let content = fs.readFileSync(pageSveltePath, 'utf-8');
// Replace "Calurcap." in the H1
content = content.replace(/Calurcap\./g, `${appName}.`);
// Replace "Calurcap" in the title/meta if present (or just generic replacements)
content = content.replace(/>\s*Calurcap\s*</g, `>${appName}<`);
fs.writeFileSync(pageSveltePath, content);
console.log('✅ Updated src/routes/+page.svelte');
}
// 7. Update MainActivity.java and move it
// Current path is hardcoded to com/avarnic/calurcap, we need to find it or assume it's there
const javaBasePath = path.join(process.cwd(), 'android/app/src/main/java');
// We need to find the current MainActivity.java to know where it is
// A simple recursive search or just assuming the default structure if it hasn't changed
// For this template, we assume it starts at com/avarnic/calurcap
const oldPackagePath = path.join(javaBasePath, 'com', 'avarnic', 'calurcap');
const oldActivityPath = path.join(oldPackagePath, 'MainActivity.java');
if (fs.existsSync(oldActivityPath)) {
let content = fs.readFileSync(oldActivityPath, 'utf-8');
content = content.replace(/package com\.avarnic\.calurcap;/, `package ${appId};`);
// Create new directory structure
const newPackagePath = path.join(javaBasePath, ...appId.split('.'));
fs.mkdirSync(newPackagePath, { recursive: true });
const newActivityPath = path.join(newPackagePath, 'MainActivity.java');
fs.writeFileSync(newActivityPath, content);
console.log(`✅ Moved MainActivity.java to ${newActivityPath}`);
// Optional: Clean up old empty directories if they are empty
// This is a bit risky if we delete something we shouldn't, so maybe just leave them or try to delete specific ones
// For now, let's just leave the old file? No, that causes duplicate class errors. We must delete the old file.
fs.unlinkSync(oldActivityPath);
// Try to remove empty parent directories
try {
if (fs.readdirSync(oldPackagePath).length === 0) fs.rmdirSync(oldPackagePath);
const parent = path.dirname(oldPackagePath);
if (fs.readdirSync(parent).length === 0) fs.rmdirSync(parent);
} catch (e) {
// Ignore errors deleting directories
}
} else {
console.log('⚠️ Could not find MainActivity.java at default location. Skipping package move.');
}
console.log('\n✨ Setup complete! You may need to run "npx cap sync" to apply changes to the Android project.\n');
rl.close();
}
main();