Skip to content

Commit a0ce148

Browse files
committed
Update code style to match codacy check
1 parent d6a7d21 commit a0ce148

File tree

2 files changed

+87
-74
lines changed

2 files changed

+87
-74
lines changed

index.js

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,80 @@
1-
'use strict';
2-
const _ = require('lodash');
3-
const BbPromise = require('bluebird');
4-
const AWS = require('aws-sdk');
5-
const dynamodbLocal = require('dynamodb-localhost');
6-
const { writeSeeds, locateSeeds } = require('./src/seeder');
1+
"use strict";
2+
const _ = require("lodash");
3+
const BbPromise = require("bluebird");
4+
const AWS = require("aws-sdk");
5+
const dynamodbLocal = require("dynamodb-localhost");
6+
const { writeSeeds, locateSeeds } = require("./src/seeder");
77

88
class ServerlessDynamodbLocal {
99
constructor(serverless, options) {
1010
this.serverless = serverless;
1111
this.service = serverless.service;
1212
this.options = options;
13-
this.provider = 'aws';
13+
this.provider = "aws";
1414
this.commands = {
1515
dynamodb: {
1616
commands: {
1717
migrate: {
18-
lifecycleEvents: ['migrateHandler'],
19-
usage: 'Creates local DynamoDB tables from the current Serverless configuration'
18+
lifecycleEvents: ["migrateHandler"],
19+
usage: "Creates local DynamoDB tables from the current Serverless configuration"
2020
},
2121
seed: {
22-
lifecycleEvents: ['seedHandler'],
23-
usage: 'Seeds local DynamoDB tables with data'
22+
lifecycleEvents: ["seedHandler"],
23+
usage: "Seeds local DynamoDB tables with data"
2424
},
2525
start: {
26-
lifecycleEvents: ['startHandler'],
27-
usage: 'Starts local DynamoDB',
26+
lifecycleEvents: ["startHandler"],
27+
usage: "Starts local DynamoDB",
2828
options: {
2929
port: {
30-
shortcut: 'p',
31-
usage: 'The port number that DynamoDB will use to communicate with your application. If you do not specify this option, the default port is 8000'
30+
shortcut: "p",
31+
usage: "The port number that DynamoDB will use to communicate with your application. If you do not specify this option, the default port is 8000"
3232
},
3333
cors: {
34-
shortcut: 'c',
35-
usage: 'Enable CORS support (cross-origin resource sharing) for JavaScript. You must provide a comma-separated "allow" list of specific domains. The default setting for -cors is an asterisk (*), which allows public access.'
34+
shortcut: "c",
35+
usage: "Enable CORS support (cross-origin resource sharing) for JavaScript. You must provide a comma-separated \"allow\" list of specific domains. The default setting for -cors is an asterisk (*), which allows public access."
3636
},
3737
inMemory: {
38-
shortcut: 'i',
39-
usage: 'DynamoDB; will run in memory, instead of using a database file. When you stop DynamoDB;, none of the data will be saved. Note that you cannot specify both -dbPath and -inMemory at once.'
38+
shortcut: "i",
39+
usage: "DynamoDB; will run in memory, instead of using a database file. When you stop DynamoDB;, none of the data will be saved. Note that you cannot specify both -dbPath and -inMemory at once."
4040
},
4141
dbPath: {
42-
shortcut: 'd',
43-
usage: 'The directory where DynamoDB will write its database file. If you do not specify this option, the file will be written to the current directory. Note that you cannot specify both -dbPath and -inMemory at once. For the path, current working directory is <projectroot>/node_modules/serverless-dynamodb-local/dynamob. For example to create <projectroot>/node_modules/serverless-dynamodb-local/dynamob/<mypath> you should specify -d <mypath>/ or --dbPath <mypath>/ with a forwardslash at the end.'
42+
shortcut: "d",
43+
usage: "The directory where DynamoDB will write its database file. If you do not specify this option, the file will be written to the current directory. Note that you cannot specify both -dbPath and -inMemory at once. For the path, current working directory is <projectroot>/node_modules/serverless-dynamodb-local/dynamob. For example to create <projectroot>/node_modules/serverless-dynamodb-local/dynamob/<mypath> you should specify -d <mypath>/ or --dbPath <mypath>/ with a forwardslash at the end."
4444
},
4545
sharedDb: {
46-
shortcut: 'h',
47-
usage: 'DynamoDB will use a single database file, instead of using separate files for each credential and region. If you specify -sharedDb, all DynamoDB clients will interact with the same set of tables regardless of their region and credential configuration.'
46+
shortcut: "h",
47+
usage: "DynamoDB will use a single database file, instead of using separate files for each credential and region. If you specify -sharedDb, all DynamoDB clients will interact with the same set of tables regardless of their region and credential configuration."
4848
},
4949
delayTransientStatuses: {
50-
shortcut: 't',
51-
usage: 'Causes DynamoDB to introduce delays for certain operations. DynamoDB can perform some tasks almost instantaneously, such as create/update/delete operations on tables and indexes; however, the actual DynamoDB service requires more time for these tasks. Setting this parameter helps DynamoDB simulate the behavior of the Amazon DynamoDB web service more closely. (Currently, this parameter introduces delays only for global secondary indexes that are in either CREATING or DELETING status.'
50+
shortcut: "t",
51+
usage: "Causes DynamoDB to introduce delays for certain operations. DynamoDB can perform some tasks almost instantaneously, such as create/update/delete operations on tables and indexes; however, the actual DynamoDB service requires more time for these tasks. Setting this parameter helps DynamoDB simulate the behavior of the Amazon DynamoDB web service more closely. (Currently, this parameter introduces delays only for global secondary indexes that are in either CREATING or DELETING status."
5252
},
5353
optimizeDbBeforeStartup: {
54-
shortcut: 'o',
55-
usage: 'Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter.'
54+
shortcut: "o",
55+
usage: "Optimizes the underlying database tables before starting up DynamoDB on your computer. You must also specify -dbPath when you use this parameter."
5656
},
5757
migrate: {
58-
shortcut: 'm',
59-
usage: 'After starting dynamodb local, create DynamoDB tables from the current serverless configuration'
58+
shortcut: "m",
59+
usage: "After starting dynamodb local, create DynamoDB tables from the current serverless configuration"
6060
},
6161
seed: {
62-
shortcut: 's',
63-
usage: 'After starting and migrating dynamodb local, injects seed data into your tables',
62+
shortcut: "s",
63+
usage: "After starting and migrating dynamodb local, injects seed data into your tables",
6464
}
6565
}
6666
},
6767
remove: {
68-
lifecycleEvents: ['removeHandler'],
69-
usage: 'Removes local DynamoDB'
68+
lifecycleEvents: ["removeHandler"],
69+
usage: "Removes local DynamoDB"
7070
},
7171
install: {
72-
usage: 'Installs local DynamoDB',
73-
lifecycleEvents: ['installHandler'],
72+
usage: "Installs local DynamoDB",
73+
lifecycleEvents: ["installHandler"],
7474
options: {
7575
localPath: {
76-
shortcut: 'x',
77-
usage: 'Local dynamodb install path'
76+
shortcut: "x",
77+
usage: "Local dynamodb install path"
7878
}
7979
}
8080

@@ -84,23 +84,23 @@ class ServerlessDynamodbLocal {
8484
};
8585

8686
this.hooks = {
87-
'dynamodb:migrate:migrateHandler': this.migrateHandler.bind(this),
88-
'dynamodb:migrate:seedHandler': this.seedHandler.bind(this),
89-
'dynamodb:remove:removeHandler': this.removeHandler.bind(this),
90-
'dynamodb:install:installHandler': this.installHandler.bind(this),
91-
'dynamodb:start:startHandler': this.startHandler.bind(this),
92-
'before:offline:start:init': this.startHandler.bind(this),
87+
"dynamodb:migrate:migrateHandler": this.migrateHandler.bind(this),
88+
"dynamodb:migrate:seedHandler": this.seedHandler.bind(this),
89+
"dynamodb:remove:removeHandler": this.removeHandler.bind(this),
90+
"dynamodb:install:installHandler": this.installHandler.bind(this),
91+
"dynamodb:start:startHandler": this.startHandler.bind(this),
92+
"before:offline:start:init": this.startHandler.bind(this),
9393
};
9494
}
9595

9696
dynamodbOptions() {
9797
const config = this.service.custom.dynamodb || {};
9898
const port = config.start && config.start.port || 8000;
9999
const dynamoOptions = {
100-
endpoint: 'http://localhost:' + port,
101-
region: 'localhost',
102-
accessKeyId: 'MOCK_ACCESS_KEY_ID',
103-
secretAccessKey: 'MOCK_SECRET_ACCESS_KEY'
100+
endpoint: "http://localhost:" + port,
101+
region: "localhost",
102+
accessKeyId: "MOCK_ACCESS_KEY_ID",
103+
secretAccessKey: "MOCK_SECRET_ACCESS_KEY"
104104
};
105105

106106
return {
@@ -120,7 +120,7 @@ class ServerlessDynamodbLocal {
120120
const { seedSources } = this;
121121
return BbPromise.each(seedSources, source => {
122122
if (!source.table) {
123-
throw new Error('seeding source "table" not defined');
123+
throw new Error("seeding source 'table' not defined");
124124
}
125125
return locateSeeds(source.sources || [])
126126
.then((seeds) => writeSeeds(documentClient, source.table, seeds));
@@ -159,7 +159,7 @@ class ServerlessDynamodbLocal {
159159
get tables() {
160160
const resources = this.service.resources.Resources;
161161
return Object.keys(resources).map((key) => {
162-
if (resources[key].Type == 'AWS::DynamoDB::Table') {
162+
if (resources[key].Type == "AWS::DynamoDB::Table") {
163163
return resources[key].Properties;
164164
}
165165
}).filter(n => n);
@@ -170,7 +170,7 @@ class ServerlessDynamodbLocal {
170170
*/
171171
get seedSources() {
172172
const config = this.service.custom.dynamodb;
173-
return _.get(config, 'start.seeds', []);
173+
return _.get(config, "start.seeds", []);
174174
}
175175

176176
createTable(dynamodb, migration) {

src/seeder.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const AWS = require('aws-sdk');
2-
const BbPromise = require('bluebird');
3-
const _ = require('lodash');
4-
const path = require('path');
5-
const fs = require('fs');
1+
const AWS = require("aws-sdk");
2+
const BbPromise = require("bluebird");
3+
const _ = require("lodash");
4+
const path = require("path");
5+
const fs = require("fs");
66

77
// DynamoDB has a 25 item limit in batch requests
88
// https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html
@@ -29,7 +29,7 @@ function writeSeedBatch(dynamodb, tableName, seeds) {
2929
},
3030
};
3131
return new BbPromise((resolve, reject) => {
32-
// interval lets us know how much time we've burnt so far. This lets us have a backoff mechanism to try
32+
// interval lets us know how much time we have burnt so far. This lets us have a backoff mechanism to try
3333
// again a few times in case the Database resources are in the middle of provisioning.
3434
let interval = 0;
3535
function execute(interval) {
@@ -47,7 +47,7 @@ function writeSeedBatch(dynamodb, tableName, seeds) {
4747
};
4848
execute(interval);
4949
});
50-
};
50+
}
5151

5252
/**
5353
* Writes a seed corpus to the given database table
@@ -57,13 +57,13 @@ function writeSeedBatch(dynamodb, tableName, seeds) {
5757
*/
5858
function writeSeeds(dynamodb, tableName, seeds) {
5959
if (!dynamodb) {
60-
throw new Error('dynamodb argument must be provided');
60+
throw new Error("dynamodb argument must be provided");
6161
}
6262
if (!tableName) {
63-
throw new Error('table name argument must be provided');
63+
throw new Error("table name argument must be provided");
6464
}
6565
if (!seeds) {
66-
throw new Error('seeds argument must be provided');
66+
throw new Error("seeds argument must be provided");
6767
}
6868

6969
if (seeds.length > 0) {
@@ -77,37 +77,50 @@ function writeSeeds(dynamodb, tableName, seeds) {
7777
}
7878
}
7979

80+
/**
81+
* A promise-based function that determines if a file exists
82+
* @param {string} fileName The path to the file
83+
*/
8084
function fileExists(fileName) {
8185
return new BbPromise((resolve) => {
8286
fs.exists(fileName, (exists) => resolve(exists));
8387
});
8488
}
8589

90+
/**
91+
* Scrapes seed files out of a given location. This file may contain
92+
* either a simple json object, or an array of simple json objects. An array
93+
* of json objects is returned.
94+
*
95+
* @param {any} location the filename to read seeds from.
96+
*/
97+
function getSeedsAtLocation(location) {
98+
// load the file as JSON
99+
const result = require(location);
100+
101+
// Ensure the output is an array
102+
if (Array.isArray(result)) {
103+
return result;
104+
} else {
105+
return [ result ];
106+
}
107+
}
108+
86109
/**
87110
* Locates seeds given a set of files to scrape
88111
* @param {string[]} sources The filenames to scrape for seeds
89112
*/
90113
function locateSeeds(sources = [], cwd = process.cwd()) {
91114
const locations = sources.map(source => path.join(cwd, source));
92115
return BbPromise.map(locations, (location) => {
93-
return fileExists(location)
94-
.then((exists) => {
116+
return fileExists(location).then((exists) => {
95117
if(!exists) {
96-
throw new Error('source file ' + location + ' does not exist');
97-
}
98-
99-
// load the file as JSON
100-
const result = require(location);
101-
102-
// Ensure the output is an array
103-
if (Array.isArray(result)) {
104-
return result;
105-
} else {
106-
return [ result ];
118+
throw new Error("source file " + location + " does not exist");
107119
}
120+
return getSeedsAtLocation(location);
108121
});
109122
// Smash the arrays together
110-
}).then(seedArrays => [].concat.apply([], seedArrays));
123+
}).then((seedArrays) => [].concat.apply([], seedArrays));
111124
}
112125

113126
module.exports = { writeSeeds, locateSeeds };

0 commit comments

Comments
 (0)