main.js 2.3 KB
Newer Older
1 2 3 4 5 6 7
//脚本文件目录 __dirname
//运行脚本的目录,即:项目的目录 process.cwd()

//配置文件
const fs = require('fs'),
	Hjson = require('hjson'),
	config = Hjson.rt.parse(fs.readFileSync(__dirname+'/config.js', 'utf-8'))
8
const change_after = require('./change_after')
9 10

if(process.argv[2] == 'change'){
11 12 13 14
	change(config,()=>{
		console.log('脚本change已经执行成功');
		change_after()
	})
15 16 17 18
}else{
	recovery(config)
}

19 20 21
function change(config,callback){
	const total = Object.keys(config).length
	let index = 0;
22 23 24 25
	for (let fileName in config) {
		let path = process.cwd() + fileName
		let copyPath = __dirname + '/copy' + fileName
		let fileText = fs.readFileSync(path, 'utf-8')
26 27
		//保持原文件名先备份一份到/uni_modules_tools/copy目录下,然后再覆盖
		writeFileRecursive(copyPath, fileText, function(err) { //创建目录并写入文件
28 29 30 31 32
			if (err) {
				return console.log(err);
			}
			//改写
			let HfileObj = Hjson.rt.parse(fileText)
33
			//递归合并,为了保留注释内容
34
			mergeJSON(HfileObj,config[fileName])
35
			
36 37 38 39 40 41 42 43 44
			fs.writeFile(path, Hjson.rt.stringify(HfileObj, {
				quotes: 'all',
				separator: true,
				multiline: "off",
				bracesSameLine: true
			}), function(err) {
				if (err) {
					return console.log(err);
				}
45 46 47 48
				index++
				if(index == total){
					callback()
				}
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
			})
		})
	}
}




function recovery(){
	let paths = Object.keys(config)
	console.log(paths);
	paths.forEach(path=>{
		console.log(__dirname + '/copy' + path);
		let oldFile = fs.readFileSync(__dirname + '/copy' + path)
		console.log(process.cwd() + path);
		fs.writeFile(process.cwd() + path, oldFile, function(err) {
			if (err) {
				console.log(err);
				return
			}
			// fs.unlinkSync(__dirname + path+'.lock')
		})
	})
}






//创建目录并写入文件
function writeFileRecursive(path, buffer, callback) {
	let lastPath = path.substring(0, path.lastIndexOf("/"));
	fs.mkdir(lastPath, {
		recursive: true
	}, (err) => {
		if (err) return callback(err);
		fs.writeFile(path, buffer, function(err) {
			if (err) return callback(err);
			return callback(null);
		});
	});
}

//递归合并,为了保留注释内容
function mergeJSON(minor, main) {
	for (var key in main) {
		if (typeof(main[key]) != 'object' ) {
			minor[key] = main[key];
		}else{
			mergeJSON(minor[key], main[key]);
		}
	}
}