Skip to content

Commit

Permalink
fix: Change the model relation generation/read, assets links will be …
Browse files Browse the repository at this point in the history
…updated correctly.
  • Loading branch information
gerard2perez committed Dec 14, 2016
1 parent 4ce763e commit b5b27da
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 49 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
branches:
only:
- master
services:
- mongodb
language: node_js
Expand All @@ -20,7 +23,6 @@ cache:
- node_modules
before_install:
- npm config set spin false
- npm install -g glob
- npm install -g ember-cli@2.9.1
- npm install -g bower
- npm install -g forever
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
![Koaton](/koaton-cli.png)

[![Build Status](https://img.shields.io/travis/gerard2p/koaton-cli.svg?style=flat-square&branch=master)](https://travis-ci.org/gerard2p/koaton-cli)[![Dependency Status](https://david-dm.org/gerard2p/koaton-cli.svg?style=flat-square)](https://david-dm.org/gerard2p/koaton-cli)[![Code Climate](https://codeclimate.com/github/gerard2p/koaton-cli/badges/gpa.svg)](https://codeclimate.com/github/gerard2p/koaton-cli)
![PRs Welcome](https://img.shields.io/badge/PRs%20🔀-Welcome-brightgreen.svg?style=flat-square)
[![Build Status](https://img.shields.io/travis/gerard2p/koaton-cli/master.svg?style=flat-square)](https://travis-ci.org/gerard2p/koaton-cli)[![Dependency Status](https://david-dm.org/gerard2p/koaton-cli.svg?style=flat-square)](https://david-dm.org/gerard2p/koaton-cli)![PRs Welcome](https://img.shields.io/badge/PRs%20🔀-Welcome-brightgreen.svg?style=flat-square)

![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)
![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)
[![Code Climate](https://codeclimate.com/github/gerard2p/koaton-cli/badges/gpa.svg)](https://codeclimate.com/github/gerard2p/koaton-cli) [![Test Coverage](https://codeclimate.com/github/gerard2p/koaton-cli/badges/coverage.svg)](https://codeclimate.com/github/gerard2p/koaton-cli/coverage) [![Issue Count](https://codeclimate.com/github/gerard2p/koaton-cli/badges/issue_count.svg)](https://codeclimate.com/github/gerard2p/koaton-cli)


![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)

Koaton is a CLI tool that allow you to easily create and manage your [Koaton](https://github.com/gerard2p/koaton) projects.

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Koaton is a CLI tool that provides a nice starting point for full stack JavaScript Web development with Koa, Ember, and Node.js along with CaminateJS and WebSockets.",
"main": "./koaton",
"scripts": {
"codeclimate_cov":"codeclimate-test-reporter < coverage/lcov.info",
"export:live": "npm run export & chokidar src -c \"cls && npm run export\"",
"eslint:live": "npm run eslint & chokidar src -c \"cls && npm run eslint\"",
"eslint": "eslint src --ignore-path .gitignore",
Expand Down
5 changes: 4 additions & 1 deletion src/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*eslint no-div-regex: 0*/
import * as path from 'upath';
import * as crypto from "crypto";
import * as fs from "fs-extra";
Expand Down Expand Up @@ -249,7 +250,9 @@ const postBuildEmber = async function postBuildEmber(application, options) {
const links = new RegExp(`<link rel="stylesheet" href=".*?assets/.*.css.*>`, "gm");
const scripts = new RegExp(`<script src=".*?assets/.*.js.*></script>`, "gm");
const transformlinks=function transformlinks(text,expresion){
return text.match(expresion).join("\n").replace(/href=".*?assets/igm, `href="/${application}/assets`).replace(new RegExp(application + "/", "gm"), options.directory + "/")
return text.match(expresion).join("\n")
.replace(/=".*?assets/igm, `="/${options.directory}/assets`)
// .replace(new RegExp(application + "/", "gm"), options.directory + "/")
};
text = utils.compile(indextemplate, {
title: options.title || application,
Expand Down
13 changes: 4 additions & 9 deletions src/commands/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,15 @@ export default (new command(__filename, description))
}
let modelname = inflector.singularize(name.toLowerCase()),
cmd = `koaton model ${modelname} ${fields} ${destmodel||""} ${as||""} ${relation_property||""} ${foreign_key||""}`.trim(),
key = foreign_key || "",
targetmodel = inflector.singularize((destmodel || "").toLowerCase());

scfg.commands.add(cmd);
let linkaction = null,
relations = scfg.database.has(modelname) ? scfg.database.relations[modelname] : [];
let linkaction = null;
if (as === "as") {
linkaction = linkactions[fields.toLowerCase()]
let relation = {};
fields = scfg.database.models[modelname];
relation[`${relation_property}`] = `${emberrel[linkaction]} ${targetmodel} ${key}`;
relations.push(relation);
linkaction = linkactions[fields.toLowerCase()];
scfg.database[modelname].relation(relation_property,targetmodel,emberrel[linkaction],foreign_key);
}
let model = ModelManager(modelname, fields, relations, scfg.database.models),
let model = scfg.database[modelname],
override = await utils.challenge(ProyPath("models", `${modelname.toLowerCase()}.js`), `The model ${modelname.green} already exits,do you want to override it?`, options.force);

if (override) {
Expand Down
27 changes: 17 additions & 10 deletions src/commands/serve.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as spawn from 'cross-spawn';
import * as chokidar from 'chokidar';
import * as nodemon from 'nodemon';
import * as livereload from 'gulp-livereload';
Expand All @@ -7,7 +8,7 @@ import * as path from 'path';
import * as fs from 'fs-extra';
import * as co from 'co';
import screen from '../welcome';
import * as MM from '../modelmanager';
import * as ModelManager from '../modelmanager';
import command from '../command';
import utils from '../utils';
import spin from '../spinner';
Expand Down Expand Up @@ -228,12 +229,14 @@ const deleted = function(file) {
serveEmber = function(app, cfg, index) {
return Promise.promisify((...args) => {
let [app, mount, cb] = args;
console.log(args);
let appst = {
log: false,
result: ""
};
const ember = utils.spawn("ember", ["serve", "-lr", "false", "--output-path", path.join("..", "..", "public", cfg.directory), "--port", 4200 + index], {
cwd: ProyPath("ember", app)
const ember = spawn("ember", ["serve", "-lr", "false", "--output-path", path.join("..", "..", "public", cfg.directory), "--port", 4200 + index], {
cwd: ProyPath("ember", app),
shell:true
});
ember.stdout.on('data', (buffer) => {
if (appst.log) {
Expand Down Expand Up @@ -290,7 +293,7 @@ const deleted = function(file) {
}
console.log(buffer.toString());
});
})(app, cfg.mount, cfg.subdomain || "");
})(app, cfg.mount);
},
seedkoaton_modules = function() {
fs.access(ProyPath("koaton_modules"), fs.RF_OK | fs.W_OK, (err) => {
Expand Down Expand Up @@ -319,7 +322,7 @@ const deleted = function(file) {
WatchModels = function WatchModels(chokidar) {
const addmodelfn = function addmodelfn(file) {
let model = path.basename(file).replace(".js", "");
let Model = MM(model, requireNoCache(file)).toMeta();
let Model = ModelManager(model, requireNoCache(file)).toMeta();
scfg.database.models[model] = Model.model;
if (Model.relations.length > 0) {
scfg.database.relations[model] = Model.relations;
Expand Down Expand Up @@ -477,11 +480,15 @@ export default (new command(__filename, 'Runs your awsome Koaton applicaction us
title: embercfg[ember_app].title
};
console.log(`Building ${ember_app.green} second plane`);
await buildcmd.preBuildEmber(ember_app, configuration);
let b = serveEmber(ember_app, embercfg[ember_app])
building.push(b);
await b;
await buildcmd.postBuildEmber(ember_app, configuration);
try {
await buildcmd.preBuildEmber(ember_app, configuration);
let b = serveEmber(ember_app, embercfg[ember_app])
building.push(b);
await b;
await buildcmd.postBuildEmber(ember_app, configuration);
} catch (e) {
console.log(e);
}
} else {
building.push(Promise.resolve({
log: false,
Expand Down
2 changes: 1 addition & 1 deletion src/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ global.TemplatePath = function(...args) {
};
if (process.env.isproyect === 'true') {
global.scfg = new(require('./support/Server').default)();
require(ProyPath('node_modules','koaton/lib/support','globals'));
// require(ProyPath('node_modules','koaton/lib/support','globals'));

} else {
global.scfg = {version: "0.0.0"};
Expand Down
15 changes: 6 additions & 9 deletions src/modelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import datatypes from './support/DataTypes';
const modelize = function(koatonmodel) {
let res = [];
res[0] = [];
res[1] = [];
res[1] = koatonmodel.relations;
Object.keys(koatonmodel.model).forEach((property) => {
res[0].push(`${property}:${koatonmodel.model[property].type.koaton}`);
});
Expand All @@ -33,17 +33,14 @@ const schema = {
}
}
schema.belongsTo = schema.hasMany;
export default (n, f, r, m) => {
let name = n,
fields = f,
relations = {},
models = m || {};
if (typeof f === 'function') {
fields = modelize(f(datatypes, schema));
export default (...args) => {
let [name,fields,relations={},models={}] = args;
if (typeof fields === 'function') {
fields = modelize(fields(datatypes, schema));
relations = fields[1];
fields = fields[0];
} else {
relations = clone(r || {});
relations = clone(relations || {});
}
return new model(name, fields, relations, models);
};
18 changes: 9 additions & 9 deletions src/support/ORMModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import inflector from './inflector';
const compileCaminte = compile.bind(null, '"use strict";\nmodule.exports = function(schema,relation) {\n\treturn {\n\t\tmodel: {\n\t\t\t{{model}}\n\t\t},\n\t\textra: {},\n\t\trelations: {\n\t\t\t{{relations}}\n\t\t}\n\t};\n};');
const compileEmber = compile.bind(null, `import Model from 'ember-data/model';\nimport attr from 'ember-data/attr';\nimport { hasMany, belongsTo } from 'ember-data/relationships';\nexport default Model.extend({\n\t{{definition}}\n});`);
const compileCRUDTable = compile.bind(null, `import Ember from 'ember';\nimport crud from 'ember-cli-crudtable/mixins/crud-controller';\nexport default Ember.Controller.extend(crud('{{model}}'), {\n\tactions: {\n\t\t{{actions}}\n\t},\n\tfieldDefinition: {\n\t\t{{definition}}\n\t}\n});`);
let database = {};
export default class ORMModel {
valueOf() {
return this._modelname.toString();
}
equals(target) {
return this._modelname.toString() === target._modelname.toString();
}
relation(property,target,mode,foreign_key){
let key = foreign_key || `${target}Id`;
this._relations[property] = [mode,target,key];
}
constructor(name, ...args) {
let [fields, relations = {}, allmodels = {}] = args;
let [fields, relations = {}] = args;
if (fields === undefined) {
throw new Error("fields can't be undefined");
}
database = Object.assign(database, allmodels);
Object.keys(relations).forEach((property) => {
let opts = relations[property].split(" ");
opts[2] = opts[2] ? opts[2] : `${property}Id`;
Expand All @@ -27,8 +29,8 @@ export default class ORMModel {
Object.defineProperty(this, '_relations', {
enumerable: false,
configurable: false,
writable: false,
value: relations
writable: true,
value: relations || {}
});
Object.defineProperty(this, '_modelname', {
enumerable: false,
Expand Down Expand Up @@ -133,12 +135,10 @@ export default class ORMModel {
});
}
toMeta() {
let relations = [];
let relations = {};
Object.keys(this._relations).forEach((property) => {
let opts = this._relations[property];
let rel = {};
rel[property] = opts.join(' ');
relations.push(rel);
relations[property] = opts.join(' ');
});
let meta = {
model: this._fields,
Expand Down
11 changes: 6 additions & 5 deletions src/support/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import MutableArray from './MutableArray';
import BundleItem from './BundleItem';
import CommandLog from './CommandLog';
import Model from './ORMModel';
import MM from '../modelmanager';
import ModelManager from '../modelmanager';
// import EmberAppItem from './EmberAppItem';

const ipformat = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
Expand Down Expand Up @@ -69,19 +69,20 @@ export default class ServerConfiguaration {
this.database.toJSON = function() {
let result = {
models: {},
relations: []
relations: {}
};
for (const model of this.target) {
result.models[model._modelname] = model.toMeta().model
result.relations.concat(model.toMeta().relations);
result.models[model._modelname] = model.toMeta().model;
Object.assign(result.relations,model.toMeta().relations);
// result.relations=model.toMeta().relations;
}
return result;
};
for (const idx in localkoaton.bundles) {
this.bundles.add(idx, localkoaton.bundles[idx]);
}
sync(ProyPath("models", "*.js")).forEach((files) => {
this.database.add(MM(path.basename(files).replace(".js", ""), require(files)));
this.database.add(ModelManager(path.basename(files).replace(".js", ""), require(files)));
});
Object.defineProperty(this.database, 'models', {
get: function() {
Expand Down

0 comments on commit b5b27da

Please sign in to comment.