Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bguiz/3f67356294241beee201 to your computer and use it in GitHub Desktop.
Save bguiz/3f67356294241beee201 to your computer and use it in GitHub Desktop.
output a composition root from conventional bower based angularjs projects to a bundled browserified project using gulp
/* jshint node: true */
'use strict';
var gulp = require('gulp'),
bower = require('./bower'),
replace = require('gulp-replace'),
through2 = require('through2');
/**
* All AngularJS application files as a stream
*/
function appFiles () {
var files = [
'./.tmp/' + bower.name + '-templates.js',
'./.tmp/app/**/*.js',
'!./.tmp/app/**/*.spec.js',
'./src/**/*.js',
'!./src/**/*.spec.js'
];
return gulp.src(files)
//.pipe(g.angularFilesort())
;
}
function moduleFiles(){
var folder = process.env.DIR;
folder = !folder ? '' : folder + '/';
folder = './src/'+folder+'**/*.js';
console.log('folder', folder);
return gulp.src([
folder,
'!**/*.spec.js'
]);
}
var regex = ( /.*angular\s*\.\s*module\s*\('([^']*)'\)\s*\.\s*(directive|factory|service|controller)\('([^']*)',.*/ );
gulp.task('temp', function() {
//var modules = [];
var moduleComponents = {};
return moduleFiles()
.pipe(through2.obj(
function transformFn(file, encoding, done) {
var contents = file.contents.toString();
var matches = regex.exec(contents);
if (!!matches && matches.constructor === Array) {
matches = matches.slice(1);
//console.log(matches, file.relative);
var moduleName = matches[0];
var components = moduleComponents[moduleName];
if (!components) {
components = [];
moduleComponents[moduleName] = components;
}
var item = {
componentType: matches[1],
name: matches[2],
path: file.relative
};
components.push(item);
}
else {
console.log('// match failure: '+file.relative);
}
done();
},
function flushFn(done) {
//console.log(moduleComponents);
var invalid = []
for (var moduleName in moduleComponents) {
if (moduleComponents.hasOwnProperty((moduleName))) {
var modules = moduleComponents[moduleName];
console.log("angular.module('"+moduleName+"')");
modules.forEach(function(module) {
var statement;
if (module.name.indexOf('[') >= 0) {
invalid.push(module.path);
}
else {
statement = module.path.replace( /\\/g , '/');
statement = 'require(\'./'+statement+'\')';
statement = ' .' + module.componentType + '(\'' + module.name + '\', ' + statement + ')';
console.log(statement);
}
});
console.log(';');
}
}
invalid.forEach(function(path) {
console.log("// invalid file: "+path);
});
done();
}
));
});
gulp.task('convert', function() {
//var modules = [];
var moduleComponents = {};
return moduleFiles()
.pipe(replace(/angular\.module\s*\(\'(.*)'\)\s*\.(directive|factory|service|controller)\(\'(.*)\'\,\s*function\s*\(([\$\w\,\s]*)\)\s*\{([\s\/\w\=\'\:\.\-\;\{\}\?\,\(\)\\\*\[\]\%\<\>\+\&\$\!\@\|\^\%\"\#]*)\s*\}\)\;/ig,
'/**\n * @ngInject\n */\nfunction $3 ($4) { $5 } \n\nmodule.exports = $3;'))
.pipe(gulp.dest('convertedFiles'))
;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment