-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add script to update all polkadot-js deps, and resolutions to latest #63
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8b79891
feat: script to update all polkadot-js deps and resolutions
TarikGul 8192ac7
fix: add use bin line
TarikGul 963dadd
add yargs for path
TarikGul 329def6
add inline documentation and fix small bug
TarikGul e5b61cb
fix yarn lock
TarikGul 76a0641
update yield to continue, add ignorePaths
TarikGul bb6b66d
add lib to ignore paths
TarikGul f4f7eef
remove execsync curl, and replace with https
TarikGul 3269278
cleanup
TarikGul b88b490
cleanup
TarikGul e5b1bc1
cleanup inline comments
TarikGul 652e86d
explicit GET
TarikGul b608707
add try catch for fs.write and refactor some code
TarikGul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,200 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const https = require('https'); | ||
const { resolve } = require('path'); | ||
const { readdir, stat } = require('fs').promises; | ||
|
||
/** | ||
* polkadot-js package information, and specifications | ||
*/ | ||
const depSpecs = { | ||
api: { | ||
releaseLink: 'https://api.github.com/repos/polkadot-js/api/releases/latest', | ||
packages: [ | ||
'api', | ||
'api-augment', | ||
'api-base', | ||
'api-derive', | ||
'rpc-core', | ||
'rpc-augment', | ||
'rpc-provider', | ||
'types', | ||
'types-augment', | ||
'types-codec', | ||
'types-create', | ||
'types-support', | ||
'types-known', | ||
], | ||
}, | ||
apps: { | ||
releaseLink: 'https://api.github.com/repos/polkadot-js/apps/releases/latest', | ||
packages: [ | ||
'apps-config' | ||
], | ||
}, | ||
common: { | ||
releaseLink: 'https://api.github.com/repos/polkadot-js/common/releases/latest', | ||
packages: [ | ||
'util', | ||
'util-crypto', | ||
'keyring', | ||
'networks', | ||
'x-fetch', | ||
'x-global', | ||
'x-bigint', | ||
'x-ws', | ||
], | ||
}, | ||
wasm: { | ||
releaseLink: 'https://api.github.com/repos/polkadot-js/wasm/releases/latest', | ||
packages: [ | ||
'wasm-crypto' | ||
], | ||
} | ||
} | ||
|
||
/** | ||
* This will fetch the latest release from github, returns the response as | ||
* a JSON object. | ||
* | ||
* @param {string} url Latest release link | ||
* @returns | ||
*/ | ||
async function fetchRelease(url) { | ||
const [h, ...args] = url.split('://')[1].split('/'); | ||
const [host, _port] = h.split(':'); | ||
|
||
const options = { | ||
method: 'GET', | ||
host, | ||
port: 443, | ||
path: '/' + args.join('/'), | ||
headers: { | ||
'User-Agent': 'request' | ||
} | ||
}; | ||
|
||
return new Promise(function(resolve, reject) { | ||
const req = https.request(options, function (res) { | ||
if (res.statusCode < 200 || res.statusCode >= 300) { | ||
return reject(new Error(`Status Code: ${res.statusCode}`)); | ||
} | ||
|
||
const data = []; | ||
|
||
res.on('data', chunk => { | ||
data.push(chunk); | ||
}); | ||
|
||
res.on('end', function() { | ||
resolve(JSON.parse(Buffer.concat(data).toString())) | ||
}); | ||
}) | ||
|
||
req.on('error', reject); | ||
|
||
req.end(); | ||
}) | ||
} | ||
|
||
/** | ||
* Given a package.json file update all the resolutions and dependencies | ||
* that are polkadot-js packages | ||
* | ||
* @param {string} path Path to the package.json file | ||
* @param {object} config Object contains keys that are package names, and values | ||
* which are their corresponding versions | ||
*/ | ||
function updatePackageJson(path, config) { | ||
const rawData = fs.readFileSync(path); | ||
const packageJson = JSON.parse(rawData); | ||
const deps = packageJson['dependencies']; | ||
const resolutions = packageJson['resolutions']; | ||
|
||
for(const packageName of Object.keys(config)) { | ||
// check and update dependencies key | ||
if (deps && Object.keys(deps).includes(packageName)) { | ||
packageJson['dependencies'][packageName] = config[packageName]; | ||
} | ||
|
||
// check and update resolutions key | ||
if (resolutions && Object.keys(resolutions).includes(packageName)) { | ||
packageJson['resolutions'][packageName] = config[packageName]; | ||
} | ||
} | ||
|
||
try { | ||
fs.writeFileSync(path, JSON.stringify(packageJson, null, 2).concat('\n')); | ||
console.log(`Succesfully updated => ${path}`); | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
|
||
/** | ||
* Generator function to recursively lazy iterate through each directory. This searches | ||
* for all package.json files in a dir, not including node_modules, and hidden dirs. | ||
* | ||
* @param {string} rootPath root path of the repository | ||
* @param {array} ignorePaths an array of paths to ignore | ||
*/ | ||
async function* getFiles(rootPath, ignorePaths) { | ||
const fileNames = await readdir(rootPath); | ||
for (const fileName of fileNames) { | ||
const path = resolve(rootPath, fileName); | ||
const curPath = path.split('/').slice(-1)[0]; | ||
if (ignorePaths.includes(curPath)) { | ||
// Do not traverse any path that is ignored | ||
continue; | ||
} else if (curPath.startsWith('.')) { | ||
// Do no traverse any hidden directories | ||
continue; | ||
} else if ((await stat(path)).isDirectory()) { | ||
yield* getFiles(path, ignorePaths); | ||
} else if (path.endsWith('package.json')) { | ||
yield path; | ||
} | ||
} | ||
} | ||
|
||
async function main(rootPath = './') { | ||
/** | ||
* Paths to ignore when travesing a directions | ||
*/ | ||
const pathsToIgnore = [ | ||
'node_modules', | ||
'build', | ||
'lib', | ||
]; | ||
// iterate through constants and create an object that stores each packages name | ||
// to their corresponding versions. | ||
const packageToVersion = {}; | ||
for (const packageKey of Object.keys(depSpecs)) { | ||
const packageRelease = await fetchRelease(depSpecs[packageKey].releaseLink); | ||
const packageVersion = packageRelease['tag_name']; | ||
for (const packageName of depSpecs[packageKey].packages) { | ||
packageToVersion[`@polkadot/${packageName}`] = packageVersion.substring(1); | ||
} | ||
} | ||
|
||
// Iterate through each file using the generator function and find the package.json | ||
for await (const path of getFiles(rootPath, pathsToIgnore)) { | ||
if (path) { | ||
updatePackageJson(path, packageToVersion); | ||
} | ||
} | ||
} | ||
|
||
const argv = require('yargs') | ||
.options({ | ||
'--path': { | ||
description: 'Path to directory', | ||
type: 'string' | ||
} | ||
}) | ||
.strict() | ||
.argv; | ||
|
||
main(argv.path).catch(err => console.log(err)).finally(() => process.exit()); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It this recursive variant an idiom in JS?
It's elegant and nice but I would still prefer iterative variant...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say in some senses yes. When it comes to most kinds of traversing, recursion is pretty popular. The main focus being, it's a little easier to maintain and read IMO. Where it could be an issue is where memory intensive processing is happening and can cause memory leaks. In this case though we are void of that.
In most cases I would prefer the iterative variant as well, but in this case for readability and maintenance I would say lets keep it recursive. It does a nice job of establishing the lazy iterator without any confusing logic.