Skip to content

Commit

Permalink
feat(gen): generate js with jscodeshift instead of babel
Browse files Browse the repository at this point in the history
this is a proper codemod tool, instead of a general transpiler like babel
  • Loading branch information
Awk34 committed Dec 23, 2016
1 parent c016979 commit bf8f9fc
Show file tree
Hide file tree
Showing 18 changed files with 68 additions and 71 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"gulp-filter": "^4.0.0",
"gulp-tap": "^0.1.3",
"insight": "~0.8.3",
"jscodeshift": "^0.3.30",
"lodash": "^4.17.0",
"semver": "^5.1.0",
"underscore.string": "^3.1.1",
Expand Down
74 changes: 35 additions & 39 deletions src/generators/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import tap from 'gulp-tap';
import filter from 'gulp-filter';
import eslint from 'gulp-eslint';
import semver from 'semver';
import jscodeshift from 'jscodeshift';

export class Generator extends Base {
constructor(...args) {
Expand Down Expand Up @@ -461,52 +462,47 @@ export class Generator extends Base {
get writing() {
return {
generateProject: function() {
/**
* var tap = require('gulp-tap');
this.registerTransformStream([
extensionFilter,
tap(function(file, t) {
var contents = file.contents.toString();
contents = beautify_js(contents, config);
file.contents = new Buffer(contents);
}),
//prettifyJs(config),
extensionFilter.restore
]);
*/

const flow = this.filters.flow;

let babelPlugins = [
'babel-plugin-syntax-flow',
'babel-plugin-syntax-class-properties',
'babel-plugin-syntax-decorators',
'babel-plugin-syntax-export-extensions',
];

if(this.filters.babel && !flow) {
babelPlugins.push('babel-plugin-transform-flow-strip-types');
}

const genDir = path.join(__dirname, '../../');

// TODO: remove babel stuff from dependencies
const codeshiftStream = tap(function(file, t) {
var contents = file.contents.toString();

// remove `implements Foo` from class declarations
contents = jscodeshift(contents)
.find(jscodeshift.ClassDeclaration)
.forEach(path => {
path.value.implements = null;
})
.toSource();

// remove any type annotations
contents = jscodeshift(contents)
.find(jscodeshift.TypeAnnotation)
.remove()
.toSource();

// remove any `type Foo = { .. }` declarations
contents = jscodeshift(contents)
.find(jscodeshift.TypeAlias)
.remove()
.toSource();

// remove any flow directive comments
contents = jscodeshift(contents)
.find(jscodeshift.Comment, path => path.type === 'CommentLine' && path.value.includes('@flow'))
.forEach(path => path.prune())
.toSource();

file.contents = new Buffer(contents);
});

let clientJsFilter = filter(['client/**/*.js'], {restore: true});
this.registerTransformStream([
clientJsFilter,
babelStream({
plugins: babelPlugins.map(require.resolve),
/* Babel get's confused about these if you're using an `npm link`ed
generator-angular-fullstack, thus the `require.resolve` */
shouldPrintComment(commentContents) {
if(flow) {
return true;
} else {
// strip `// @flow` comments if not using flow
return !(/@flow/.test(commentContents));
}
},
babelrc: false // don't grab the generator's `.babelrc`
}),
codeshiftStream,
eslint({
fix: true,
configFile: path.join(genDir, 'templates/app/client/.eslintrc(babel)')
Expand Down
4 changes: 2 additions & 2 deletions templates/app/client/app/account(auth)/account.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { LoginComponent } from './login/login.component';
import { SignupComponent } from './signup/signup.component';
import { SettingsComponent } from './settings/settings.component';

export let AccountModule = @NgModule({
@NgModule({
imports: [
FormsModule,
<%_ if (filters.uirouter) { -%>
Expand All @@ -28,4 +28,4 @@ export let AccountModule = @NgModule({
SettingsComponent,
],
})
class AccountModule {}
export class AccountModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ interface User {
}
<%_ } -%>

export let LoginComponent = @Component({
@Component({
selector: 'login',
template: require('./login.<%=templateExt%>'),
})
class LoginComponent {
export class LoginComponent {
user: User = {
name: '',
email: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ interface User {
}
<%_ } -%>

export let SettingsComponent = @Component({
@Component({
selector: 'settings',
template: require('./settings.<%=templateExt%>'),
})
class SettingsComponent {
export class SettingsComponent {
user: User = {
oldPassword: '',
newPassword: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ interface User {
password: string;
}<% } %>

export let SignupComponent = @Component({
@Component({
selector: 'signup',
template: require('./signup.<%=templateExt%>'),
directives: [...ANGULARCLASS_MATCH_CONTROL_DIRECTIVES]
})
class SignupComponent {
export class SignupComponent {
user: User = {
name: '',
email: '',
Expand Down
4 changes: 2 additions & 2 deletions templates/app/client/app/app.component.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Component } from '@angular/core';

export let AppComponent = @Component({
@Component({
selector: 'app',
template: `<navbar></navbar>
<ui-view></ui-view>
<footer></footer>`
})
class AppComponent {}
export class AppComponent {}
4 changes: 2 additions & 2 deletions templates/app/client/app/app.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ if(constants.env === 'development') {
providers.push({ provide: RequestOptions, useClass: HttpOptions });
}

export let AppModule = @NgModule({
@NgModule({
providers,
imports: [
BrowserModule,
Expand All @@ -99,4 +99,4 @@ export let AppModule = @NgModule({
],
bootstrap: [AppComponent]
})
class AppModule {}
export class AppModule {}
4 changes: 2 additions & 2 deletions templates/app/client/app/main/main.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Component, OnInit<% if(filters.socketio) { %>, OnDestroy<% } %> } from
import { Http } from '@angular/http';
import { SocketService } from '../../components/socket/socket.service';

export let MainComponent = @Component({
@Component({
selector: 'main',
template: require('./main.<%=templateExt%>'),
styles: [require('./main.<%=styleExt%>')],
})
class MainComponent implements OnInit<% if(filters.socketio) { %>, OnDestroy<% } %> {
export class MainComponent implements OnInit<% if(filters.socketio) { %>, OnDestroy<% } %> {
Http;
<%_ if(filters.socketio) { -%>
SocketService;<% } %>
Expand Down
4 changes: 2 additions & 2 deletions templates/app/client/app/main/main.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const STATES = [
{ name: 'main', url: '/', component: MainComponent },
];<% } %>

export let MainModule = @NgModule({
@NgModule({
imports: [
BrowserModule,
FormsModule,
Expand All @@ -45,4 +45,4 @@ export let MainModule = @NgModule({
MainComponent,
],
})
class MainModule {}
export class MainModule {}
4 changes: 2 additions & 2 deletions templates/app/client/components/auth(auth)/auth.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { NgModule } from '@angular/core';
import { AuthService } from './auth.service';
import { UserService } from './user.service';

export let AuthModule = @NgModule({
@NgModule({
providers: [
AuthService,
UserService
]
})
class AuthModule {}
export class AuthModule {}
4 changes: 2 additions & 2 deletions templates/app/client/components/auth(auth)/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class User {
$promise = undefined;
}

export let AuthService = @Injectable()
class AuthService {
@Injectable()
export class AuthService {
_currentUser: User = {};
@Output() currentUserChanged = new EventEmitter(true);
userRoles = userRoles || [];
Expand Down
4 changes: 2 additions & 2 deletions templates/app/client/components/auth(auth)/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type UserType = {
email: string;
}

export let UserService = @Injectable()
class UserService {
@Injectable()
export class UserService {
static parameters = [AuthHttp];
constructor(authHttp: AuthHttp) {
this.AuthHttp = authHttp;
Expand Down
4 changes: 2 additions & 2 deletions templates/app/client/components/directives.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FooterComponent } from './footer/footer.component';
<%_ if(filters.oauth) { -%>
import { OauthButtonsComponent } from './oauth-buttons/oauth-buttons.component';<% } %>

export let DirectivesModule = @NgModule({
@NgModule({
imports: [
CommonModule,
UIRouterModule.forChild(),
Expand All @@ -30,4 +30,4 @@ export let DirectivesModule = @NgModule({
OauthButtonsComponent,<% } %>
]
})
class DirectivesModule {}
export class DirectivesModule {}
4 changes: 2 additions & 2 deletions templates/app/client/components/footer/footer.component.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Component } from '@angular/core';

export let FooterComponent = @Component({
@Component({
selector: 'footer',
template: require('./footer.html'),
styles: [require('./footer.scss')]
})
class FooterComponent {}
export class FooterComponent {}
4 changes: 2 additions & 2 deletions templates/app/client/components/navbar/navbar.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { Component } from '@angular/core';
import { StateService } from 'ui-router-ng2';<% } %>
import { AuthService } from '../auth/auth.service';<% } %>

export let NavbarComponent = @Component({
@Component({
selector: 'navbar',
template: require('./navbar.html')
})
class NavbarComponent {
export class NavbarComponent {
isCollapsed = true;
isLoggedIn;
isAdmin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Component } from '@angular/core';

export let OauthButtonsComponent = @Component({
@Component({
selector: 'oauth-buttons',
template: require('./oauth-buttons.<%=templateExt%>'),
styles: [require('./oauth-buttons.<%=styleExt%>')],
})
class OauthButtonsComponent {
export class OauthButtonsComponent {
loginOauth(provider) {
window.location.href = `/auth/${provider}`;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { noop, find, remove } from 'lodash';
import io from 'socket.io-client';
import constants from '../../app/app.constants';

export let SocketService = @Injectable()
class SocketService {
@Injectable()
export class SocketService {
socket;

constructor() {
Expand Down

0 comments on commit bf8f9fc

Please sign in to comment.