Skip to content

Commit

Permalink
fix(cli): options parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Selman <danscode@selman.org>
  • Loading branch information
dselman committed Mar 20, 2023
1 parent 234e55a commit de24f27
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 32 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@accordproject/template-cli",
"version": "1.0.7",
"version": "1.0.8",
"description": "Command Line Interface for template engine and data format conversion",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -31,6 +31,7 @@
"@accordproject/markdown-template": "^0.16.6-20230317163634",
"@accordproject/markdown-transform": "^0.16.6-20230317163634",
"@accordproject/template-engine": "^1.0.8",
"@commander-js/extra-typings": "^10.0.3",
"commander": "^10.0.0",
"dayjs": "^1.11.7",
"figlet": "^1.5.2"
Expand Down
28 changes: 18 additions & 10 deletions src/index.ts
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();
3 changes: 2 additions & 1 deletion src/modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
declare module '@accordproject/markdown-template'
declare module '@accordproject/markdown-template'
declare module '@accordproject/markdown-transform'
74 changes: 54 additions & 20 deletions src/ten-generate.ts
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}`);
}

0 comments on commit de24f27

Please sign in to comment.