-
-
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.
Merge pull request #14 from capricorn86/add-support-for-set-workspace…
…-version fix: [#1] Adds support for tool that can set workspace version
- Loading branch information
Showing
4 changed files
with
494 additions
and
53 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
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,137 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
/* eslint-disable no-console */ | ||
|
||
const Path = require('path'); | ||
const FS = require('fs'); | ||
const { glob } = require('glob'); | ||
|
||
const SHELL_CODES = { | ||
reset: '\x1b[0m', | ||
bold: '\x1b[1m', | ||
blue: '\x1b[34m', | ||
red: '\x1b[31m', | ||
gray: '\x1b[90m', | ||
green: '\x1b[32m' | ||
}; | ||
|
||
const VERSION_REGEXP = /[0-9]+\.[0-9]+\.[0-9]+/; | ||
|
||
process.on('unhandledRejection', (reason) => { | ||
console.error(SHELL_CODES.red, reason, SHELL_CODES.reset); | ||
process.exit(1); | ||
}); | ||
|
||
main(); | ||
|
||
/** | ||
* Returns arguments. | ||
* | ||
* @returns {object} Arguments. | ||
*/ | ||
function getArguments() { | ||
const args = { | ||
version: null | ||
}; | ||
|
||
for (const arg of process.argv) { | ||
if (arg.startsWith('--version=')) { | ||
args.version = arg.split('=')[1]; | ||
} | ||
} | ||
|
||
return args; | ||
} | ||
|
||
async function getWorkspacePackages() { | ||
const rootPackageJsonFilepath = Path.resolve('package.json'); | ||
const rootPackageJson = require(rootPackageJsonFilepath); | ||
const workspaces = rootPackageJson.workspaces; | ||
const rootDirectory = Path.dirname(rootPackageJsonFilepath); | ||
|
||
if (!workspaces) { | ||
return { root: { path: rootDirectory, packageJson: rootPackageJson } }; | ||
} | ||
|
||
const workspacePackages = {}; | ||
const promises = []; | ||
|
||
for (const workspace of workspaces) { | ||
promises.push(glob(workspace, { cwd: Path.dirname(rootPackageJsonFilepath) })); | ||
} | ||
|
||
const workspaceMatches = await Promise.all(promises); | ||
|
||
for (const workspaceMatch of workspaceMatches) { | ||
for (const directory of workspaceMatch) { | ||
const packageJson = require(Path.join(rootDirectory, directory, 'package.json')); | ||
|
||
if (!packageJson.private) { | ||
workspacePackages[packageJson.name] = { | ||
path: Path.join(rootDirectory, directory), | ||
packageJson | ||
}; | ||
} | ||
} | ||
} | ||
|
||
return workspacePackages; | ||
} | ||
|
||
/** | ||
* Main method. | ||
*/ | ||
async function main() { | ||
const args = getArguments(); | ||
|
||
if (!args.version) { | ||
throw new Error('Invalid arguments. Expected "--version={version}".'); | ||
} | ||
|
||
const version = args.version.replace('v', ''); | ||
const workspacePackages = await getWorkspacePackages(); | ||
const promises = []; | ||
|
||
console.log( | ||
SHELL_CODES.blue, | ||
SHELL_CODES.bold, | ||
`Setting workspace version to ${version}:`, | ||
SHELL_CODES.reset | ||
); | ||
|
||
for (const workspacePackageName of Object.keys(workspacePackages)) { | ||
const workspacePackage = workspacePackages[workspacePackageName]; | ||
|
||
console.log( | ||
SHELL_CODES.gray, | ||
SHELL_CODES.bold, | ||
` - ${workspacePackage.path}`, | ||
SHELL_CODES.reset | ||
); | ||
|
||
workspacePackage.packageJson.version = version; | ||
|
||
for (const dependencyType of ['dependencies', 'devDependencies', 'peerDependencies']) { | ||
const dependencies = workspacePackage.packageJson[dependencyType]; | ||
if (dependencies) { | ||
for (const dependency of Object.keys(dependencies)) { | ||
if (workspacePackages[dependency]) { | ||
dependencies[dependency] = dependencies[dependency].replace(VERSION_REGEXP, version); | ||
} | ||
} | ||
} | ||
} | ||
|
||
promises.push( | ||
FS.promises.writeFile( | ||
Path.join(workspacePackage.path, 'package.json'), | ||
JSON.stringify(workspacePackage.packageJson, null, 2) | ||
) | ||
); | ||
} | ||
|
||
console.log(SHELL_CODES.green, SHELL_CODES.bold, 'Done.', SHELL_CODES.reset); | ||
|
||
await Promise.all(promises); | ||
} |
Oops, something went wrong.