Skip to content

Commit 3f05dd4

Browse files
authored
Merge pull request #2423 from angular-fullstack/jscodeshift
feat(gen): generate js with jscodeshift instead of babel
2 parents fc62084 + a55691a commit 3f05dd4

File tree

20 files changed

+79
-73
lines changed

20 files changed

+79
-73
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"gulp-filter": "^4.0.0",
5353
"gulp-tap": "^0.1.3",
5454
"insight": "~0.8.3",
55+
"jscodeshift": "^0.3.30",
5556
"lodash": "^4.17.0",
5657
"semver": "^5.1.0",
5758
"underscore.string": "^3.1.1",

src/generators/app/index.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import tap from 'gulp-tap';
1212
import filter from 'gulp-filter';
1313
import eslint from 'gulp-eslint';
1414
import semver from 'semver';
15+
import jscodeshift from 'jscodeshift';
1516

1617
export class Generator extends Base {
1718
constructor(...args) {
@@ -458,52 +459,51 @@ export class Generator extends Base {
458459
get writing() {
459460
return {
460461
generateProject: function() {
461-
/**
462-
* var tap = require('gulp-tap');
463-
this.registerTransformStream([
464-
extensionFilter,
465-
tap(function(file, t) {
466-
var contents = file.contents.toString();
467-
contents = beautify_js(contents, config);
468-
file.contents = new Buffer(contents);
469-
}),
470-
//prettifyJs(config),
471-
extensionFilter.restore
472-
]);
473-
*/
474-
475462
const flow = this.filters.flow;
476463

477-
let babelPlugins = [
478-
'babel-plugin-syntax-flow',
479-
'babel-plugin-syntax-class-properties',
480-
'babel-plugin-syntax-decorators',
481-
'babel-plugin-syntax-export-extensions',
482-
];
483-
484-
if(this.filters.babel && !flow) {
485-
babelPlugins.push('babel-plugin-transform-flow-strip-types');
486-
}
487-
488464
const genDir = path.join(__dirname, '../../');
489465

466+
// TODO: remove babel stuff from dependencies
467+
const codeshiftStream = tap(function(file, t) {
468+
var contents = file.contents.toString();
469+
470+
// remove `implements Foo` from class declarations
471+
contents = jscodeshift(contents)
472+
.find(jscodeshift.ClassDeclaration)
473+
.forEach(path => {
474+
path.value.implements = null;
475+
})
476+
.toSource();
477+
478+
// remove any type annotations
479+
contents = jscodeshift(contents)
480+
.find(jscodeshift.TypeAnnotation)
481+
.remove()
482+
.toSource();
483+
contents = jscodeshift(contents)
484+
.find(jscodeshift.GenericTypeAnnotation)
485+
.remove()
486+
.toSource();
487+
488+
// remove any `type Foo = { .. }` declarations
489+
contents = jscodeshift(contents)
490+
.find(jscodeshift.TypeAlias)
491+
.remove()
492+
.toSource();
493+
494+
// remove any flow directive comments
495+
contents = jscodeshift(contents)
496+
.find(jscodeshift.Comment, path => path.type === 'CommentLine' && path.value.includes('@flow'))
497+
.forEach(path => path.prune())
498+
.toSource();
499+
500+
file.contents = new Buffer(contents);
501+
});
502+
490503
let clientJsFilter = filter(['client/**/*.js'], {restore: true});
491504
this.registerTransformStream([
492505
clientJsFilter,
493-
babelStream({
494-
plugins: babelPlugins.map(require.resolve),
495-
/* Babel get's confused about these if you're using an `npm link`ed
496-
generator-angular-fullstack, thus the `require.resolve` */
497-
shouldPrintComment(commentContents) {
498-
if(flow) {
499-
return true;
500-
} else {
501-
// strip `// @flow` comments if not using flow
502-
return !(/@flow/.test(commentContents));
503-
}
504-
},
505-
babelrc: false // don't grab the generator's `.babelrc`
506-
}),
506+
codeshiftStream,
507507
eslint({
508508
fix: true,
509509
configFile: path.join(genDir, 'templates/app/client/.eslintrc(babel)')

templates/app/client/app/account(auth)/account.module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { LoginComponent } from './login/login.component';
1212
import { SignupComponent } from './signup/signup.component';
1313
import { SettingsComponent } from './settings/settings.component';
1414

15-
export let AccountModule = @NgModule({
15+
@NgModule({
1616
imports: [
1717
FormsModule,
1818
<%_ if (filters.uirouter) { -%>
@@ -28,4 +28,4 @@ export let AccountModule = @NgModule({
2828
SettingsComponent,
2929
],
3030
})
31-
class AccountModule {}
31+
export class AccountModule {}

templates/app/client/app/account(auth)/login/login.component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ interface User {
2020
}
2121
<%_ } -%>
2222

23-
export let LoginComponent = @Component({
23+
@Component({
2424
selector: 'login',
2525
template: require('./login.<%=templateExt%>'),
2626
})
27-
class LoginComponent {
27+
export class LoginComponent {
2828
user: User = {
2929
name: '',
3030
email: '',

templates/app/client/app/account(auth)/settings/settings.component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ interface User {
1717
}
1818
<%_ } -%>
1919

20-
export let SettingsComponent = @Component({
20+
@Component({
2121
selector: 'settings',
2222
template: require('./settings.<%=templateExt%>'),
2323
})
24-
class SettingsComponent {
24+
export class SettingsComponent {
2525
user: User = {
2626
oldPassword: '',
2727
newPassword: '',

templates/app/client/app/account(auth)/signup/signup.component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ interface User {
1919
password: string;
2020
}<% } %>
2121

22-
export let SignupComponent = @Component({
22+
@Component({
2323
selector: 'signup',
2424
template: require('./signup.<%=templateExt%>'),
2525
directives: [...ANGULARCLASS_MATCH_CONTROL_DIRECTIVES]
2626
})
27-
class SignupComponent {
27+
export class SignupComponent {
2828
user: User = {
2929
name: '',
3030
email: '',
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Component } from '@angular/core';
22

3-
export let AppComponent = @Component({
3+
@Component({
44
selector: 'app',
55
template: `<navbar></navbar>
66
<ui-view></ui-view>
77
<footer></footer>`
88
})
9-
class AppComponent {}
9+
export class AppComponent {}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export default from '../../server/config/environment/shared';
1+
<%_ if(filters.babel) { -%>
2+
export default from '../../server/config/environment/shared';<% } %>
3+
<%_ if(filters.ts) { -%>
4+
import shared from '../../server/config/environment/shared';
5+
6+
module.exports.default = shared;<% } %>

templates/app/client/app/app.module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ if(constants.env === 'development') {
8383
providers.push({ provide: RequestOptions, useClass: HttpOptions });
8484
}
8585

86-
export let AppModule = @NgModule({
86+
@NgModule({
8787
providers,
8888
imports: [
8989
BrowserModule,
@@ -99,4 +99,4 @@ export let AppModule = @NgModule({
9999
],
100100
bootstrap: [AppComponent]
101101
})
102-
class AppModule {}
102+
export class AppModule {}

templates/app/client/app/main/main.component.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { Component, OnInit<% if(filters.socketio) { %>, OnDestroy<% } %> } from
22
import { Http } from '@angular/http';
33
import { SocketService } from '../../components/socket/socket.service';
44

5-
export let MainComponent = @Component({
5+
@Component({
66
selector: 'main',
77
template: require('./main.<%=templateExt%>'),
88
styles: [require('./main.<%=styleExt%>')],
99
})
10-
class MainComponent implements OnInit<% if(filters.socketio) { %>, OnDestroy<% } %> {
10+
export class MainComponent implements OnInit<% if(filters.socketio) { %>, OnDestroy<% } %> {
1111
Http;
1212
<%_ if(filters.socketio) { -%>
1313
SocketService;<% } %>

0 commit comments

Comments
 (0)