Created
June 17, 2014 05:39
-
-
Save rchild-okta/728e4b53fa0666578a61 to your computer and use it in GitHub Desktop.
Walks through mvc directory and converts to commonJs style
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
// This script will take an input mvc directory, walk it, and write out | |
// to an out directory (for testing). Should be easy to write back to the | |
// original file. | |
var DIR_MVC = '/Users/rchild/okta/okta-core/WebContent/js/mvc' | |
, DIR_OUT = path.normalize(__dirname + '/out') | |
, formatStyle = yoda; | |
var fs = require('fs') | |
, R = require('ramda') | |
, file = require('file') | |
, path = require('path') | |
, NEW_HEADER = 'define([\'require\'], function (require) {'; | |
var hasDependenciesRe = /^\s*define\s*\(\s*\[/ | |
, headerRe = /define\(\[([^\]]*)\]\s*,\s*function\s*\(([^)]*)\)\s*{/; | |
// --------------------------------------------------------------------------- | |
// Formatting styles | |
// var foo = require('bar'); | |
// var baz = require('boo'); | |
// var bobo = require('rah'); | |
function standard(lines) { | |
return R.map(function (line) { | |
if (line.indexOf('=') === -1) { | |
return ' ' + line + ';'; | |
} | |
return ' var ' + line + ';'; | |
}, lines) | |
} | |
// Note, if choosing yoda or somethingElse style, will have to account for | |
// 1) 1 line case | |
// 2) no variable (i.e. jquery-color) | |
// var foo = require('bar') | |
// , baz = require('boo') | |
// , bobo = require('rah'); | |
function yoda(lines) { | |
var last = lines.length - 1; | |
return R.map.idx(function (line, i) { | |
if (i === 0) { | |
return ' var ' + line; | |
} else if (i === last) { | |
return ' , ' + line + ';'; | |
} else { | |
return ' , ' + line; | |
} | |
}, lines); | |
} | |
// var foo = require('bar'), | |
// baz = require('boo') | |
// bobo = require('rah'); | |
function somethingElse(lines) { | |
var last = lines.length - 1; | |
return R.map.idx(function (line, i) { | |
if (i === 0) { | |
return ' var ' + line + ','; | |
} else if (i === last) { | |
return ' ' + line + ';'; | |
} else { | |
return ' ' + line + ','; | |
} | |
}, lines); | |
} | |
// --------------------------------------------------------------------------- | |
// String -> [] | |
function split(row) { | |
return row.replace(/\s+/g, '').split(','); | |
} | |
// [String, String] -> String | |
function line(pieces) { | |
var req = pieces[0] | |
, variable = pieces[1] | |
, statement = 'require(' + req + ')'; | |
if (variable) { | |
statement = variable + ' = ' + statement; | |
} | |
return statement; | |
} | |
// [] -> [] | |
var addHeader = R.merge([NEW_HEADER, '']); | |
// [Req], [Param] -> [Line] | |
var getHeaderLines = R.compose(addHeader, formatStyle, R.map(line), R.zip); | |
// FileString -> FileString | |
function replaceHeader(file) { | |
var matches = file.match(headerRe) | |
, reqs = split(matches[1]) | |
, params = split(matches[2]); | |
return file.replace(headerRe, getHeaderLines(reqs, params).join("\n")); | |
} | |
// File -> File | |
function replacePath(file) { | |
return file.replace(DIR_MVC, DIR_OUT); | |
} | |
// File -> FileContents | |
function readFile(file) { | |
return fs.readFileSync(file, { encoding: 'utf8' }); | |
} | |
// [File, FileContents] -> undefined | |
function writeFile(fileArgs) { | |
var name = fileArgs[0] | |
, contents = fileArgs[1]; | |
console.log(name); | |
fs.writeFileSync(name, contents); | |
} | |
// File -> Boolean | |
function isCandidate(fileName) { | |
// 1. Is a javascript file | |
if (fileName.indexOf('.js') === -1) { | |
return false; | |
} | |
// 2. Not a main file (module is special dependency) | |
if (fileName.indexOf('main-') !== -1) { | |
return false; | |
} | |
// 3. Has dependencies to replace | |
var file = readFile(fileName); | |
if (!file.match(hasDependenciesRe)) { | |
return false; | |
} | |
return true; | |
} | |
// [File] -> [FileContent] | |
var replaceHeaderInFiles = R.map(R.compose(replaceHeader, readFile)); | |
file.walk(DIR_MVC, function (err, dirPath, dirs, files) { | |
var outDir = replacePath(dirPath) | |
, candidates = R.filter(isCandidate, files) | |
, withNewHeader = replaceHeaderInFiles(candidates) | |
, newFileNames = R.map(replacePath, candidates) | |
, zipped = R.zip(newFileNames, withNewHeader); | |
file.mkdirsSync(outDir); | |
R.each(writeFile, zipped); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Noticed I have some spacing issues on yoda (specifically, first line). Fix it.