Skip to content

Commit

Permalink
Merge 766cea6 into 3c29bf7
Browse files Browse the repository at this point in the history
  • Loading branch information
sharvit committed Aug 24, 2019
2 parents 3c29bf7 + 766cea6 commit 31d0f6f
Show file tree
Hide file tree
Showing 46 changed files with 1,399 additions and 151 deletions.
29 changes: 29 additions & 0 deletions .esdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"source": "./src",
"destination": "./docs",
"excludes": ["\\.config\\.js$", "__mocks__", "e2e"],
"plugins": [
{
"name": "esdoc-standard-plugin",
"option": {
"lint": { "enable": true },
"accessor": {
"access": ["public", "protected"],
"autoPrivate": true
},
"undocumentIdentifier": { "enable": false },
"unexportedIdentifier": { "enable": false },
"typeInference": { "enable": true },
"test": {
"source": "./src",
"interfaces": ["describe", "it", "context", "suite", "test"],
"includes": ["\\.test\\.js$"],
"excludes": ["\\.config\\.js$"]
}
}
}, {
"name": "esdoc-ecmascript-proposal-plugin",
"option": { "all": true }
}
]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
templates/
node_modules/
dist/
docs/
coverage/
.nyc_output/
package-lock.json
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ typings/

# compiled
dist
docs

# sandboxes
sandboxes/
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ jobs:
provider: script
skip_cleanup: true
script: yarn semantic-release
- stage: publish docs
if: branch = master AND type = push
node_js: 12
script: yarn build:docs
deploy:
provider: pages
skip_cleanup: true
github_token: $GH_TOKEN
local_dir: ./docs
keep_history: true
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"build": "yarn build:dist",
"build:clean": "rimraf dist",
"build:dist": "babel src/ --out-dir dist/ --ignore 'src/**/*.test.js','src/e2e/*','src/**/__mocks__/*','src/lib/utils/test-helpers.js','src/setup-test-env.js'",
"build:docs": "esdoc -c .esdoc.json",
"develop:docs": "watch \"yarn build:docs\" ./src",
"test": "cross-env NODE_ENV=test nyc --no-cache ava",
"test:watch": "yarn test --watch",
"test:check-coverage": "nyc check-coverage --statements 100 --branches 100 --functions 100 --lines 100",
Expand Down Expand Up @@ -108,6 +110,9 @@
"commitlint-config-cz": "^0.12.0",
"coveralls": "^3.0.2",
"cross-env": "^5.2.0",
"esdoc": "^1.1.0",
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
"esdoc-standard-plugin": "^1.0.0",
"eslint": "6.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-config-standard": "^14.0.1",
Expand All @@ -122,7 +127,8 @@
"prettier": "^1.15.3",
"prettier-eslint": "^9.0.0",
"rimraf": "^3.0.0",
"sinon": "^7.1.1"
"sinon": "^7.1.1",
"watch": "^1.0.2"
},
"optionalDependencies": {
"cz-conventional-changelog": "^3.0.2",
Expand Down
4 changes: 4 additions & 0 deletions src/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import 'regenerator-runtime/runtime';
import { runCommand, getCommandAndArgvFromCli } from '../lib/commands/helpers';
import { exit } from '../lib/utils/helpers';

/**
* Main entry point, run md-seed cli
* @return {Promise}
*/
const run = async () => {
try {
// recive the command and the arguments input from the cli
Expand Down
26 changes: 26 additions & 0 deletions src/lib/commands/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,55 @@ import help from './help';
import init from './init';
import run from './run';

/**
* Available command names
*
* Map command key to command name
* @type {Map<string, string>}
*/
export const commands = {
GENERATE: 'generate',
HELP: 'help',
INIT: 'init',
RUN: 'run',
};

/**
* Available command aliases
*
* Map alias to command name
* @type {Map<string, string>}
*/
export const aliases = {
g: commands.GENERATE,
h: commands.HELP,
};

/**
* All available command names as list (includes aliases)
* @type {string[]}
*/
export const availableCommandsList = [
null, // no command should run help
...Object.values(commands),
...Object.keys(aliases),
];

/**
* Commands map
*
* Map command name to the actuall command function
* @type {Map<string, Function>}
*/
export const commandsMap = {
[commands.GENERATE]: generate,
[commands.HELP]: help,
[commands.INIT]: init,
[commands.RUN]: run,
};

/**
* The fefault command name
* @type {string}
*/
export const defaultCommand = commands.HELP;
5 changes: 5 additions & 0 deletions src/lib/commands/generate/generate-seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { SeederGenerator } from '../../core';
import { validateUserConfig } from '../../utils/helpers';
import config from '../../config';

/**
* Generate a new seeder.
* @param {string} name seeder name
* @return {Promise}
*/
const generateSeeder = async name => {
validateUserConfig();

Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/generate/help.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import usageGuide from './usage-guide';

/**
* Prints the generate command user-guide
*/
const help = () => console.log(usageGuide);

export default help;
5 changes: 5 additions & 0 deletions src/lib/commands/generate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { getOptions } from './options';
import help from './help';
import generateSeeder from './generate-seeder';

/**
* mongoose-data-seed generate command
* @param {stringp[]} argv cli arguments
* @return {Promise}
*/
export default async argv => {
const { seederName, helpWanted } = getOptions(argv);

Expand Down
4 changes: 4 additions & 0 deletions src/lib/commands/generate/option-definitions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* Generate command option defenitions
* @type {Object[]}
*/
const optionDefinitions = [
{
name: 'name',
Expand Down
14 changes: 14 additions & 0 deletions src/lib/commands/generate/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { trim } from 'lodash';
import help from './help';
import optionDefinitions from './option-definitions';

/**
* Get generate options from argv
* @param {string[]} argv cli argv
* @return {Object} run options
* @property {string} seederName
* @property {boolean} helpWanted
*/
export const getOptions = argv => {
const { name: seederName, help: helpWanted } = commandLineArgs(
optionDefinitions,
Expand All @@ -18,6 +25,13 @@ export const getOptions = argv => {
return options;
};

/**
* Validate generate command options
* @param {Object} [options={}] Options
* @param {string} options.seederName seeder name to generate
* @param {boolean} options.helpWanted help wanted?
* @throws {Error} throw error when options are not valid.
*/
export const validateOptions = ({ seederName, helpWanted } = {}) => {
if (
!helpWanted &&
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/generate/usage-guide.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import generateUsageGuide from 'command-line-usage';
import optionDefinitions from './option-definitions';

/**
* @private
*/
const usageGuide = generateUsageGuide([
{
header: 'Generate Seeder',
Expand Down
7 changes: 6 additions & 1 deletion src/lib/commands/help/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import usageGuide from './usage-guide';

export default async () => {
/**
* Prints the help
*/
const help = async () => {
console.log(usageGuide);
};

export default help;
4 changes: 4 additions & 0 deletions src/lib/commands/help/usage-guide.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import generateUsageGuide from 'command-line-usage';

/**
* Help command user guide
* @type {string}
*/
const usageGuide = generateUsageGuide([
{
header: 'Mongoose Data Seeder',
Expand Down
26 changes: 26 additions & 0 deletions src/lib/commands/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ import {
availableCommandsList,
} from './constants';

/**
* Whether a given command is an alias
* @param {string} command
* @return {Boolean}
*/
export const isAlias = command => Object.keys(aliases).includes(command);

/**
* Get the command name of a given alias
* @param {string} alias
* @return {string}
*/
export const aliasToCommand = alias => aliases[alias];

/**
* Get the function of a given command
* @param {string} command command name
* @return {Function} command function
*/
export const commandToFunction = command => {
command = command || defaultCommand;

Expand All @@ -20,12 +35,23 @@ export const commandToFunction = command => {
return commandsMap[command];
};

/**
* Get the command and the arguments from the cli
* @return {Object}
* @property {string} command command name
* @property {string[]} argv command arguments
*/
export const getCommandAndArgvFromCli = () => {
const { command, argv } = commandLineCommands(availableCommandsList);

return { command, argv };
};

/**
* Run command
* @param {string} command command name
* @param {string} argv command arguments
*/
export const runCommand = (command, argv) => {
const commandFunction = commandToFunction(command);

Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/init/help.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import usageGuide from './usage-guide';

/**
* Prints the install command user-guide
*/
const help = () => console.log(usageGuide);

export default help;
5 changes: 5 additions & 0 deletions src/lib/commands/init/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { getOptions } from './options';
import help from './help';
import runInstaller from './run-installer';

/**
* mongoose-data-seed init command
* @param {stringp[]} argv cli arguments
* @return {Promise}
*/
export default async argv => {
const { helpWanted, ...options } = getOptions(argv);

Expand Down
15 changes: 15 additions & 0 deletions src/lib/commands/init/installer-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import chalk from 'chalk';
import BaseLogger from '../../utils/base-logger';
import { Installer } from '../../core';

/**
* Installer Logger
*/
export default class InstallerLogger extends BaseLogger {
/**
* Log next notification
* @param {Object} notification notification to log
* @param {string} notification.type operation type
* @param {Object} notification.payload operation payload
*/
next({ type, payload }) {
switch (type) {
case Installer.operations.WRITE_USER_GENERETOR_CONFIG_SUCCESS:
Expand Down Expand Up @@ -44,6 +53,12 @@ export default class InstallerLogger extends BaseLogger {
}
}

/**
* Log error
* @param {Object} error error to log
* @param {string} error.type error type
* @param {Object} error.payload error payload
*/
error({ type, payload }) {
switch (type) {
case Installer.operations.WRITE_USER_GENERETOR_CONFIG_ERROR:
Expand Down
4 changes: 4 additions & 0 deletions src/lib/commands/init/option-definitions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* Init command option defenitions
* @type {Object[]}
*/
const optionDefinitions = [
{
name: 'seedersFolder',
Expand Down
15 changes: 15 additions & 0 deletions src/lib/commands/init/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import {
import { promptSeedersFolder, promptSeederTemplate } from './prompts';
import optionDefinitions from './option-definitions';

/**
* Get init options from argv
* @param {string[]} argv cli argv
* @return {Object} init options
* @property {string} seedersFolder
* @property {string} customSeederTemplate
* @property {boolean} helpWanted
*/
export const getOptions = argv => {
const {
seedersFolder,
Expand All @@ -17,6 +25,13 @@ export const getOptions = argv => {
return { seedersFolder, customSeederTemplate, helpWanted };
};

/**
* Prompt missing options for init command
* @param {Object} [options={}] Init command options
* @param {[type]} options.seedersFolder seeders folder
* @param {[type]} options.customSeederTemplate custom seeder template
* @return {Promise} Options without missing
*/
export const promptMissingOptions = async ({
seedersFolder,
customSeederTemplate,
Expand Down

0 comments on commit 31d0f6f

Please sign in to comment.