-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
release.js
209 lines (177 loc) · 5.29 KB
/
release.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
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
/* eslint-disable no-console */
const fs = require('fs')
const path = require('path')
const AdmZip = require('adm-zip')
const EXTENSION_DIR = 'extension'
const MANIFEST_PATH = 'manifest.json'
const PACKAGE_PATH = 'package.json'
const args = process.argv.slice(2)
const releaseType = args[0]
const bumpVersion = (currentVersion, releaseType) => {
const [major, minor, patch] = currentVersion.split('.').map(Number)
switch (releaseType) {
case 'major':
return `${major + 1}.0.0`
case 'minor':
return `${major}.${minor + 1}.0`
case 'patch':
return `${major}.${minor}.${patch + 1}`
default:
throw new Error('Invalid release type')
}
}
const createZipWithContents = (zip, contentPath, zipName) => {
const addFolderContentsToZip = (folderPath, zipFolderPath = '') => {
const items = fs.readdirSync(folderPath)
if (items.length === 0) {
zip.addFile(zipFolderPath + '/', Buffer.alloc(0))
} else {
items.forEach((item) => {
const itemPath = folderPath + '/' + item
if (fs.statSync(itemPath).isDirectory()) {
addFolderContentsToZip(
itemPath,
zipFolderPath ? zipFolderPath + '/' + item : item,
)
} else if (itemPath.slice(-4).toLowerCase() !== '.zip') {
zip.addFile(
zipFolderPath ? zipFolderPath + '/' + item : item,
fs.readFileSync(itemPath),
)
}
})
}
}
addFolderContentsToZip(contentPath)
zip.writeZip(zipName)
fs.renameSync(zipName, EXTENSION_DIR + '/' + zipName)
}
const addAttributesToManifest = (attributes) => {
const manifestContent = JSON.parse(
fs.readFileSync(path.join(EXTENSION_DIR, MANIFEST_PATH), 'utf-8'),
)
Object.assign(manifestContent, attributes)
fs.writeFileSync(
path.join(EXTENSION_DIR, MANIFEST_PATH),
JSON.stringify(manifestContent, null, 2),
)
}
const removeAttributesFromManifest = (attributes) => {
const manifestContent = JSON.parse(
fs.readFileSync(path.join(EXTENSION_DIR, MANIFEST_PATH), 'utf-8'),
)
attributes.forEach((attribute) => {
delete manifestContent[attribute]
})
fs.writeFileSync(
path.join(EXTENSION_DIR, MANIFEST_PATH),
JSON.stringify(manifestContent, null, 2),
)
}
const modifyManifest = (attributesToAdd, attributesToRemove) => {
if (attributesToAdd && Object.keys(attributesToAdd).length > 0) {
addAttributesToManifest(attributesToAdd)
}
if (attributesToRemove && attributesToRemove.length > 0) {
removeAttributesFromManifest(attributesToRemove)
}
}
const processReleaseType = (releaseType) => {
const validReleaseTypes = ['major', 'minor', 'patch']
if (!validReleaseTypes.includes(releaseType)) {
console.log(
"Error: Invalid release type. Use 'major', 'minor', or 'patch'.",
)
process.exit(1)
}
const manifestContent = JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf-8'))
const packageContent = JSON.parse(fs.readFileSync(PACKAGE_PATH, 'utf-8'))
const newVersion = bumpVersion(manifestContent.version, releaseType)
manifestContent.version = newVersion
packageContent.version = newVersion
fs.writeFileSync(MANIFEST_PATH, JSON.stringify(manifestContent, null, 2))
fs.writeFileSync(PACKAGE_PATH, JSON.stringify(packageContent, null, 2))
console.log(
`Version in manifest.json and package.json updated to: ${newVersion}`,
)
modifyManifest({ version: newVersion }, [])
processZipCreation(EXTENSION_DIR, newVersion, '')
modifyManifest(
{
browser_specific_settings: {
gecko: {
id: '{9282bc49-b1b4-4f46-b135-1dfe00f182c9}',
},
},
},
['background'],
)
processZipCreation(EXTENSION_DIR, newVersion, '-firefox')
modifyManifest(
{
background: {
service_worker: 'background.js',
},
},
['browser_specific_settings'],
)
packageSource()
process.exit(0)
}
const processZipCreation = (contentPath, newVersion, fileNameSuffix) => {
const zip = new AdmZip()
createZipWithContents(
zip,
contentPath,
`weather-please-${newVersion}${fileNameSuffix}.zip`,
)
}
const packageSource = () => {
const zip = new AdmZip()
// function to recursively add files and folders to the zip
const addContentToZip = (dir, zipDir) => {
const entries = fs.readdirSync(dir)
entries.forEach((entry) => {
const fullPath = dir + '/' + entry
const zipPath = zipDir + '/' + entry
if (fs.statSync(fullPath).isDirectory()) {
// add the directory itself (for empty folders too)
zip.addFile(zipPath + '/', Buffer.alloc(0))
// recursively add its content
addContentToZip(fullPath, zipPath)
} else if (
!fullPath.includes('extension') &&
fullPath.slice(-4) !== '.zip'
) {
zip.addLocalFile(fullPath, zipDir)
}
})
}
// add all FILES from the root directory
fs.readdirSync('./').forEach((file) => {
const fullPath = './' + file
if (
fs.statSync(fullPath).isFile() &&
fullPath.slice(-4) !== '.zip' &&
fullPath !== './.sentryclirc' &&
fullPath !== './.env.local'
) {
zip.addLocalFile(fullPath)
}
})
// add 'src' and 'public' folders, preserving their structure
addContentToZip('./src', 'src')
addContentToZip('./public', 'public')
const zipFileName = 'src.zip'
zip.writeZip(zipFileName)
fs.renameSync(zipFileName, EXTENSION_DIR + '/' + zipFileName)
}
if (!releaseType) {
console.log("Enter the release type ('major', 'minor', or 'patch'): ")
process.stdin.once('data', (data) => {
const input = data.toString().trim()
processReleaseType(input)
})
} else {
processReleaseType(releaseType)
}