Skip to content

Commit 5c19629

Browse files
committed
chore: copy-file-to-all-plugins.ts替换copy-file-to-all-plugins.js
1 parent 6dd0d3d commit 5c19629

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed
Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
const { execSync } = require('child_process')
2-
const fs = require('fs')
3-
const path = require('path')
1+
import { execSync } from 'child_process'
2+
import fs from 'fs'
3+
import path from 'path'
44

55

66
const GENERAL_FILE_LIST = [
77
'.github/workflows/deploy.yml',
8-
'scripts/commit.js',
8+
'scripts/commit.ts',
9+
'scripts/copy-file-to-all-plugins.ts',
10+
'scripts/update-packages-to-all-plugins.ts',
911
'src/types/global.d.ts',
1012
'src/index.ts',
1113
'.gitignore',
@@ -22,9 +24,9 @@ const pluginsPath = path.resolve(__dirname, '../../')
2224
const currentPath = path.resolve(__dirname, '../')
2325

2426

25-
const pluginsList = []
27+
const pluginsList: string[] = []
2628
// 记录需要更新的插件
27-
const pluginsNeedUpdate = []
29+
const pluginsNeedUpdate: string[] = []
2830

2931
fs.readdirSync(pluginsPath).forEach(file => {
3032
const fullPath = path.join(pluginsPath, file)
@@ -42,29 +44,29 @@ console.log(`[INFO] Total files to sync: ${GENERAL_FILE_LIST.length}`)
4244
console.log(`[INFO] Target plugins: ${pluginsList.length}\n`)
4345

4446
// 检查文件是否相同
45-
function areFilesIdentical(sourceFile, targetFile) {
47+
function areFilesIdentical(sourceFile: string, targetFile: string): boolean {
4648
if (!fs.existsSync(targetFile)) {
4749
return false
4850
}
49-
51+
5052
try {
5153
const sourceContent = fs.readFileSync(sourceFile)
5254
const targetContent = fs.readFileSync(targetFile)
5355
return Buffer.compare(sourceContent, targetContent) === 0
5456
} catch (error) {
55-
console.error(`[ERROR] Error comparing files ${sourceFile} and ${targetFile}: ${error.message}`)
57+
console.error(`[ERROR] Error comparing files ${sourceFile} and ${targetFile}: ${(error as Error).message}`)
5658
return false
5759
}
5860
}
5961

60-
function copyFile(sourceFile, targetFile) {
62+
function copyFile(sourceFile: string, targetFile: string): boolean {
6163
try {
6264
// 先检查文件是否相同
6365
if (areFilesIdentical(sourceFile, targetFile)) {
6466
console.log(`[INFO] Skipped: ${path.basename(sourceFile)} (identical)`)
6567
return false // 返回false表示未复制(文件相同)
6668
}
67-
69+
6870
const targetDir = path.dirname(targetFile)
6971
if (!fs.existsSync(targetDir)) {
7072
fs.mkdirSync(targetDir, { recursive: true })
@@ -79,7 +81,7 @@ function copyFile(sourceFile, targetFile) {
7981
console.log(`[SUCCESS] Copied: ${path.basename(sourceFile)} (${fileSizeKB} KB) to ${targetFile}`)
8082
return true // 返回true表示已复制(文件不同)
8183
} catch (error) {
82-
console.error(`[ERROR] Failed to copy ${sourceFile} to ${targetFile}: ${error.message}`)
84+
console.error(`[ERROR] Failed to copy ${sourceFile} to ${targetFile}: ${(error as Error).message}`)
8385
return true // 出错时默认需要更新
8486
}
8587
}
@@ -88,10 +90,10 @@ let pluginCounter = 0;
8890
pluginsList.forEach(pluginPath => {
8991
pluginCounter++;
9092
console.log(`\n[INFO] Processing plugin ${pluginCounter}/${pluginsList.length}: ${path.basename(pluginPath)}`)
91-
93+
9294
let fileCounter = 0;
9395
let needUpdate = false;
94-
96+
9597
GENERAL_FILE_LIST.forEach(file => {
9698
fileCounter++;
9799
const sourceFile = path.join(currentPath, file)
@@ -108,7 +110,7 @@ pluginsList.forEach(pluginPath => {
108110
needUpdate = true;
109111
}
110112
})
111-
113+
112114
if (needUpdate) {
113115
pluginsNeedUpdate.push(pluginPath);
114116
console.log(`[INFO] Plugin ${path.basename(pluginPath)} marked for update`);
@@ -121,18 +123,19 @@ console.log('\n[INFO] File synchronization completed successfully!')
121123
console.log(`[INFO] Plugins requiring updates: ${pluginsNeedUpdate.length}/${pluginsList.length}`)
122124

123125

124-
function getCurrentVersion(projectPath) {
125-
const packageJson = require(path.join(projectPath, 'package.json'))
126+
function getCurrentVersion(projectPath: string): string {
127+
const packageJsonPath = path.join(projectPath, 'package.json')
128+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
126129
return packageJson.version
127130
}
128131

129-
function incrementVersion(version) {
132+
function incrementVersion(version: string): string {
130133
const parts = version.split('.')
131134
const patch = parseInt(parts[2], 10) + 1
132135
return `${parts[0]}.${parts[1]}.${patch}`
133136
}
134137

135-
function updateVersion(projectPath, newVersion) {
138+
function updateVersion(projectPath: string, newVersion: string): void {
136139
const packageJsonPath = path.join(projectPath, 'package.json')
137140
const packageLockPath = path.join(projectPath, 'package-lock.json')
138141

@@ -147,7 +150,7 @@ function updateVersion(projectPath, newVersion) {
147150
}
148151
}
149152

150-
function executeGitCommit(projectPath, commitMessage) {
153+
function executeGitCommit(projectPath: string, commitMessage: string): void {
151154
try {
152155
execSync('git add .', { cwd: projectPath, stdio: 'inherit' })
153156
execSync(`git commit -m "${commitMessage}"`, { cwd: projectPath, stdio: 'inherit' })
@@ -158,7 +161,7 @@ function executeGitCommit(projectPath, commitMessage) {
158161
}
159162
}
160163

161-
function createGitTag(projectPath, version) {
164+
function createGitTag(projectPath: string, version: string): void {
162165
try {
163166
execSync(`git tag v${version}`, { cwd: projectPath, stdio: 'inherit' })
164167
} catch (error) {
@@ -168,7 +171,7 @@ function createGitTag(projectPath, version) {
168171
}
169172
}
170173

171-
function executeGitPush(projectPath) {
174+
function executeGitPush(projectPath: string): void {
172175
try {
173176
execSync('git push --follow-tags', { cwd: projectPath, stdio: 'inherit' })
174177
} catch (error) {
@@ -183,27 +186,26 @@ if (pluginsNeedUpdate.length > 0) {
183186
pluginsNeedUpdate.forEach(pluginPath => {
184187
const currentVersion = getCurrentVersion(pluginPath)
185188
const newVersion = incrementVersion(currentVersion)
186-
189+
187190
console.log('\n\n[INFO] Committing:', path.basename(pluginPath))
188191
console.log(`[INFO] Incremented version: ${currentVersion} -> ${newVersion}`)
189-
192+
190193
updateVersion(pluginPath, newVersion)
191-
194+
192195
console.log('')
193196
executeGitCommit(pluginPath, COMMIT_MESSAGE)
194197
createGitTag(pluginPath, newVersion)
195198
})
196-
199+
197200
console.log('\n\n[SUCCESS] All updated plugins have been committed and tagged.')
198-
201+
199202
pluginsNeedUpdate.forEach(pluginPath => {
200203
console.log('\n\n[INFO] Pushing:', path.basename(pluginPath))
201204
executeGitPush(pluginPath)
202205
})
203-
206+
204207
console.log('\n\n[SUCCESS] All updated plugins have been pushed to the remote repository.')
205208
} else {
206209
console.log('\n\n[INFO] No plugins need updates, skipping version increment and commit process.')
207210
}
208211

209-

0 commit comments

Comments
 (0)