Skip to content

Commit

Permalink
Fixes #14 - Custom seeder template
Browse files Browse the repository at this point in the history
* Allows to generate seeders from a custom template
* Allows to install a custom template when running the install command
  • Loading branch information
sharvit committed Jul 16, 2019
1 parent 2be6745 commit 6420911
Show file tree
Hide file tree
Showing 31 changed files with 736 additions and 117 deletions.
3 changes: 0 additions & 3 deletions examples/md-seed-example/md-seed-generator.json

This file was deleted.

26 changes: 26 additions & 0 deletions src/e2e/generate-sandboxes/sandbox-1/md-seed-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import mongoose from 'mongoose';

const mongoURL =
process.env.MONGO_URL ||
'mongodb://localhost:27017/md-seed-generate-sandbox-1';

/**
* Seeders List
* order is important
* @type {Object}
*/
export const seedersList = {};
/**
* Connect to mongodb implementation
* @return {Promise}
*/
export const connect = async () =>
await mongoose.connect(
mongoURL,
{ useNewUrlParser: true }
);
/**
* Drop/Clear the database implementation
* @return {Promise}
*/
export const dropdb = async () => mongoose.connection.db.dropDatabase();
14 changes: 14 additions & 0 deletions src/e2e/generate-sandboxes/sandbox-1/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "md-seed-example",
"version": "2.0.0",
"description": "Example of using mongoose-data-seed",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Avi Sharvit <sharvita@gmail.com> (https://sharvit.github.io)",
"license": "MIT",
"dependencies": {
"mongoose": "^5.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(
'I am <%= seederName %> and I created by a custom seeder template.'
);
26 changes: 26 additions & 0 deletions src/e2e/generate-sandboxes/sandbox-2/md-seed-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import mongoose from 'mongoose';

const mongoURL =
process.env.MONGO_URL ||
'mongodb://localhost:27017/md-seed-generate-sandbox-1';

/**
* Seeders List
* order is important
* @type {Object}
*/
export const seedersList = {};
/**
* Connect to mongodb implementation
* @return {Promise}
*/
export const connect = async () =>
await mongoose.connect(
mongoURL,
{ useNewUrlParser: true }
);
/**
* Drop/Clear the database implementation
* @return {Promise}
*/
export const dropdb = async () => mongoose.connection.db.dropDatabase();
18 changes: 18 additions & 0 deletions src/e2e/generate-sandboxes/sandbox-2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "md-seed-example",
"version": "2.0.0",
"description": "Example of using mongoose-data-seed",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Avi Sharvit <sharvita@gmail.com> (https://sharvit.github.io)",
"license": "MIT",
"dependencies": {
"mongoose": "^5.0.0"
},
"mdSeed": {
"seedersFolder": "./my-custom-seeders-folder",
"customSeederTemplate": "./custom-seeder-template.js"
}
}
46 changes: 27 additions & 19 deletions src/e2e/generate.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,15 @@ import FilesSandbox from './utils/files-sandbox';
import { runCommand } from '../lib/commands/helpers';
import config from '../lib/config';

const createSandbox = () => {
const sandbox = new FilesSandbox('generate-');

const { sandboxPath } = sandbox;
const getSandboxExamplePath = (exampleName = 'sandbox-1') =>
path.join(__dirname, `./generate-sandboxes/${exampleName}`);

const examplesFolderName = 'md-seed-example';
const createSandbox = async sandboxOriginFilesPath => {
const sandbox = new FilesSandbox('generate-');

fs.copyFileSync(
path.join(__dirname, `../../examples/${examplesFolderName}/package.json`),
path.join(sandboxPath, 'package.json')
);
fs.copyFileSync(
path.join(
__dirname,
`../../examples/${examplesFolderName}/md-seed-config.js`
),
path.join(sandboxPath, 'md-seed-config.js')
);
await sandbox.copyFolderToSandbox(sandboxOriginFilesPath);

config.update(sandboxPath);
config.update(sandbox.sandboxPath);

return sandbox;
};
Expand All @@ -36,7 +25,10 @@ const getFilesForSnapshot = sandbox =>
sandbox
.readFiles()
.filter(
({ name }) => name !== 'md-seed-config.js' && name !== 'package.json'
({ name }) =>
name !== 'md-seed-config.js' &&
name !== 'custom-seeder-template.js' &&
name !== 'package.json'
);

test.beforeEach('mock', t => {
Expand Down Expand Up @@ -67,7 +59,7 @@ test.serial(
);

test.serial('md-seed generate some-seeder', async t => {
const sandbox = createSandbox();
const sandbox = await createSandbox(getSandboxExamplePath('sandbox-1'));

await t.notThrows(runCommand('generate', 'some-name'));

Expand All @@ -78,3 +70,19 @@ test.serial('md-seed generate some-seeder', async t => {
t.snapshot(console.log.args, 'log results');
t.snapshot(files, 'sandbox content');
});

test.serial(
'md-seed generate some-seeder with custom template and seeders folder',
async t => {
const sandbox = await createSandbox(getSandboxExamplePath('sandbox-2'));

await t.notThrows(runCommand('generate', 'some-name'));

const files = getFilesForSnapshot(sandbox);

sandbox.clean();

t.snapshot(console.log.args, 'log results');
t.snapshot(files, 'sandbox content');
}
);
27 changes: 27 additions & 0 deletions src/e2e/generate.e2e.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,30 @@ Generated by [AVA](https://ava.li).
> Snapshot 1
'Must contain md-seed-config.js at the project root. run `md-seed init` to create the config file.'

## md-seed generate some-seeder with custom template and seeders folder

> log results
[
[
'CREATED my-custom-seeders-folder/some-name.seeder.js',
],
]

> sandbox content
[
{
content: [
{
content: `console.log(␊
'I am SomeName and I created by a custom seeder template.'␊
);␊
`,
name: 'some-name.seeder.js',
},
],
name: 'my-custom-seeders-folder',
},
]
Binary file modified src/e2e/generate.e2e.js.snap
Binary file not shown.
23 changes: 14 additions & 9 deletions src/e2e/init.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,22 @@ test.serial('md-seed init --help', async t => {
t.snapshot(results);
});

test.serial('md-seed init --seedersFolder=folder-name', async t => {
const argv = '--seedersFolder=folder-name'.split(' ');
test.serial(
'md-seed init --seedersFolder=folder-name seederTemplate=file-path.js',
async t => {
const argv = '--seedersFolder=folder-name --seederTemplate=file-path.js'.split(
' '
);

const sandbox = createSandbox();
const sandbox = createSandbox();

await t.notThrows(runCommand('init', argv));
await t.notThrows(runCommand('init', argv));

const files = sandbox.readFiles();
const files = sandbox.readFiles();

sandbox.clean();
sandbox.clean();

t.snapshot(console.log.args, 'log results');
t.snapshot(files, 'sandbox content');
});
t.snapshot(console.log.args, 'log results');
t.snapshot(files, 'sandbox content');
}
);
34 changes: 29 additions & 5 deletions src/e2e/init.e2e.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ Generated by [AVA](https://ava.li).
Synopsis␊
$ md-seed init [[1m--seedersFolder[22m=[4mfolder-name[24m] ␊
$ md-seed init [1m--help[22m ␊
$ md-seed init [[1m--seedersFolder[22m=[4mfolder-name[24m] [[1m--seederTemplate[22m=[4mfile-path[24m]
$ md-seed init [1m--help[22m
Options␊
-f, --seedersFolder string Seeders folder name ␊
-h, --help Show usage guide ␊
-f, --seedersFolder string Seeders folder name ␊
-t, --seederTemplate string Seeder template file path ␊
-h, --help Show usage guide ␊
`

## md-seed init --seedersFolder=folder-name
## md-seed init --seedersFolder=folder-name seederTemplate=file-path.js

> log results
Expand All @@ -44,6 +45,29 @@ Generated by [AVA](https://ava.li).
> sandbox content
[
{
content: `import { Seeder } from 'mongoose-data-seed';␊
import { Model } from '../server/models';␊
const data = [{␊
}];␊
class <%= seederName %>Seeder extends Seeder {␊
async shouldRun() {␊
return Model.countDocuments().exec().then(count => count === 0);␊
}␊
async run() {␊
return Model.create(data);␊
}␊
}␊
export default <%= seederName %>Seeder;␊
`,
name: 'file-path.js',
},
{
content: [],
name: 'folder-name',
Expand Down
Binary file modified src/e2e/init.e2e.js.snap
Binary file not shown.
3 changes: 0 additions & 3 deletions src/e2e/run-sandboxes/sandbox-1/md-seed-generator.json

This file was deleted.

14 changes: 2 additions & 12 deletions src/e2e/run.e2e.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import test from 'ava';
import sinon from 'sinon';
import path from 'path';
import { ncp } from 'ncp';

import FilesSandbox from './utils/files-sandbox';

Expand All @@ -11,21 +10,12 @@ import config from '../lib/config';
const getSandboxExamplePath = (exampleName = 'sandbox-1') =>
path.join(__dirname, `./run-sandboxes/${exampleName}`);

const copyFolder = (source, destination) =>
new Promise((resolve, reject) => {
ncp(source, destination, err => {
if (err) return reject(err);
resolve();
});
});

const createSandbox = async sandboxOriginFilesPath => {
const sandbox = new FilesSandbox('run-');
const { sandboxPath } = sandbox;

await copyFolder(sandboxOriginFilesPath, sandboxPath);
await sandbox.copyFolderToSandbox(sandboxOriginFilesPath);

config.update(sandboxPath);
config.update(sandbox.sandboxPath);

return sandbox;
};
Expand Down
10 changes: 10 additions & 0 deletions src/e2e/utils/files-sandbox.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs';
import path from 'path';
import rimraf from 'rimraf';
import { ncp } from 'ncp';

const sandboxesPath = path.join(__dirname, '../../sandboxes');

Expand Down Expand Up @@ -29,6 +30,15 @@ export default class FilesSandbox {
this.sandboxPath = fs.mkdtempSync(path.join(sandboxesPath, prefix));
}

copyFolderToSandbox(source) {
return new Promise((resolve, reject) =>
ncp(source, this.sandboxPath, err => {
if (err) return reject(err);
resolve();
})
);
}

readFiles() {
return readFolderFiles(this.sandboxPath);
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib/commands/init/option-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ const optionDefinitions = [
type: String,
description: 'Seeders folder name',
},
{
name: 'seederTemplate',
alias: 't',
type: String,
description: 'Seeder template file path',
},
{
name: 'help',
alias: 'h',
Expand Down
Loading

0 comments on commit 6420911

Please sign in to comment.