Last active
August 5, 2025 13:49
-
-
Save peaBerberian/c199f08e0f28a854e3929abea6964eed to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env node | |
import * as fs from 'fs'; | |
import * as path from 'path'; | |
import { fileURLToPath } from 'url'; | |
const VERSION_REGXP = | |
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; | |
const URL_REGXP = /^[a-zA-Z]+:\/\//; | |
const branchOrHashOrVersion = process.argv[2]; | |
if (branchOrHashOrVersion === undefined) { | |
console.error('Error: No branch or hash argument given'); | |
console.log('Usage: node update_rx_player <git hash>'); | |
process.exit(1); | |
} | |
const currentDirectory = path.dirname(fileURLToPath(import.meta.url)); | |
updatePackageJson(currentDirectory); | |
function updatePackageJson(dir) { | |
const packageJsonFileName = path.join(dir, 'package.json'); | |
let jsonStr; | |
try { | |
jsonStr = String(fs.readFileSync(packageJsonFileName)); | |
} catch (e) { | |
if (dir === currentDirectory && e?.code === 'ENOENT') { | |
console.error( | |
'Error: no package.json file in the current directory. ' + | |
'Are you using this script in a JavaScript project?', | |
); | |
} else if (e?.code === 'ENOENT') { | |
console.error( | |
`Error: no package.json file in the workspace found at ${dir}.`, | |
); | |
} else { | |
console.error( | |
`Error: impossible to read ${dir}:`, | |
e instanceof Error ? e.toString() : 'Unknown Error', | |
); | |
} | |
return; | |
} | |
let parsed; | |
try { | |
parsed = JSON.parse(jsonStr); | |
} catch (e) { | |
console.error( | |
`Error: issue while parsing ${packageJsonFileName}:`, | |
e instanceof Error ? e.toString() : 'Unknown Error', | |
); | |
process.exit(1); | |
} | |
let updated = false; | |
if (parsed.dependencies?.['rx-player'] !== undefined) { | |
console.log(`Updating ${packageJsonFileName} dependencies`); | |
if ( | |
VERSION_REGXP.test(branchOrHashOrVersion) || | |
URL_REGXP.test(branchOrHashOrVersion) | |
) { | |
parsed.dependencies['rx-player'] = branchOrHashOrVersion; | |
} else { | |
parsed.dependencies['rx-player'] = | |
`canalplus/rx-player#${branchOrHashOrVersion}`; | |
} | |
updated = true; | |
} | |
if (parsed.devDependencies?.['rx-player'] !== undefined) { | |
console.log(`Updating ${packageJsonFileName} devDependencies`); | |
if ( | |
VERSION_REGXP.test(branchOrHashOrVersion) || | |
URL_REGXP.test(branchOrHashOrVersion) | |
) { | |
parsed.dependencies['rx-player'] = branchOrHashOrVersion; | |
} else { | |
parsed.devDependencies['rx-player'] = | |
`canalplus/rx-player#${branchOrHashOrVersion}`; | |
} | |
updated = true; | |
} | |
if (updated) { | |
try { | |
fs.writeFileSync( | |
packageJsonFileName, | |
JSON.stringify(parsed, null, 2) + '\n', | |
); | |
} catch (e) { | |
console.error( | |
`Error: issue while writing an updated ${packageJsonFileName}:`, | |
e instanceof Error ? e.toString() : 'Unknown Error', | |
); | |
process.exit(1); | |
} | |
} | |
let workspaces = []; | |
if (Array.isArray(parsed.workspaces) && parsed.workspaces.length > 0) { | |
workspaces = parsed.workspaces; | |
} else if ( | |
Array.isArray(parsed.workspaces?.packages) && | |
parsed.workspaces?.packages.length > 0 | |
) { | |
workspaces = parsed.workspaces.packages; | |
} | |
for (const workspace of workspaces) { | |
// IMPORTANT: globSync is experimental as of now (Node.js 22) | |
let dirs; | |
try { | |
dirs = fs.globSync(workspace); | |
} catch (e) { | |
console.error( | |
`Error: issue while calling fs.globSync, please ensure you have an up-to-date Node.js executable:`, | |
e instanceof Error ? e.toString() : 'Unknown Error', | |
); | |
process.exit(1); | |
} | |
for (const newDir of dirs) { | |
try { | |
const stats = fs.statSync(newDir); | |
if (!stats.isDirectory()) { | |
continue; | |
} | |
} catch (err) { | |
console.error( | |
`Could not determine if "${newDir}" was a directory:`, | |
err, | |
); | |
continue; | |
} | |
updatePackageJson(newDir); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples:
Also works for published URLs like:
And also works for commits and tags:
# Sha1 of a commit or tag name node update_rx_version.mjs 6828dcaf14dc531c3a78e8d32ce5c6056d7eb583 yarn