Created
June 13, 2014 03:13
-
-
Save dqaria/1c140a6424c5939e1acf to your computer and use it in GitHub Desktop.
Genreate Charles Local Mapping Proxy File
This file contains 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
#! /usr/bin/env node | |
var fs = require("fs") | |
, program = require('commander') | |
, prompt = require('prompt') | |
, path = require("path") | |
, first_root = process.cwd() | |
, root = process.cwd() | |
, out_file = 'MapLocal.xml' | |
; | |
var templateHeader = | |
"<?xml version=\'1.0\' encoding=\'UTF-8\' ?>" + "\n" + | |
"<?charles serialisation-version=\'2.0\' ?>" +"\n" + | |
"<mapLocal>" +"\n" + | |
" <toolEnabled>true</toolEnabled>" +"\n" + | |
" <mappings>" +"\n"; | |
// host : cschannel.d3023aqcn.alipay.net | |
// remotePath: /htdocs/portal/main/status.js | |
// localPath: /Users/dqaria/Develop/ali01286856_CP_20140522_cschannel/htdocs/htdocs/portal/main/status.js | |
function generateXmlTemplate(host, remotePath, localPath) { | |
var template = | |
" <mapLocalMapping>" +"\n" + | |
" <sourceLocation>" +"\n" + | |
" <protocol>http</protocol>" +"\n" + | |
" <host>" + host + "</host>" + "\n" + | |
" <port>80</port>" +"\n" + | |
" <path>" + remotePath + "</path>" +"\n" + | |
" </sourceLocation>" +"\n" + | |
" <dest>" + localPath +"</dest>" +"\n" + | |
" <enabled>true</enabled>" +"\n" + | |
" <caseSensitive>true</caseSensitive>" +"\n" + | |
" </mapLocalMapping>" +"\n"; | |
return template; | |
} | |
var templateFooter = | |
" </mappings>" +"\n" + | |
"</mapLocal>"+"\n"; | |
program | |
.version('0.1') | |
.option('-g --generate', 'generate MapLocal.xml File') | |
.parse(process.argv); | |
prompt.start(); | |
console.log("Hello wolrd! Please make sure your current working directory is " + root); | |
prompt.get(['host', 'directory'], function(err, result) { | |
console.log("Great! Wait for a second and I will generate this great file for you!"); | |
var text = templateHeader + getFiles(result.host, root + '/' + result.directory) + templateFooter; | |
writeFile(root + "/" + out_file, text ); | |
}) | |
// getFiles output an array | |
function getFiles(host, root) { | |
var res = [], files = fs.readdirSync(root); | |
files.forEach(function(file) { | |
var pathname = root + '/' + file | |
, regex = new RegExp(first_root) | |
, remoatePath = pathname.replace(regex, "") | |
, lstat = fs.lstatSync(pathname) | |
, extname = path.extname(pathname) | |
; | |
if(!lstat.isDirectory() ) { | |
if( extname.length > 0 ) res.push(generateXmlTemplate(host , remoatePath, pathname)); // remove .DS_Store file | |
} else { | |
res = res.concat(getFiles(host, pathname)); | |
} | |
}); | |
return res; | |
} | |
function writeFile( file_path, text ) { | |
fs.writeFile(file_path , text , 'utf8', function(e) { | |
if(e) { | |
console.log("fail, Please check!"); | |
} else { | |
console.log("done, check " + root + "/MapLocal.xml!"); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment