Skip to content

Commit

Permalink
added compile step and node module resolution to translucent-blueprint
Browse files Browse the repository at this point in the history
  • Loading branch information
Piefayth committed Feb 3, 2024
1 parent ca2f464 commit 4191b4f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 20 deletions.
Binary file modified bun.lockb
Binary file not shown.
5 changes: 0 additions & 5 deletions packages/translucent-blueprint/cli.ts

This file was deleted.

5 changes: 4 additions & 1 deletion packages/translucent-blueprint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "translucent-blueprint",
"module": "index.ts",
"bin": {
"translucent-blueprint": "cli.ts"
"translucent-blueprint": "./dist/cli.js"
},
"scripts": {
"build": "tsc --project ./tsconfig.json --outDir ./dist"
},
"type": "module",
"version": "0.0.1"
Expand Down
6 changes: 4 additions & 2 deletions packages/translucent-blueprint/src/blueprint.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { readFile, writeFile } from 'fs/promises';

type Blueprint = {
preamble: {
title: string;
Expand Down Expand Up @@ -41,7 +43,7 @@ type Blueprint = {
};

export async function parseBlueprint(blueprint: string, plutusTs: string) {
const plutusJson: Blueprint = JSON.parse(await Bun.file(blueprint).text());
const plutusJson: Blueprint = JSON.parse(await readFile(blueprint, { encoding: 'utf8' }));

const plutusVersion =
"Plutus" + plutusJson.preamble.plutusVersion.toUpperCase();
Expand Down Expand Up @@ -99,7 +101,7 @@ import { applyParamsToScript, Data, Validator } from "../translucent/index.ts"`;

const plutus = imports + "\n\n" + validators.join("\n\n");

await Bun.write(plutusTs, plutus);
await writeFile(plutusTs, plutus, { encoding: 'utf8' });

console.log(
"%cGenerated %cplutus.ts",
Expand Down
15 changes: 15 additions & 0 deletions packages/translucent-blueprint/src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env node

import { parseBlueprint } from "./blueprint.js";

function getCliArgs() {
if (typeof Bun !== 'undefined' && Bun.argv) {
return Bun.argv.slice(2);
} else {
return process.argv.slice(2);
}
}

const args = getCliArgs();

parseBlueprint(args[0], args[1]);
23 changes: 11 additions & 12 deletions packages/translucent-blueprint/test/blueprint.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { parseBlueprint } from "../src/blueprint";
import path from "path";
import expectedResult from "./fixtures/plutus-vesting.ts.txt";
import { readFile, writeFile } from 'fs/promises'
import inputPlutusJson from "./fixtures/plutus-vesting.json"
import expectedResult from "./fixtures/plutus-vesting.ts.txt"
import { jest, test, expect, mock} from "bun:test"

jest.spyOn(Bun, "write").mockImplementation((path, data) => {
return Promise.resolve(data.toString().length);
});
mock.module("fs/promises", () => ({
writeFile: jest.fn().mockResolvedValue(0),
readFile: jest.fn().mockResolvedValue(JSON.stringify(inputPlutusJson)),
}))

it("should successfully parse any plutus.json", async () => {
test("should successfully parse any plutus.json", async () => {
const TARGET_FILENAME = "plutus.ts";

const plutusLocation = path.join(
import.meta.dir,
"./fixtures/plutus-vesting.json",
);
await parseBlueprint(plutusLocation, TARGET_FILENAME);
await parseBlueprint("/whatever.json", TARGET_FILENAME);

const writeMock = Bun.write as jest.MockedFunction<typeof Bun.write>;
const writeMock = writeFile as jest.Mock<typeof writeFile>;

expect(writeMock).toHaveBeenCalled();

Expand Down
21 changes: 21 additions & 0 deletions packages/translucent-blueprint/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src",
"types": [
"bun-types",
"jest"
],
"lib": [
"ESNext"
],
"module": "esnext",
"moduleResolution": "Node",
},
"include": [
"./src/**/*.ts"
],
"exclude": [
"**/node_modules"
]
}

0 comments on commit 4191b4f

Please sign in to comment.