Skip to content

Commit

Permalink
Merge pull request #15 from Once-Upon/benguyen0214/ou-1168-move-the-g…
Browse files Browse the repository at this point in the history
…enerator-cli-to-the-public-repo-and-ensure-it

Move the creator cli to the public repo but exclude it from the published package
  • Loading branch information
pcowgill authored Nov 30, 2023
2 parents e6b45eb + 9f7ee54 commit 58c759e
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 5 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/commands
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.hbs
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ If you want your protocol to work well across all of web3, contribute to **EVM C

### Generating a new contextualization

Run this command `npm run create:contextualizer -- --name [name of protocol]`
Run this command `npm run create:contextualizer [name of protocol]`

This will generate a new file called `protocol/[name of protocol].ts` and a test file called `protocol/[name of protocol].spec.ts`.

Expand Down
94 changes: 92 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"prepublishOnly": "npm run build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"grab-transaction": "node ./scripts/grab-transaction.js"
"grab-transaction": "node ./scripts/grab-transaction.js",
"create:contextualizer": "npm run build && node ./dist/commands/main.js create-contextualizer"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -54,6 +55,9 @@
"testEnvironment": "node"
},
"dependencies": {
"ethers": "^5.6.4"
"commander": "^11.1.0",
"ethers": "^5.6.4",
"handlebars": "^4.7.8",
"path": "^0.12.7"
}
}
78 changes: 78 additions & 0 deletions src/commands/createContextualizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import * as fs from 'fs';
import * as Handlebars from 'handlebars';
import * as path from 'path';
import { program } from './main';

export function registerCreateContextualizerCommand() {
program
.command('create-contextualizer')
.description('Create a new contextualizer')
.argument('<name>', 'name of contextualizer')
.action((name, options) => {
const srcDir = path.join(__dirname, '..', '..', 'src');
const contextualizerTemplateFilePath = path.join(
srcDir,
'template',
'contextualizer.template.hbs',
);
const contextualizerSpecTemplateFilePath = path.join(
srcDir,
'template',
'contextualizer.spec.template.hbs',
);
const newContextualizerFilePath = path.join(
srcDir,
'protocol',
`${name}.ts`,
);
const newContextualizerSpecFilePath = path.join(
srcDir,
'protocol',
`${name}.spec.ts`,
);

try {
console.log(`Creating a new contextualizer: ${name}`);

const contextualizerSource = fs.readFileSync(
contextualizerTemplateFilePath,
'utf8',
);
const contextualizerSpecSource = fs.readFileSync(
contextualizerSpecTemplateFilePath,
'utf8',
);
const contextualizerTemplate = Handlebars.compile(contextualizerSource);
const contextualizerSpecTemplate = Handlebars.compile(
contextualizerSpecSource,
);
// Data to replace variables
const data = {
lowercaseName: name,
camelCaseName: capitalize(name),
};
// Replace with actual contextualizer name
const contextualizerContent = contextualizerTemplate(data);
const contextualizerSpecContent = contextualizerSpecTemplate(data);
// Write the modified contents to the new contextualizer file
fs.writeFileSync(newContextualizerFilePath, contextualizerContent);
fs.writeFileSync(
newContextualizerSpecFilePath,
contextualizerSpecContent,
);

console.log(
`Successfully created a new contextualizer: ${newContextualizerFilePath}`,
);

process.exit(0); // Successful exit
} catch (error) {
console.error('Error during file operation:', error);
process.exit(1); // Exit with error
}
});
}

function capitalize(str: string) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
12 changes: 12 additions & 0 deletions src/commands/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { Command } = require('commander');
import { registerCreateContextualizerCommand } from './createContextualizer';
export const program = new Command();

program
.name('Onceupon command')
.description('CLI to some contextualizer utils')
.version('0.1.0');

registerCreateContextualizerCommand();

program.parse(process.argv);
10 changes: 10 additions & 0 deletions src/template/contextualizer.spec.template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Transaction } from '../types';
import { detect{{camelCaseName}}, generate{{camelCaseName}}Context } from './{{lowercaseName}}';

describe('{{camelCaseName}}', () => {
it('Should detect {{camelCaseName}} transaction', () => {
});

it('Should generate {{camelCaseName}} context', () => {
});
});
58 changes: 58 additions & 0 deletions src/template/contextualizer.template.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Transaction } from '../types';

export const {{lowercaseName}}Contextualizer = (transaction: Transaction): Transaction => {
const is{{camelCaseName}} = detect{{camelCaseName}}(transaction);
if (!is{{camelCaseName}}) return transaction;

return generate{{camelCaseName}}Context(transaction);
};

export const detect{{camelCaseName}} = (transaction: Transaction): boolean => {
/** implement your detection logic */
if (!transaction.value) {
return false;
}

return true;
};

// Contextualize for mined txs
export const generate{{camelCaseName}}Context = (transaction: Transaction): Transaction => {
/** implement your context generation logic */
transaction.context = {
variables: {
subject: {
type: '',
value: '',
},
asset1: {
type: '',
value: '',
},
asset2: {
type: '',
value: '',
},
userCount: {
type: 'emphasis',
value: '70',
},
},
summaries: {
category: '<CATEGORY>', // e.g. FUNGIBLE_TOKEN
en: {
title: '<TITLE>', // e.g. ERC20 Swap
variables: {
contextAction: {
type: 'contextAction',
value: '<ACTION_TEXT>', // text should be lowercase. e.g. purchased
},
},
default:
'[[subject]] [[contextAction]] [[asset1]] for [[asset2]] [[userCount]] users',
},
},
};

return transaction;
};

0 comments on commit 58c759e

Please sign in to comment.