|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { join } from 'path'; |
| 4 | + |
| 5 | +import { readFile, writeFile } from 'fs/promises'; |
| 6 | + |
| 7 | +import { inc, parse } from 'semver'; |
| 8 | + |
| 9 | +import parseCmdArgs from 'parse-cmd-args'; |
| 10 | + |
| 11 | +const { input } = parseCmdArgs(null, { |
| 12 | + requireUserInput: true |
| 13 | +}); |
| 14 | + |
| 15 | +const loadAndParsePackageFile = async path => { |
| 16 | + return JSON.parse(await readFile(path, 'utf8')); |
| 17 | +}; |
| 18 | + |
| 19 | +(async () => { |
| 20 | + const { workspaces } = await loadAndParsePackageFile('./package.json'); |
| 21 | + |
| 22 | + const versions = ( |
| 23 | + await Promise.all( |
| 24 | + workspaces.map(async workspace => { |
| 25 | + const pkg = await loadAndParsePackageFile( |
| 26 | + join(workspace, './package.json') |
| 27 | + ); |
| 28 | + |
| 29 | + const { prerelease } = parse(pkg.version); |
| 30 | + |
| 31 | + const nextVersion = inc(pkg.version, input, prerelease[0]); |
| 32 | + |
| 33 | + await writeFile( |
| 34 | + join(workspace, './package.json'), |
| 35 | + `${JSON.stringify( |
| 36 | + { ...pkg, version: nextVersion }, |
| 37 | + null, |
| 38 | + 2 |
| 39 | + )}\n` |
| 40 | + ); |
| 41 | + |
| 42 | + return { |
| 43 | + name: pkg.name, |
| 44 | + version: nextVersion |
| 45 | + }; |
| 46 | + }) |
| 47 | + ) |
| 48 | + ).reduce((all, workspace) => ({ ...all, [workspace.name]: workspace }), {}); |
| 49 | + |
| 50 | + await Promise.all( |
| 51 | + workspaces.map(async workspace => { |
| 52 | + const pkg = await loadAndParsePackageFile( |
| 53 | + join(workspace, './package.json') |
| 54 | + ); |
| 55 | + |
| 56 | + ['dependencies', 'peerDependencies', 'devDependencies'].map( |
| 57 | + type => { |
| 58 | + if (pkg[type]) { |
| 59 | + Object.keys(pkg[type]).map(dependency => { |
| 60 | + if (Object.keys(versions).includes(dependency)) { |
| 61 | + pkg[type][dependency] = |
| 62 | + versions[dependency].version; |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + ); |
| 68 | + |
| 69 | + await writeFile( |
| 70 | + join(workspace, './package.json'), |
| 71 | + `${JSON.stringify(pkg, null, 2)}\n` |
| 72 | + ); |
| 73 | + }) |
| 74 | + ); |
| 75 | + |
| 76 | + console.log(versions); |
| 77 | +})(); |
0 commit comments