-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Once-Upon/benguyen0214/ou-1168-move-the-g…
…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
Showing
9 changed files
with
259 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.hbs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', () => { | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |