Skip to content

Commit

Permalink
chore(boxes): refactor npx to improve readability, added upgrade opti…
Browse files Browse the repository at this point in the history
…on and manual versioning
  • Loading branch information
signorecello committed Feb 29, 2024
1 parent c8cd2ad commit d8eadad
Show file tree
Hide file tree
Showing 8 changed files with 483 additions and 418 deletions.
48 changes: 48 additions & 0 deletions boxes/bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node
import { Command } from "commander";
const program = new Command();
import path from "path";
import os from "os";
import axios from "axios";
import { chooseAndCloneBox } from "./scripts/steps/chooseBox.js";
import { sandboxRun } from "./scripts/steps/sandbox/run.js";
import { sandboxInstallOrUpdate } from "./scripts/steps/sandbox/install.js";

const { GITHUB_TOKEN } = process.env;

const axiosOpts = {};
if (GITHUB_TOKEN) {
axiosOpts.headers = { Authorization: `token ${GITHUB_TOKEN}` };
}

const { data } = await axios.get(
`https://api.github.com/repos/AztecProtocol/aztec-packages/releases`,
axiosOpts,
);

// versioning is confusing here because "latest" and "master" point to the same thing at times
// so let's clarify a bit:
//
// if the user has set a version (ex. "master" or "0.23.0"), use that
// otherwise use the stable release (ex. 0.24.0)
const stable = `${data[0].tag_name.split("-v")[1]}`;
const version = process.env.VERSION || stable;

// if the user has set a semver version (matches the regex), fetch that tag (i.e. aztec-packages-v0.23.0)
// otherwise use the version as the tag
const tag = version.match(/^\d+\.\d+\.\d+$/)
? `aztec-packages-v${version}`
: version;

program.action(async () => {
// STEP 1: Choose the boilerplate
await chooseAndCloneBox(tag, version);

// STEP 2: Install the Sandbox
await sandboxInstallOrUpdate(stable, version);

// STEP 3: Running the Sandbox
await sandboxRun(version);
});

program.parse();
255 changes: 0 additions & 255 deletions boxes/npx.js

This file was deleted.

7 changes: 4 additions & 3 deletions boxes/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"name": "create-aztec-app",
"packageManager": "[email protected]",
"version": "0.1.1",
"version": "0.2.2",
"type": "module",
"scripts": {
"compile": "yarn workspaces foreach -A -v run compile",
"build": "yarn workspaces foreach -A -v run build"
"build": "yarn workspaces foreach -A -v run build",
"publish": "yarn npm publish"
},
"workspaces": [
"react",
"vanilla-js"
],
"bin": "npx.js",
"bin": "VERSION=${VERSION:+} bin.js",
"resolutions": {
"@aztec/accounts": "portal:../yarn-project/accounts",
"@aztec/aztec.js": "portal:../yarn-project/aztec.js",
Expand Down
52 changes: 52 additions & 0 deletions boxes/scripts/steps/chooseBox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import select from "@inquirer/select";
import input from "@inquirer/input";
import tiged from "tiged";
import { replacePaths } from "../utils.js";
import chalk from "chalk";
const { log } = console;

export async function chooseAndCloneBox(tag, version) {
const appType = await select({
message: "Please choose your Aztec boilerplate:",
choices: [
{ value: "vanilla-js", name: "HTML/TS project" },
{ value: "react", name: "React project" },
{ value: "skip", name: "Skip this step" },
],
});

if (appType === "skip") return;

log(chalk.yellow(`You chose: ${appType}`));

try {
// STEP 1: Clone the box
const appName = await input({
message: "Your app name:",
default: "my-aztec-app",
});

chalk.blue("Cloning the boilerplate code...");
console.log(
`AztecProtocol/aztec-packages/boxes/${appType}${["latest", "master"].includes(tag) ? "" : `#${tag}`}`,
);
const emitter = tiged(
// same as the nargo dependencies above:
// "master" and "latest" both mean "master" for the github repo
// but if the user has set a semver version, we want that tag (i.e. aztec-packages-v0.23.0)
`AztecProtocol/aztec-packages/boxes/${appType}${["latest", "master"].includes(tag) ? "" : `#${tag}`}`,
);

emitter.on("info", (info) => {
log(info.message);
});

await emitter.clone(`./${appName}`).then(() => {
replacePaths(`./${appName}`, tag, version);
log(chalk.bgGreen("Your code is ready!"));
});
} catch (error) {
log(chalk.bgRed(error.message));
process.exit(1);
}
}
Loading

0 comments on commit d8eadad

Please sign in to comment.