Skip to content

Commit

Permalink
feat: use watchman for file watcher with fs.watch fallback
Browse files Browse the repository at this point in the history
refactor: add more type annotations

fix: port not being set by --port when running lux serve
  • Loading branch information
zacharygolba committed Jun 12, 2016
1 parent 97bed29 commit 425b8de
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 83 deletions.
21 changes: 21 additions & 0 deletions decl/watchman.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declare module 'fb-watchman' {
declare class Client {
on(event: string, listener: Function): Client;

end(): void;

command(
args: Array<Object|string>,
callback: (err: ?Error, resp: {
clock: Object,
watch: Object,
relative_path: ?string
}) => void
): void;

capabilityCheck(
options: {},
callback: (err: ?Error, resp: {}) => void
): void;
}
}
9 changes: 6 additions & 3 deletions src/packages/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,17 @@ export default function CLI() {
hot = (environment === 'development')
} = {}) => {
return tryCatch(async () => {
port = parseInt(port, 10);

process.env.PORT = port;
process.env.NODE_ENV = environment;

if (hot) {
const watcher = new Watcher(PWD);
const watcher = await new Watcher(PWD);

watcher.on('change', async (type, file) => {
watcher.on('change', async (changed) => {
await compile(PWD, environment);
process.emit('update');
process.emit('update', changed);
});
}

Expand Down
41 changes: 37 additions & 4 deletions src/packages/controller/middleware/sanitize-params.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
// @flow
import { camelize } from 'inflection';

import pick from '../../../utils/pick';
import entries from '../../../utils/entries';

export default function sanitizeParams(req, res) {
const { modelName, model: { relationshipNames } } = this;
import type { IncomingMessage, ServerResponse } from 'http';

/**
* @private
*/
export default function sanitizeParams(
req: IncomingMessage,
res: ServerResponse
): void {
const {
modelName,
model: {
relationshipNames
}
}: {
modelName: string,
model: {
relationshipNames: Array<string>
}
} = this;

const params = { ...req.params };
let { page, limit, sort, filter, include, fields } = params;

let {
page,
limit,
sort,
filter,
include,
fields
} = params;

if (!page) {
page = 1;
Expand Down Expand Up @@ -92,7 +120,12 @@ export default function sanitizeParams(req, res) {
};

if (/^(POST|PATCH)$/g.test(req.method)) {
let { type, attributes } = params.data;
let {
data: {
type,
attributes = {}
}
} = params;

Object.assign(req.params, {
data: {
Expand Down
103 changes: 76 additions & 27 deletions src/packages/database/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,72 @@ import entries from '../../../utils/entries';
import underscore from '../../../utils/underscore';

class Model {
/**
* @private
*/
static table;

/**
* @private
*/
static store;
static logger;
static serializer;
static attributes;
static belongsTo;
static hasOne;
static hasMany;

static _tableName;
/**
*
*/
static logger;

static hooks = {};
static validates = {};
static primaryKey = 'id';
static defaultPerPage = 25;
/**
* @private
*/
static serializer;

constructor(props = {}, initialize = true) {
/**
* @private
*/
static attributes: {};

/**
*
*/
static belongsTo: {};

/**
*
*/
static hasOne: {};

/**
*
*/
static hasMany: {};

/**
* @private
*/
static _tableName: ?string;

/**
*
*/
static hooks: {} = {};

/**
*
*/
static validates: {} = {};

/**
*
*/
static primaryKey: string = 'id';

/**
*
*/
static defaultPerPage: number = 25;

constructor(attrs: {} = {}, initialize: boolean = true): Model {
const {
constructor: {
attributeNames,
Expand Down Expand Up @@ -64,34 +113,34 @@ class Model {

Object.assign(
this,
pick(props, ...attributeNames, ...relationshipNames)
pick(attrs, ...attributeNames, ...relationshipNames)
);

return this;
}

get isDirty() {
get isDirty(): boolean {
return Boolean(this.dirtyAttributes.size);
}

get modelName() {
get modelName(): string {
return this.constructor.modelName;
}

static get modelName() {
static get modelName(): string {
return dasherize(underscore(this.name));
}

static get tableName() {
static get tableName(): string {
return this._tableName ?
this._tableName : pluralize(underscore(this.name));
}

static set tableName(value) {
static set tableName(value): void {
this._tableName = value;
}

static get relationships() {
static get relationships(): {} {
const {
belongsTo,
hasOne,
Expand All @@ -105,15 +154,15 @@ class Model {
};
}

static get attributeNames() {
static get attributeNames(): Array<string> {
return Object.keys(this.attributes);
}

static get relationshipNames() {
static get relationshipNames(): Array<string> {
return Object.keys(this.relationships);
}

async update(props = {}) {
async update(props = {}): Model {
const {
constructor: {
primaryKey,
Expand Down Expand Up @@ -170,7 +219,7 @@ class Model {
return this;
}

async destroy() {
async destroy(): Model {
const {
constructor: {
primaryKey,
Expand Down Expand Up @@ -212,7 +261,7 @@ class Model {
return this;
}

format(dest, ...only) {
format(dest: string, ...only: Array<string>): {} {
const {
constructor: {
attributes
Expand Down Expand Up @@ -469,7 +518,7 @@ class Model {
return record ? record : null;
}

static getColumn(key) {
static getColumn(key): {} {
const {
attributes: {
[key]: column
Expand All @@ -479,15 +528,15 @@ class Model {
return column;
}

static getColumnName(key) {
static getColumnName(key): string {
const column = this.getColumn(key);

if (column) {
return column.columnName;
}
}

static getRelationship(key) {
static getRelationship(key): {} {
const {
relationships: {
[key]: relationship
Expand Down
65 changes: 53 additions & 12 deletions src/packages/database/validation/index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
// @flow
import typeof Model from '../model';

class Validation {
key;
value;
model;
validator;

constructor({ key, value, model, validator = () => true } = {}) {
return Object.assign(this, {
key,
value,
model,
validator
key: string;

value: mixed;

model: Model;

validator: () => boolean;

constructor({
key,
value,
model,
validator = () => true
}: {
key: string,
value: mixed,
model: Model,
validator: () => boolean
} = {}) {
Object.defineProperties(this, {
key: {
value: key,
writable: false,
enumerable: true,
configurable: false
},

value: {
value,
writable: false,
enumerable: true,
configurable: false
},

model: {
value: model,
writable: false,
enumerable: false,
configurable: false
},

validator: {
value: validator,
writable: false,
enumerable: false,
configurable: false
}
});

return this;
}

get isValid() {
get isValid(): boolean {
const {
model,
value,
Expand Down
9 changes: 8 additions & 1 deletion src/packages/pm/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ class Cluster extends EventEmitter {
exec: joinPath(path, 'dist/boot.js')
});

process.on('update', () => this.reload());
process.on('update', (changed) => {
changed.forEach(({ name: filename }) => {
logger.info(`${green('update')} ${filename}`);
});

this.reload();
});

this.forkAll().then(() => this.emit('ready'));

return this;
Expand Down
Loading

0 comments on commit 425b8de

Please sign in to comment.