forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-copyright.mjs
More file actions
246 lines (204 loc) · 6.21 KB
/
Copy pathadd-copyright.mjs
File metadata and controls
246 lines (204 loc) · 6.21 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import fs from 'node:fs/promises'
import path from 'node:path'
const TARGET_DIRS = ['src', 'scripts']
const SOURCE_EXTENSIONS = new Set([
'.cjs',
'.css',
'.js',
'.jsx',
'.mjs',
'.scss',
'.ts',
'.tsx',
])
const EXCLUDED_DIRS = new Set([
'.git',
'.rsbuild',
'.turbo',
'build',
'coverage',
'dist',
'node_modules',
])
const GENERATED_FILE_MARKERS = [
'This file was automatically generated',
'This file is auto-generated',
'This file is generated',
'DO NOT EDIT',
'You should NOT make any changes in this file',
]
const COPYRIGHT_HEADER = `/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
`
const PROJECT_COPYRIGHT_BLOCK_PATTERN =
/^\/\*\r?\nCopyright \(C\) .+? QuantumNous\r?\n[\s\S]*?For commercial licensing, please contact support@quantumnous\.com\r?\n\*\/\r?\n?/
const THIRD_PARTY_COPYRIGHT_PATTERN =
/^\/\*[\s\S]*?Copyright[\s\S]*?\*\/\r?\n?/i
const checkMode = process.argv.includes('--check')
function isGeneratedFile(filePath) {
return path.basename(filePath).includes('.gen.')
}
function hasGeneratedMarker(text) {
return GENERATED_FILE_MARKERS.some((marker) => text.includes(marker))
}
function hasThirdPartyCopyright(text) {
return (
THIRD_PARTY_COPYRIGHT_PATTERN.test(text) &&
!PROJECT_COPYRIGHT_BLOCK_PATTERN.test(text)
)
}
async function collectSourceFiles(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true })
const files = []
for (const entry of entries) {
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
if (!EXCLUDED_DIRS.has(entry.name)) {
files.push(...(await collectSourceFiles(fullPath)))
}
continue
}
if (
entry.isFile() &&
SOURCE_EXTENSIONS.has(path.extname(entry.name)) &&
!isGeneratedFile(fullPath)
) {
files.push(fullPath)
}
}
return files
}
async function collectTargetFiles(rootDir) {
const files = []
for (const targetDir of TARGET_DIRS) {
const fullPath = path.join(rootDir, targetDir)
try {
const stat = await fs.stat(fullPath)
if (stat.isDirectory()) {
files.push(...(await collectSourceFiles(fullPath)))
}
} catch (error) {
if (error.code !== 'ENOENT') {
throw error
}
}
}
return files.sort()
}
function splitShebang(text) {
if (!text.startsWith('#!')) {
return ['', text]
}
const lineEnd = text.indexOf('\n')
if (lineEnd === -1) {
return [text, '']
}
return [text.slice(0, lineEnd + 1), text.slice(lineEnd + 1)]
}
function applyHeader(text) {
const newline = text.includes('\r\n') ? '\r\n' : '\n'
const header = COPYRIGHT_HEADER.replaceAll('\n', newline)
const [shebang, body] = splitShebang(text)
const hadHeader = PROJECT_COPYRIGHT_BLOCK_PATTERN.test(body)
let strippedBody = body
while (PROJECT_COPYRIGHT_BLOCK_PATTERN.test(strippedBody)) {
strippedBody = strippedBody
.replace(PROJECT_COPYRIGHT_BLOCK_PATTERN, '')
.replace(/^(?:\r?\n)+/, '')
}
if (strippedBody.length === 0) {
return {
action: hadHeader ? 'updated' : 'added',
text: shebang + header,
}
}
return {
action: hadHeader ? 'updated' : 'added',
text: shebang + header + strippedBody,
}
}
function formatPath(rootDir, filePath) {
return path.relative(rootDir, filePath).replaceAll(path.sep, '/')
}
async function main() {
const rootDir = process.cwd()
const sourceFiles = await collectTargetFiles(rootDir)
const stats = {
added: 0,
checked: 0,
skippedGenerated: 0,
skippedThirdParty: 0,
updated: 0,
}
const pendingFiles = []
for (const file of sourceFiles) {
stats.checked += 1
const originalText = await fs.readFile(file, 'utf8')
const bom = originalText.startsWith('\uFEFF') ? '\uFEFF' : ''
const text = bom ? originalText.slice(1) : originalText
const [, body] = splitShebang(text)
if (hasGeneratedMarker(body)) {
stats.skippedGenerated += 1
continue
}
if (hasThirdPartyCopyright(body)) {
stats.skippedThirdParty += 1
continue
}
const result = applyHeader(text)
const nextText = bom + result.text
if (nextText !== originalText) {
stats[result.action] += 1
pendingFiles.push(formatPath(rootDir, file))
if (!checkMode) {
await fs.writeFile(file, nextText)
}
}
}
console.log(
[
`copyright: checked ${stats.checked}`,
`added ${stats.added}`,
`updated ${stats.updated}`,
`skipped generated ${stats.skippedGenerated}`,
`skipped third-party ${stats.skippedThirdParty}`,
].join(', ')
)
if (checkMode && pendingFiles.length > 0) {
console.error('copyright: headers need to be updated in:')
for (const file of pendingFiles) {
console.error(`- ${file}`)
}
process.exitCode = 1
}
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})