-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
65 lines (53 loc) · 2.08 KB
/
index.ts
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
import * as fs from 'node:fs'
import * as path from 'node:path'
import process from 'node:process'
import { Command } from 'commander'
const program = new Command()
program
.version('0.2.0')
.requiredOption('-s, --source <source>', 'Source directory containing the package.json file')
.requiredOption('-t, --target <target>', 'Target directory where the package.json file will be synchronized')
.option('-e, --exclude <dependencyName>', 'dependencies to be excluded, separated by commas')
.parse(process.argv)
const { source, target, exclude } = program.opts()
function readPackageJson(filePath: string): any {
const absolutePath = path.resolve(process.cwd(), filePath)
if (!fs.existsSync(absolutePath))
throw new Error(`package.json not found at ${absolutePath}`)
const content = fs.readFileSync(absolutePath, 'utf8')
return JSON.parse(content)
}
function writePackageJson(filePath: string, content: object): any {
const absolutePath = path.resolve(process.cwd(), filePath)
fs.writeFileSync(absolutePath, `${JSON.stringify(content, null, 2)}\n`)
}
function syncPackageJsonVersions(sourcePath: string, targetPath: string): void {
const sourcePackageJson = readPackageJson(sourcePath)
const targetPackageJson = readPackageJson(targetPath)
let isUpdated = false
const excludedPackages = exclude?.split(',') ?? []
Object.keys(sourcePackageJson.dependencies).forEach((dep) => {
if (excludedPackages.includes(dep))
return
if (sourcePackageJson.dependencies[dep] !== targetPackageJson.dependencies[dep]) {
targetPackageJson.dependencies[dep] = sourcePackageJson.dependencies[dep]
isUpdated = true
}
})
if (isUpdated) {
writePackageJson(targetPath, targetPackageJson)
console.log('package.json has been synchronized successfully.')
}
else {
console.log('package.json is already up to date.')
}
}
export async function main(): Promise<void> {
try {
syncPackageJsonVersions(path.join(source, 'package.json'), path.join(target, 'package.json'))
}
catch (error) {
console.error(`Error: ${error.message}`)
process.exit(1)
}
}