-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add cli command update aztec dependencies (#3128)
This PR adds a new command to aztec-cli to update a contract's Aztec.nr and `@aztec` dependencies. LE: This command was refactored to `update` and now updates both Nargo.toml _and_ package.json. Fix #2870 # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [x] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [x] Every change is related to the PR description. - [x] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist).
- Loading branch information
Showing
25 changed files
with
501 additions
and
91 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
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
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,3 @@ | ||
export const GITHUB_OWNER = 'AztecProtocol'; | ||
export const GITHUB_REPO = 'aztec-packages'; | ||
export const GITHUB_TAG_PREFIX = 'aztec-packages'; |
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
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,16 @@ | ||
/** | ||
* Tracks changes to dependencies | ||
*/ | ||
export type DependencyChanges = { | ||
/** Which file was changed */ | ||
file: string; | ||
/** changes done to the file */ | ||
dependencies: Array<{ | ||
/** Name of the dependency being changed */ | ||
name: string; | ||
/** Previous version of the dependency */ | ||
from: string; | ||
/** New version of the dependency (after the update) */ | ||
to: string; | ||
}>; | ||
}; |
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,79 @@ | ||
import { LogFn } from '@aztec/foundation/log'; | ||
import { NoirPackageConfig, parseNoirPackageConfig } from '@aztec/foundation/noir'; | ||
|
||
import TOML from '@ltd/j-toml'; | ||
import { readFile } from 'fs/promises'; | ||
import { EOL } from 'os'; | ||
import { join, relative, resolve } from 'path'; | ||
|
||
import { atomicUpdateFile } from '../utils.js'; | ||
import { DependencyChanges } from './common.js'; | ||
|
||
/** | ||
* Updates Aztec.nr dependencies | ||
* @param contractPath - Path to the contract to be updated | ||
* @param tag - The tag to update to | ||
* @param log - Logging function | ||
*/ | ||
export async function updateAztecNr(contractPath: string, tag: string, log: LogFn): Promise<DependencyChanges> { | ||
const configFilepath = resolve(join(contractPath, 'Nargo.toml')); | ||
const packageConfig = parseNoirPackageConfig(TOML.parse(await readFile(configFilepath, 'utf-8'))); | ||
const changes: DependencyChanges = { | ||
dependencies: [], | ||
file: configFilepath, | ||
}; | ||
|
||
log(`Updating Aztec.nr libraries to ${tag} in ${relative(process.cwd(), changes.file)}`); | ||
for (const dep of Object.values(packageConfig.dependencies)) { | ||
if (!('git' in dep)) { | ||
continue; | ||
} | ||
|
||
// remove trailing slash | ||
const gitUrl = dep.git.toLowerCase().replace(/\/$/, ''); | ||
if (gitUrl !== 'https://github.com/aztecprotocol/aztec-packages') { | ||
continue; | ||
} | ||
|
||
if (dep.tag !== tag) { | ||
// show the Aztec.nr package name rather than the lib name | ||
const dirParts = dep.directory?.split('/') ?? []; | ||
changes.dependencies.push({ | ||
name: dirParts.slice(-2).join('/'), | ||
from: dep.tag, | ||
to: tag, | ||
}); | ||
|
||
dep.tag = tag; | ||
} | ||
} | ||
|
||
if (changes.dependencies.length > 0) { | ||
const contents = prettyPrintTOML(packageConfig); | ||
await atomicUpdateFile(configFilepath, contents); | ||
} | ||
|
||
return changes; | ||
} | ||
|
||
/** | ||
* Pretty prints a NoirPackageConfig to a string | ||
* @param packageConfig - Nargo.toml contents | ||
* @returns The Nargo.toml contents as a string | ||
*/ | ||
function prettyPrintTOML(packageConfig: NoirPackageConfig): string { | ||
// hint to TOML.stringify how we want the file to look like | ||
return TOML.stringify( | ||
{ | ||
package: TOML.Section(packageConfig.package), | ||
dependencies: TOML.Section( | ||
Object.fromEntries(Object.entries(packageConfig.dependencies).map(([name, dep]) => [name, TOML.inline(dep)])), | ||
), | ||
}, | ||
{ | ||
indent: 2, | ||
newline: EOL as any, | ||
newlineAround: 'section', | ||
}, | ||
); | ||
} |
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,129 @@ | ||
import { LogFn } from '@aztec/foundation/log'; | ||
|
||
import { spawnSync } from 'child_process'; | ||
import { existsSync } from 'fs'; | ||
import { readFile } from 'fs/promises'; | ||
import { join, relative, resolve } from 'path'; | ||
import { SemVer, parse } from 'semver'; | ||
|
||
import { atomicUpdateFile } from '../utils.js'; | ||
import { DependencyChanges } from './common.js'; | ||
|
||
/** | ||
* Looks up a package.json file and returns its contents | ||
* @param projectPath - Path to Nodejs project | ||
* @returns The parsed package.json | ||
*/ | ||
export async function readPackageJson(projectPath: string): Promise<{ | ||
/** dependencies */ | ||
dependencies?: Record<string, string>; | ||
}> { | ||
const configFilepath = resolve(join(projectPath, 'package.json')); | ||
const pkg = JSON.parse(await readFile(configFilepath, 'utf-8')); | ||
|
||
return pkg; | ||
} | ||
|
||
/** | ||
* Queries the npm registry for the latest version of a package | ||
* @param packageName - The package to query | ||
* @param distTag - The distribution tag | ||
* @returns The latest version of the package on that distribution tag | ||
*/ | ||
export async function getNewestVersion(packageName: string, distTag = 'latest'): Promise<SemVer> { | ||
const url = new URL(packageName, 'https://registry.npmjs.org/'); | ||
const response = await fetch(url); | ||
if (!response.ok) { | ||
throw new Error(`Failed to fetch ${url}`); | ||
} | ||
|
||
const body = await response.json(); | ||
const latestVersion = parse(body['dist-tags'][distTag]); | ||
if (!latestVersion) { | ||
throw new Error(`Failed to get latest version from registry`); | ||
} | ||
|
||
return latestVersion; | ||
} | ||
|
||
/** | ||
* Updates a project's \@aztec/* dependencies to the specific version | ||
* @param projectPath - Path to Nodejs project | ||
* @param aztecVersion - The version to update to | ||
* @returns True if the project was updated | ||
*/ | ||
export async function updateAztecDeps( | ||
projectPath: string, | ||
aztecVersion: SemVer, | ||
log: LogFn, | ||
): Promise<DependencyChanges> { | ||
const pkg = await readPackageJson(projectPath); | ||
const changes: DependencyChanges = { | ||
file: resolve(join(projectPath, 'package.json')), | ||
dependencies: [], | ||
}; | ||
|
||
if (!pkg.dependencies) { | ||
return changes; | ||
} | ||
|
||
log(`Updating @aztec packages to ${aztecVersion} in ${relative(process.cwd(), changes.file)}`); | ||
const version = aztecVersion.version; | ||
|
||
for (const name of Object.keys(pkg.dependencies)) { | ||
if (!name.startsWith('@aztec/')) { | ||
continue; | ||
} | ||
|
||
// different release schedule | ||
if (name === '@aztec/aztec-ui') { | ||
continue; | ||
} | ||
|
||
if (pkg.dependencies[name] !== version) { | ||
changes.dependencies.push({ | ||
name, | ||
from: pkg.dependencies[name], | ||
to: version, | ||
}); | ||
|
||
pkg.dependencies[name] = version; | ||
} | ||
} | ||
|
||
if (changes.dependencies.length > 0) { | ||
const contents = JSON.stringify(pkg, null, 2) + '\n'; | ||
await atomicUpdateFile(resolve(join(projectPath, 'package.json')), contents); | ||
} | ||
|
||
return changes; | ||
} | ||
|
||
/** | ||
* Updates a project's yarn.lock or package-lock.json | ||
* @param projectPath - Path to Nodejs project | ||
*/ | ||
export function updateLockfile(projectPath: string, log: LogFn): void { | ||
const isNpm = existsSync(resolve(join(projectPath, 'package-lock.json'))); | ||
const isYarn = existsSync(resolve(join(projectPath, 'yarn.lock'))); | ||
const isPnpm = existsSync(resolve(join(projectPath, 'pnpm-lock.yaml'))); | ||
|
||
if (isPnpm) { | ||
spawnSync('pnpm', ['install'], { | ||
cwd: projectPath, | ||
stdio: 'inherit', | ||
}); | ||
} else if (isYarn) { | ||
spawnSync('yarn', ['install'], { | ||
cwd: projectPath, | ||
stdio: 'inherit', | ||
}); | ||
} else if (isNpm) { | ||
spawnSync('npm', ['install'], { | ||
cwd: projectPath, | ||
stdio: 'inherit', | ||
}); | ||
} else { | ||
log(`No lockfile found in ${projectPath}. Skipping lockfile update...`); | ||
} | ||
} |
Oops, something went wrong.