Created
August 31, 2016 18:29
-
-
Save beatak/d2625bfde79b574fb62b08c8b3314364 to your computer and use it in GitHub Desktop.
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
var fs = require('fs'); | |
var path = require('path'); | |
var shell = require('shelljs'); | |
var options = require('optimist'); | |
var ETSYWEB_DIR = path.join(process.env.HOME, 'development', 'Etsyweb'); | |
var DEPSY_PATH = 'bin/fei/depsy/bin/depsy.js'; | |
var JS_BASE_PATH = 'htdocs/assets/js/'; | |
var global_start, global_end; | |
var global_missing; | |
var global_missing_keys; | |
var global_missing_pointer = 0; | |
var datesalt = (new Date()).toISOString().replace(/\:/g, '_').split('.')[0]; | |
var global_result_dir = path.join(process.env.HOME, 'dots', datesalt); | |
var global_svg_dir = path.join('/tmp', 'translated-missing', datesalt); | |
var main = function () { | |
global_start = Date.now(); | |
shell.mkdir('-p', global_result_dir); | |
shell.mkdir('-p', global_svg_dir); | |
global_missing = JSON.parse( | |
fs.readFileSync(options.argv._[0], 'utf8').trim() | |
).compared; | |
global_missing_keys = Object.keys(global_missing); | |
setImmediate(iter); | |
}; | |
var iter = function () { | |
if (global_missing_pointer >= global_missing_keys.length) { | |
end(); | |
} | |
var module = global_missing_keys[ global_missing_pointer++ ]; | |
var missing_modules = global_missing[module].missing; | |
if (missing_modules.length === 0) { | |
setImmediate(iter); | |
return; | |
} | |
var module_basename = module.replace(/[\/\.]/g, '-'); | |
var dotfile_path = path.join(global_result_dir, module_basename + '.dot'); | |
var command = [ | |
'cd ', ETSYWEB_DIR, | |
' && ', DEPSY_PATH, ' --dot . ', JS_BASE_PATH, module, | |
' > ', dotfile_path | |
]; | |
// console.log(command.join('')); | |
shell.exec( | |
command.join(''), | |
{ | |
silent: true | |
}, | |
function (code, stdout, stderr) { | |
if (code !== 0) { | |
console.error(stderr); | |
setImmediate(iter); | |
return; | |
} | |
var dot_contents = fs.readFileSync(dotfile_path, 'utf8'); | |
missing_modules.forEach(function (missingmod, i) { | |
var moduleid = missingmod.split('/').slice(1).join('/'); | |
// var greg = new RegExp(foo.replace(/\\/g, '\\\\/'), 'g'); | |
// var gmatch = greg.exec(dot_contents); | |
// console.error( gmatch === null ? foo + ' is not found' : foo + ' is found ' + gmatch.length + ' times'); | |
var reg = new RegExp('\\[(label\\=\\"templates\\/' + missingmod.replace(/\\/g, '\\\\/') + '\\.mustache\\")\\]'); | |
if (reg.test(dot_contents)) { | |
dot_contents = dot_contents.replace(reg, '[$1 style="dotted"]'); | |
} | |
}); | |
fs.writeFileSync(dotfile_path, dot_contents); | |
var com2 = [ | |
'cd ', global_svg_dir, | |
' && dot -Tsvg -o', module_basename, '.svg', | |
' < ', dotfile_path | |
]; | |
shell.exec( | |
com2.join(''), | |
function (code, sout, serr) { | |
if (code !== 0) { | |
console.error('Failed! ' + module); | |
console.error(serr); | |
} | |
setImmediate(iter); | |
} | |
); | |
} | |
); | |
}; | |
var end = function () { | |
global_end = Date.now(); | |
console.log('DONE! It takes ' + (global_end - global_start) + ' msec'); | |
process.exit(0); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment