Skip to content

Commit

Permalink
Introduce TS runtime in CLI (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Sep 12, 2018
1 parent 7d97e63 commit 9ac5934
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 18 deletions.
8 changes: 2 additions & 6 deletions cli/bin/seed.js
@@ -1,9 +1,5 @@
#!/usr/bin/env node
'use strict';

const commandLineArgs = require('command-line-args');
const { optionsDefinition } = require('../dist/options');
const cli = require('../dist/index');

const options = commandLineArgs(optionsDefinition);
cli.run(options);
require('ts-node').register();
require('../dist/index').run();
191 changes: 190 additions & 1 deletion cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions cli/package.json
Expand Up @@ -59,9 +59,11 @@
"@types/node": "^10.9.0",
"command-line-args": "^5.0.0",
"command-line-usage": "^5.0.0",
"mongo-seeding": "^2.2.0"
"mongo-seeding": "^2.2.0",
"ts-node": "^7.0.1",
"typescript": "^3.0.0"
},
"devDependencies": {
"typescript": "^3.0.0"
"@types/command-line-args": "^5.0.0"
}
}
10 changes: 7 additions & 3 deletions cli/src/index.ts
Expand Up @@ -3,21 +3,25 @@ process.env.DEBUG = 'mongo-seeding';

import { seedDatabase } from 'mongo-seeding';
import {
convertOptions,
populateOptions,
optionsDefinition,
shouldShowHelp,
CommandLineOptions,
validateOptions,
} from './options';
import { showHelp } from './help';

export const run = async (options: CommandLineOptions) => {
import * as commandLineArgs from "command-line-args";

export const run = async () => {
const options: CommandLineOptions = commandLineArgs(optionsDefinition) as CommandLineOptions;

if (shouldShowHelp(options)) {
showHelp();
return;
}

const partialConfig = convertOptions(options);
const partialConfig = populateOptions(options);
try {
validateOptions(options);
await seedDatabase(partialConfig);
Expand Down
3 changes: 2 additions & 1 deletion cli/src/options.ts
Expand Up @@ -120,7 +120,7 @@ const validatePositiveNumber = (variable: number | undefined, name: string) => {
}
};

export const convertOptions = (
export const populateOptions = (
options: CommandLineOptions,
): DeepPartial<AppConfig> => ({
database: {
Expand All @@ -137,4 +137,5 @@ export const convertOptions = (
dropCollection: options['drop-collection'],
replaceIdWithUnderscoreId: options['replace-id'],
reconnectTimeoutInSeconds: options['reconnect-timeout'],
supportedExtensions: ["js", "json", "ts"]
});
2 changes: 2 additions & 0 deletions samples/example-ts/README.md
@@ -0,0 +1,2 @@
## WORK IN PROGRESS
For a limited time the samples work only with new, unreleased Mongo Seeding CLI (3.0.0). Currently they break compatibility with current Mongo Seeding Docker Image. Please see examples for [latest stable version](https://github.com/pkosiec/mongo-seeding/tree/v2.2.0/samples).
9 changes: 6 additions & 3 deletions samples/example-ts/data/1-categories/categories.ts
@@ -1,9 +1,12 @@
import { mapToEntities } from "@helpers/index";
import { mapToEntities } from "../../helpers";
import { Category } from "../../models";

const categories = [
const categoryNames = [
"Uncategorized",
"Cats",
"Dogs"
];

export = mapToEntities(categories);
const categories: Category[] = mapToEntities(categoryNames);

export = categories
4 changes: 2 additions & 2 deletions samples/example-ts/data/2-posts/posts.ts
@@ -1,5 +1,5 @@
import { getObjectId } from '@helpers/index';
import { Post, Comment } from '@models/index';
import { getObjectId } from "../../helpers";
import { Post } from "../../models";

const posts: Post[] = [
{
Expand Down

0 comments on commit 9ac5934

Please sign in to comment.