Skip to content

Commit

Permalink
feat: update command handles Dockerized sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Dec 8, 2023
1 parent 50351c7 commit 4de60ce
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 80 deletions.
4 changes: 2 additions & 2 deletions yarn-project/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,11 +749,11 @@ export function getProgram(log: LogFn, debugLogger: DebugLogger): Command {
.description('Updates Nodejs and Noir dependencies')
.argument('[projectPath]', 'Path to the project directory', process.cwd())
.option('--contract [paths...]', 'Paths to contracts to update dependencies', [])
.option('--sandbox-version <semver>', 'The sandbox version to update to. Defaults to latest', 'latest')
.option('--aztec-version <semver>', 'The version to update Aztec packages to. Defaults to latest', 'latest')
.addOption(pxeOption)
.action(async (projectPath: string, options) => {
const { contract } = options;
await update(projectPath, contract, options.rpcUrl, options.sandboxVersion, log, debugLogger);
await update(projectPath, contract, options.rpcUrl, options.aztecVersion, log, debugLogger);
});

compileNoir(program, 'compile', log);
Expand Down
20 changes: 20 additions & 0 deletions yarn-project/cli/src/update/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { SemVer, parse } from 'semver';
import { atomicUpdateFile } from '../utils.js';
import { DependencyChanges } from './common.js';

const deprecatedNpmPackages = new Set(['@aztec/aztec-sandbox', '@aztec/cli']);
const npmDeprecationMessage = `
The following packages have been deprecated and will no longer be updated on the npm registry:
${Array.from(deprecatedNpmPackages)
.map(pkg => ` - ${pkg}`)
.join('\n')}
Remove them from package.json and switch to Docker images instead. See https://docs.aztec.network/dev_docs/getting_started/quickstart#install-the-sandbox
`;

/**
* Looks up a package.json file and returns its contents
* @param projectPath - Path to Nodejs project
Expand Down Expand Up @@ -68,6 +77,8 @@ export async function updateAztecDeps(
log(`Updating @aztec packages to ${aztecVersion} in ${relative(process.cwd(), changes.file)}`);
const version = aztecVersion.version;

let detectedDeprecatedPackages = false;

for (const depType of ['dependencies', 'devDependencies'] as const) {
const dependencies = pkg[depType];
if (!dependencies) {
Expand All @@ -84,6 +95,11 @@ export async function updateAztecDeps(
continue;
}

if (deprecatedNpmPackages.has(name)) {
detectedDeprecatedPackages = true;
continue;
}

if (dependencies[name] !== version) {
changes.dependencies.push({
name,
Expand All @@ -96,6 +112,10 @@ export async function updateAztecDeps(
}
}

if (detectedDeprecatedPackages) {
log(npmDeprecationMessage);
}

if (changes.dependencies.length > 0) {
const contents = JSON.stringify(pkg, null, 2) + '\n';
await atomicUpdateFile(resolve(join(projectPath, 'package.json')), contents);
Expand Down
125 changes: 47 additions & 78 deletions yarn-project/cli/src/update/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,90 +2,69 @@
import { DebugLogger, LogFn } from '@aztec/foundation/log';

import { relative, resolve } from 'path';
import { SemVer, coerce, gt, lt, parse } from 'semver';
import { SemVer, lt, parse } from 'semver';

import { createCompatibleClient } from '../client.js';
import { GITHUB_TAG_PREFIX } from '../github.js';
import { DependencyChanges } from './common.js';
import { updateAztecNr } from './noir.js';
import { getNewestVersion as getLatestVersion, readPackageJson, updateAztecDeps, updateLockfile } from './npm.js';
import { getNewestVersion, updateAztecDeps, updateLockfile } from './npm.js';

const PXE_PACKAGE = '@aztec/pxe';

// Since we don't have an npm package for sandbox or cli anymore,
// 1. Remind people to update docker image
// 2. Update local npm dependencies
// 3. Error if @aztec/aztec-sandbox or @aztec/cli is in package.json
// 4. Update nargo.toml to new aztec-nr version.
const AZTECJS_PACKAGE = '@aztec/aztec.js';
const UPDATE_DOCS_URL = 'https://docs.aztec.network/dev_docs/updating';

export async function update(
projectPath: string,
contracts: string[],
pxeUrl: string,
sandboxVersion: string,
aztecVersion: string,
log: LogFn,
debugLog: DebugLogger,
): Promise<void> {
const targetSandboxVersion =
sandboxVersion === 'latest' ? await getLatestVersion(PXE_PACKAGE, 'latest') : parse(sandboxVersion);
const targetAztecVersion =
aztecVersion === 'latest' ? await getNewestVersion(AZTECJS_PACKAGE, 'latest') : parse(aztecVersion);

if (!targetSandboxVersion) {
throw new Error(`Invalid aztec version ${sandboxVersion}`);
if (!targetAztecVersion) {
throw new Error(`Invalid aztec version ${aztecVersion}`);
}

// TODO: Change.
let currentSandboxVersion = await getNpmSandboxVersion(projectPath, log);

if (!currentSandboxVersion) {
currentSandboxVersion = await getRemoteSandboxVersion(pxeUrl, log, debugLog);
await compareSandboxVersion(targetAztecVersion, pxeUrl, log, debugLog);

if (currentSandboxVersion && lt(currentSandboxVersion, targetSandboxVersion)) {
log(`
Sandbox is older than version ${targetSandboxVersion}. If running via docker-compose, follow update instructions:
https://docs.aztec.network/dev_docs/cli/updating
Once the sandbox is updated, run the \`aztec-cli update\` command again`);
return;
const projectDependencyChanges: DependencyChanges[] = [];
try {
const npmChanges = await updateAztecDeps(resolve(process.cwd(), projectPath), targetAztecVersion, log);
if (npmChanges.dependencies.length > 0) {
updateLockfile(projectPath, log);
}
}

if (!currentSandboxVersion) {
throw new Error('Sandbox version could not be detected');
}

// sanity check
if (gt(currentSandboxVersion, targetSandboxVersion)) {
throw new Error('Local sandbox version is newer than latest version.');
}

const npmChanges = await updateAztecDeps(projectPath, targetSandboxVersion, log);
if (npmChanges.dependencies.length > 0) {
updateLockfile(projectPath, log);
projectDependencyChanges.push(npmChanges);
} catch (err) {
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') {
log(`No package.json found in ${projectPath}. Skipping npm update...`);
} else {
throw err;
}
}

const contractChanges: DependencyChanges[] = [];
for (const contract of contracts) {
try {
contractChanges.push(
projectDependencyChanges.push(
await updateAztecNr(
resolve(projectPath, contract),
`${GITHUB_TAG_PREFIX}-v${targetSandboxVersion.version}`,
resolve(process.cwd(), projectPath, contract),
`${GITHUB_TAG_PREFIX}-v${targetAztecVersion.version}`,
log,
),
);
} catch (err) {
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') {
log(`No Nargo.toml found in ${relative(process.cwd(), contract)}. Skipping...`);
process.exit(1);
} else {
throw err;
}

throw err;
}
}

printChanges(npmChanges, log);

contractChanges.forEach(changes => {
projectDependencyChanges.forEach(changes => {
printChanges(changes, log);
});
}
Expand All @@ -101,39 +80,29 @@ function printChanges(changes: DependencyChanges, log: LogFn): void {
}
}

async function getNpmSandboxVersion(projectPath: string, log: LogFn): Promise<SemVer | null> {
try {
const pkg = await readPackageJson(projectPath);
// use coerce instead of parse because it eliminates semver operators like ~ and ^
if (pkg.dependencies?.[PXE_PACKAGE]) {
return coerce(pkg.dependencies[PXE_PACKAGE]);
} else if (pkg.devDependencies?.[PXE_PACKAGE]) {
return coerce(pkg.devDependencies[PXE_PACKAGE]);
} else {
return null;
}
} catch (err) {
if (err instanceof Error && 'code' in err && err.code === 'ENOENT') {
log(`No package.json found in ${projectPath}`);
process.exit(1);
}

throw err;
}
}

async function getRemoteSandboxVersion(pxeUrl: string, log: LogFn, debugLog: DebugLogger): Promise<SemVer | null> {
/**
* Checks if the sandbox version is older than the target version and prints a warning if so.
* If the sandbox is not running then it will print a warning message instead of throwing.
*
* @param targetAztecVersion - The version to update to
* @param pxeUrl - URL of the PXE.
* @param log - Logging function
* @param debugLog - Logging function
*/
async function compareSandboxVersion(targetAztecVersion: SemVer, pxeUrl: string, log: LogFn, debugLog: DebugLogger) {
try {
const client = await createCompatibleClient(pxeUrl, debugLog);
const nodeInfo = await client.getNodeInfo();

return parse(nodeInfo.sandboxVersion);
} catch (err) {
if (err instanceof Error && err.message === 'fetch failed') {
log(`Could not connect to Sandbox running on ${pxeUrl}`);
process.exit(1);
const runningSandboxVersion = parse(nodeInfo.sandboxVersion);
if (!runningSandboxVersion) {
throw new Error();
}

throw err;
if (lt(runningSandboxVersion, targetAztecVersion)) {
log(`
Aztec Sandbox is older than version ${targetAztecVersion}. Follow update instructions at ${UPDATE_DOCS_URL}\n`);
}
} catch (err) {
log(`Aztec Sandbox not running. Update to ${targetAztecVersion} by following instructions at ${UPDATE_DOCS_URL}\n`);
}
}

0 comments on commit 4de60ce

Please sign in to comment.