Skip to content

Commit

Permalink
Fixes #14 - Custom seeder template
Browse files Browse the repository at this point in the history
  • Loading branch information
sharvit committed Nov 16, 2018
1 parent 77b926e commit 7302c43
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 40 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.
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
11 changes: 8 additions & 3 deletions src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,26 @@ const config = {
},

update(projectRoot = getProjectRoot()) {
const userGeneratorConfig = this.getUserGeneratorConfig(projectRoot);
const { seedersFolder, customSeederTemplate } = this.getUserGeneratorConfig(
projectRoot
);

const userSeedersFolderName = userGeneratorConfig.seedersFolder;
const userSeedersFolderName = seedersFolder;
const userSeedersFolderPath = path.join(projectRoot, userSeedersFolderName);

const userConfigFilename = 'md-seed-config.js';
const userConfigFilepath = path.join(projectRoot, userConfigFilename);
const userConfigExists = fs.existsSync(userConfigFilepath);

const seederTemplate = path.join(__dirname, '../../templates/seeder.js');
const configTemplate = path.join(
__dirname,
'../../templates/md-seed-config.js'
);

const seederTemplate = customSeederTemplate
? path.join(projectRoot, customSeederTemplate)
: path.join(__dirname, '../../templates/seeder.js');

this.projectRoot = projectRoot;
this.userConfigFilename = userConfigFilename;
this.userConfigFilepath = userConfigFilepath;
Expand Down

0 comments on commit 7302c43

Please sign in to comment.