-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathport.js
125 lines (96 loc) · 3.3 KB
/
port.js
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
// This package was originally written for Node.js in JavaScript and was then
// ported to Deno in TypeScript. The Deno version turned out to be more pleasant
// to write for, so now the Node.js version has become a port of the source code
// of the Deno version. This script fetches the source code and applies the
// necessary changes to make it work on Node.js. Unit tests are ported as well.
const fs = require('fs');
const { spawn } = require('child_process');
const replaceString = require('replace-string');
// Which version to patch.
DENO_RSYNC_PARSER_VERSION = 'v2.2.1';
// Runs a shell command and pipes output to stdout.
async function shell(command) {
// Preserves stack trace.
let failure = new Error('Command failed.');
return new Promise((resolve, reject) => {
let process = spawn(command, {
shell: true,
stdio: 'inherit'
});
process.on('error', reject);
process.on('exit', (code) => {
if (code === 0) {
resolve(code);
} else {
reject(failure);
}
});
});
}
// Patches code and creates files.
let pieces = [
// rsync_itemize_changes_parser.ts
async function () {
let code = await fs.promises.readFile('deno-rsync-parser/rsync_itemize_changes_parser.ts', 'utf-8');
code = replaceString(
code,
'import { StringReader } from "https://deno.land/[email protected]/io/readers.ts"',
'import { Readable } from "stream";');
code = replaceString(
code,
'from "https://deno.land/[email protected]/io/bufio.ts"',
'from "./port/read-lines"');
code = replaceString(
code,
'Deno.Reader',
'Readable');
code = replaceString(
code,
'new StringReader(',
'Readable.from(');
await fs.promises.writeFile('src/rsync-itemize-changes-parser.ts', code);
},
// mod.ts
async function () {
let code = await fs.promises.readFile('deno-rsync-parser/mod.ts', 'utf-8');
code = replaceString(
code,
'from "./rsync_itemize_changes_parser.ts";',
'from "./rsync-itemize-changes-parser"');
await fs.promises.writeFile('src/index.ts', code);
},
// rsync_itemize_changes_parser.test.ts
async function () {
let code = await fs.promises.readFile('deno-rsync-parser/rsync_itemize_changes_parser.test.ts', 'utf-8');
code = 'import { test } from "./port/test";' + code;
code = replaceString(
code,
'from "https://deno.land/[email protected]/testing/asserts.ts";',
'from "./port/assert";');
code = replaceString(
code,
'import { StringReader } from "https://deno.land/[email protected]/io/readers.ts";',
'import { Readable } from "stream";');
code = replaceString(code, 'Deno.test(', 'test(');
code = replaceString(
code,
'from "./rsync_itemize_changes_parser.ts";',
'from "./rsync-itemize-changes-parser"');
code = replaceString(
code,
'new StringReader(',
'Readable.from(');
await fs.promises.writeFile('src/rsync-itemize-changes-parser.test.ts', code);
}
];
async function main() {
if (!fs.existsSync('deno-rsync-parser')) {
await shell('git clone https://github.com/daniel-araujo/deno-rsync-parser.git');
}
await shell(`cd deno-rsync-parser; git checkout ${DENO_RSYNC_PARSER_VERSION}`);
for (let piece of pieces) {
await piece();
}
await shell(`npx tsc`);
}
main();