-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
184 lines (154 loc) · 4.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import 'babel-polyfill';
import { Base } from 'yeoman-generator';
import yosay from 'yosay';
import camelcase from 'camelcase';
import chalk from 'chalk';
import read from 'fs-readdir-recursive';
class asfAddOnGenerator extends Base {
constructor(...args) {
super(...args);
/* Setting up our config object
*/
this.addon = {};
}
prompting() {
/* Welcome the user!!
*/
this.log(yosay(`Hello and welcome to the Angular Schema Form Add-on Generator!`));
/* We tell yeoman we don't want to continue until we run our done().
*/
const done = this.async();
this.prompt([
{
type: 'input',
name: 'name',
message: 'Your add-on name',
default: this.appname,
},
{
type: 'list',
name: 'type',
message: 'What kind of form type do you want?',
choices: ['input', 'empty'], /*, 'Radio', 'Checkbox', 'Array'*/
default: 'input',
},
{
type: 'confirm',
name: 'useDirective',
message: 'Do you want a directive added to your addon?',
default: true,
},
{
type: 'input',
name: 'username',
message: 'What\'s your Github username?',
store: true,
},
], (answers) => {
/* Nice, we now have all the answers.
*/
Object.assign(this.addon, answers);
/* Changing cases
*/
this.addon.module = camelcase(this.addon.name); // ex. add on > addOn
this.addon.formType = this.addon.name.replace(/ /g, ''); // ex. add on > addon
this.addon.paramName = this.addon.name.replace(/ /g, '-'); // ex. add on > add-on
/* We are done here... Let's continue
*/
done();
});
}
configure() {
/* Let's get a list of our base files and type specific files.
*/
this.addon.files = {};
/* I do not like this, it looks ugly too me. but hell, it works for now.
*/
this.addon.files.base = read(`${this.templatePath()}/base`);
this.addon.files[this.addon.type] = read(`${this.templatePath()}/${this.addon.type}/src`);
if (!this.addon.useDirective) {
this.addon.files[this.addon.type] = this.addon.files[this.addon.type].filter(dir => !dir.includes('directives'))
}
}
writing() {
const done = this.async();
const schema = this.fs.read(this.templatePath(`${this.addon.type}/schema.json`));
let form = this.fs.read(this.templatePath(`${this.addon.type}/form.json`));
/* TODO: Just a fast and easy fix for now..
*/
form = JSON.parse(form);
if (form[0].hasOwnProperty('type')) {
form[0].type = this.addon.formType;
}
const sources = [];
const testModule = ['schemaForm'];
if (this.addon.type !== 'empty') {
testModule.push(this.addon.module);
sources.push('src/module.js', 'src/**/*.js');
}
this.addon.files.base.forEach((file) => {
/* What to inject in the test controller
*/
const dest = file.replace('_', '.');
/* Base files */
this.fs.copyTpl(
this.templatePath(this.templatePath('base/') + file),
this.destinationPath('./') + dest,
{
name: this.addon.name,
module: this.addon.module,
testModuleInj: JSON.stringify(testModule),
formType: this.addon.formType,
paramName: this.addon.paramName,
schema: schema,
form: JSON.stringify(form),
username: this.addon.username,
sources: JSON.stringify(sources),
}
);
});
/* Type files */
this.addon.files[this.addon.type].forEach((file) => {
const dest = file.replace('_', '.')
.replace('template.html', `${this.addon.paramName}.html`)
.replace('template.js', `${this.addon.paramName}.js`);
this.fs.copyTpl(
this.templatePath(this.templatePath(`${this.addon.type}/src/`) + file),
this.destinationPath('./src/') + dest,
{
name: this.addon.name,
module: this.addon.module,
formType: this.addon.formType,
paramName: this.addon.paramName,
directive: this.addon.useDirective,
}
);
});
done();
}
install() {
const npmDevDeps = [
'gulp',
'gulp-angular-templatecache',
'gulp-concat',
'gulp-connect',
'gulp-livereload',
'gulp-rename',
'gulp-server-livereload',
'gulp-uglify',
'streamqueue',
];
const bowerDevDeps = [
'angular-schema-form',
'angular-schema-form-bootstrap',
'bootstrap',
];
this.log(chalk.white(`\nAlmost done! Just running ${chalk.green.bold('npm install')} and ${chalk.green.bold('bower install')} for you!\n`));
this.npmInstall(npmDevDeps, { saveDev: true });
this.bowerInstall(bowerDevDeps, { saveDev: true });
}
end() {
this.log(chalk.green(`\nEverything is done! \nJust run ${chalk.bold.green('gulp')} to start a livereload server to test your addon.`));
}
}
module.exports = asfAddOnGenerator;