Skip to content

Commit

Permalink
feat(decorator): Added support for extracting decorators from model f…
Browse files Browse the repository at this point in the history
…iles (#44)

* feat(decorator): added support for extracting decorator from models

Signed-off-by: Muskan Bararia <muskan.b@docusign.com>

* feat(decorator): added support for extracting decorator from models

Signed-off-by: Muskan Bararia <muskan.b@docusign.com>

* chore: bumped up the concerto-core version

Signed-off-by: Muskan Bararia <muskan.b@docusign.com>

* chore: bumped up the concerto-core version

Signed-off-by: Muskan Bararia <muskan.b@docusign.com>

* chore: bumped up the concerto version

Signed-off-by: Muskan Bararia <muskan.b@docusign.com>

* feat: added default dir output for extract decorators and updated test cases

Signed-off-by: Muskan Bararia <muskan.b@docusign.com>

---------

Signed-off-by: Muskan Bararia <muskan.b@docusign.com>
Co-authored-by: Muskan Bararia <muskan.b@docusign.com>
  • Loading branch information
muskanbararia and Muskan Bararia committed Nov 15, 2023
1 parent 3258869 commit 22b445a
Show file tree
Hide file tree
Showing 8 changed files with 643 additions and 80 deletions.
37 changes: 37 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,43 @@ require('yargs')
Logger.error(err.message);
});
})
.command('extract-decorators','extracts the decorator command sets and vocabularies from a list of model files', yargs =>{
yargs.demandOption('models','Please provide a model');
yargs.option('models', {
describe: 'The file location of the source models',
alias: 'model',
type: 'string',
array:true,
});
yargs.option('locale', {
describe: 'The locale for extracted vocabularies',
type: 'string',
default:'en'
});
yargs.option('removeDecoratorsFromSource', {
describe: 'Flag to determine whether to remove decorators from source model',
type: 'boolean',
default: false
});
yargs.option('output', {
describe: 'output directory path',
type: 'string',
default: 'output'
});
}, argv => {
const options = {
locale: argv.locale,
removeDecoratorsFromModel: argv.removeDecoratorsFromSource,
output: argv.output
};
return Commands.extractDecorators(argv.models,options)
.then(result => {
Logger.info(result);
})
.catch((err) => {
Logger.error(err.message);
});
})
.option('verbose', {
alias: 'v',
default: false
Expand Down
52 changes: 52 additions & 0 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,58 @@ class Commands {
throw new Error(e);
}
}

/**
* Extract the decorator command sets and vocabularies from a list of modelFiles
* @param {string} modelFiles - the model from which vocabularies and decorators have to be extracted
* @param {object} options - optional parameters
* @param {string} options.locale - locale for extracted vocabularies
* @param {string} options.removeDecoratorsFromSource - the flag to determine whether to remove decorators from source model files
* @param {string} options.output - the output directory where the extracted models, decorators and vocabularies are to be written
* @returns {Object} - Extracted models, decorators and vocabularies
*/
static async extractDecorators(modelFiles, options) {
try {
const updatedModels = [];
const allModelContent = modelFiles.map(file => fs.readFileSync(file, 'utf-8'));
let modelManager = new ModelManager();
allModelContent.forEach(modelContent => {
modelManager.addModel(modelContent);
});

const resp = DecoratorManager.extractDecorators(modelManager, options);

if (!fs.existsSync(options.output)) {
// If it doesn't exist, create the directory
fs.mkdirSync(options.output);
}

const namespaces = resp.modelManager.getNamespaces().filter(namespace=>namespace!=='concerto@1.0.0' && namespace!=='concerto');
namespaces.forEach(name=>{
let model = resp.modelManager.getModelFile(name);
let modelAst=model.getAst();
let data = Printer.toCTO(modelAst);

updatedModels.push(data);
const filePath = path.join(options.output, model.namespace.split('@')[0]+'.cto');
fs.writeFileSync(filePath, data);
});

resp.decoratorCommandSet.forEach((dcs, index) => {
const fileName = 'dcs_'+ index.toString();
const filePath = path.join(options.output, fileName+'.json');
fs.writeFileSync(filePath, JSON.stringify(dcs));
});
resp.vocabularies.forEach((vocab, index) => {
const fileName = 'vocabulary_'+ index.toString();
const filePath = path.join(options.output, fileName+'.yml');
fs.writeFileSync(filePath, vocab);
});
return `Extracted Decorators and models in ${options.output}/.`;
} catch (e) {
throw new Error(e);
}
}
}

module.exports = Commands;

0 comments on commit 22b445a

Please sign in to comment.