****************************************
转码文件
****************************************
354689
async function transform(src, dist) {
    if (!src.endsWith('node_modules')) {
        let stats = await fs.stat(src);

        // 查看状态
        if (stats) {

            // 处理目录
            if (stats.isDirectory()) {

                // 判断是否指定目录`
                if (!dist) {
                    return stdout.error('Error: The output dir is must!');
                }

                // 获取目标文件夹信息
                stats = await fs.stat(dist);

                // 不存在文件
                if (stats) {
                    if (!stats.isDirectory()) {
                        return stdout.error('Error: The output dir is not a Directory!');
                    }
                } else {
                    await fs.mkdir(dist);
                }

                // 处理子文件
                return await Promise.all((await fs.readdir(src)).map(
                    name => transform(path.resolve(src, name), path.resolve(dist, name))
                ));
            }

            // 生成目录文件
            if (!dist) {
                dist = src.replace(/\.?(\w+)$/, '.min.$1');
            }

            // 打印信息
            stdout.info(`copy: ${relative(src)} --> ${relative(dist)}`);

            // 转码文件
            if (src.endsWith('.js')) {
                let code = await fs.readFile(src, 'utf-8');

                // 判断是否需要转码
                if (code.indexOf('\nimport ') !== -1 || code.indexOf('\nexport ') !== -1) {
                    code = transformCode(code).code;
                }

                // 写入文件
                return await fs.writeFile(dist, code);
            }

            // 拷贝文件
            await fs.copyFile(src, dist);
        }
    }
}
475484
private static void watchFile(final File file, ResourceLoader resourceLoader, String resourcePaths) {
        LOGGER.info("监控文件："+file.toString());
        DirectoryWatcher.WatcherCallback watcherCallback = new DirectoryWatcher.WatcherCallback(){
            private long lastExecute = System.currentTimeMillis();
            @Override
            public void execute(WatchEvent.Kind<?> kind, String path) {
                if(System.currentTimeMillis() - lastExecute > 1000){
                    lastExecute = System.currentTimeMillis();
                    if(!path.equals(file.toString())){
                        return;
                    }
                    LOGGER.info("事件："+kind.name()+" ,路径："+path);
                    synchronized(AutoDetector.class){
                        DirectoryWatcher dw = WATCHER_CALLBACKS.get(this);
                        String paths = RESOURCES.get(dw);
                        ResourceLoader loader = RESOURCE_LOADERS.get(dw);
                        LOGGER.info("重新加载数据");
                        loadAndWatch(loader, paths);
                    }
                }
            }

        };
        DirectoryWatcher fileWatcher = DirectoryWatcher.getDirectoryWatcher(watcherCallback,
                StandardWatchEventKinds.ENTRY_MODIFY,
                    StandardWatchEventKinds.ENTRY_DELETE);
        fileWatcher.watchDirectory(file.getParent());
        WATCHER_CALLBACKS.put(watcherCallback, fileWatcher);
        RESOURCES.put(fileWatcher, resourcePaths);
        RESOURCE_LOADERS.put(fileWatcher, resourceLoader);
    }
1289022
private static void validateFiles(File zipFile, File... srcFiles) throws UtilException {
		if(zipFile.isDirectory()) {
			throw new UtilException("Zip file [{}] must not be a directory !", zipFile.getAbsoluteFile());
		}
		
		for (File srcFile : srcFiles) {
			if(null == srcFile) {
				continue;
			}
			if (false == srcFile.exists()) {
				throw new UtilException(StrUtil.format("File [{}] not exist!", srcFile.getAbsolutePath()));
			}

			try {
				final File parentFile = zipFile.getCanonicalFile().getParentFile();
				// 压缩文件不能位于被压缩的目录内
				if (srcFile.isDirectory() && parentFile.getCanonicalPath().contains(srcFile.getCanonicalPath())) {
					throw new UtilException("Zip file path [{}] must not be the child directory of [{}] !", zipFile.getCanonicalPath(), srcFile.getCanonicalPath());
				}

			} catch (IOException e) {
				throw new UtilException(e);
			}
		}
	}
174023
function (uri, fromUri, source) {
        if (!/^\./.test(uri) || EXTNAME_RE.test(uri)) {

            var line;

            // 如果只出现一次这个字符串，很容易确认模板错误行
            if (source.split(uri).length === 2) {
                source.split(/\n/).forEach(function (code, index) {
                    if (code.indexOf(uri) !== -1) {
                        line = index + 1;
                        source = code.trim();
                    }
                });
            }

            var error = {
                name: 'Syntax Error',
                line: line,
                source: source,
                message: 'Template must be a relative path, and can not have a suffix.'
            };

            if (fromUri) {
                error.filename = fromUri;
            }

            throw error;
        }
    }
1133829
function(url, filepath, fn){
        // 提交请求
        try{
            fs.createReadStream(filepath).pipe(request.post(url, fn))
        }
        catch(e){ fn(e) }
    }
1146428
function removeFormpart(TPL,ctx)
{
    let files = ctx.files;
    for(let f in files) {
        fs.unlink(files[f].path, (err)=> {
            err && TPL.params[f] && console.error('删除文件失败', files[f])
        });//异步清除临时目录中的上传文件
    }
}
1172148
private void internalCopyFile(File src, File dest) throws IORuntimeException {
		if (null != copyFilter && false == copyFilter.accept(src)) {
			//被过滤的文件跳过
			return;
		}
		
		// 如果已经存在目标文件，切为不覆盖模式，跳过之
		if (dest.exists()) {
			if(dest.isDirectory()) {
				//目标为目录，目录下创建同名文件
				dest = new File(dest, src.getName());
			}
			
			if(dest.exists() && false == isOverride) {
				//非覆盖模式跳过
				return;
			}
		}else {
			//路径不存在则创建父目录
			dest.getParentFile().mkdirs();
		}
		
		final ArrayList<CopyOption> optionList = new ArrayList<>(2);
		if(isOverride) {
			optionList.add(StandardCopyOption.REPLACE_EXISTING);
		}
		if(isCopyAttributes) {
			optionList.add(StandardCopyOption.COPY_ATTRIBUTES);
		}
		
		try {
			Files.copy(src.toPath(), dest.toPath(), optionList.toArray(new CopyOption[optionList.size()]));
		} catch (IOException e) {
			throw new IORuntimeException(e);
		}
	}
712370
private static void constructNGram(Path file){
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file.toFile()),"utf-8"));){
            String line;
            while( (line = reader.readLine()) != null ){
                LINES_COUNT.incrementAndGet();
                //去除首尾空白字符
                line = line.trim();
                //忽略空行
                if(!"".equals(line)){
                    //词和词之间以空格隔开
                    String[] words = line.split("\\s+");
                    if(words == null){
                        //忽略不符合规范的行
                        continue;
                    }
                    List<String> list = new ArrayList<>();
                    StringBuilder phrase = new StringBuilder();
                    int phraseCount=0;
                    boolean find=false;
                    for(String word : words){
                        String[] attr = word.split("/");
                        if(attr == null || attr.length < 1){
                            //忽略不符合规范的词
                            continue;
                        }
                        if(attr[0].trim().startsWith("[")){
                            find = true;
                        }                        
                        //去掉[和]
                        String item = attr[0].replace("[", "").replace("]", "").trim();
                        list.add(item);
                        //不重复词
                        addWord(item);
                        //词数目
                        WORD_COUNT.incrementAndGet();
                        //字符数目
                        CHAR_COUNT.addAndGet(item.length());
                        if(find){
                            phrase.append(item).append(" ");
                            phraseCount++;
                        }
                        //短语标注错误
                        if(phraseCount > 10){
                            find = false;
                            phraseCount = 0;
                            phrase.setLength(0);
                        }
                        if(find && attr.length > 1 && attr[1].trim().endsWith("]")){
                            find = false;
                            PHRASES.add(phrase.toString());
                            phrase.setLength(0);
                        }
                    }
                    //计算bigram模型
                    int len = list.size();
                    if(len < 2){
                        continue;
                    }
                    for(int i=0; i<len-1; i++){
                        String first = list.get(i);
                        if(!Utils.isChineseCharAndLengthAtLeastOne(first)){
                            continue;
                        }
                        String second = list.get(i+1);
                        if(!Utils.isChineseCharAndLengthAtLeastOne(second)){
                            //跳过一个词
                            i++;
                            continue;
                        }
                        //忽略长度均为1的模型
                        if(first.length() < 2 && second.length() < 2){
                            continue;
                        }
                        String key = first+":"+second;
                        Integer value = BIGRAM.get(key);
                        if(value == null){
                            value = 1;
                        }else{
                            value++;
                        }
                        BIGRAM.put(key, value);
                    }
                    if(len < 3){
                        continue;
                    }
                    //计算trigram模型
                    for(int i=0; i<len-2; i++){
                        String first = list.get(i);
                        if(!Utils.isChineseCharAndLengthAtLeastOne(first)){
                            continue;
                        }
                        String second = list.get(i+1);
                        if(!Utils.isChineseCharAndLengthAtLeastOne(second)){
                            //跳过一个词
                            i++;
                            continue;
                        }
                        String third = list.get(i+2);
                        if(!Utils.isChineseCharAndLengthAtLeastOne(third)){
                            //跳过二个词
                            i += 2;
                            continue;
                        }
                        //忽略长度均为1的模型
                        if(first.length() < 2 && second.length() < 2 && third.length() < 2){
                            continue;
                        }
                        String key = first+":"+second+":"+third;
                        Integer value = TRIGRAM.get(key);
                        if(value == null){
                            value = 1;
                        }else{
                            value++;
                        }
                        TRIGRAM.put(key, value);
                    }                    
                }
            }
        }catch(Exception e){
            LOGGER.info("分析语料库 "+file+" 失败：", e);
        }        
    }
