Created
May 3, 2016 08:51
-
-
Save jshbrntt/f39a18ba9cf7dc007127658ed1d7c4f0 to your computer and use it in GitHub Desktop.
A script written to partially convert modular AngularJS code to module bundler compatible code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint loopfunc: true */ | |
(function() { | |
'use strict'; | |
var beautify = require('js-beautify').js_beautify; | |
var fs = require('fs'); | |
var glob = require('glob'); | |
var path = require('path'); | |
var mkdirp = require('mkdirp'); | |
var process = require('process'); | |
var regex = { | |
angular: /((?:\s+)?angular(?:\s+)?.module\(.+\)(?:\s+)?\.\w+\(.+\);?\n)/, | |
iife: /\(function\(\)(?:\s+)?\{(?:\s+)?(?:['|"]use strict['|"];?)?(?:\s+)?((?:.|\n)+)(?:\}\(\)\)|\}\)\(\));/, | |
directive: /(?:angular(?:\s+)?\.module(?:.+)(?:\s+)?\.(.+)\((?:['|"](.+)['|"],\s+(\w+)|(.+))\);?)/, | |
}; | |
function getDirectiveAttributes(input) { | |
try { | |
var definition = regex.directive.exec(input); | |
var output = { | |
type: definition[1] | |
}; | |
if (definition[2] !== undefined) { | |
output.name = definition[2]; | |
} | |
if (definition[3] !== undefined) { | |
output.function = definition[3]; | |
} | |
if (definition[4] !== undefined) { | |
output.function = definition[4]; | |
} | |
return output; | |
} catch (e) { | |
console.log(input); | |
exit(1); | |
} | |
} | |
function stripAngularDefinition(input) { | |
var output = regex.angular.exec(input); | |
return output ? output[1] : null; | |
} | |
function stripIIFE(input) { | |
var output = regex.iife.exec(input); | |
return output ? output[1] : null; | |
} | |
function formatJS(input) { | |
var output = beautify(input, { | |
indent_size: 2 | |
}); | |
return output; | |
} | |
function processModule(input, directives) { | |
var output = stripIIFE(input); | |
output = formatJS(output); | |
output = `var ngModule = ${output}\n`; | |
for (let directive of directives) { | |
if (directive.attributes.hasOwnProperty('name')) { | |
output += `ngModule.${directive.attributes.type}('${directive.attributes.name}', require('${directive.path.relative}'));\n`; | |
} else { | |
output += `ngModule.${directive.attributes.type}(require('${directive.path.relative}'));\n`; | |
} | |
} | |
output += 'module.exports = ngModule.name;'; | |
return output; | |
} | |
function processDirectives(directives) { | |
for (let directive of directives) { | |
let output = stripIIFE(directive.body); | |
try { | |
output = output.replace(regex.angular.exec(output)[1], ''); | |
} catch (e) { | |
console.log(directive.body); | |
console.log(output); | |
console.log(e); | |
exit(1); | |
} | |
output = formatJS(output); | |
output = `${output}\nmodule.exports = ${directive.attributes.function};\n`; | |
directive.output = output; | |
// console.log(directive.output); | |
} | |
} | |
var exportFiles = []; | |
function outputFile(newPath, content) { | |
var filePath = `./new/${newPath}`; | |
mkdirp(path.dirname(filePath), (err) => { | |
if (err) return cb(err); | |
fs.writeFile(filePath, content, (err) => { | |
if (err) return cb(err); | |
console.log(`Saved: ${filePath}`); | |
}); | |
}); | |
} | |
glob(`${process.argv[2]}/**/*.module.js`, {}, (err, moduleFiles) => { | |
for (let moduleFile of moduleFiles) { | |
let moduleDirectory = path.dirname(moduleFile); | |
glob(`${moduleDirectory}/**/!(*.module|*.bootstrap).js`, {}, (err, directivesFiles) => { | |
let directives = []; | |
for (let directiveFile of directivesFiles) { | |
fs.readFile(directiveFile, 'utf8', (err, directiveData) => { | |
let directive = { | |
body: directiveData, | |
path: { | |
absolute: directiveFile, | |
relative: './' + path.relative(moduleDirectory, directiveFile) | |
}, | |
attributes: getDirectiveAttributes(directiveData) | |
}; | |
directives.push(directive); | |
// Read all directive files. | |
if (directives.length === directivesFiles.length) { | |
fs.readFile(moduleFile, 'utf8', (err, moduleData) => { | |
processDirectives(directives); | |
processModule(moduleData, directives); | |
outputFile(moduleFile, processModule(moduleData, directives)); | |
for (let directive of directives) { | |
outputFile(directive.path.absolute, directive.output); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment