Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { casex } from 'battlecry'; | ||
|
||
const ALIASES = { | ||
Number: ["int", "integer"], | ||
String: ["text"], | ||
Boolean: ["bool"] | ||
}; | ||
|
||
export default class Attribute { | ||
constructor(value) { | ||
const [name, type, ...modifiers] = value.split(":"); | ||
this.name = name; | ||
this.type = type || "string"; | ||
this.modifiers = modifiers; | ||
} | ||
|
||
static alias(type) { | ||
const lowerType = type.toLowerCase(); | ||
const keys = Object.keys(ALIASES); | ||
|
||
const alias = keys.find(key => { | ||
return ALIASES[key].find( | ||
alias => alias.toLowerCase() === lowerType | ||
); | ||
}); | ||
|
||
return alias || casex(type, 'NaMe'); | ||
} | ||
|
||
get aliasedType() { | ||
return this.array ? "Array" : this.itemType; | ||
} | ||
|
||
get itemType() { | ||
return Attribute.alias(this.type.split("[]")[0]); | ||
} | ||
|
||
get required() { | ||
return this.modifiers.includes("required"); | ||
} | ||
|
||
get default() { | ||
return this.modifiers.includes("default"); | ||
} | ||
|
||
get array() { | ||
return this.type.includes("[]"); | ||
} | ||
|
||
get complex() { | ||
return this.array || this.required || this.default; | ||
} | ||
|
||
get text() { | ||
if (!this.complex) return ` __naMe__: ${this.aliasedType},`; | ||
|
||
this.contents = []; | ||
this.push(" __naMe__: {"); | ||
this.push(` type: ${this.aliasedType},`); | ||
this.push(` itemType: ${this.itemType},`, this.array); | ||
this.push(" required: true,", this.required); | ||
this.push(" default: [],", this.default); | ||
this.push(" },"); | ||
|
||
return this.contents; | ||
} | ||
|
||
push(value, condition = true) { | ||
if (condition) this.contents.push(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Generator, File } from 'battlecry'; | ||
import Attribute from './Attribute'; | ||
|
||
const DIST = 'src/domain/__na-me__' | ||
|
||
export default class SchemaGenerator extends Generator { | ||
config = { | ||
generate: { | ||
args: 'name ...props?', | ||
description: 'Create or modify schema adding attributes and methods' | ||
} | ||
}; | ||
|
||
get props() { | ||
const props = this.args.props.length ? this.args.props : ['name']; | ||
return props.reverse(); | ||
} | ||
|
||
generate() { | ||
const template = this.template(); | ||
let file = new File(`${DIST}/${template.filename}`, this.args.name); | ||
if(!file.exists) file = template; | ||
|
||
this.props.forEach(prop => { | ||
if(prop.startsWith(':')) this.addMethod(file, prop) | ||
else this.addAttribute(file, prop); | ||
}); | ||
|
||
file.saveAs(`${DIST}/${template.filename}`, this.args.name); | ||
} | ||
|
||
addAttribute(file, name) { | ||
const attribute = new Attribute(name); | ||
file.after('attributes({', attribute.text, attribute.name); | ||
} | ||
|
||
addMethod(file, name) { | ||
file.after('class ', [ | ||
' __naMe__() {', | ||
' // Do something here', | ||
' }', | ||
'' | ||
], name.substring(1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { attributes } = require('structure'); | ||
|
||
const __NaMe__ = attributes({ | ||
})(class __NaMe__ { | ||
}); | ||
|
||
module.exports = __NaMe__; |