395457
function jsonFile2TreeStr(){
    if(Type.isBoolean(argJ)){
        help();
        return;
    }
    if(argD){
        warningOut("OK! You'd better not use both -d(--d) and -j(--json) together in one cli. Pick one of them to exec, plz!");
    }
    let targetJson;
    let argJson = (Array.isArray(argJ) ? argJ[0] : argJ) || "";
    if(argJson.match(/^\{[\S\s:.]+}$/g)){// 简单判断一下输入的是否是json格式的字符串
        try{
            targetJson = JSON.parse(argJson);
        }catch (e1){
            try{
                targetJson = (new Function("return "+argJson+";"))();
            }catch(e2){
                errorOut(`Parse the input string to json obj failed!:\n${e1.toString()}\n${e2.toString()}`);
                targetJson = false;
            }
        }
    }else{
        argJson = trimAndDelQuotes(argJson);
        let filePath = path.normalize(argJson);
        if(filePath.match(/(\.json[1-9]?)$/)){// 文件：*.json || *.json(1~9)
            jsonName = path.basename(filePath);
            try{
                targetJson = JSON.parse(fs.readFileSync(path.join(cwd, filePath), 'utf8'));
            }catch (e3){
                errorOut(e3.toString());
                targetJson = false;
            }
        }else{
            targetJson = false;
            if(existsSync(filePath) && fs.statSync(filePath).isDirectory()){// 如果路径存在，且是一个文件夹，则进行遍历文件夹
                argD = filePath;
                dir2TreeStr();
            }else{
                warningOut(`It looks like that u've typed in an incorrect json-file-path '${filePath}'! it should be 'xx/xxx/*.json', check, plz!`)
            }
        }
    }
    // finally parse
    s = Type.isNaN(s) ? 3 : s;
    v = Type.isNaN(v) ? 1 : v;
    _doMain(targetJson);
}
191576
function gotoLoadLanguageFile (langsFiles, ext, successCallback) {
    /**
     * [_tryLoad 尝试加载]
     * @param  {[string]} file        [文件路径]
     * @param  {[string]} langKey     [关键KEY]
     * @param  {[string]} ext         [扩展名：只支持两种: .josn 和 .js]
     * @param  {[function]} fnNext     [出错下一步执行函数]
     * @param  {[function]} fnCallback [成功加载回调函数]
     * @return {[type]}             [description]
     */
    function _tryLoad (file, langKey, ext, fnNext, fnCallback) {
      try {
        $.ajax({
          url: file,
          dataType: (ext === '.js' ? 'script' : 'json'),
          success: function (data, status) {
            console.log('[x] ' + status + ' =' + file);
            var obj = {
              data: data,
              status: status,
              info: {
                file: file,
                langKey: langKey,
                langID: googleLangIDMaps.getGoogleLangID(langKey),
                ext: ext
              }
            };

            t$.loadedLanguage.insert(obj.info, ext);
            fnCallback && fnCallback(obj);
          },
          error: function (req, status, err) {
            console.log(err);
            try {
              throw new Error('[x]no found... continue.. = ' + file)
            } catch (error) {
              console.warn(error);
              fnNext && fnNext(fnCallback);
            }
          }
        });
      } catch (error) {
        console.error(error);
        fnNext && fnNext();
      }
    }

    /**
     * [gotoTry 尝试]
     * @param  {[type]}   list     [文件对象列表]
     * @param  {[type]}   ext      [扩展名]
     * @param  {Function} callback [成功回调]
     * @return {[type]}            [description]
     */
    function _gotoTry (list, ext, callback) {
      if (Tool.isArray(list) && list.length > 0) {
        var ele = list[0];
        _tryLoad(ele.path, ele.key, ext, function (cb) {
          var newLangFileList = list.splice(1);
          _gotoTry(newLangFileList, ext, cb);
        }, callback);
      } else {
        console.warn('[x] language list length is 0 or not a array. TYPE=' + Tool.getType(list));
      }
    }

    // Try start
    _gotoTry(langsFiles, ext, successCallback);
  }
446412
function unzip(src, dist) {
    if (isString(src)) {

        // 获取输出目录
        if (arguments.length === 1) {
            dist = src.replace(/\.zip$/, '');
        }

        // 校验参数
        if (isString(dist)) {
            return new Promise((resolve, reject) => {
                let archive = fs.createReadStream(src),
                    output = unarchiver.Extract({ path: dist });

                // 监听回调
                archive.on('error', reject);
                output.on('error', reject);
                output.on('close', resolve);

                // 设置管道输出
                archive.pipe(output);
            });
        }
    }
}
324533
private static void extractText(Path file, BufferedWriter writer, String separator, boolean includePhrase){
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file.toFile()),"utf-8"));){
            String line;
            while( (line = reader.readLine()) != null ){
                //去除首尾空白字符
                line = line.trim();
                //忽略空行
                if(!"".equals(line)){
                    //词和词之间以空格隔开
                    String[] words = line.split("\\s+");
                    if(words == null){
                        //忽略不符合规范的行
                        continue;
                    }
                    StringBuilder phrase = new StringBuilder();
                    int phraseCount=0;
                    boolean find=false;
                    for(String word : words){
                        String[] attr = word.split("/");
                        if(attr == null || attr.length < 1){
                            //忽略不符合规范的词
                            continue;
                        }
                        if(attr[0].trim().startsWith("[")){
                            find = true;
                        }                        
                        //去掉[和]
                        String item = attr[0].replace("[", "").replace("]", "").trim();
                        writer.write(item+separator);
                        if(find){
                            phrase.append(item);
                            phraseCount++;
                        }
                        //短语标注错误
                        if(phraseCount > 10){
                            find = false;
                            phraseCount = 0;
                            phrase.setLength(0);
                        }
                        if(find && attr.length > 1 && attr[1].trim().endsWith("]")){
                            find = false;
                            if(includePhrase){
                                writer.write(phrase.toString()+separator);
                            }
                            phrase.setLength(0);
                        }
                        //词数目
                        WORD_COUNT.incrementAndGet();
                        //字符数目
                        CHAR_COUNT.addAndGet(item.length());
                    }
                    //换行
                    writer.write("\n");
                }
            }
        }catch(Exception e){
            LOGGER.info("从语料库 "+file+" 中抽取文本失败：", e);
        }        
    }
1173354
async function copy(src, dist) {
    let stats = await stat(src),
        files;

    // 处理文件
    if (!stats.isDirectory()) {
        return await copyFile(src, dist);
    }

    // 生成目标路径
    if (!await stat(dist)) {
        await local.mkdir(dist);
    }

    // 读取文件夹
    files = await local.readdir(src);

    // 处理文件
    await Promise.all(files.map(file => (
        file === 'node_modules' ? Promise.resolve() : copy(path.resolve(src, file), path.resolve(dist, file))
    )));
}
132523
function (src, dst, callback) {
    // 读取目录中的所有文件/目录
    var paths = fs.readdirSync(src);
    paths.forEach(function (path) {
        if (fs.existsSync(dst) == false) mkdir(dst);
        var _src = src + '/' + path,
            _dst = dst + '/' + path;
        // readable, writable;   

        var st = fs.statSync(_src);
        // 判断是否为文件
        if (st.isFile()) {
            // 创建读取流
            //readable = fs.createReadStream( _src );
            // 创建写入流

            //writable = fs.createWriteStream( _dst );   
            // 通过管道来传输流
            //readable.pipe( writable );
            fs.writeFileSync(_dst, fs.readFileSync(_src))

        }
        // 如果是目录则递归调用自身
        else if (st.isDirectory()) {
            exists(_src, _dst, copy);
        }
    });
    if (callback)
        callback();
}
1041612
function (resourcePath, ext) {
            var self = this;
            var staticPath;
            if (_.isEmpty(self.map)) {
                // map 为空时, 直接拼接路径, 并确保文件存在
                staticPath = FileLoader.matchFile(path.join(self.staticPath, resourcePath) + ext);
                if (staticPath) {
                    staticPath = staticPath.substr(staticPath.indexOf(self.staticFolder));
                }
            } else {
                var resourceKey = resourcePath + ext;
                var result = self.map['res'] && self.map['res'][resourceKey];
                staticPath = result && result.uri;
            }
            return staticPath;
        }
1225581
function _TPL(ctx)
{
    let method = ctx.method;
    let route = url.parse(ctx.url).pathname.substr(1);
    isDebug && console.log('route',route,ctx.url)
    let data = null;
    let routeName ;//路由名字

    if(((itFace[route] && itFace[route].method == method) || 
        (itFace[`${route}_${method}`] && itFace[`${route}_${method}`].method == method)) 
        && !isDebug){
        if(itFace[route] && itFace[route].method == method){
            routeName = route;
        }
        if(itFace[`${route}_${method}`] && itFace[`${route}_${method}`].method == method){
            routeName = `${route}_${method}`;
        }
        data = itFace[`${routeName}`];
    }else{
        //读取接口文档,并缓存起来
        // console.log('aaaa',`${interFaceRoot}${`${route}_${method}`}.json`)
        if(isExist(path.join(`${interFaceRoot}${route}.json`))){
            data = fs.readFileSync(path.join(`${interFaceRoot}${route}.json`), 'utf-8');
            try {
                data = eval('('+data+')');
            }catch (err){
                return {TPL:"接口文档定义错误!"}
            }
            //如果请求方法对不上,则清空现有数据
            if(!data || data.method != method){//
                data = null;
            }else {
                itFace[route] = data;
                routeName = route;
            }
        }
        //如果本文件不存在,则通过匹配文件名加上请求参数
        if(!data && isExist(path.join(`${interFaceRoot}${`${route}_${method}`}.json`))){
            data = fs.readFileSync(path.join(`${interFaceRoot}${`${route}_${method}`}.json`), 'utf-8');
            try {
                data = eval('('+data+')');
            }catch (err){
                data = null;
            }
            if(!data || data.method != method){//
                data = null;
            }else {
                itFace[route] = data;
                routeName = route;
            }
        }
        if(data){
            //处理数据中,含有的特殊字符
            let map={"{}":"$B","{S}":"$S","{U}":"$U","{X}":"$X"};
            if(data.pretreat)
            {
                if(String==data.pretreat.constructor) data.pretreat=[data.pretreat];
                data.pretreat=data.pretreat.join(";").replace(/{[SUX]?}/g,function($0)
                {
                    return map[$0];
                });
            }
            if(data.grant)
                data.grant=_formatCnd(data.grant,map);
            if(data.check)
                data.check=_formatCnd(data.check,map);
        }
    }
    if(data){
        if(data.params){
            for(let key in data.params){
                if('file'==data.params[key].type) data.hasFile=1;
            }
        }
    }
    return {TPL:data,routeName:routeName}
}
856115
function parseDeps(options, contents, modData) {
    var isSeajsUse = !!~contents.indexOf('fang.use('),
        id = modData.id,
        deps = [],
        defineDeps = [],
        asyncDeps = [],
        configResult, name, base, matches;

    // 标准模块
    if (!isSeajsUse) {
        defineDeps = pullDefineDeps(options, rDefine, contents, modData);
        deps = pullDeps(options, rRequire, contents, modData);
        asyncDeps = pullDeps(options, rRequireAsync, contents, modData);
    }
    // 解析seajs.use
    else {
        configResult = parseConfig(contents);
        contents = configResult.contents;

        for (name in configResult.config) {
            options.config[name] = configResult.config[name];
        }

        matches = contents.match(rSeajsUse);

        matches.forEach(function (item) {
            var _deps = [];

            if (~item.indexOf('fang.use')) {
                _deps = pullDeps(options, rDeps, item);
                deps = deps.concat(_deps);
            }
        });
    }
    // 合并依赖数组
    for (var i = 0, len = defineDeps.length; i < len; i++) {
        var theDeps = defineDeps[i];
        var hasvalue = false;
        for (var j = 0, lenj = deps.length; j < lenj; j++) {
            if (theDeps.origId === deps[j].origId) {
                hasvalue = true;
                break;
            }
        }
        if (!hasvalue) {
            deps.unshift(theDeps);
        }
    }
    base = options.base || path.resolve(modData.path, '..');
    deps = mergePath(options, deps, base);
    asyncDeps = mergePath(options, asyncDeps, base);

    options.modArr.push({
        id: id,
        deps: deps,
        path: modData.path,
        asyncMod: modData.asyncMod,
        contents: contents,
        extName: modData.extName,
        origId: modData.origId || id
    });

    options.asyncTemMod.push(...asyncDeps);
    return deps;
}