1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
| 'use strict';
const fs = require('fs'); const path = require('path');
const PKG_NAME = 'hexo-deployer-rsync'; const PATCH_ID = '[patch-deployer]';
const PROJECT_ROOT = path.resolve(__dirname, '..'); const TARGET_FILE = path.join(PROJECT_ROOT, 'node_modules', PKG_NAME, 'lib', 'deployer.js');
const PATCH_VERSION_SIGNAL = '// HEXO_DEPLOYER_RSYNC_PATCHED_BY_WEFUXI'; const PATCH_VERSION = 1;
const EXIT_OK = 0;
function warn(...a) { console.warn(PATCH_ID, ...a); } function log(...a) { console.log(PATCH_ID, ...a); }
function read(file) { return fs.readFileSync(file, 'utf8'); } function write(file, s) { fs.writeFileSync(file, s, 'utf8'); }
function regexpEscape(s) { return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); }
function replaceOnceOrSkip(state, before, after, title, alreadyFragments) { const srcBefore = state.src; if (Array.isArray(alreadyFragments) && alreadyFragments.length && alreadyFragments.every(f => srcBefore.indexOf(f) !== -1)) { state.skipped.push(title); return; } const re = (before instanceof RegExp) ? before : new RegExp(regexpEscape(before)); if (re.test(srcBefore)) { const matches = srcBefore.match(new RegExp(re.source, (re.flags || '') + (re.global ? '' : 'g'))) || []; if (matches.length > 1) { state.failed.push({ title, multi: true, count: matches.length }); return; } state.src = srcBefore.replace(re, after); state.applied.push(title); return; } if (alreadyFragments && alreadyFragments.length && alreadyFragments.every(f => srcBefore.indexOf(f) !== -1)) { state.skipped.push(title); return; } const sig = (typeof after === 'string') ? after.replace(/^\s+|\s+$/g, '').slice(40, 120) : ''; if (sig && srcBefore.indexOf(sig) !== -1) { state.skipped.push(title); return; } state.failed.push({ title, needle: String(before).slice(0, 120) + (String(before).length > 120 ? '…' : ''), }); }
function ensurePatchedHeader(state) { if (state.src.indexOf(PATCH_VERSION_SIGNAL) !== -1) { state.skipped.push('patched-version-header'); return; } const lines = state.src.split(/\r?\n/); lines.splice(1, 0, PATCH_VERSION_SIGNAL + ' v' + PATCH_VERSION); state.src = lines.join('\n'); state.applied.push('patched-version-header'); }
function buildPatchSteps() { return [ { title: 'add rsync / ssh / ssh_options help lines', before: / help \+= ' key: <key>\\n';\r?\n help \+= ' verbose:/, after: " help += ' key: <key>\\n';\n" + " help += ' rsync: <rsync command path> # e.g. D:/apps/cwrsync/bin/rsync.exe (Windows)\\n';\n" + " help += ' ssh: <ssh command path> # e.g. D:/apps/cwrsync/bin/ssh.exe (Windows)\\n';\n" + " help += ' ssh_options: <extra ssh options> # e.g. \\'-o StrictHostKeyChecking=accept-new\\'\\n';\n" + " help += ' verbose:", already: [ "help += ' rsync: <rsync command path>", "help += ' ssh: <ssh command path>", "help += ' ssh_options: <extra ssh options>" ] }, { title: 'insert rsynccmd + sshcmd vars after params, before if(port)', before: /\n if \(args\.port && args\.port > 0 && args\.port < 65536\) \{/, after: '\n' + " const cwrsyncHome = process.env.CWRSYNCHOME;\n" + " let rsynccmd = args.rsync || (cwrsyncHome ? cwrsyncHome + '/bin/rsync.exe' : 'rsync');\n" + " let sshcmd = args.ssh || (cwrsyncHome ? cwrsyncHome + '/bin/ssh.exe' : 'ssh');\n" + '\n' + ' if (args.port && args.port > 0 && args.port < 65536) {', already: [ "const cwrsyncHome = process.env.CWRSYNCHOME", "let rsynccmd = args.rsync || (cwrsyncHome ? cwrsyncHome + '/bin/rsync.exe' : 'rsync')", "let sshcmd = args.ssh || (cwrsyncHome ? cwrsyncHome + '/bin/ssh.exe' : 'ssh')" ] }, { title: 'replace whole if(port){...} block with unified rshCmd build + ssh_options', before: /^[ \t]*if \(args\.port && args\.port > 0 && args\.port < 65536\) \{\r?\n[\s\S]*?\n[ \t]*\}[ \t]*\r?\n\s*(?=[ \t]*if \(args\.verbose\))/m, after: [ " let rshCmd = '';", ' if (args.rsh) {', " rshCmd = `'${args.rsh}'`;", " if (args.key) rshCmd += ' -i ' + args.key;", " if (args.port && args.port > 0 && args.port < 65536) rshCmd += ' -p ' + args.port;", ' } else if (args.port && args.port > 0 && args.port < 65536) {', ' rshCmd = sshcmd;', " if (args.key) rshCmd += ' -i ' + args.key;", " rshCmd += ' -p ' + args.port;", ' } else if (args.key) {', " rshCmd = sshcmd + ' -i ' + args.key;", ' }', ' if (args.ssh_options) {', ' if (!rshCmd) rshCmd = sshcmd;', " rshCmd += ' ' + args.ssh_options;", ' }', ' if (rshCmd) {', " params.splice(params.length - 2, 0, '-e', rshCmd);", ' }', '' ].join('\n'), already: [ "let rshCmd = '';", 'if (args.ssh_options) {', "params.splice(params.length - 2, 0, '-e', rshCmd);" ] }, { title: "create_before_update branch 1st spawn('rsync',...) -> spawn(rsynccmd, ...)", before: /return spawn\('rsync', params, \{verbose: true\}\)\.then\(\(\) => \{/, after: 'return spawn(rsynccmd, params, {verbose: true}).then(() => {', already: [ 'return spawn(rsynccmd, params, {verbose: true}).then(() => {' ] }, { title: "create_before_update branch 2nd spawn('rsync',...) -> spawn(rsynccmd, ...)", before: /return spawn\('rsync', params, \{verbose: true\}\);\r?\n \}\);/, after: 'return spawn(rsynccmd, params, {verbose: true});\n });', already: [ 'return spawn(rsynccmd, params, {verbose: true});\n });' ] }, { title: "normal exit spawn('rsync',...) -> spawn(rsynccmd, ...)", before: /return spawn\('rsync', params, \{verbose: true\}\);[ \t]*\r?\n\};[ \t]*\s*$/, after: 'return spawn(rsynccmd, params, {verbose: true});\n};', already: [ 'return spawn(rsynccmd, params, {verbose: true});\n};' ] } ]; }
function sanityCheck(state) { const requiredFragments = [ "help += ' rsync: <rsync command path>", "help += ' ssh: <ssh command path>", "help += ' ssh_options: <extra ssh options>", "const cwrsyncHome = process.env.CWRSYNCHOME", "let rsynccmd = args.rsync || (cwrsyncHome ? cwrsyncHome + '/bin/rsync.exe' : 'rsync')", "let sshcmd = args.ssh || (cwrsyncHome ? cwrsyncHome + '/bin/ssh.exe' : 'ssh')", "let rshCmd = ''", 'if (args.ssh_options) {', "params.splice(params.length - 2, 0, '-e', rshCmd)", 'return spawn(rsynccmd, params, {verbose: true}).then(() => {', ]; const missing = requiredFragments.filter(s => state.src.indexOf(s) === -1); if (missing.length) { state.failed.push({ title: 'sanity-check: expected fragments missing', missing }); return false; } const spawnFixed = (state.src.match(/spawn\(rsynccmd, params, \{verbose: true\}\)/g) || []).length; if (spawnFixed !== 3) { state.failed.push({ title: 'sanity-check: spawn(rsynccmd,...) count!=3', got: spawnFixed }); return false; } if (/spawn\(['"]rsync['"]\s*,/.test(state.src)) { state.failed.push({ title: 'sanity-check: leftover hardcoded spawn(\'rsync\',...) calls remain' }); return false; } return true; }
function main() { if (!fs.existsSync(path.dirname(TARGET_FILE))) { warn(PKG_NAME + ' not installed yet. Skipping patch; re-run after `npm install`.'); return EXIT_OK; } if (!fs.existsSync(TARGET_FILE)) { warn('target file missing: ' + TARGET_FILE); return 1; }
const state = { src: read(TARGET_FILE), applied: [], skipped: [], failed: [], };
ensurePatchedHeader(state);
const steps = buildPatchSteps(); for (const step of steps) { replaceOnceOrSkip(state, step.before, step.after, step.title, step.already); }
if (state.applied.length) log('applied patches :', state.applied.join(' | ')); if (state.skipped.length) log('skipped (idempotent):', state.skipped.join(' | ')); if (state.failed.length) { warn('the following patch rules could not be auto-applied to'); warn(' ' + TARGET_FILE); warn('This probably means hexo-deployer-rsync upgraded and the upstream source'); warn('shape changed. Please review the rules in scripts/patch-deployer.cjs and fix.'); for (const f of state.failed) { if (f.missing) { warn(' - [' + f.title + '] missing fragments: ' + f.missing.join(' ; ')); } else if (f.multi) { warn(' - [' + f.title + '] expected 1 match but found ' + f.count + '; ambiguous abort.'); } else { warn(' - [' + f.title + '] needle not found: ' + (f.needle || '')); } } const diffPath = path.join(PROJECT_ROOT, 'node_modules', '.' + PKG_NAME + '-deployer.patch.failed.tmp'); try { write(diffPath, state.src); } catch (_) {} warn('(current state after partial apply was dumped to ' + diffPath + ' for inspection.)'); return 2; }
if (!sanityCheck(state)) { warn('sanity check failed. The patched file is missing required fragments.'); warn('DO NOT use the generated file; fix the patch rules. Dumping state...'); const diffPath = path.join(PROJECT_ROOT, 'node_modules', '.' + PKG_NAME + '-deployer.patch.failed.tmp'); try { write(diffPath, state.src); } catch (_) {} warn(' dumped -> ' + diffPath); return 3; }
write(TARGET_FILE, state.src);
log('ok -> ' + TARGET_FILE); return EXIT_OK; }
process.exit(main());
|