UNPKG

svnp

Version:
119 lines (118 loc) 4.26 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.zip = exports.init = exports.readAuth = exports.createConfigFile = exports.readConfig = exports.CONFIG_FILE_PATH = exports.CONFIG_FILE_NAME = exports.Config = void 0; const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const readline_1 = __importDefault(require("readline")); const adm_zip_1 = __importDefault(require("adm-zip")); class Config { constructor(repo = '/') { this.localDirectory = path_1.default.join(process.cwd(), 'dist'); this.remoteDirectory = '/'; this.compress = true; this.zipFileName = 'dist.zip'; this.remoteDirectory = repo; } } exports.Config = Config; const comments = { localDirectory: '项目打包后的包目录', remoteDirectory: 'SVN仓库地址(SVN上存放包的目录)', compress: '是否压缩为zip包', zipFileName: '压缩包名称,如果不需要压缩,此字段可为空', }; exports.CONFIG_FILE_NAME = '.svn-pub'; exports.CONFIG_FILE_PATH = path_1.default.join(process.cwd(), exports.CONFIG_FILE_NAME); function readConfig() { const conf = new Config(); const keys = Object.keys(conf); let configStr = ''; try { configStr = fs_1.default.readFileSync(exports.CONFIG_FILE_PATH, 'utf-8'); } catch (e) { console.error('读取配置文件失败,请尝试运行 "svnp init" 生成配置文件并填写配置项\r\n'); return null; } const arr = configStr.split('\r\n'); arr.forEach(str => { str = str.trim(); if (str[0] === '#') return; const i = str.indexOf('='); if (i <= 0 || i >= str.length - 1) return; const key = str.slice(0, i).trim(); const value = str.slice(i + 1).trim(); if (keys.indexOf(key) === -1) { console.error('无效的配置项:', key); return; } ; if (key === 'compress') { conf.compress = value === 'true'; return; } conf[key] = value; }); return conf; } exports.readConfig = readConfig; function createConfigFile(repo) { const conf = new Config(repo); let confStr = ''; Object.keys(conf).forEach(k => { confStr += '# ' + comments[k] + '\r\n'; confStr += k + '='; confStr += conf[k]; confStr += '\r\n\r\n'; }); fs_1.default.writeFileSync(exports.CONFIG_FILE_PATH, confStr, 'utf-8'); return conf; } exports.createConfigFile = createConfigFile; function readAuth() { return new Promise(resolve => { const line = readline_1.default.createInterface({ input: process.stdin, output: process.stdout }); line.question(`请输入SVN远程仓库地址: `, repository => { line.question(`请输入SVN账号: `, username => { line.question(`请输入SVN密码: `, password => { resolve({ repository, username, password }); line.close(); }); }); }); }); } exports.readAuth = readAuth; function init(repo) { if (fs_1.default.existsSync(exports.CONFIG_FILE_PATH)) fs_1.default.unlinkSync(exports.CONFIG_FILE_PATH); createConfigFile(repo); console.log('配置文件已生成,请检查并填写配置项:', exports.CONFIG_FILE_PATH, '\r\n'); } exports.init = init; function zip(dir, outputFileName) { const admZip = new adm_zip_1.default(); const files = fs_1.default.readdirSync(dir); files.forEach(f => { const p = path_1.default.join(dir, f); const stat = fs_1.default.statSync(p); if (stat.isFile()) { admZip.addLocalFile(p); } else { admZip.addLocalFolder(p); } }); const output = path_1.default.join(dir, outputFileName); admZip.writeZip(output); return output; } exports.zip = zip;