forked from abpframework/abp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to change package version
- Loading branch information
1 parent
35527c1
commit 7d73af1
Showing
3 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import glob from "glob"; | ||
import fse from "fs-extra"; | ||
import {program} from "commander"; | ||
|
||
(function findPackageJsonFiles() { | ||
setupCommander(); | ||
const options = { | ||
ignore: [ | ||
"../../**/node_modules/**", | ||
"../../**/dist/**", | ||
"../../**/build/**", | ||
"../../**/scripts/**", | ||
"../../**/wwwroot/**" | ||
] | ||
}; | ||
|
||
const workingDir = "../../"; | ||
glob(`${workingDir}**/package.json`, options, (err, files) => { | ||
if (err) throw err; | ||
|
||
const {packageName,targetVersion}= program.opts(); | ||
|
||
for (const file of files) { | ||
readPackageJsonFile(file, packageName, targetVersion); | ||
} | ||
}); | ||
})(); | ||
|
||
function readPackageJsonFile(path, key, newVersion) { | ||
const replace = (block, key, newVersion) => { | ||
const founded = Object.keys(block).filter(x => x === key); | ||
if (founded.length > 0) { | ||
let value = block[key]; | ||
value = value.replace(semverRegex, newVersion); | ||
return [true, { | ||
...block, | ||
[key]:value | ||
}]; | ||
} | ||
return [false, block]; | ||
}; | ||
fse.readJson(path, (err, packageObj) => { | ||
if (err) throw err; | ||
|
||
const { dependencies, peerDependencies, devDependencies } = packageObj; | ||
const results = []; | ||
|
||
let result = { ...packageObj }; | ||
if (dependencies) { | ||
const [founded, d] = replace(dependencies, key, newVersion); | ||
results.push(founded); | ||
result = { | ||
...result, dependencies: d | ||
}; | ||
} | ||
if (peerDependencies) { | ||
const [founded, p] = replace(peerDependencies, key, newVersion); | ||
results.push(founded); | ||
result = { | ||
...result, peerDependencies: p | ||
}; | ||
} | ||
if (devDependencies) { | ||
const [founded, d] = replace(devDependencies, key, newVersion); | ||
results.push(founded); | ||
result = { | ||
...result, devDependencies: d | ||
}; | ||
} | ||
const anyChanges = !results.some(x => x); | ||
if (anyChanges) { | ||
return; | ||
} | ||
console.log("changed", path); | ||
writeFile(path, result); | ||
} | ||
) | ||
; | ||
} | ||
export const semverRegex = | ||
/\d+\.\d+\.\d+(?:-[a-zA-Z0-9]+(?:\.[a-zA-Z0-9-]+)*)?(?:\+[a-zA-Z0-9]+(?:\.[a-zA-Z0-9-]+)*)?$/; | ||
|
||
function writeFile(path, result) { | ||
return fse.writeJson(path, result, { spaces: 2 }); | ||
} | ||
|
||
|
||
function setupCommander() { | ||
program | ||
.option( | ||
"-n, --packageName <packageName>", | ||
"Package name" | ||
) | ||
.option( | ||
"-v, --targetVersion <targetVersion>", | ||
"Version number of the package" | ||
); | ||
|
||
program.parse(process.argv); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -417,16 +417,16 @@ combined-stream@^1.0.6, combined-stream@~1.0.6: | |
dependencies: | ||
delayed-stream "~1.0.0" | ||
|
||
commander@^11.0.0: | ||
version "11.1.0" | ||
resolved "https://registry.yarnpkg.com/commander/-/commander-11.1.0.tgz#62fdce76006a68e5c1ab3314dc92e800eb83d906" | ||
integrity sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ== | ||
|
||
commander@^6.1.0: | ||
version "6.1.0" | ||
resolved "https://registry.yarnpkg.com/commander/-/commander-6.1.0.tgz#f8d722b78103141006b66f4c7ba1e97315ba75bc" | ||
integrity sha512-wl7PNrYWd2y5mp1OK/LhTlv8Ff4kQJQRXXAvF+uU/TPNiVJUxZLRYGj/B0y/lPGAVcSbJqH2Za/cvHmrPMC8mA== | ||
|
||
commander@^8.3.0: | ||
version "8.3.0" | ||
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66" | ||
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww== | ||
|
||
[email protected]: | ||
version "0.0.1" | ||
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" | ||
|