Skip to content

Commit

Permalink
Added support for output and standardised arg params
Browse files Browse the repository at this point in the history
Signed-off-by: Muskan Bararia <[email protected]>
  • Loading branch information
Muskan Bararia committed Oct 19, 2023
1 parent 259264f commit f5b3133
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ apply the decorators and vocabs to the target models from given list of dcs file
Options:
--model The CTO file
--voc The List of vocab files
--dcs The List of decorator command set files
--f output format (cto or json), default: cto
--vocabulary The List of vocab files
--decorator The List of decorator command set files
--format output format (cto or json), default: cto
--output output directory path
```

## License <a name="license"></a>
Expand Down
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,34 +414,39 @@ require('yargs')
describe: 'The file location of the source model',
type: 'string',
});
yargs.option('dcs', {
yargs.option('decorator', {
describe: 'List of dcs files to be applied to model',
type: 'string',
array:true
});
yargs.option('voc', {
yargs.option('vocabulary', {
describe: 'List of vocab files to be applied to model',
type: 'string',
array:true
});
yargs.option('f', {
yargs.option('format', {
describe: 'Output format (json or cto)',
type: 'string',
default:'cto',
choices: ['json', 'cto']
});
yargs.option('output', {
describe: 'output directory path',
type: 'string',
});
yargs.check((args) => {
// Custom validation to ensure at least one of the two options is provided
if (!args.dcs && !args.voc) {
if (!args.decorator && !args.vocabulary) {
throw new Error('You must provide at least one of dcs files or voc files');
}
return true;
});
}, argv => {
let options={};
options.format=argv.f;
options.format=argv.format;
options.output=argv.output;

return Commands.decorate(argv.model, argv.dcs,argv.voc,options)
return Commands.decorate(argv.model, argv.decorator,argv.vocabulary,options)
.then(obj => {
console.log(obj);
})
Expand Down
40 changes: 19 additions & 21 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,29 +643,27 @@ class Commands {
const modelContent = fs.readFileSync(path.resolve(modelFile), 'utf-8');
const allDCSFiles = dcsFiles ? dcsFiles.map(file => fs.readFileSync(file, 'utf8')) : [];
const allVocsFiles = vocFiles ? vocFiles.map(file => fs.readFileSync(file, 'utf8')) : [];
const modelManager = new ModelManager();
let modelManager = new ModelManager();
modelManager.addModel(modelContent);
let updatedModelManager = modelManager;
if (allDCSFiles.length > 0) {
allDCSFiles.forEach(content => {
updatedModelManager = DecoratorManager.decorateModels(updatedModelManager, JSON.parse(content));
});
}
if (allVocsFiles.length > 0) {
const vocManager = new VocabularyManager({ missingTermGenerator: VocabularyManager.englishMissingTermGenerator });
allVocsFiles.forEach(content => {
vocManager.addVocabulary(content);
});
const commandSet = vocManager.generateDecoratorCommands(updatedModelManager, 'en-gb');
updatedModelManager = DecoratorManager.decorateModels(updatedModelManager, commandSet);
}
const namespace = updatedModelManager.getModels().map(obj=>obj.name.replace(/\.cto$/, ''))[0];
let modelAst = updatedModelManager.getModelFile(namespace).getAst();
if (options.format === 'cto') {
return Printer.toCTO(modelAst);
} else {
return JSON.stringify(modelAst);
allDCSFiles.forEach(content => {
modelManager = DecoratorManager.decorateModels(modelManager, JSON.parse(content));
});
const vocManager = new VocabularyManager({ missingTermGenerator: VocabularyManager.englishMissingTermGenerator });
const namespace = modelManager.getModels().map(obj=>obj.name.replace(/\.cto$/, ''))[0];
allVocsFiles.forEach(content => {
vocManager.addVocabulary(content);
});
const vocabKeySet=(Object.keys(vocManager.vocabularies)).map(key=>key.replace(new RegExp(`^${namespace}/`), ''));
vocabKeySet.map(voc=>{
let commandSet = vocManager.generateDecoratorCommands(modelManager, voc);
modelManager = DecoratorManager.decorateModels(modelManager, commandSet);
});
let modelAst = modelManager.getModelFile(namespace).getAst();
let data = (options.format === 'cto') ? Printer.toCTO(modelAst):JSON.stringify(modelAst);
if (options.output) {
fs.writeFileSync(options.output, data);
}
return data;
} catch (e) {
throw new Error(e);
}
Expand Down
16 changes: 15 additions & 1 deletion test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ describe('concerto-cli', () => {
};
const expected = fs.readFileSync(path.resolve(__dirname, 'models', 'decorate-model-expected-with-vocabs-and-deco.json'),'utf-8');
const result =await Commands.decorate(model,decorators,vocabs,options);
result.should.equal(expected);
result.trim().should.equal(expected.trim());
dir.cleanup();
});
it('should throw error if data is invalid', async () => {
Expand All @@ -742,5 +742,19 @@ describe('concerto-cli', () => {
}
dir.cleanup();
});
it('should write to a file if output is provided', async () => {
const output = await tmp.file({ unsafeCleanup: true });
const model = path.resolve(__dirname, 'models', 'decorate-model.cto');
const vocabs = [path.resolve(__dirname, 'data', 'decorate-voc')];
const decorators =undefined;
const options={
format:'json',
output:output.path
};
const expected = await Commands.decorate(model,decorators,vocabs,options);
const result = fs.readFileSync(output.path, 'utf-8');
result.trim().should.equal(expected.trim());
output.cleanup();
});
});
});

0 comments on commit f5b3133

Please sign in to comment.