Last active
August 30, 2024 14:26
-
-
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', | |
); | |
} | |
process.exit(1); | |
} | |
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); | |
} | |
} | |
if (Array.isArray(parsed.workspaces) && parsed.workspaces.length > 0) { | |
for (const workspace of parsed.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) { | |
updatePackageJson(newDir); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment