zweizhao.github.io

项目,文章,随笔,博客,Markdown,个人网站,人生感悟。

View on GitHub
fs = require 'fs'
path = require 'path'

module.exports = class
	constructor: (type) ->
		root_path = path.resolve __dirname, '../'
		utilFile = root_path + '/src/util.js'
		fs.readFile utilFile, (err, data) =>
			throw err if err
			fileStr = data.toString()
			fileStr = @.clear fileStr, 'sit'
			fileStr =	@.clear fileStr, 'dev'

			switch type
				when 'sit' then	result = @.concat fileStr, 'dev'
				when 'dev' then result = @.concat fileStr, 'sit'
				when 'uat'
					fileStr = @.concat fileStr, 'sit'
					result = @.concat fileStr, 'dev'
				else console.log '不存在的打包环境'
			if result
				fs.writeFile utilFile, result, (err) -> console.log if err then err else '写入成功'
			else
				console.log '打包失败'
	clear: (fileStr, type) ->
		source = "// host = 'http://#{type}.yl9158.com/'"
		noComment = "host = 'http://#{type}.yl9158.com/'"
		fileStr.replace source, noComment
	concat: (fileStr, type) ->
		source = "host = 'http://#{type}.yl9158.com/'"
		comment = "// host = 'http://#{type}.yl9158.com/'"
		fileStr.replace source, comment