Skip to content

Commit 976f7c0

Browse files
authored
Change the way sources are fetched during the project generation. (#662)
1 parent e1f1ec9 commit 976f7c0

File tree

13 files changed

+139
-209
lines changed

13 files changed

+139
-209
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
build
33
reports
4+
coverage

.npmignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,29 @@
33

44
# System
55
.DS_Store
6+
7+
# Ignore the following only in the root directory
8+
/.git
9+
/.github
10+
/.husky
11+
/playwright.config.ts
12+
/jest.config.js
13+
/.prettierrc
14+
/.prettierignore
15+
/.npmignore
16+
/.gitignore
17+
/.gitattributes
18+
/.eslintrc
19+
/.eslintignore
20+
/tsconfig.json
21+
/src/lib/**/*.test.js
22+
/src/lib/**/*.test.ts
23+
/update-changelog.sh
24+
25+
# Allow everything within the templates/project-ts
26+
!/templates/project-ts/**
27+
28+
# Ignore the following globally
29+
node_modules
30+
coverage
31+
tests

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1717

1818
## Unreleased
1919

20-
## [0.21.4](https://github.com/o1-labs/zkapp-cli/compare/0.21.3...0.21.4) - 2024-05-21
20+
## [0.21.5](https://github.com/o1-labs/zkapp-cli/compare/0.21.4...0.21.5) - 2024-06-06
21+
22+
### Changed
23+
24+
- Improved the NPM packaging and changed the way sources are fetched during the project generation. [#662](https://github.com/o1-labs/zkapp-cli/pull/662)
25+
- Generic project and example templates are now fetched from the locally installed `zkapp-cli` package path (local file system copy).
26+
27+
## [0.21.4](https://github.com/o1-labs/zkapp-cli/compare/0.21.3...0.21.4) - 2024-06-06
2128

2229
### Fixed
2330

package-lock.json

Lines changed: 6 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zkapp-cli",
3-
"version": "0.21.4",
3+
"version": "0.21.5",
44
"description": "CLI to create zkApps (zero-knowledge apps) for Mina Protocol",
55
"homepage": "https://github.com/o1-labs/zkapp-cli/",
66
"keywords": [
@@ -60,7 +60,6 @@
6060
"fast-glob": "^3.3.2",
6161
"find-npm-prefix": "^1.0.2",
6262
"fs-extra": "^11.2.0",
63-
"gittar": "^0.1.1",
6463
"mina-signer": "^3.0.7",
6564
"o1js": "^1.*",
6665
"opener": "^1.5.2",

src/lib/example.js

Lines changed: 36 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import chalk from 'chalk';
22
import enquirer from 'enquirer';
33
import fs from 'fs-extra';
4-
import gittar from 'gittar';
4+
import url from 'node:url';
55
import ora from 'ora';
66
import path from 'path';
77
import shell from 'shelljs';
88
import util from 'util';
99
import Constants from './constants.js';
10+
import { setupProject } from './helpers.js';
11+
12+
const __filename = url.fileURLToPath(import.meta.url);
13+
const __dirname = path.dirname(__filename);
1014

1115
const shellExec = util.promisify(shell.exec);
1216

@@ -18,6 +22,11 @@ const shellExec = util.promisify(shell.exec);
1822
* @return {Promise<void>}
1923
*/
2024
export async function example(example) {
25+
if (!shell.which('git')) {
26+
console.error(chalk.red('Please ensure Git is installed, then try again.'));
27+
shell.exit(1);
28+
}
29+
2130
if (!example) {
2231
const res = await enquirer.prompt({
2332
type: 'select',
@@ -44,20 +53,18 @@ export async function example(example) {
4453
}
4554

4655
const dir = findUniqueDir(example);
47-
const lang = 'ts';
4856
const isWindows = process.platform === 'win32';
4957

50-
if (!(await fetchProjectTemplate(dir, lang))) shell.exit(1);
51-
if (!(await extractExample(example, dir, lang))) shell.exit(1);
58+
if (!(await setupProject(path.join(shell.pwd().toString(), dir)))) {
59+
shell.exit(1);
60+
}
61+
if (!(await updateExampleSources(example, dir))) {
62+
shell.exit(1);
63+
}
5264

5365
// Set dir for shell commands. Doesn't change user's dir in their CLI.
5466
shell.cd(dir);
5567

56-
if (!shell.which('git')) {
57-
console.error(chalk.red('Please ensure Git is installed, then try again.'));
58-
shell.exit(1);
59-
}
60-
6168
await step('Initialize Git repo', 'git init -q');
6269

6370
await step(
@@ -90,48 +97,6 @@ export async function example(example) {
9097
process.exit(0);
9198
}
9299

93-
/**
94-
* Fetch project template.
95-
* @param {string} example Name of the destination dir.
96-
* @param {string} lang ts or js
97-
* @returns {Promise<boolean>} True if successful; false if not.
98-
*/
99-
async function fetchProjectTemplate(name, lang) {
100-
const projectName = lang === 'ts' ? 'project-ts' : 'project';
101-
const step = 'Fetch project template';
102-
const spin = ora(`${step}...`).start();
103-
104-
try {
105-
const TEMP = '.temp-dir';
106-
const templatePath = `templates/${projectName}`;
107-
108-
if (process.env.CI) {
109-
shell.mkdir('-p', path.join(TEMP, templatePath));
110-
shell.cp('-r', `${templatePath}/.`, path.join(TEMP, templatePath));
111-
} else {
112-
const src = 'github:o1-labs/zkapp-cli#main';
113-
await gittar.fetch(src, { force: true });
114-
115-
// Note: Extract will overwrite any existing dir's contents. But we're
116-
// using an always-unique name.
117-
await gittar.extract(src, TEMP, {
118-
filter(path) {
119-
return path.includes(templatePath);
120-
},
121-
});
122-
}
123-
124-
shell.mv(path.join(TEMP, templatePath), name);
125-
shell.rm('-r', TEMP);
126-
spin.succeed(chalk.green(step));
127-
return true;
128-
} catch (err) {
129-
spin.fail(step);
130-
console.error(err);
131-
return false;
132-
}
133-
}
134-
135100
/**
136101
* Helper for any steps that need to call a shell command.
137102
* @param {string} step Name of step to show user
@@ -210,46 +175,40 @@ export function kebabCase(str) {
210175
}
211176

212177
/**
213-
* Fetch an example & place in the `src` directory.
214-
* @param {string} example Name of the example, as found in our GitHub repo.
178+
* Updates the example sources.
179+
* @param {string} example Name of the example.
215180
* @param {string} name Destination dir name.
216-
* @param {string} lang ts or js
181+
* @param {string} lang ts (default) or js
217182
* @returns {Promise<boolean>} True if successful; false if not.
218183
*/
219-
export async function extractExample(example, name, lang) {
220-
const step = 'Extract example';
184+
export async function updateExampleSources(example, name, lang = 'ts') {
185+
const step = 'Update example sources';
221186
const spin = ora(`${step}...`).start();
222187

223188
try {
224-
const TEMP = '.temp-dir';
225-
const examplePath = `examples/${example}/${lang}/src`;
226-
227-
if (process.env.CI) {
228-
shell.mkdir('-p', path.join(TEMP, examplePath));
229-
shell.cp('-r', `${examplePath}/.`, path.join(TEMP, examplePath));
230-
} else {
231-
const src = 'github:o1-labs/zkapp-cli#main';
232-
233-
// Note: Extract will overwrite any existing dir's contents. That's ok here.
234-
await gittar.extract(src, TEMP, {
235-
filter(path) {
236-
return path.includes(examplePath);
237-
},
238-
});
239-
}
189+
const examplePath = path.resolve(
190+
__dirname,
191+
'..',
192+
'..',
193+
'examples',
194+
example,
195+
lang,
196+
'src'
197+
);
240198

241199
// Example not found. Delete the project template & temp dir to clean up.
242-
if (isEmpty(TEMP)) {
200+
if (isEmpty(examplePath)) {
243201
spin.fail(step);
244202
console.error(chalk.red('Example not found'));
245-
shell.rm('-r', `${process.cwd()}/${name}`, TEMP);
246203
return false;
247204
}
248205

249206
// Delete the project template's `src` & use the example's `src` instead.
250-
shell.rm('-r', `${name}/src`);
251-
shell.mv(`${TEMP}/${examplePath}`, `${name}/src`);
252-
shell.rm('-r', TEMP);
207+
const srcPath = path.resolve(name, 'src');
208+
shell.rm('-r', srcPath);
209+
// `node:fs.cpSync` instead of the `shell.cp` because `ShellJS` does not implement `cp -a`
210+
// https://github.com/shelljs/shelljs/issues/79#issuecomment-30821277
211+
fs.cpSync(`${examplePath}/`, `${srcPath}/`, { recursive: true });
253212
spin.succeed(chalk.green(step));
254213
return true;
255214
} catch (err) {

0 commit comments

Comments
 (0)