Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:

strategy:
matrix:
node-version: [16.x, 20.x]
node-version: [20.x, 22.x, 24.x, 26.x]

steps:
- uses: actions/checkout@v3
Expand All @@ -79,6 +79,11 @@ jobs:
run: |
npm i -g npm@7 --registry=https://registry.npmjs.org

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 10

- name: Cache node modules
id: cache-nodemodules
uses: actions/cache@v3
Expand All @@ -98,4 +103,6 @@ jobs:
if: steps.cache-nodemodules.outputs.cache-hit != 'true'
run: yarn install
- run: yarn run build
# Exercises `npm install` / `yarn install` / `pnpm install` + build for
# every supported --package-manager option of `hooks-cli init`.
- run: yarn run test:integration
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ Use:
You can initialize a new project by running:

```bash
hooks-cli init
hooks-cli init <c|js> <folderName>
```

By default, the generated project's scripts use `npm`. To use `yarn` or `pnpm` instead, pass `--package-manager`:

```bash
hooks-cli init js my-project --package-manager pnpm
```

To build the c contracts, run:
Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export async function main(target?: Target) {
.description("Initialize a new project")
.argument("type", "The type of project to initialize, 'c' or 'js'")
.argument("folderName", "The name of the folder to initialize")
.option(
"-p, --package-manager <manager>",
"The package manager to use in the generated project ('npm', 'yarn', or 'pnpm')",
"npm"
)
.showHelpAfterError()
.action(initCommand);

Expand Down
42 changes: 41 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ import { addListeners, ISelect } from "./debug";
import axios from "axios";
import dotenv from "dotenv";

export type PackageManager = "npm" | "yarn" | "pnpm";
const PACKAGE_MANAGERS: PackageManager[] = ["npm", "yarn", "pnpm"];

const applyPackageManager = (
projectDir: string,
packageManager: PackageManager
) => {
const packageJsonPath = path.join(projectDir, "package.json");
const pkg = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
if (pkg.scripts) {
for (const name of Object.keys(pkg.scripts)) {
pkg.scripts[name] = pkg.scripts[name].replace(
/\bnpm\b/g,
packageManager
);
}
}
fs.writeFileSync(
packageJsonPath,
`${JSON.stringify(pkg, null, 2)}\n`,
"utf-8"
);
};

const copyFiles = (source: string, destination: string) => {
fs.readdirSync(source).forEach((file) => {
const srcFile = path.join(source, file);
Expand Down Expand Up @@ -90,7 +114,22 @@ const validateJSCode = (filePath: string) => {
}
};

export const initCommand = async (type: "c" | "js", folderName: string) => {
export const initCommand = async (
type: "c" | "js",
folderName: string,
options?: { packageManager?: string }
) => {
const packageManager = (options?.packageManager ??
"npm") as PackageManager;
if (!PACKAGE_MANAGERS.includes(packageManager)) {
console.error(
`Invalid package manager "${packageManager}". Use one of: ${PACKAGE_MANAGERS.join(
", "
)}.`
);
process.exit(1);
}

const templateDir = path.join(__dirname, "init", type);
const newProjectDir = path.join(process.cwd(), folderName);

Expand All @@ -103,6 +142,7 @@ export const initCommand = async (type: "c" | "js", folderName: string) => {

if (type === "c" || type === "js") {
copyFiles(templateDir, newProjectDir);
applyPackageManager(newProjectDir, packageManager);
console.log(
`Created ${
type === "c" ? "CHooks" : "JSHooks"
Expand Down
12 changes: 5 additions & 7 deletions src/init/c/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
"main": "src/index.ts",
"scripts": {
"build": "hooks-cli compile-c contracts build/ --headers contracts/include",
"deploy": "yarn run build && ts-node src/index.ts"
},
"author": {
"name": "Denis Angell",
"url": "https://github.com/dangell7"
"deploy": "npm run build && ts-node src/index.ts"
},
"license": "ISC",
"dependencies": {
"@transia/hooks-toolkit": "^2.0.0-alpha.4",
"dotenv": "^16.3.1"
"@xahau/hooks-toolkit": "^2.1.0",
"dotenv": "^16.3.1",
"xahau": "^4.1.1"
},
"devDependencies": {
"@tsconfig/node24": "^24.0.4",
"@xahau/hooks-cli": "^2.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
}
Expand Down
20 changes: 7 additions & 13 deletions src/init/c/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import {
Client,
Wallet,
Invoke,
SetHookFlags,
TransactionMetadata,
} from "@transia/xrpl";
import { Client, Wallet, Invoke, TransactionMetadata } from "xahau";
import { HookFlags } from "xahau/dist/npm/models/common/xahau";
import {
createHookPayload,
setHooksV3,
setHooks,
SetHookParams,
Xrpld,
ExecutionUtility,
} from "@transia/hooks-toolkit";
} from "@xahau/hooks-toolkit";
import "dotenv/config";

export async function main(): Promise<void> {
const client = new Client(process.env.XRPLD_WSS || "");
await client.connect();
client.networkID = await client.getNetworkID();

const aliceWallet = Wallet.fromSeed(process.env.ALICE_SEED || "");

const hook = createHookPayload({
version: 0,
createFile: "base",
namespace: "base",
flags: SetHookFlags.hsfOverride,
flags: HookFlags.hsfOverride,
hookOnArray: ["Invoke"],
});

await setHooksV3({
await setHooks({
client: client,
seed: aliceWallet.seed,
wallet: aliceWallet,
hooks: [{ Hook: hook }],
} as SetHookParams);

Expand Down
12 changes: 5 additions & 7 deletions src/init/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
"main": "src/index.ts",
"scripts": {
"build": "hooks-cli compile-js contracts/base.ts build/",
"deploy": "yarn run build && ts-node src/index.ts"
},
"author": {
"name": "Denis Angell",
"url": "https://github.com/dangell7"
"deploy": "npm run build && ts-node src/index.ts"
},
"license": "ISC",
"dependencies": {
"@transia/hooks-toolkit": "^2.0.0-alpha.4",
"@xahau/hooks-toolkit": "^2.1.0",
"dotenv": "^16.3.1",
"jshooks-api": "^1.0.5"
"jshooks-api": "^1.0.5",
"xahau": "^4.1.1"
},
"devDependencies": {
"@tsconfig/node24": "^24.0.4",
"@xahau/hooks-cli": "^2.1.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
}
Expand Down
16 changes: 5 additions & 11 deletions src/init/js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
import {
Client,
Wallet,
Invoke,
SetHookFlags,
TransactionMetadata,
} from "@transia/xrpl";
import { Client, Wallet, Invoke, TransactionMetadata } from "xahau";
import { HookFlags } from "xahau/dist/npm/models/common/xahau";
import {
createHookPayload,
setHooksV3,
SetHookParams,
Xrpld,
ExecutionUtility,
} from "@transia/hooks-toolkit";
} from "@xahau/hooks-toolkit";
import "dotenv/config";

export async function main(): Promise<void> {
const client = new Client(process.env.XRPLD_WSS || "");
await client.connect();
client.networkID = await client.getNetworkID();

const aliceWallet = Wallet.fromSeed(process.env.ALICE_SEED || "");

const hook = createHookPayload({
version: 1,
createFile: "base",
namespace: "base",
flags: SetHookFlags.hsfOverride,
flags: HookFlags.hsfOverride,
hookOnArray: ["Invoke"],
fee: "100000",
});

await setHooksV3({
client: client,
seed: aliceWallet.seed,
wallet: aliceWallet,
hooks: [{ Hook: hook }],
} as SetHookParams);

Expand Down
6 changes: 5 additions & 1 deletion test/integration/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,27 @@ import * as path from "path";
describe("Build Tests", () => {
const originalConsoleLog = console.log;
const originalConsoleError = console.error;
const originalConsoleWarn = console.warn;
const originalProcessExit = process.exit;

beforeAll(() => {
// Mock console.log to suppress log messages during tests
console.log = jest.fn();
// Mock console.error to suppress error messages during tests
console.error = jest.fn();
// Mock console.warn to suppress warning messages during tests
console.warn = jest.fn();
// replace process.exit with a function that throws an error
process.exit = jest.fn(() => {
throw Error("Process exit called");
});
});

afterAll(() => {
// Restore original console.log, console.error and process.exit
// Restore original console.log, console.error, console.warn and process.exit
console.log = originalConsoleLog;
console.error = originalConsoleError;
console.warn = originalConsoleWarn;
process.exit = originalProcessExit;
});

Expand Down
Loading
Loading