Skip to content

Commit

Permalink
feat: added default dir output for extract decorators and updated tes…
Browse files Browse the repository at this point in the history
…t cases

Signed-off-by: Muskan Bararia <muskan.b@docusign.com>
  • Loading branch information
Muskan Bararia committed Nov 15, 2023
1 parent ca3915e commit 381402c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 38 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ require('yargs')
yargs.option('output', {
describe: 'output directory path',
type: 'string',
default: 'output'
});
}, argv => {
const options = {
Expand Down
42 changes: 16 additions & 26 deletions lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,9 @@ class Commands {

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

if (options.output) {
if (!fs.existsSync(options.output)) {
// If it doesn't exist, create the directory
fs.mkdirSync(options.output);
}
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');
Expand All @@ -722,29 +720,21 @@ class Commands {
let data = Printer.toCTO(modelAst);

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

if (options.output) {
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+'.json');
fs.writeFileSync(filePath, vocab);
});
}
return {
models: updatedModels,
extractedDecorators: resp.decoratorCommandSet,
extractedVocabularies: resp.vocabularies
};
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);
}
Expand Down
34 changes: 22 additions & 12 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,16 +791,21 @@ describe('concerto-cli', () => {
it('should extract the vocabularies and decorators from source model and should not alter the original model by default', async () => {
const dir = await tmp.dir({ unsafeCleanup: true });
const options = {
locale: 'en-gb'
locale: 'en-gb',
output: dir.path
};
const model = [(path.resolve(__dirname, 'models', 'extract-deco-and-vocab.cto'))];
const expectedModels = fs.readFileSync(path.resolve(__dirname, 'models', 'extract-deco-and-vocab.cto'),'utf-8');
const expectedVocabs = ['locale: en-gb\nnamespace: test@1.0.0\ndeclarations:\n - Person: Person Class\n properties:\n - firstName: HI\n - bio: some\n cus: con\n'];
const expectedDecos = [JSON.parse(fs.readFileSync(path.resolve(__dirname, 'data', 'extract-expected-deco.json'),'utf-8'))];
const expectedVocabs = 'locale: en-gb\nnamespace: test@1.0.0\ndeclarations:\n - Person: Person Class\n properties:\n - firstName: HI\n - bio: some\n cus: con\n';
const expectedDecos = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'data', 'extract-expected-deco.json'),'utf-8'));
const result = await Commands.extractDecorators(model, options);
result.extractedVocabularies.should.deep.equal(expectedVocabs) ;
result.extractedDecorators.should.deep.equal(expectedDecos);
result.models[0].replace(/[\r\n]+/g, '\n').replace(/[\t\n]+/g, '\n').trim().should.equal(expectedModels.replace(/[\r\n]+/g, '\n').replace(/[\t\n]+/g, '\n').trim());
const actualModels = fs.readFileSync(path.resolve(dir.path, 'test.cto'),'utf-8');
const actualVocabs = fs.readFileSync(path.resolve(dir.path, 'vocabulary_0.yml'),'utf-8');
const actualDecorators = JSON.parse(fs.readFileSync(path.resolve(dir.path, 'dcs_0.json'),'utf-8'));
result.should.include('Extracted Decorators and models in');
actualDecorators.should.eql(expectedDecos);
actualVocabs.should.eql(expectedVocabs);
actualModels.replace(/[\r\n]+/g, '\n').trim().should.eql(expectedModels.replace(/[\r\n]+/g, '\n').trim());
dir.cleanup();
});
it('should throw error if data is invalid', async () => {
Expand All @@ -819,16 +824,21 @@ describe('concerto-cli', () => {
const dir = await tmp.dir({ unsafeCleanup: true });
const options = {
locale: 'en-gb',
removeDecoratorsFromModel: true
removeDecoratorsFromModel: true,
output: dir.path
};
const model = [(path.resolve(__dirname, 'models', 'extract-deco-and-vocab.cto'))];
const expectedModels = fs.readFileSync(path.resolve(__dirname, 'models', 'extracted-model.cto'),'utf-8');
const expectedVocabs = ['locale: en-gb\nnamespace: test@1.0.0\ndeclarations:\n - Person: Person Class\n properties:\n - firstName: HI\n - bio: some\n cus: con\n'];
const expectedDecos = [JSON.parse(fs.readFileSync(path.resolve(__dirname, 'data', 'extract-expected-deco.json'),'utf-8'))];
const expectedVocabs = 'locale: en-gb\nnamespace: test@1.0.0\ndeclarations:\n - Person: Person Class\n properties:\n - firstName: HI\n - bio: some\n cus: con\n';
const expectedDecos = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'data', 'extract-expected-deco.json'),'utf-8'));
const result = await Commands.extractDecorators(model, options);
result.extractedVocabularies.should.deep.equal(expectedVocabs) ;
result.extractedDecorators.should.deep.equal(expectedDecos);
result.models[0].replace(/[\r\n]+/g, '\n').trim().should.equal(expectedModels.replace(/[\r\n]+/g, '\n').trim());
const actualModels = fs.readFileSync(path.resolve(dir.path, 'test.cto'),'utf-8');
const actualVocabs = fs.readFileSync(path.resolve(dir.path, 'vocabulary_0.yml'),'utf-8');
const actualDecorators = JSON.parse(fs.readFileSync(path.resolve(dir.path, 'dcs_0.json'),'utf-8'));
result.should.include('Extracted Decorators and models');
actualDecorators.should.eql(expectedDecos);
actualVocabs.should.eql(expectedVocabs);
actualModels.replace(/[\r\n]+/g, '\n').trim().should.eql(expectedModels.replace(/[\r\n]+/g, '\n').trim());
dir.cleanup();
});
it('should extract the vocabularies and decorators from source model and write result in given folder', async () => {
Expand Down

0 comments on commit 381402c

Please sign in to comment.