-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dan Selman <[email protected]>
- Loading branch information
Showing
5 changed files
with
85 additions
and
32 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,22 +1,30 @@ | ||
#!/usr/bin/env node | ||
const { Command } = require("commander"); | ||
const figlet = require("figlet"); | ||
import { generateCommand } from "./ten-generate"; | ||
|
||
const program = new Command(); | ||
|
||
import { Argument, program } from '@commander-js/extra-typings'; | ||
import { generateCommandHandler } from './ten-generate'; | ||
console.log(figlet.textSync("Template Manager")); | ||
|
||
program | ||
.version('1.0.0') | ||
.name('ten') | ||
.command('generate <templateFile> <modelFile> <dataJson> <outputFormat> <outputDir>') | ||
.command('generate') | ||
.alias('gen') | ||
.description('Generate a document from a template') | ||
.action(generateCommand); | ||
.argument('<templateDir>', 'path to template directory') | ||
.argument('<dataFile>', 'path to JSON data file') | ||
.addArgument(new Argument('<outputFormat>', 'output file format').choices(['pdf', 'markdown', 'html'])) | ||
.argument('<outputFile>', 'path to output file') | ||
.option('--libraryFile [value]', 'path to library file') | ||
.option('--now [value]', 'date/time to use for \'now\' (ISO-8601 format)') | ||
.option('--verbose', 'verbose output') | ||
.action(generateCommandHandler); | ||
|
||
async function run() { | ||
await program.parseAsync(); | ||
|
||
if (!process.argv.slice(2).length) { | ||
program.outputHelp(); | ||
if (!process.argv.slice(2).length) { | ||
program.outputHelp(); | ||
} | ||
} | ||
|
||
program.parseAsync(process.argv); | ||
run(); |
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 |
---|---|---|
@@ -1,29 +1,63 @@ | ||
import { ModelManager } from "@accordproject/concerto-core"; | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
import dayjs from 'dayjs'; | ||
|
||
const { TemplateMarkInterpreter } = require('@accordproject/template-engine'); | ||
import { ModelManager } from "@accordproject/concerto-core"; | ||
import { TemplateMarkInterpreter } from '@accordproject/template-engine'; | ||
import { TemplateMarkTransformer } from '@accordproject/markdown-template'; | ||
const { transform } = require('@accordproject/markdown-transform'); | ||
const dayjs = require('dayjs'); | ||
import { transform } from '@accordproject/markdown-transform'; | ||
|
||
export async function generateCommandHandler(templateDir: string, dataFile: string, outputFormat: string, outputFile: string, options: { libraryFile?: string | true | undefined; now?: string | true | undefined; verbose?: true | undefined; }) { | ||
|
||
const template = fs.readFileSync(path.join(templateDir, 'template.md'), 'utf-8'); | ||
const data = JSON.parse(fs.readFileSync(dataFile, 'utf-8')); | ||
|
||
const ctoFileNames = fs.readdirSync(templateDir, { withFileTypes: true }) | ||
.filter(item => !item.isDirectory() && item.name.endsWith('.cto')) | ||
.map(item => item.name); | ||
|
||
export async function generateCommand(templateFile: string, modelFile: string, dataJson: string, outputFormat: string, outputFile: string) { | ||
const model = readFileSync(modelFile, 'utf-8'); | ||
const template = readFileSync(templateFile, 'utf-8'); | ||
const data = JSON.parse(readFileSync(dataJson, 'utf-8')); | ||
const modelManager = new ModelManager({ strict: true }); | ||
modelManager.addCTOModel(model); | ||
const engine = new TemplateMarkInterpreter(modelManager, {}); | ||
ctoFileNames.forEach( ctoFile => { | ||
modelManager.addCTOModel(fs.readFileSync(path.join(templateDir, ctoFile), 'utf-8'), ctoFile, true); | ||
}) | ||
await modelManager.updateExternalModels(); | ||
|
||
const clauseLibrary = options.libraryFile ? JSON.parse(fs.readFileSync(options.libraryFile as string, 'utf-8')) : {}; | ||
|
||
if(options.verbose) { | ||
console.log('Library:'); | ||
console.log(clauseLibrary); | ||
} | ||
|
||
const engine = new TemplateMarkInterpreter(modelManager, clauseLibrary); | ||
const templateMarkTransformer = new TemplateMarkTransformer(); | ||
const templateMarkDom = templateMarkTransformer.fromMarkdownTemplate({ content: template }, modelManager, 'contract', { verbose: options.verbose }); | ||
|
||
if(options.verbose) { | ||
console.log('TemplateMark DOM:'); | ||
console.log(JSON.stringify(templateMarkDom, null, 2)); | ||
} | ||
|
||
const genNow = options.now ? dayjs(options.now as string) : dayjs(); | ||
|
||
if(options.now ) { | ||
console.log(`Value of 'now' is: ${genNow.toISOString()}` ); | ||
} | ||
|
||
const ciceroMark = await engine.generate(templateMarkDom, data, genNow); | ||
|
||
if(options.verbose) { | ||
console.log('AgreementMark DOM:'); | ||
console.log(JSON.stringify(ciceroMark, null, 2)); | ||
} | ||
|
||
const result = await transform(ciceroMark.toJSON(), 'ciceromark_parsed', [outputFormat], {}, {verbose: options.verbose}); | ||
|
||
const templateMarkDom = templateMarkTransformer.fromMarkdownTemplate({ content: template }, modelManager, 'contract', { verbose: false }); | ||
if(options.verbose) { | ||
console.log(`${outputFormat}:`); | ||
console.log(result); | ||
} | ||
|
||
//const now = dayjs('2023-03-17T00:00:00.000Z'); | ||
const now = null; | ||
const ciceroMark = await engine.generate(templateMarkDom, data, now); | ||
console.log(typeof ciceroMark); | ||
console.log(JSON.stringify(ciceroMark, null, 2)); | ||
const result = await transform(ciceroMark.toJSON(), 'ciceromark_parsed', [outputFormat], {}, {}); | ||
console.log(JSON.stringify(result, null, 2)); | ||
writeFileSync(outputFile, result); | ||
fs.writeFileSync(outputFile, result); | ||
console.log(`Created ${outputFormat} and saved to file: ${outputFile}`); | ||
} |