Skip to content

Commit 070e99e

Browse files
committed
chore: sync files from nfp-template
1 parent fb510a1 commit 070e99e

File tree

7 files changed

+173
-53
lines changed

7 files changed

+173
-53
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@
5959
"webpack-cli": "^5.1.4",
6060
"webpack-dev-server": "^5.1.0"
6161
}
62-
}
62+
}

prettier.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from './node_modules/.cache/rhine-lint/prettier.config.mjs'

rhine-lint.config.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { type Config } from 'rhine-lint';
2+
3+
export default {
4+
// Project level: 'normal' | 'react' | 'next'
5+
level: 'react',
6+
7+
// Enable TypeScript support
8+
typescript: true,
9+
10+
// Enable project-based type checking
11+
projectTypeCheck: true,
12+
13+
// Additional ignore patterns
14+
ignores: [],
15+
16+
// ESLint specific configuration
17+
eslint: {
18+
enable: true,
19+
config: [
20+
// Add custom ESLint rules here
21+
// {
22+
// rules: {
23+
// 'no-console': 'warn',
24+
// }
25+
// }
26+
]
27+
},
28+
29+
// Prettier specific configuration
30+
prettier: {
31+
enable: true,
32+
config: {
33+
// Add custom Prettier options here
34+
// printWidth: 100,
35+
// semi: true,
36+
}
37+
}
38+
} as Config;

scripts/copy-file-to-all-plugins.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { execSync } from 'child_process'
22
import fs from 'fs'
33
import path from 'path'
4-
import { fileURLToPath } from 'url'
5-
6-
const __filename = fileURLToPath(import.meta.url)
7-
const __dirname = path.dirname(__filename)
84

