Skip to content

Commit c8b7ea3

Browse files
committed
feat(generators): Initiate model,migration and contoller generator
1 parent 96900e0 commit c8b7ea3

25 files changed

+795
-748
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ coverage
22
node_modules
33
.DS_Store
44
npm-debug.log
5+
test/integration/app
6+
test/unit/app

package.json

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,43 @@
11
{
22
"name": "adonis-commands",
3-
"version": "2.0.5",
4-
"description": "Set of core commands, shipped with adonis",
3+
"version": "2.0.0",
4+
"description": "Project scaffolding for Adonis framework",
55
"main": "index.js",
6-
"scripts": {
7-
"test": "npm run standard && istanbul cover _mocha --report lcovonly -- -R spec test && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage",
8-
"coverage": "istanbul cover _mocha test --bail",
9-
"standard": "standard test/*.js providers/*.js src/**/*.js"
10-
},
11-
"author": "adonisjs",
12-
"license": "MIT",
13-
"devDependencies": {
14-
"adonis-fold": "^3.0.0",
15-
"chai": "^3.2.0",
16-
"co-mocha": "^1.1.2",
17-
"coveralls": "^2.11.6",
18-
"cz-conventional-changelog": "^1.1.5",
19-
"istanbul": "^0.4.1",
20-
"mocha": "^2.3.2",
21-
"mocha-lcov-reporter": "^1.0.0",
22-
"standard": "^5.4.1"
23-
},
24-
"peerDependencies": {
25-
"adonis-fold": "^3.0.0"
26-
},
27-
"dependencies": {
28-
"change-case": "^2.3.0",
29-
"co-fs-extra": "^1.1.0",
30-
"inflect": "^0.3.0",
31-
"pope": "^1.0.0"
32-
},
336
"directories": {
347
"test": "test"
358
},
9+
"scripts": {
10+
"test": "npm run standard && npm run test:unit && npm run test:integration",
11+
"test:unit": "_mocha test/unit",
12+
"test:integration": "_mocha test/integration",
13+
"standard": "standard src/**/*.js test/**/*js"
14+
},
3615
"repository": {
3716
"type": "git",
3817
"url": "git+https://github.com/adonisjs/adonis-commands.git"
3918
},
40-
"keywords": [
41-
"generators",
42-
"adonis"
43-
],
19+
"author": "adonisjs",
20+
"license": "MIT",
4421
"bugs": {
4522
"url": "https://github.com/adonisjs/adonis-commands/issues"
4623
},
4724
"homepage": "https://github.com/adonisjs/adonis-commands#readme",
48-
"config": {
49-
"commitizen": {
50-
"path": "./node_modules/cz-conventional-changelog"
51-
}
25+
"peerDependencies": {
26+
"adonis-fold": "^3.0.0"
27+
},
28+
"devDependencies": {
29+
"adonis-ace": "git+https://github.com/adonisjs/ace.git#develop",
30+
"adonis-fold": "^3.0.0",
31+
"chai": "^3.5.0",
32+
"co-mocha": "^1.1.2",
33+
"commander": "^2.9.0",
34+
"istanbul": "^0.4.2",
35+
"mocha": "^2.4.5",
36+
"standard": "^6.0.8"
37+
},
38+
"dependencies": {
39+
"co-fs-extra": "^1.2.1",
40+
"hogan.js": "^3.0.2",
41+
"inflect": "^0.3.0"
5242
}
5343
}

providers/GeneratorProvider.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

providers/GeneratorsProvider.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
/**
4+
* adonis-commands
5+
*
6+
* (c) Harminder Virk <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
const ServiceProvider = require('adonis-fold').ServiceProvider
13+
14+
class GeneratorsProvider extends ServiceProvider {
15+
16+
constructor () {
17+
super()
18+
this.generators = ['Controller', 'Migration', 'Model']
19+
}
20+
21+
* register() {
22+
this.generators.forEach((generator) => {
23+
this.app.bind(`Adonis/Commands/Make:${generator}`, (app) => {
24+
const Helpers = app.use('Adonis/Src/Helpers')
25+
const Generator = require(`../src/Generators/${generator}`)
26+
return new Generator(Helpers)
27+
})
28+
})
29+
}
30+
31+
}
32+
33+
module.exports = GeneratorsProvider

src/Generators/Base.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
'use strict'
2+
3+
/**
4+
* adonis-commands
5+
*
6+
* (c) Harminder Virk <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
const path = require('path')
13+
const hogan = require('hogan.js')
14+
const i = require('inflect')
15+
const Ioc = require('adonis-fold').Ioc
16+
const Command = Ioc.use('Adonis/Src/Command')
17+
const fs = require('co-fs-extra')
18+
19+
class Base extends Command {
20+
21+
constructor (Helpers) {
22+
super()
23+
this.helpers = Helpers
24+
}
25+
26+
/**
27+
* makes path to a given template
28+
* @param {String} template
29+
* @return {String}
30+
*
31+
* @private
32+
*/
33+
_makeTemplatePath (template) {
34+
return path.join(__dirname, '../../templates', `${template}.mustache`)
35+
}
36+
37+
/**
38+
* makes entity name to be used as the file name
39+
* @param {String} name
40+
* @param {String} entity
41+
* @param {Boolean} needPrefix
42+
* @param {String} [noun]
43+
* @return {String}
44+
*
45+
* @private
46+
*/
47+
_makeEntityName (name, entity, needPrefix, noun) {
48+
const method = `${noun}ize`
49+
const regExp = new RegExp(`-?_?${entity}`, 'g')
50+
name = i.underscore(name).replace(regExp, '')
51+
name = i[method] ? i[method](name) : name
52+
return needPrefix ? i.camelize(`${name}_${entity}`) : i.camelize(name)
53+
}
54+
55+
/**
56+
* returns contents for a given template
57+
* @param {String} template
58+
* @return {String}
59+
*
60+
* @private
61+
*/
62+
* _getContents (template) {
63+
return yield fs.readFile(template, 'utf-8')
64+
}
65+
66+
/**
67+
* tells whether a template exists or not
68+
* @param {String} dest
69+
* @return {Boolean}
70+
*
71+
* @private
72+
*/
73+
* _hasFile (dest) {
74+
return yield fs.exists(dest)
75+
}
76+
77+
/**
78+
* writes contents to a given destination
79+
* @param {String} dest
80+
* @param {String} contents
81+
* @return {String}
82+
*
83+
* @private
84+
*/
85+
* _writeContents (dest, contents) {
86+
return yield fs.outputFile(dest, contents)
87+
}
88+
89+
/**
90+
* writes template contents to a given destination.
91+
* @param {String} template
92+
* @param {String} dest
93+
* @param {Object} options
94+
* @return {String}
95+
*
96+
* @public
97+
*/
98+
* write (template, dest, options) {
99+
const contents = yield this._getContents(this._makeTemplatePath(template))
100+
const temp = hogan.compile(contents)
101+
const hasFile = yield this._hasFile(dest)
102+
if (hasFile) {
103+
throw new Error(`I am afraid ${path.basename(dest)} already exists`)
104+
}
105+
return yield this._writeContents(dest, temp.render(options))
106+
}
107+
108+
/**
109+
* logs the completed message on the console by
110+
* making incrementalPath path
111+
* @param {String} basePath [description]
112+
* @param {String} toPath [description]
113+
*
114+
* @private
115+
*/
116+
_logCreate (basePath, toPath) {
117+
const regeExp = new RegExp(`${basePath}${path.sep}?`)
118+
const incrementalPath = toPath.replace(regeExp, '')
119+
this.completed('create', incrementalPath)
120+
}
121+
122+
}
123+
124+
module.exports = Base

src/Generators/Command.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)