-
Notifications
You must be signed in to change notification settings - Fork 3
212 lines (194 loc) Β· 9.99 KB
/
build.yml
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
name: Build
on:
push:
branches: [ main ]
schedule:
- cron: '0 0 * * *'
jobs:
build:
runs-on: windows-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
path: top-user-agents
- name: Build
shell: bash
run: |
cd top-user-agents
# Configure git to be more predictable and deterministic
git config core.autocrlf false
git config core.ignorecase false
git config core.fscache true
git config core.longpaths true
git config diff.renameLimit 0
git config status.renameLimit 0
git config merge.renameLimit 0
git config http.lowSpeedLimit 0
git config http.lowSpeedTime 300
git config http.postBuffer 1048576000
git config pack.threads 1
git config index.threads 0
# Check top-user-agents version on npm
export top_user_agents_version=$(curl -sS 'https://registry.npmjs.org/top-user-agents' | python -c "import sys, json; print(json.load(sys.stdin)['dist-tags']['latest'])")
if [ -f "package.json" ] && [ $top_user_agents_version == $(cat package.json | python -c "import sys, json; print(json.load(sys.stdin)['version'])") ]; then
# Install using npm 6
# Later versions fail to create a sane dependency tree in some situations
export npm_version=$(curl -sS 'https://registry.npmjs.org/npm' | python -c "import sys, json; print(json.load(sys.stdin)['dist-tags']['latest-6'])")
curl -sSo "npm-$npm_version.tgz" "https://registry.npmjs.org/npm/-/npm-$npm_version.tgz"
mkdir -p "bin/npm"
tar -xzf "npm-$npm_version.tgz" --strip-components=1 -C "bin/npm"
rm "npm-$npm_version.tgz"
# Install dependencies
node bin/npm/bin/npm-cli.js install --no-audit --no-bin-links --no-fund --ignore-scripts --no-optional "electron" "json-stringify-nice"
cd node_modules/electron
node install.js
cd ~-
# Update user agents
cat << 'EOF' > script.js
if(process.versions.electron) {
console.log('Running on Electron ' + process.versions.electron + ' + Node ' + process.versions.node)
const electronUserAgent = require('./index.json')[0]
const fs = require('fs')
const { app, BrowserWindow } = require('electron')
const stringify = require('json-stringify-nice')
app.whenReady().then(() => {
const win = new BrowserWindow({
width: 1920,
height: 969,
frame: false,
show: false,
webPreferences: {
backgroundThrottling: false
}
})
win.loadURL('https://techblog.willshouse.com/2012/01/03/most-common-user-agents/', {userAgent: electronUserAgent})
win.webContents.on('did-finish-load', () => {
win.webContents.executeJavaScript(`[...document.querySelectorAll('tbody .useragent')].map(e => e.innerText)`).then((result) => {
if(result.length > 0) {
fs.writeFileSync('./index.json', stringify(result), 'utf-8')
app.exit()
}
})
})
})
}
else {
console.log('Running on Node ' + process.versions.node)
const electron = require('electron')
const { spawn } = require('child_process')
spawn(electron, ['--use_strict', 'script.js'], { stdio: 'inherit' })
}
EOF
node --use_strict script.js
rm script.js
# Remove npm
rm -r bin/
git checkout -- package.json
rm package-lock.json
rm -r node_modules/
# Push to GitHub
git add -f index.json
git -c user.name="GitHub" -c user.email="noreply@github.com" commit --author="github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>" -m"Update user agents" | sed -n 1p
git tag -f "$top_user_agents_version"
git push
git push -f origin "refs/tags/$top_user_agents_version:refs/tags/$top_user_agents_version"
exit 0
fi
# Roll back the repository
git reset --hard $(git rev-list --max-parents=0 HEAD)
git clean -dffx
# Install using npm 6
# Later versions fail to create a sane dependency tree in some situations
export npm_version=$(curl -sS 'https://registry.npmjs.org/npm' | python -c "import sys, json; print(json.load(sys.stdin)['dist-tags']['latest-6'])")
curl -sSo "npm-$npm_version.tgz" "https://registry.npmjs.org/npm/-/npm-$npm_version.tgz"
mkdir -p "bin/npm"
tar -xzf "npm-$npm_version.tgz" --strip-components=1 -C "bin/npm"
rm "npm-$npm_version.tgz"
# Initialize dummy package
node bin/npm/bin/npm-cli.js init -y
sed -i 's/"name": "top-user-agents"/"name": "npm"/' -- 'package.json'
# Install top-user-agents as a dependency
node bin/npm/bin/npm-cli.js install --no-audit --no-bin-links --no-fund --ignore-scripts --no-optional "electron" "json-stringify-nice" "top-user-agents@$top_user_agents_version"
cd node_modules/electron
node install.js
cd ~-
# Update user agents
cat << 'EOF' > script.js
if(process.versions.electron) {
console.log('Running on Electron ' + process.versions.electron + ' + Node ' + process.versions.node)
const electronUserAgent = require('top-user-agents')[0]
const fs = require('fs')
const { app, BrowserWindow } = require('electron')
const stringify = require('json-stringify-nice')
app.whenReady().then(() => {
const win = new BrowserWindow({
width: 1920,
height: 969,
frame: false,
show: false,
webPreferences: {
backgroundThrottling: false
}
})
win.loadURL('https://techblog.willshouse.com/2012/01/03/most-common-user-agents/', {userAgent: electronUserAgent})
win.webContents.on('did-finish-load', () => {
win.webContents.executeJavaScript(`[...document.querySelectorAll('tbody .useragent')].map(e => e.innerText)`).then((result) => {
if(result.length > 0) {
fs.writeFileSync('./node_modules/top-user-agents/index.json', stringify(result), 'utf-8')
app.exit()
}
})
})
})
}
else {
console.log('Running on Node ' + process.versions.node)
const electron = require('electron')
const { spawn } = require('child_process')
spawn(electron, ['--use_strict', 'script.js'], { stdio: 'inherit' })
}
EOF
node --use_strict script.js
rm script.js
# Remove npm
rm -r bin/
rm package.json
rm package-lock.json
# Delete install script since it doesn't function without dependencies
rm -r node_modules/top-user-agents/scripts/
# Modify package.json to match the top-user-agents tarball from npm
sed -i -z 's|\n "_phantomChildren": {\n "[a-zA-Z0-9\n -"'\''-*,./:@_~-]*\n },\n|\n|' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_requested": {/,/}/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_requiredBy": \[/,/\]/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"scripts": {/,/}/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"dependencies": {/,/}/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"devDependencies": {/,/}/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"engines": {/,/}/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"files": \[/,/\]/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"commitlint": {/,/}/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"nano-staged": {/,/}/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"simple-git-hooks": {/,/}/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_from":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_id":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_inBundle":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_integrity":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_location":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_phantomChildren":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_resolved":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_shasum":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_spec":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"_where":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"bundleDependencies":/d' -- 'node_modules/top-user-agents/package.json'
sed -i '/"deprecated":/d' -- 'node_modules/top-user-agents/package.json'
# Add top-user-agents to the repository
rm -rf node_modules/top-user-agents/node_modules/
cp -r node_modules/top-user-agents ..
rm -r node_modules/
git add -f .
# Push to GitHub
git -c user.name="GitHub" -c user.email="noreply@github.com" commit --author="github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>" -m"$top_user_agents_version" | sed -n 1p
git tag -f "$top_user_agents_version"
git push --force
git push -f origin "refs/tags/$top_user_agents_version:refs/tags/$top_user_agents_version"