95
const GENERAL_FILE_LIST = [
106
'.github/workflows/deploy.yml',
@@ -18,22 +14,22 @@ const GENERAL_FILE_LIST = [
1814
'tsconfig.json',
1915
'tsconfig.production.json',
2016
'webpack.config.js',
17+
'prettier.config.js',
18+
'rhine-lint.config.ts',
2119
]
2220

2321
const COMMIT_MESSAGE = 'chore: sync files from nfp-template'
2422

25-
26-
const pluginsPath = path.resolve(__dirname, '../../')
27-
const currentPath = path.resolve(__dirname, '../')
28-
23+
const PROJECT_PATH = process.cwd()
24+
const PLUGINS_PATH = path.resolve(PROJECT_PATH, '../')
25+
const PLUGIN_PROJECT_PREFIX = 'nfp-'
2926

3027
const pluginsList: string[] = []
31-
// 记录需要更新的插件
32-
const pluginsNeedUpdate: string[] = []
28+
const pluginsNeedUpdate: string[] = [] // 记录需要更新的插件
3329

34-
fs.readdirSync(pluginsPath).forEach(file => {
35-
const fullPath = path.join(pluginsPath, file)
36-
if (fs.statSync(fullPath).isDirectory() && file.startsWith('nfp-') && fullPath !== currentPath) {
30+
fs.readdirSync(PLUGINS_PATH).forEach(file => {
31+
const fullPath = path.join(PLUGINS_PATH, file)
32+
if (fs.statSync(fullPath).isDirectory() && file.startsWith(PLUGIN_PROJECT_PREFIX) && fullPath !== PROJECT_PATH) {
3733
pluginsList.push(fullPath)
3834
}
3935
})
@@ -42,7 +38,7 @@ console.log('[INFO] Target Plugins:')
4238
console.log(pluginsList)
4339

4440
console.log('\n[INFO] Starting file synchronization process...')
45-
console.log(`[INFO] Source directory: ${currentPath}`)
41+
console.log(`[INFO] Source directory: ${PROJECT_PATH}`)
4642
console.log(`[INFO] Total files to sync: ${GENERAL_FILE_LIST.length}`)
4743
console.log(`[INFO] Target plugins: ${pluginsList.length}\n`)
4844

@@ -99,7 +95,7 @@ pluginsList.forEach(pluginPath => {
9995

10096
GENERAL_FILE_LIST.forEach(file => {
10197
fileCounter++;
102-
const sourceFile = path.join(currentPath, file)
98+
const sourceFile = path.join(PROJECT_PATH, file)
10399

104100
if (!fs.existsSync(sourceFile)) {
105101
console.warn(`[WARNING] Source file does not exist: ${sourceFile}`)

scripts/update-packages-to-all-plugins.ts

Lines changed: 120 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { execSync } from 'child_process'
2-
import * as fs from 'fs'
3-
import * as path from 'path'
4-
import * as readline from 'readline'
5-
import { fileURLToPath } from 'url'
6-
7-
const __filename = fileURLToPath(import.meta.url)
8-
const __dirname = path.dirname(__filename)
2+
import fs from 'fs'
3+
import path from 'path'
4+
import readline from 'readline'
95

106
// 配置项
117
const COMMIT_MESSAGE = 'chore: update package version'
12-
const pluginsPath = path.resolve(__dirname, '../../')
13-
const pluginProjectPrefix = 'nfp-'
14-
const currentPath = path.resolve(__dirname, '../')
8+
const PROJECT_PATH = process.cwd()
9+
const PLUGINS_PATH = path.resolve(PROJECT_PATH, '../')
10+
const PLUGIN_PROJECT_PREFIX = 'nfp-'
1511

1612
interface PackageJson {
1713
name: string
@@ -41,7 +37,7 @@ function askQuestion(query: string): Promise<string> {
4137

4238
// 读取当前项目的包版本
4339
function readCurrentPackageVersions(packageNames: string[]): PackageVersions {
44-
const packageJsonPath = path.join(currentPath, 'package.json')
40+
const packageJsonPath = path.join(PROJECT_PATH, 'package.json')
4541
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
4642

4743
const versions: PackageVersions = {}
@@ -78,12 +74,12 @@ function readCurrentPackageVersions(packageNames: string[]): PackageVersions {
7874
function getPluginProjects(): string[] {
7975
const pluginsList: string[] = []
8076

81-
fs.readdirSync(pluginsPath).forEach(file => {
82-
const fullPath = path.join(pluginsPath, file)
77+
fs.readdirSync(PLUGINS_PATH).forEach(file => {
78+
const fullPath = path.join(PLUGINS_PATH, file)
8379
if (
8480
fs.statSync(fullPath).isDirectory() &&
85-
file.startsWith(pluginProjectPrefix) &&
86-
fullPath !== currentPath
81+
file.startsWith(PLUGIN_PROJECT_PREFIX) &&
82+
fullPath !== PROJECT_PATH
8783
) {
8884
pluginsList.push(fullPath)
8985
}
@@ -159,6 +155,60 @@ function checkAndUpdatePlugin(
159155
return false
160156
}
161157

158+
// 检查并删除插件的指定包
159+
function checkAndRemovePackages(pluginPath: string, packageNames: string[]): boolean {
160+
const packageJsonPath = path.join(pluginPath, 'package.json')
161+
162+
if (!fs.existsSync(packageJsonPath)) {
163+
console.warn(`[WARNING] package.json not found in ${pluginPath}`)
164+
return false
165+
}
166+
167+
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
168+
let hasChanges = false
169+
const removals: string[] = []
170+
171+
// 检查并删除dependencies中的包
172+
if (packageJson.dependencies) {
173+
packageNames.forEach(packageName => {
174+
if (packageJson.dependencies![packageName]) {
175+
removals.push(` ${packageName} (from dependencies)`)
176+
delete packageJson.dependencies![packageName]
177+
hasChanges = true
178+
}
179+
})
180+
}
181+
182+
// 检查并删除devDependencies中的包
183+
if (packageJson.devDependencies) {
184+
packageNames.forEach(packageName => {
185+
if (packageJson.devDependencies![packageName]) {
186+
removals.push(` ${packageName} (from devDependencies)`)
187+
delete packageJson.devDependencies![packageName]
188+
hasChanges = true
189+
}
190+
})
191+
}
192+
193+
if (hasChanges) {
194+
console.log(`[INFO] Removals for ${path.basename(pluginPath)}:`)
195+
removals.forEach(removal => console.log(removal))
196+
197+
// 更新版本号
198+
const currentVersion = packageJson.version
199+
const newVersion = incrementVersion(currentVersion)
200+
packageJson.version = newVersion
201+
console.log(`[INFO] Version: ${currentVersion} -> ${newVersion}`)
202+
203+
// 写入package.json
204+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2) + '\n')
205+
206+
return true
207+
}
208+
209+
return false
210+
}
211+
162212
// 版本号递增
163213
function incrementVersion(version: string): string {
164214
const parts = version.split('.')
@@ -236,36 +286,64 @@ function gitPush(pluginPath: string): void {
236286

237287
// 主函数
238288
async function main() {
239-
console.log('[INFO] Package Update Tool for All Plugins\n')
289+
console.log('[INFO] Package Management Tool for All Plugins\n')
290+
291+
// 询问用户选择操作模式
292+
const mode = await askQuestion('Choose mode (update/remove): ')
293+
const isRemoveMode = mode.trim().toLowerCase() === 'remove'
294+
295+
if (!isRemoveMode && mode.trim().toLowerCase() !== 'update') {
296+
console.error('[ERROR] Invalid mode. Please choose "update" or "remove"')
297+
process.exit(1)
298+
}
299+
300+
console.log(`\n[INFO] Mode: ${isRemoveMode ? 'REMOVE' : 'UPDATE'}\n`)
240301

241302
// 询问用户输入包名
242303
const input = await askQuestion(
243-
'Enter package names (space-separated, leave empty for all packages): '
304+
isRemoveMode
305+
? 'Enter package names to remove (space-separated): '
306+
: 'Enter package names (space-separated, leave empty for all packages): '
244307
)
245308
const packageNames = input
246309
.trim()
247310
.split(/\s+/)
248311
.filter(name => name.length > 0)
249312

313+
if (isRemoveMode && packageNames.length === 0) {
314+
console.error('[ERROR] Please specify at least one package name to remove')
315+
process.exit(1)
316+
}
317+
250318
console.log(
251319
`\n[INFO] Target packages: ${packageNames.length === 0 ? 'ALL' : packageNames.join(', ')}\n`
252320
)
253321

254-
// 读取目标版本
255-
const targetVersions = readCurrentPackageVersions(packageNames)
322+
let commitMessage = COMMIT_MESSAGE
256323

257-
if (Object.keys(targetVersions).length === 0) {
258-
console.error('[ERROR] No packages found to update')
259-
process.exit(1)
260-
}
324+
if (isRemoveMode) {
325+
// 删除模式
326+
commitMessage = 'chore: remove packages'
327+
console.log('[INFO] Packages to remove:')
328+
packageNames.forEach(name => console.log(` ${name}`))
329+
console.log('')
330+
} else {
331+
// 更新模式
332+
const targetVersions = readCurrentPackageVersions(packageNames)
261333

262-
console.log('[INFO] Target versions:')
263-
Object.entries(targetVersions).forEach(([name, version]) => {
264-
console.log(` ${name}: ${version}`)
265-
})
266-
console.log('')
334+
if (Object.keys(targetVersions).length === 0) {
335+
console.error('[ERROR] No packages found to update')
336+
process.exit(1)
337+
}
338+
339+
console.log('[INFO] Target versions:')
340+
Object.entries(targetVersions).forEach(([name, version]) => {
341+
console.log(` ${name}: ${version}`)
342+
})
343+
console.log('')
344+
}
267345

268-
console.log(`[INFO] Commit message: ${COMMIT_MESSAGE}\n`)
346+
console.log(`[INFO] Commit message: ${commitMessage}\n`)
269347

270348
// 获取所有插件项目
271349
const pluginsList = getPluginProjects()
@@ -277,25 +355,32 @@ async function main() {
277355
for (const pluginPath of pluginsList) {
278356
console.log(`\n[INFO] Checking ${path.basename(pluginPath)}...`)
279357

280-
const needsUpdate = checkAndUpdatePlugin(pluginPath, targetVersions)
358+
let needsUpdate = false
359+
360+
if (isRemoveMode) {
361+
needsUpdate = checkAndRemovePackages(pluginPath, packageNames)
362+
} else {
363+
const targetVersions = readCurrentPackageVersions(packageNames)
364+
needsUpdate = checkAndUpdatePlugin(pluginPath, targetVersions)
365+
}
281366

282367
if (needsUpdate) {
283368
// 读取更新后的版本
284369
const packageJsonPath = path.join(pluginPath, 'package.json')
285370
const packageJson: PackageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
286371
pluginsNeedUpdate.push({ path: pluginPath, version: packageJson.version })
287-
console.log(`[SUCCESS] ${path.basename(pluginPath)} marked for update`)
372+
console.log(`[SUCCESS] ${path.basename(pluginPath)} marked for ${isRemoveMode ? 'removal' : 'update'}`)
288373
} else {
289-
console.log(`[INFO] ${path.basename(pluginPath)} is up to date`)
374+
console.log(`[INFO] ${path.basename(pluginPath)} ${isRemoveMode ? 'does not have these packages' : 'is up to date'}`)
290375
}
291376
}
292377

293378
if (pluginsNeedUpdate.length === 0) {
294-
console.log('\n[INFO] All plugins are up to date. No updates needed.')
379+
console.log(`\n[INFO] No plugins need ${isRemoveMode ? 'package removal' : 'updates'}.`)
295380
return
296381
}
297382

298-
console.log(`\n[INFO] ${pluginsNeedUpdate.length} plugins need updates\n`)
383+
console.log(`\n[INFO] ${pluginsNeedUpdate.length} plugins need ${isRemoveMode ? 'package removal' : 'updates'}\n`)
299384

300385
const pluginsReadyToPush: string[] = []
301386

@@ -311,7 +396,7 @@ async function main() {
311396
runBunInstall(pluginPath)
312397

313398
// 3. Git提交和创建tag
314-
gitCommitAndTag(pluginPath, COMMIT_MESSAGE, version)
399+
gitCommitAndTag(pluginPath, commitMessage, version)
315400

316401
pluginsReadyToPush.push(pluginPath)
317402
console.log(`[SUCCESS] ${path.basename(pluginPath)} prepared successfully`)

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
/* Output */
2626
"outDir": "./dist",
27-
"typeRoots": ["./node_modules/@types"],
27+
"typeRoots": ["./node_modules/@types", "./src/types"],
28+
"types": ["react", "react-dom"],
2829

2930
"declaration": true,
3031
"declarationMap": true,

webpack.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ export default (env, argv) => {
148148
valtio: 'Valtio',
149149
'rhine-var': 'RhineVar',
150150
'rhine-var/react': 'RhineVarReact',
151-
antd: 'AntD',
152151
'file-type': 'FileType',
153152
mime: 'Mime',
154153
'brotli-wasm': 'BrotliWasm',

0 commit comments

Comments
 (0)