-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
148 lines (129 loc) · 4.07 KB
/
index.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
#! /usr/bin/env node
const fs = require('fs')
const path = require('path')
const argv = require('minimist')(process.argv.slice(2), {
string: ['others', 'ignore'],
boolean: ['install-after-changed'],
})
const rootPkgPath = path.join(process.cwd(), '/package.json')
const rootPkg = require(rootPkgPath)
const allDeps = Object.assign({}, rootPkg.dependencies, rootPkg.devDependencies)
const findDependencies = dependency =>
Object.keys(allDeps).findIndex(d => d === dependency) >= 0 &&
allDeps[dependency]
const rootDeps = rootPkg.dependencies
const rootDevDeps = rootPkg.devDependencies
const log = (name, info) => console.log(`[${name}]`, info)
const warn = (name, info) => console.warn(`[${name}]`, info)
const error = (name, info) => console.error(`[${name}]`, info)
const ignore =
(argv.ignore || process.env.YARN_SYNC_IGNORE) &&
new RegExp(argv.ignore || process.env.YARN_SYNC_IGNORE)
const skip =
(argv.skip || process.env.YARN_SYNC_SKIP) &&
new RegExp(argv.skip || process.env.YARN_SYNC_SKIP)
const fix = !!(argv.fix || process.env.YARN_SYNC_FIX)
let rootChanged = false
let pkgChanged = false
const updateDependencies = (name, deps, ignoredList, isDev) => {
if (!deps) return
Object.keys(deps).forEach(d => {
const version = findDependencies(d)
if (ignoredList.indexOf(d) >= 0) return
if (!version) {
if (ignore && ignore.test(d)) return
if (fix) {
if (isDev) {
rootDevDeps[d] = deps[d]
} else {
rootDeps[d] = deps[d]
}
ignoredList.push(d)
rootChanged = true
} else {
error(
name,
`Dependency \`${d}\` not found in root dependencies, please add it. Run with --fix to add automatically`,
)
}
return
}
const mayOldVersion = deps[d]
if (version !== mayOldVersion) {
log(name, `Sync dependency \`${d}\`: ${mayOldVersion} -> ${version}`)
deps[d] = version
pkgChanged = true
}
})
}
const handleWorkspace = workspace => {
let packages
if (workspace.endsWith('*')) {
const dirname = workspace.replace(/\*/, '')
const dirs = fs.readdirSync(dirname)
packages = dirs.map(dir => {
const pkgPath = path.join(process.cwd(), dirname, dir, 'package.json')
if (fs.existsSync(pkgPath) && !(skip && skip.test(pkgPath))) {
const pkg = require(pkgPath)
pkg._path = pkgPath
return pkg
} else {
return false
}
})
} else {
const pkgPath = path.join(process.cwd(), workspace, 'package.json')
if (fs.existsSync(pkgPath) && !(skip && skip.test(pkgPath))) {
const pkg = require(pkgPath)
pkg._path = pkgPath
packages = [pkg]
} else {
packages = []
}
}
const nameList = packages.map(pkg => pkg.name)
packages.forEach(pkg => {
if (pkg.devDependencies) {
warn(pkg.name, 'has sub packages in devDependencies.')
}
if (!pkg.dependencies && !pkg.devDependencies) {
if (pkg.name !== undefined) {
log(pkg.name, 'No dependencies')
}
return
}
updateDependencies(pkg.name, pkg.dependencies, nameList)
updateDependencies(pkg.name, pkg.devDependencies, nameList, true)
const pkgPath = pkg._path
delete pkg._path
fs.writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`)
})
}
let workspaces = [
...(Array.isArray(rootPkg.workspaces)
? rootPkg.workspaces
: rootPkg.workspaces.packages),
]
if (argv.others) {
workspaces = workspaces.concat(argv.others)
}
workspaces.forEach(handleWorkspace)
if (rootChanged) {
rootPkg.dependencies = Object.keys(rootDeps)
.sort()
.reduce((acc, key) => {
acc[key] = rootDeps[key]
return acc
}, {})
rootPkg.devDependencies = Object.keys(rootDevDeps)
.sort()
.reduce((acc, key) => {
acc[key] = rootDevDeps[key]
return acc
}, {})
fs.writeFileSync(rootPkgPath, `${JSON.stringify(rootPkg, null, 2)}\n`)
}
if (argv['install-after-changed'] && (rootChanged || pkgChanged)) {
log('yarnw-sync-deps', 'Install deps after changed...')
require('child_process').execSync('yarn install --ignore-scripts')
}