@aztec/cli 0.7.4 → 0.7.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/unbox.ts CHANGED
@@ -20,12 +20,12 @@ const NOIR_CONTRACTS_PATH = 'yarn-project/noir-contracts/src/contracts';
20
20
  const BOXES_PATH = 'yarn-project/boxes';
21
21
 
22
22
  /**
23
- * Converts a contract name in "upper camel case" to a folder name in snake case.
23
+ * Converts a contract name in "upper camel case" to a folder name in snake case or kebab case.
24
24
  * @param contractName - The contract name.
25
25
  * @returns The folder name.
26
26
  * */
27
- function contractNameToFolder(contractName: string): string {
28
- return contractName.replace(/[\w]([A-Z])/g, m => m[0] + '_' + m[1]).toLowerCase();
27
+ function contractNameToFolder(contractName: string, separator = '-'): string {
28
+ return contractName.replace(/[\w]([A-Z])/g, m => `${m[0]}${separator}${m[1]}`).toLowerCase();
29
29
  }
30
30
 
31
31
  /**
@@ -33,8 +33,13 @@ function contractNameToFolder(contractName: string): string {
33
33
  * Otherwise, we download the contract source code from the `noir-contracts` and `noir-libs` subpackages.
34
34
  */
35
35
  async function isDirectoryNonEmpty(directoryPath: string): Promise<boolean> {
36
- const files = await fs.readdir(directoryPath);
37
- return files.length > 0;
36
+ try {
37
+ const files = await fs.readdir(directoryPath);
38
+ return files.length > 0;
39
+ } catch (e) {
40
+ // Directory does not exist.
41
+ return false;
42
+ }
38
43
  }
39
44
 
40
45
  /**
@@ -44,7 +49,7 @@ async function isDirectoryNonEmpty(directoryPath: string): Promise<boolean> {
44
49
  * @param localOutputPath - local path to copy to
45
50
  */
46
51
  async function copyFolderFromGithub(data: JSZip, repositoryFolderPath: string, localOutputPath: string, log: LogFn) {
47
- log('downloading from github:', repositoryFolderPath);
52
+ log(`Downloading from github: ${repositoryFolderPath}`);
48
53
  const repositoryDirectories = Object.values(data.files).filter(file => {
49
54
  return file.dir && file.name.startsWith(repositoryFolderPath);
50
55
  });
@@ -83,13 +88,14 @@ async function downloadContractAndBoxFromGithub(
83
88
  outputPath: string,
84
89
  log: LogFn,
85
90
  ): Promise<void> {
91
+ const tagName = `aztec-packages-v${tagVersion}`;
86
92
  // small string conversion, in the ABI the contract name looks like PrivateToken
87
93
  // but in the repostory it looks like private_token
88
- const snakeCaseContractName = contractNameToFolder(contractName);
89
94
 
90
- log(`Downloaded '@aztex/boxes/${snakeCaseContractName}' to ${outputPath}`);
95
+ const kebabCaseContractName = contractNameToFolder(contractName, '-');
96
+ log(`Downloading @aztex/boxes/${kebabCaseContractName} to ${outputPath}...`);
91
97
  // Step 1: Fetch the monorepo ZIP from GitHub, matching the CLI version
92
- const url = `https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/archive/refs/tags/aztec-packages-v${tagVersion}.zip`;
98
+ const url = `https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/archive/refs/tags/${tagName}.zip`;
93
99
  const response = await fetch(url);
94
100
  const buffer = await response.arrayBuffer();
95
101
 
@@ -98,39 +104,39 @@ async function downloadContractAndBoxFromGithub(
98
104
 
99
105
  // Step 2: copy the '@aztec/boxes/{contract-name}' subpackage to the output directory
100
106
  // this is currently only implemented for PrivateToken under 'boxes/private-token/'
101
- const repoDirectoryPrefix = `${GITHUB_REPO}-v${tagVersion}/`;
107
+ const repoDirectoryPrefix = `${GITHUB_REPO}-${tagName}`;
102
108
 
103
- const boxPath = `${repoDirectoryPrefix}${BOXES_PATH}/${snakeCaseContractName}`;
109
+ const boxPath = `${repoDirectoryPrefix}/${BOXES_PATH}/${kebabCaseContractName}`;
104
110
  await copyFolderFromGithub(data, boxPath, outputPath, log);
105
111
 
106
- const boxContainsNoirSource = await isDirectoryNonEmpty(`${outputPath}/src/contracts`);
112
+ const contractTargetDirectory = path.join(outputPath, 'src', 'contracts');
113
+ const boxContainsNoirSource = await isDirectoryNonEmpty(contractTargetDirectory);
107
114
  if (boxContainsNoirSource) {
108
115
  return;
109
116
  }
117
+
110
118
  // this remaining logic only kicks in if the box doesn't already have a src/contracts folder
111
119
  // in which case we optimistically grab the noir source files from the
112
120
  // noir-contracts and noir-libs subpackages and pray that the versions are compatible
121
+ log('Copying noir contracts...');
113
122
 
114
123
  // source noir files for the contract are in this folder
115
- const contractFolder = `${NOIR_CONTRACTS_PATH}/${snakeCaseContractName}_contract`;
124
+ const snakeCaseContractName = contractNameToFolder(contractName, '_');
125
+ const contractDirectoryPath = `${repoDirectoryPrefix}/${NOIR_CONTRACTS_PATH}/${snakeCaseContractName}_contract`;
116
126
  // copy the noir contracts to the output directory under subdir /src/contracts/
117
- const contractDirectoryPath = `${repoDirectoryPrefix}${contractFolder}/`;
118
-
119
127
  const contractFiles = Object.values(data.files).filter(file => {
120
128
  return !file.dir && file.name.startsWith(contractDirectoryPath);
121
129
  });
122
130
 
123
- const contractTargetDirectory = path.join(outputPath, 'src', 'contracts');
124
- await fs.mkdir(contractTargetDirectory, { recursive: true });
125
131
  // Nargo.toml file needs to be in the root of the contracts directory,
126
132
  // and noir files in the src/ subdirectory
127
133
  await fs.mkdir(path.join(contractTargetDirectory, 'src'), { recursive: true });
128
134
  for (const file of contractFiles) {
129
- const targetPath = path.join(contractTargetDirectory, file.name.replace(contractDirectoryPath, ''));
130
- log(`Copying ${file.name} to ${targetPath}`);
135
+ const filename = file.name.replace(`${contractDirectoryPath}/`, '');
136
+ const targetPath = path.join(contractTargetDirectory, filename);
131
137
  const content = await file.async('nodebuffer');
132
138
  await fs.writeFile(targetPath, content);
133
- log(`Copied ${file.name} to ${targetPath}`);
139
+ log(` ${filename}`);
134
140
  }
135
141
  }
136
142
  /**
@@ -143,7 +149,8 @@ async function downloadContractAndBoxFromGithub(
143
149
  */
144
150
  async function updatePackagingConfigurations(packageVersion: string, outputPath: string, log: LogFn): Promise<void> {
145
151
  await updatePackageJsonVersions(packageVersion, outputPath, log);
146
- await updateTsConfig(outputPath, log);
152
+ await updateTsConfig('tsconfig.json', outputPath, log);
153
+ await updateTsConfig('tsconfig.dest.json', outputPath, log);
147
154
  await updateNargoToml(packageVersion, outputPath, log);
148
155
  }
149
156
 
@@ -160,7 +167,7 @@ async function updateNargoToml(packageVersion: string, outputPath: string, log:
160
167
  const updatedLines = lines.map(line => line.replace(/tag="master"/g, `tag="v${packageVersion}"`));
161
168
  const updatedContent = updatedLines.join('\n');
162
169
  await fs.writeFile(nargoTomlPath, updatedContent);
163
- log(`Updated Nargo.toml to point to local copy of noir-libs`);
170
+ log(`Updated Nargo.toml to point to local copy of noir-libs.`);
164
171
  }
165
172
 
166
173
  /**
@@ -169,9 +176,9 @@ async function updateNargoToml(packageVersion: string, outputPath: string, log:
169
176
  * so we remove the entries to install the the workspace packages from npm.
170
177
  * @param outputPath - directory we are unboxing to
171
178
  */
172
- async function updateTsConfig(outputPath: string, log: LogFn) {
179
+ async function updateTsConfig(filename: string, outputPath: string, log: LogFn) {
173
180
  try {
174
- const tsconfigJsonPath = path.join(outputPath, 'tsconfig.json');
181
+ const tsconfigJsonPath = path.join(outputPath, filename);
175
182
  const data = await fs.readFile(tsconfigJsonPath, 'utf8');
176
183
  const config = JSON.parse(data);
177
184
 
@@ -180,9 +187,9 @@ async function updateTsConfig(outputPath: string, log: LogFn) {
180
187
  const updatedData = JSON.stringify(config, null, 2);
181
188
  await fs.writeFile(tsconfigJsonPath, updatedData, 'utf8');
182
189
 
183
- log('tsconfig.json has been updated');
190
+ log(`Updated ${filename}.`);
184
191
  } catch (error) {
185
- log('Error updating tsconfig.json:', error);
192
+ log(`Error updating ${filename}.`);
186
193
  throw error;
187
194
  }
188
195
  }
@@ -234,7 +241,7 @@ async function updatePackageJsonVersions(packageVersion: string, outputPath: str
234
241
  const updatedContent = JSON.stringify(packageData, null, 2);
235
242
  await fs.writeFile(packageJsonPath, updatedContent);
236
243
 
237
- log(`Updated package.json versions to ${packageVersion}`);
244
+ log(`Updated package.json versions to: ${packageVersion}`);
238
245
  }
239
246
 
240
247
  /**