Skip to content

Commit

Permalink
feat(relation): add support to create relations
Browse files Browse the repository at this point in the history
not fully supported since test are not yet ready
  • Loading branch information
gerard2perez committed Apr 20, 2017
1 parent 210e00e commit b643732
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/commands/relation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export default (new Command(__filename, 'makes a relation betwen two models\n
['-r', '--rest', 'Makes the model REST enabled.']
])
.Action(async function (sourcemodel, linkmode, targetmodel, options) {
if (!sourcemodel || !targetmodel) {
console.log(' The command cannot be run this way.\n\tkoaton relation -h\n to see help.'.yellow);
return 0;
}
let mode = linkmodes[linkmode.toLowerCase()];
if (!mode) {
console.log(`Invalid mode selected: ${linkmode.red}\nAvaliable options ${'hasMany, belongsTo'.cyan}.`);
Expand Down
13 changes: 6 additions & 7 deletions src/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import WatchFileToCopy from '../support/WatchFileToCopy';
import CopyStatic from '../support/CopyStatic';
import deamon from '../deamon';
import imagecompressor from '../functions/imagecompressor';
import {
postBuildEmber,
preBuildEmber
} from '../functions/emberBuilder';
import { postBuildEmber, preBuildEmber } from '../functions/emberBuilder';

let watching = [],
building = [];
let nginxbuilt = false;
const deleted = function (file) {
try {
fs.unlinkSync(file.replace('assets', 'public'));
Expand Down Expand Up @@ -66,7 +64,7 @@ const deleted = function (file) {
console.log(buffer.toString());
} else if (buffer.toString().indexOf('Build successful') > -1) {
if (cb) {
appst.result = `${app.yellow} → http://${scfg.hostname}:${scfg.port}${mount.cyan}`;
appst.result = `${app.yellow} → http://${scfg.hostname}${nginxbuilt ? '' : ':' + scfg.port}${mount.cyan}`;
appst.pid = ember.pid;
cb(null, appst);
cb = null;
Expand Down Expand Up @@ -143,7 +141,8 @@ export default (new Command(__filename, 'Runs your awsome Koaton applicaction es
if (options.nginx) {
const getnginxpath = require('../functions/nginx').getnginxpath;
await utils.copy(ProyPath(`${scfg.name}.conf`), path.join(await getnginxpath(), 'enabled_sites', `${scfg.name}.conf`), 1);
await utils.shell('Restarting Nginx', ['nginx', '-s', 'reload'], process.cwd());
nginxbuilt = await utils.shell('Restarting Nginx', ['nginx', '-s', 'reload'], process.cwd());
nginxbuilt = nginxbuilt === 0;
}
if (options.production === 'development') {
await CopyStatic();
Expand Down Expand Up @@ -237,7 +236,7 @@ export default (new Command(__filename, 'Runs your awsome Koaton applicaction es
}).then((EmberPids) => {
return Events('pre', 'serve').then(() => {
return new Promise(function (resolve, reject) {
let server = deamon(resolve, reject, EmberPids);
let server = deamon(resolve, reject, EmberPids, nginxbuilt);
process.once('SIGINT', function () {
server.kill();
resolve(0);
Expand Down
10 changes: 5 additions & 5 deletions src/deamon.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import livereload from './utils/livereload';
import * as spawn from 'cross-spawn';
import * as psTree from 'ps-tree';

function LoadServer (resolve, reject, EmberPids) {
function LoadServer (resolve, reject, EmberPids, nginx) {
let PIDPromises = [];
if (process.env.istesting) {
for (const pid of EmberPids) {
Expand All @@ -25,7 +25,7 @@ function LoadServer (resolve, reject, EmberPids) {
let text = buffer.toString();
let found = text.indexOf('Enviroment') > -1;
if (found) {
notifier('Koaton', `Serving http://${scfg.hostname}:${scfg.port}`);
notifier('Koaton', `Serving http://${scfg.hostname}${nginx ? '' : ':' + scfg.port}`);
livereload.reload();
if (process.env.istesting) {
Promise.all(PIDPromises).then((PIDs) => {
Expand All @@ -47,7 +47,7 @@ function LoadServer (resolve, reject, EmberPids) {
return KoatonServer;
}

export default function StartKoatonServer (resolve, reject, EmberPids) {
export default function StartKoatonServer (resolve, reject, EmberPids, nginx = false) {
let watcher = new Watch('./**', {
awaitWriteFinish: {
stabilityThreshold: 500,
Expand All @@ -70,10 +70,10 @@ export default function StartKoatonServer (resolve, reject, EmberPids) {
'*.conf'
]
});
let server = LoadServer(resolve, reject, EmberPids);
let server = LoadServer(resolve, reject, EmberPids, nginx);
watcher.on('all', (event, path) => {
server.kill();
server = LoadServer(resolve, reject, EmberPids);
server = LoadServer(resolve, reject, EmberPids, nginx);
});
return server;
}
2 changes: 1 addition & 1 deletion src/functions/emberBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fs from 'fs';
import {join} from 'path';

const getinflections = async function getInflections (appName, show = true) {
const inflections = require(join(process.cwd(), 'config', 'inflections.js')),
const inflections = require(join(process.cwd(), 'config', 'inflections.js')).default,
irregular = (inflections.plural || [])
.concat(inflections.singular || [])
.concat(inflections.irregular || []),
Expand Down
1 change: 1 addition & 0 deletions src/support/ORMModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export default class ORMModel {
return res.join(' ');
}
});
console.log('+-0-', this._relations);
}
toCaminte () {
let definition = [];
Expand Down
2 changes: 1 addition & 1 deletion templates/ember_apps/inflector.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function initialize(/* container, application */) {
inflector.irregular(inflection[0],inflection[1]);
});
uncontable.forEach(function(inflection){
inflector.uncontable(inflection);
inflector.uncountable(inflection);
});
}
export default {
Expand Down
26 changes: 26 additions & 0 deletions test/configuration/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,33 @@ tests.push(new TestNode(cmdname, ['user', 'name lastname age:number email:email'
embercontroller === model.toCRUDTable() &&
emberemplate === undefined;
});
tests.push(new TestNode(cmdname, ['post', 'date:date tags content', {
force: true,
rest: true
}], true, true))
.Expect('Creates koaton model', true, (_, project) => {
let model = ModelManager('post', require(path.join(project, 'models', 'post.js')).default);
return model.date.toString() === 'date' &&
model.tags.toString() === 'string' &&
model.content.toString() === 'string';
});
tests.push(new TestNode(cmdname, ['category', 'name', {
force: true,
rest: true
}], true, true))
.Expect('Creates koaton model', true, (_, project) => {
let model = ModelManager('category', require(path.join(project, 'models', 'category.js')).default);
return model.name.toString() === 'string';
});

tests.push(new TestNode(cmdname, ['onlyone', 'name', {
force: true,
rest: true
}], true, true))
.Expect('Creates koaton model', true, (_, project) => {
let model = ModelManager('onlyone', require(path.join(project, 'models', 'onlyone.js')).default);
return model.name.toString() === 'string';
});
tests.last.CleanUp(() => {
process.chdir('..');
});
Expand Down
62 changes: 62 additions & 0 deletions test/configuration/relation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as path from 'upath';
import * as fs from 'fs-extra';
import TestNode from '../support/TestNode';
import '../support/array';
import ModelManager from '../../src/modelmanager';
import ServerConfiguaration from '../../src/support/Server';

let tests = [];
let cmdname = 'koaton relation';
tests.push(new TestNode('(no args)', [undefined, {}], true))
.SetUp(() => {
process.chdir('testingapp');
process.env.isproyect = 'true';
global.scfg = new ServerConfiguaration();
})
.Expect('Ask to render help.', ' The command cannot be run this way.\n\tkoaton relation -h\n to see help.', (log) => log);

tests.push(new TestNode(cmdname, [{
H: true
}], true, true))
.Expect('Renders help', true, (log) => {
return log.indexOf(cmdname) > -1;
});

tests.push(new TestNode(cmdname, ['user', 'hasOne', 'onlyone', {
force: true
}], true, true))
.Expect('Creates koaton relation on user model', true, (_, project) => {
let model1 = ModelManager('user', requireNoCache(path.join(project, 'models', 'user.js')).default);
let model2 = ModelManager('onlyone', requireNoCache(path.join(project, 'models', 'onlyone.js')).default);
console.log(model1.toMeta());
console.log(model2.toMeta());
return false;
});
tests.push(new TestNode(cmdname, ['user', 'hasMany', 'post', {
force: true
}], true, true))
.Expect('Creates koaton relation on user model', true, (_, project) => {
let model = ModelManager('user', requireNoCache(path.join(project, 'models', 'user.js')).default);
// console.log(model.toCaminte());
// console.log(model.toEmberModel());
// console.log(model.toCRUDTable());
// console.log(model.toMeta());
return false;
// return model.name.toString() === 'string' &&
// model.lastname.toString() === 'string' &&
// model.age.toString() === 'number' &&
// model.email.toString() === 'email';
});
tests.push(new TestNode(cmdname, ['user', 'hasMany', 'post', {
force: true
}], true, true))
.Expect('Creates koaton relation on user model', true, (_, project) => {
let model = ModelManager('user', requireNoCache(path.join(project, 'models', 'user.js')).default);
return false;
});

tests.last.CleanUp(() => {
process.chdir('..');
});

export { tests as config, cmdname as testname };
17 changes: 10 additions & 7 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import configuration from './configuration';

const commands = importindex(ProyPath('/src/commands')).default;
const CommandOrder = [].concat([
'new', 'adapter', 'ember', 'model',
// 'new', 'adapter', 'ember',
'model',
'relation'
// 'nginx',
'install', 'build', 'seed', 'modulify'
// 'install', 'build', 'seed', 'modulify'
// 'serve'
]);

Expand Down Expand Up @@ -43,15 +45,16 @@ const testcase = function testcase (TestConfig, cwd, testname, command) {
const WriteO = process.stdout.write;
try {
let buffer = '';
process.stderr.write = () => {
};
process.stdout.write = () => {
};
// process.stderr.write = () => {
// };
// process.stdout.write = () => {
// };
console.log = (data) => {
// ori(data);
ori(data);
buffer += (data || '').toString();
};
if (testdata.asyncs) {
ori(testdata.args);
let res = command.action.apply(null, testdata.args);
testdata.expect.splice(0, 1);
return res.then((childIPIDs) => {
Expand Down
4 changes: 2 additions & 2 deletions test/support/TestNode.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as inflecor from 'inflection';
import * as inflector from 'inflection';
import * as fs from 'fs-extra';

class TestNode {
Expand All @@ -10,7 +10,7 @@ class TestNode {
terms = terms.join(' ');
let params = typeof args.last === 'object' ? args.last : {};
name = ` ${terms}` + Object.keys(params).map((key) => {
let newkey = inflecor.dasherize(inflecor.underscore(key));
let newkey = inflector.dasherize(inflector.underscore(key));
if (typeof params[key] === 'boolean') {
return '--' + newkey;
} else {
Expand Down

0 comments on commit b643732

Please sign in to comment.