Skip to content

Commit a5f6e3c

Browse files
committed
feat(config): implement configuration with .dynamitrc
Added an ability to provide config with .dynamitrc file of different format close #3, fix #6
1 parent b63dd7f commit a5f6e3c

File tree

7 files changed

+80
-75
lines changed

7 files changed

+80
-75
lines changed

.plop/plopfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports = function (plop) {
44
plop.setGenerator('migration', {
55
actions: () => [{
66
type: 'add',
7-
path: path.resolve(process.cwd(), './migrations/{{timestamp}}-{{name}}.js'),
7+
path: path.resolve(process.cwd(), './{{migrationsPath}}/{{timestamp}}-{{name}}.js'),
88
templateFile: path.join(__dirname, './migration.js.hbs'),
99
}],
1010
});

package-lock.json

Lines changed: 59 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"dependencies": {
6868
"aws-sdk": "^2.673.0",
6969
"cli-color": "^2.0.0",
70+
"cosmiconfig": "^6.0.0",
7071
"node-plop": "^0.25.0",
7172
"ramda": "^0.27.0",
7273
"umzug": "^2.3.0",

src/commands/migration-generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default {
2222

2323
logger.log(
2424
'New migration was created at',
25-
clc.blueBright(path.resolve(process.cwd(), 'migrations', args.name)),
25+
clc.blueBright(path.resolve(process.cwd(), args.migrationsPath, args.name)),
2626
'.'
2727
);
2828

src/core/migrator.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface MigratorOptions {
1717
dynamodb?: DocumentClient;
1818
tableName?: string;
1919
attributeName?: string;
20+
migrationsPath?: string;
2021
}
2122

2223
interface Generator {
@@ -25,6 +26,7 @@ interface Generator {
2526

2627
export class Migrator extends Umzug implements Generator {
2728
private generator;
29+
private migrationsPath: string;
2830

2931
/**
3032
* Migrator factory function, creates an umzug instance with dynamodb storage.
@@ -51,10 +53,15 @@ export class Migrator extends Umzug implements Generator {
5153

5254
const plop = nodePlop(path.join(__dirname, '../../.plop/plopfile.js'));
5355
this.generator = plop.getGenerator('migration');
56+
this.migrationsPath = options.migrationsPath || 'migrations';
5457
}
5558

5659
async generate(migrationName: string) {
57-
await this.generator.runActions({timestamp: getCurrentYYYYMMDDHHmms(), name: migrationName});
60+
await this.generator.runActions({
61+
migrationsPath: this.migrationsPath,
62+
timestamp: getCurrentYYYYMMDDHHmms(),
63+
name: migrationName
64+
});
5865
}
5966
}
6067

src/core/yargs.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ export interface BaseCliOptions {
1616

1717
export function baseOptions(yargs: Argv<BaseCliOptions>) {
1818
return yargs
19-
.option('options-path', {
20-
describe: 'The path to a JSON file with additional options',
21-
type: 'string'
22-
})
2319
.option('migrations-path', {
2420
describe: 'The path to the migrations folder',
2521
default: 'migrations',
@@ -59,6 +55,7 @@ export function baseHandler<T extends BaseCliOptions>(callback: (args: Arguments
5955
...pick(['region', 'accessKeyId', 'secretAccessKey'], args),
6056
tableName: args.tableName,
6157
attributeName: args.attributeName,
58+
migrationsPath: args.migrationsPath,
6259
});
6360

6461
callback(args, migrator);

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import clc from 'cli-color';
44
import yargs from 'yargs';
5+
import {cosmiconfigSync} from 'cosmiconfig';
56

67
import migrate from './commands/migrate';
78
import migrateUndo from './commands/migrate-undo';
@@ -10,6 +11,7 @@ import migrationGenerate from './commands/migration-generate';
1011
import logger from './helpers/logger';
1112
import version from './helpers/version';
1213

14+
const moduleName = 'dynamit';
1315
const versions = [
1416
'Node: ' + version.getNodeVersion(),
1517
'CLI: ' + version.getCliVersion(),
@@ -30,6 +32,13 @@ const cli = yargs
3032
.wrap(yargs.terminalWidth())
3133
.strict();
3234

35+
const cosminstance = cosmiconfigSync(moduleName);
36+
const cosmic = cosminstance.search();
37+
38+
if (cosmic?.config) {
39+
cli.config(cosmic.config);
40+
}
41+
3342
const args = cli.argv;
3443

3544
// if no command then show help

0 commit comments

Comments
 (0)