Last active
February 1, 2024 09:54
-
-
Save Danielduel/3a98a96e10da61a3516d8e42b156e332 to your computer and use it in GitHub Desktop.
Use Deno to upgrade yarn dependencies with filters
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
import { parseArgs } from "https://deno.land/[email protected]/cli/parse_args.ts"; | |
const args = parseArgs(Deno.args, { | |
string: [ "node_version", "project_path", "filters" ] | |
}); | |
const requiredArg = (argument: unknown, argumentName: string) => { | |
if (!argument) { | |
console.log(`${argumentName} is required`); | |
Deno.exit(); | |
} | |
} | |
const projectPath = args.project_path!; | |
requiredArg(projectPath, "project_path"); | |
const nodeVersion = args.node_version!; | |
requiredArg(nodeVersion, "node_version"); | |
const filtersAsString = args.filters!; | |
requiredArg(filtersAsString, "filters"); | |
const filters = filtersAsString | |
.split(",") | |
.filter(x => !!x); | |
console.log("Parsed filter array: " + JSON.stringify(filters)); | |
const packageContent: Record<string, Record<string, unknown>> = JSON.parse(await Deno.readTextFile(`${projectPath}/package.json`)); | |
const getProjectYarn = async (projectPath: string) => { | |
const command = new Deno.Command( | |
"/bin/sh", | |
{ | |
cwd: projectPath, | |
args: [ | |
"-c", | |
"which yarn" | |
], | |
} | |
); | |
const {stdout, /* stderr */ } = await command.output(); | |
const stdoutParsed = new TextDecoder().decode(stdout); | |
return stdoutParsed.replaceAll("\n", ""); | |
} | |
const yarnPath = await getProjectYarn(projectPath); | |
const packagesInProject = [ | |
...Object.keys(packageContent.dependencies), | |
...Object.keys(packageContent.devDependencies), | |
]; | |
const packagesToUpgrade = packagesInProject | |
.filter(packageName => (filters.some(filter => packageName.startsWith(filter)))); | |
const upgradePackages = async (projectPath: string, packageNames: string[]) => { | |
const joinedPackageNames = packageNames.join(" "); | |
const command = new Deno.Command( | |
"/bin/sh", | |
{ | |
cwd: projectPath, | |
args: [ | |
"-c", | |
`source ~/.nvm/nvm.sh && nvm use ${nodeVersion} && ${yarnPath} upgrade ${joinedPackageNames} --latest` | |
], | |
} | |
); | |
const { stdout, stderr } = await command.output(); | |
const stdoutParsed = new TextDecoder().decode(stdout); | |
const stderrParsed = new TextDecoder().decode(stderr); | |
console.log(stdoutParsed); | |
console.log(stderrParsed); | |
return new TextDecoder().decode(stdout); | |
} | |
const results = await upgradePackages(projectPath, packagesToUpgrade); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment