// usage: node addPathType.json source.json dest.json // const fs = require('fs'); const path = require('path'); let Converter = { sourceFile: process.argv[2], destFile: process.argv[3], start() { // 非绝对路径则前缀加上当前目录 if (!path.isAbsolute(this.sourceFile)) { this.sourceFile = path.join(__dirname, this.sourceFile); } if (!path.isAbsolute(this.destFile)) { this.destFile = path.join(__dirname, this.destFile); } this.traversalDir(this.sourceFile, this.destFile); }, // 遍历文件夹找到 traversalDir: function(sourceFile, destFile) { if (sourceFile.length <= 0 || destFile.length <= 0) { return; } fs.readFile(sourceFile, (err, data) => { if (err) { console.error(err); return; } console.log('Success to read file: ' +sourceFile); let str = this.writePath(data); if (str && str.length > 0) { fs.writeFile(destFile, str, (err) => { if (err) { console.error(err); return; } console.log('Success to write file: ' +destFile); }); } }); }, writePath(data) { if (!data || data.length <= 0) { cc.warn('writePath: path is invalid'); return ''; } let str = ''; try { let paths = JSON.parse(data); for (let i = 0, len = paths.length; i < len; i++) { let path = paths[i]; path.type = 0; } str = JSON.stringify(paths); } catch(e) { cc.error('writePath: error to format path'); } return str; }, }; module.exports = Converter; Converter.start();