-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (55 loc) · 1.91 KB
/
index.js
File metadata and controls
70 lines (55 loc) · 1.91 KB
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
import filter from 'gulp-filter';
import fs from 'fs';
import jscodeshift, { ImportDeclaration } from 'jscodeshift';
import path from 'path';
import tap from 'gulp-tap';
import {BaseGenerator} from '../base';
import {addModule} from './module-transform';
function readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, 'utf8', (err, data) => {
if(err) return reject(err);
resolve(data);
});
});
}
function writeFile(path, source) {
return new Promise((resolve, reject) => {
fs.writeFile(path, source, err => {
if(err) return reject(err);
resolve();
});
});
}
class Generator extends BaseGenerator {
prompting() {
var prompts = [{
name: 'dir',
message: 'Where would you like to create this route?',
default: this.config.get('routeDirectory'),
}, {
name: 'route',
message: 'What will the url of your route be?',
default: `${this.name}`,
}];
return this.prompt(prompts).then(props => {
this.route = props.route;
this.dir = path.join(props.dir, this.name);
});
}
async writing() {
this.sourceRoot(path.join(__dirname, '../../templates/route'));
this.processDirectory('.', this.dir);
const appModulePath = this.config.get('appModulePath');
const appModuleFolder = appModulePath.substring(0, appModulePath.lastIndexOf('/'));
const newModuleFilePath = path.normalize(`${this.dir}/${this.name}.module`);
const relativeModulePath = `./${path.normalize(path.relative(appModuleFolder, newModuleFilePath))}`
.replace(/\\/g, '/');
let source = await readFile(appModulePath);
source = addModule(source, `${this.classedName}Module`, relativeModulePath);
// FIXME: Bug in jscodeshift/recast removing `@` from modified `NgModule` decorator
source = source.replace(/\nNgModule/, '\n@NgModule');
await writeFile(appModulePath, source);
}
}
module.exports = Generator;