Skip to content

Commit

Permalink
feat: generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
la-jamesh committed Oct 31, 2022
1 parent 3171c3d commit 1a9e368
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
11 changes: 10 additions & 1 deletion bin/oprah
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ program
oprahPromise = makeOprah({ stage, config }).init();
});

program
.command('generate')
.description('Generate an example configuration file.')
.action(() => {
const { stage, config } = program.opts();

oprahPromise = makeOprah({ stage, config }).generate();
});

program
.command('export')
.description(
Expand Down Expand Up @@ -146,7 +155,7 @@ if (!process.argv.slice(2).length) {
displayHelpAndExit();
}

if (!program.opts().stage) {
if (program.args[0] !== 'generate' && !program.opts().stage) {
logError('Invalid options!! You must specify stage.');
displayHelpAndExit();
}
Expand Down
41 changes: 41 additions & 0 deletions lib/commands/generate/make-generate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-disable no-template-curly-in-string */
const { writeFile, existsSync } = require('fs');
const { dump } = require('js-yaml');

const defaultConfig = {
service: 'my-service',
provider: {
name: 'ssm'
},
config: {
path: '/${stage}/config',
defaults: {
DB_NAME: 'my-database',
DB_HOST: 3200
},
required: {
DB_TABLE: 'some database table name for ${stage}'
}
},
secret: {
path: '/${stage}/secret',
required: {
DB_PASSWORD: 'secret database password'
}
}
};

const makeGenerate = () => async () => {
if (existsSync('oprah.yml')) {
throw new Error(
`oprah.yml file already exists in the following directory -- ${process.cwd()}`
);
}

writeFile('oprah.yml', dump(defaultConfig), () => {});
};

module.exports = {
defaultConfig,
makeGenerate
};
36 changes: 36 additions & 0 deletions lib/commands/generate/make-generate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const { dump } = require('js-yaml');

const mockWriteFile = jest.fn();
const mockExists = jest.fn();

jest.mock('fs', () => ({
writeFile: mockWriteFile,
existsSync: mockExists
}));

const { makeGenerate, defaultConfig } = require('./make-generate');

describe('make generate', () => {
describe('when file exists', () => {
it('throws an error', async () => {
mockExists.mockReturnValueOnce(true);
const generate = makeGenerate();
await expect(generate).rejects.toThrow(
`oprah.yml file already exists in the following directory -- ${process.cwd()}`
);
});
});

describe('when file doesnt exist', () => {
it('generates file', async () => {
mockExists.mockReturnValueOnce(false);
const generate = makeGenerate();
await generate();
expect(mockWriteFile).toHaveBeenCalledWith(
'oprah.yml',
dump(defaultConfig),
expect.any(Function)
);
});
});
});
2 changes: 2 additions & 0 deletions lib/make-oprah.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const { makeFetch } = require('./commands/fetch/make-fetch');
const { makeInit } = require('./commands/init/make-init');
const { makeConfigure } = require('./commands/configure/make-configure');
const { makeCleanup } = require('./commands/cleanup/make-cleanup');
const { makeGenerate } = require('./commands/generate/make-generate');

const makeOprah = ({
stage,
Expand Down Expand Up @@ -57,6 +58,7 @@ const makeOprah = ({
return {
configure,
init,
generate: makeGenerate(),
run: makeRun({ init, configure, cleanUp }),
list: makeList({ settingsService, parameterStore }),
export: makeExport({ settingsService, parameterStore }),
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oprah",
"version": "5.3.1",
"version": "5.4.0",
"description": "Package to deploy parameters to AWS",
"repository": "https://github.com/ACloudGuru/oprah.git",
"author": "subash adhikari <subash.adhikari@acloud.guru>",
Expand Down Expand Up @@ -60,4 +60,4 @@
"pinst": "^2.1.6",
"prettier": "^1.18.2"
}
}
}

0 comments on commit 1a9e368

Please sign in to comment.