forked from nachaphon-phontree/UpdatesAPIDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush-update.js
More file actions
113 lines (100 loc) · 3.56 KB
/
push-update.js
File metadata and controls
113 lines (100 loc) · 3.56 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
const path = require("path")
const fs = require("fs/promises")
const spawnAsync = require("@expo/spawn-async")
const usage = () => {
console.log("Usage: push-update.js ")
console.log(" Parameters:")
console.log(
" <--message|-m> (message) (required) Sets the message passed into the EAS update command",
)
console.log(
" <--critical|-c> (optional) If present, increments the criticalIndex counter to mark this update as critical",
)
console.log(
" <--breakTheApp|-b> (optional) If present, introduces a bug in App.tsx that will cause a crash",
)
console.log(
" <--channel|-ch> (optional) Sets the channel passed into the EAS update command (defaults to main)",
)
}
const incrementCriticalIndexIfNeeded = async (critical, projectRoot) => {
const criticalIndexPath = path.resolve(projectRoot, ".criticalIndex")
let criticalIndex = 0
try {
const criticalIndexText = await fs.readFile(criticalIndexPath, { encoding: "utf-8" })
criticalIndex = parseInt(criticalIndexText, 10)
} catch (_error) {}
const updatedCriticalIndex = critical ? criticalIndex + 1 : criticalIndex
await fs.writeFile(criticalIndexPath, `${updatedCriticalIndex}`, { encoding: "utf-8" })
return updatedCriticalIndex
}
const pushUpdateAsync = async (message, critical, breakTheApp, channel, projectRoot) => {
console.log("Modifying app.json...")
const appJsonPath = path.resolve(projectRoot, "app.json")
const appJsonOriginalText = await fs.readFile(appJsonPath, { encoding: "utf-8" })
const appJsonOriginal = JSON.parse(appJsonOriginalText)
const criticalIndex = await incrementCriticalIndexIfNeeded(critical, projectRoot)
const appJson = {
expo: {
...appJsonOriginal.expo,
extra: {
...appJsonOriginal.expo.extra,
message,
criticalIndex,
},
},
}
const appJsonText = JSON.stringify(appJson, null, 2)
await fs.rm(appJsonPath)
await fs.writeFile(appJsonPath, appJsonText, { encoding: "utf-8" })
const appTsxPath = path.resolve(projectRoot, "App.tsx")
const appTsxOriginalText = await fs.readFile(appTsxPath, { encoding: "utf-8" })
if (breakTheApp) {
const appTsxText = appTsxOriginalText.replace("<App", "<Bogus")
await fs.rm(appTsxPath)
await fs.writeFile(appTsxPath, appTsxText, { encoding: "utf-8" })
}
console.log("Publishing update...")
await spawnAsync("eas", ["update", `--message=${message}`, `--channel=${channel}`], {
stdio: "inherit",
path: projectRoot,
})
console.log("Restoring original App.tsx...")
await fs.rm(appTsxPath)
await fs.writeFile(appTsxPath, appTsxOriginalText, { encoding: "utf-8" })
console.log("Done.")
}
const params = process.argv.filter((a, i) => i > 0)
const projectRoot = path.resolve(__dirname, "..")
let message = ""
let critical = false
let breakTheApp = false
let channel = "main"
while (params.length) {
if (params[0] === "--message" || params[0] === "-m") {
message = params[1]
params.shift()
}
if (params[0] === "--critical" || params[0] === "-c") {
critical = true
}
if (params[0] === "--breakTheApp" || params[0] === "-b") {
breakTheApp = true
}
if (params[0] === "--channel" || params[0] === "-ch") {
channel = params[1]
params.shift()
}
params.shift()
}
if (message.length === 0) {
usage()
process.exit(0)
}
console.log(`message = ${message}`)
console.log(`critical = ${critical}`)
console.log(`breakTheApp = ${breakTheApp}`)
console.log(`channel = ${channel}`)
pushUpdateAsync(message, critical, breakTheApp, channel, projectRoot).catch((error) =>
console.log(`Error in script: ${error}`),
)