-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathbuild.js
More file actions
159 lines (140 loc) · 4.64 KB
/
build.js
File metadata and controls
159 lines (140 loc) · 4.64 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
152
153
154
155
156
157
158
159
const fs = require("fs");
const path = require("path");
const { spawn } = require("child_process");
const inquirer = require("inquirer");
const APP_JSON_PATH = path.join(process.cwd(), "app.json");
const EAS_JSON_PATH = path.join(process.cwd(), "eas.json");
// Function to validate semver
function isValidSemver(version) {
const semverRegex = /^\d+\.\d+\.\d+$/;
return semverRegex.test(version);
}
// Function to run EAS build command
function runEasBuild(platform, profile, local) {
const command = `eas build --platform ${platform} --profile ${profile}${local ? " --local" : ""}`;
console.log(`\nExecuting: ${command}\n`);
const build = spawn(
"eas",
["build", "--platform", platform, "--profile", profile, ...(local ? ["--local"] : [])],
{
stdio: "inherit",
shell: true,
},
);
build.on("error", (error) => {
console.error("Failed to start EAS build:", error);
});
}
async function main() {
try {
// Check if required files exist
if (!fs.existsSync(APP_JSON_PATH)) {
console.error("Error: app.json not found");
process.exit(1);
}
if (!fs.existsSync(EAS_JSON_PATH)) {
console.error("Error: eas.json not found");
process.exit(1);
}
// Read build profiles from eas.json
let easConfig;
try {
const easJsonContent = fs.readFileSync(EAS_JSON_PATH, "utf8");
console.log("EAS JSON content:", easJsonContent.substring(0, 100) + "...");
easConfig = JSON.parse(easJsonContent);
} catch (error) {
console.error("Error parsing eas.json:", error);
console.error("Error position:", error.message);
process.exit(1);
}
const buildProfiles = Object.keys(easConfig.build || {});
if (buildProfiles.length === 0) {
console.error("Error: No build profiles found in eas.json");
process.exit(1);
}
// Get user inputs
const answers = await inquirer.prompt([
{
type: "list",
name: "platform",
message: "Select platform:",
choices: ["android", "ios"],
},
{
type: "list",
name: "profile",
message: "Select build profile:",
choices: buildProfiles,
},
{
type: "confirm",
name: "local",
message: "Build locally?",
default: false,
},
]);
// Handle version update
let appJson;
try {
const appJsonContent = fs.readFileSync(APP_JSON_PATH, "utf8");
console.log("APP JSON content:", appJsonContent.substring(0, 100) + "...");
appJson = JSON.parse(appJsonContent);
} catch (error) {
console.error("Error parsing app.json:", error);
console.error("Error position:", error.message);
process.exit(1);
}
const currentVersion = appJson.expo.version;
const versionAnswer = await inquirer.prompt([
{
type: "input",
name: "version",
message: `Enter version to use (current: ${currentVersion}):`,
default: currentVersion,
validate: (input) => {
if (!isValidSemver(input)) {
return "Version must follow semver format (e.g., 1.0.0)";
}
return true;
},
},
]);
const newVersion = versionAnswer.version;
appJson.expo.version = newVersion;
// Update build number/version code
if (answers.platform === "ios") {
const currentBuild = parseInt(appJson.expo.ios.buildNumber);
appJson.expo.ios.buildNumber = String(currentBuild + 1);
console.log(
`iOS: Updated version to ${newVersion} and build number to ${appJson.expo.ios.buildNumber}`,
);
} else {
const currentCode = appJson.expo.android.versionCode;
appJson.expo.android.versionCode = currentCode + 1;
console.log(
`Android: Updated version to ${newVersion} and version code to ${appJson.expo.android.versionCode}`,
);
}
// Write back to app.json
try {
const jsonString = JSON.stringify(appJson, null, 2);
fs.writeFileSync(APP_JSON_PATH, jsonString);
// Verify the written JSON is valid
const verifyContent = fs.readFileSync(APP_JSON_PATH, "utf8");
JSON.parse(verifyContent); // This will throw if invalid
console.log("Successfully wrote and verified app.json");
} catch (error) {
console.error("Error writing or verifying app.json:", error);
console.error("Error details:", error.message);
process.exit(1);
}
// Run EAS build
runEasBuild(answers.platform, answers.profile, answers.local);
} catch (error) {
console.error("Error:", error);
console.error("Error message:", error.message);
console.error("Error stack:", error.stack);
process.exit(1);
}
}
main();