Skip to content

Commit bf8f9fc

Browse files
committed
feat(gen): generate js with jscodeshift instead of babel
this is a proper codemod tool, instead of a general transpiler like babel
1 parent c016979 commit bf8f9fc

File tree

18 files changed

+68
-71
lines changed

18 files changed

+68
-71
lines changed

Diff for: package.json

+1
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",

Diff for: src/generators/app/index.js

+35-39
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import tap from 'gulp-tap';
1515
import filter from 'gulp-filter';
1616
import eslint from 'gulp-eslint';
1717
import semver from 'semver';
18+
import jscodeshift from 'jscodeshift';
1819

1920
export class Generator extends Base {
2021
constructor(...args) {
@@ -461,52 +462,47 @@ export class Generator extends Base {
461462
get writing() {
462463
return {
463464
generateProject: function() {
464-
/**
465-
* var tap = require('gulp-tap');
466-
this.registerTransformStream([
467-
extensionFilter,
468-
tap(function(file, t) {
469-
var contents = file.contents.toString();
470-
contents = beautify_js(contents, config);
471-
file.contents = new Buffer(contents);
472-
}),
473-
//prettifyJs(config),
474-
extensionFilter.restore
475-
]);
476-
*/
477-
478465
const flow = this.filters.flow;
479466

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

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

Diff for: templates/app/client/app/account(auth)/account.module.js

+2-2
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 {}

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

+2-2
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: '',

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

+2-2
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: '',

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

+2-2
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: '',

Diff for: templates/app/client/app/app.component.js

+2-2
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 {}

Diff for: templates/app/client/app/app.module.js

+2-2
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 {}

Diff for: templates/app/client/app/main/main.component.js

+2-2
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;<% } %>

Diff for: templates/app/client/app/main/main.module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const STATES = [
2121
{ name: 'main', url: '/', component: MainComponent },
2222
];<% } %>
2323

24-
export let MainModule = @NgModule({
24+
@NgModule({
2525
imports: [
2626
BrowserModule,
2727
FormsModule,
@@ -45,4 +45,4 @@ export let MainModule = @NgModule({
4545
MainComponent,
4646
],
4747
})
48-
class MainModule {}
48+
export class MainModule {}

Diff for: templates/app/client/components/auth(auth)/auth.module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import { NgModule } from '@angular/core';
1010
import { AuthService } from './auth.service';
1111
import { UserService } from './user.service';
1212

13-
export let AuthModule = @NgModule({
13+
@NgModule({
1414
providers: [
1515
AuthService,
1616
UserService
1717
]
1818
})
19-
class AuthModule {}
19+
export class AuthModule {}

Diff for: templates/app/client/components/auth(auth)/auth.service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class User {
1717
$promise = undefined;
1818
}
1919

20-
export let AuthService = @Injectable()
21-
class AuthService {
20+
@Injectable()
21+
export class AuthService {
2222
_currentUser: User = {};
2323
@Output() currentUserChanged = new EventEmitter(true);
2424
userRoles = userRoles || [];

Diff for: templates/app/client/components/auth(auth)/user.service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type UserType = {
1414
email: string;
1515
}
1616

17-
export let UserService = @Injectable()
18-
class UserService {
17+
@Injectable()
18+
export class UserService {
1919
static parameters = [AuthHttp];
2020
constructor(authHttp: AuthHttp) {
2121
this.AuthHttp = authHttp;

Diff for: templates/app/client/components/directives.module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { FooterComponent } from './footer/footer.component';
1010
<%_ if(filters.oauth) { -%>
1111
import { OauthButtonsComponent } from './oauth-buttons/oauth-buttons.component';<% } %>
1212

13-
export let DirectivesModule = @NgModule({
13+
@NgModule({
1414
imports: [
1515
CommonModule,
1616
UIRouterModule.forChild(),
@@ -30,4 +30,4 @@ export let DirectivesModule = @NgModule({
3030
OauthButtonsComponent,<% } %>
3131
]
3232
})
33-
class DirectivesModule {}
33+
export class DirectivesModule {}
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Component } from '@angular/core';
22

3-
export let FooterComponent = @Component({
3+
@Component({
44
selector: 'footer',
55
template: require('./footer.html'),
66
styles: [require('./footer.scss')]
77
})
8-
class FooterComponent {}
8+
export class FooterComponent {}

Diff for: templates/app/client/components/navbar/navbar.component.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { Component } from '@angular/core';
44
import { StateService } from 'ui-router-ng2';<% } %>
55
import { AuthService } from '../auth/auth.service';<% } %>
66

7-
export let NavbarComponent = @Component({
7+
@Component({
88
selector: 'navbar',
99
template: require('./navbar.html')
1010
})
11-
class NavbarComponent {
11+
export class NavbarComponent {
1212
isCollapsed = true;
1313
isLoggedIn;
1414
isAdmin;

Diff for: templates/app/client/components/oauth-buttons(oauth)/oauth-buttons.component.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Component } from '@angular/core';
22

3-
export let OauthButtonsComponent = @Component({
3+
@Component({
44
selector: 'oauth-buttons',
55
template: require('./oauth-buttons.<%=templateExt%>'),
66
styles: [require('./oauth-buttons.<%=styleExt%>')],
77
})
8-
class OauthButtonsComponent {
8+
export class OauthButtonsComponent {
99
loginOauth(provider) {
1010
window.location.href = `/auth/${provider}`;
1111
};

Diff for: templates/app/client/components/socket(socketio)/socket.service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { noop, find, remove } from 'lodash';
44
import io from 'socket.io-client';
55
import constants from '../../app/app.constants';
66

7-
export let SocketService = @Injectable()
8-
class SocketService {
7+
@Injectable()
8+
export class SocketService {
99
socket;
1010

1111
constructor() {

0 commit comments

Comments
 (0)