/
watch.ts
200 lines (168 loc) · 4.96 KB
/
watch.ts
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
import path from 'path';
import { setTimeout } from 'timers/promises';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { tsx } from '../utils/tsx';
export default testSuite(async ({ describe }, fixturePath: string) => {
describe('watch', ({ test, describe }) => {
test('require file path', async () => {
const tsxProcess = await tsx({
args: ['watch'],
});
expect(tsxProcess.exitCode).toBe(1);
expect(tsxProcess.stderr).toMatch('Error: Missing required parameter "script path"');
});
test('watch files for changes', async () => {
const fixture = await createFixture({
'index.js': 'console.log(1)',
});
const tsxProcess = tsx({
args: [
'watch',
path.join(fixture.path, 'index.js'),
],
});
await new Promise<void>((resolve) => {
async function onStdOut(data: Buffer) {
const chunkString = data.toString();
if (chunkString.match('1\n')) {
await fixture.writeFile('index.js', 'console.log(2)');
} else if (chunkString.match('2\n')) {
tsxProcess.kill();
resolve();
}
}
tsxProcess.stdout!.on('data', onStdOut);
tsxProcess.stderr!.on('data', onStdOut);
});
}, 5000);
test('suppresses warnings & clear screen', async () => {
const tsxProcess = tsx({
args: [
'watch',
path.join(fixturePath, 'log-argv.ts'),
],
});
const stdout = await new Promise<string>((resolve) => {
let aggregateStdout = '';
let hitEnter = false;
function onStdOut(data: Buffer) {
const chunkString = data.toString();
// console.log({ chunkString });
aggregateStdout += chunkString;
if (chunkString.match('log-argv.ts')) {
if (!hitEnter) {
hitEnter = true;
tsxProcess.stdin?.write('enter');
} else {
tsxProcess.kill();
resolve(aggregateStdout);
}
}
}
tsxProcess.stdout!.on('data', onStdOut);
tsxProcess.stderr!.on('data', onStdOut);
});
expect(stdout).not.toMatch('Warning');
expect(stdout).toMatch('\u001Bc');
}, 5000);
test('passes flags', async () => {
const tsxProcess = tsx({
args: [
'watch',
path.join(fixturePath, 'log-argv.ts'),
'--some-flag',
],
});
const stdout = await new Promise<string>((resolve) => {
tsxProcess.stdout!.on('data', (chunk) => {
const chunkString = chunk.toString();
if (chunkString.startsWith('[')) {
resolve(chunkString);
}
});
});
tsxProcess.kill();
expect(stdout).toMatch('"--some-flag"');
await tsxProcess;
}, 5000);
describe('help', ({ test }) => {
test('shows help', async () => {
const tsxProcess = await tsx({
args: ['watch', '--help'],
});
expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch('Run the script and watch for changes');
expect(tsxProcess.stderr).toBe('');
});
// BUGL Currently doesn't pass through `--help
// test('doesn\'t show help with file', async () => {
// const tsxProcess = tsx({
// args: [
// 'watch',
// path.join(fixturePath, 'log-argv.ts'),
// '--help',
// ],
// });
// const stdout = await new Promise<string>((resolve) => {
// tsxProcess.stdout!.on('data', (chunk) => {
// const chunkString = chunk.toString();
// if (chunkString.startsWith('[')) {
// resolve(chunkString);
// }
// });
// });
// tsxProcess.kill();
// console.log(stdout);
// expect(stdout).toMatch('"--help"');
// await tsxProcess;
// }, 2000);
});
describe('ignore', ({ test }) => {
test('file path & glob', async () => {
const entryFile = 'index.js';
const fileA = 'file-a.js';
const fileB = 'directory/file-b.js';
let value = Date.now();
const fixture = await createFixture({
[entryFile]: `
import valueA from './${fileA}'
import valueB from './${fileB}'
console.log(valueA, valueB)
`.trim(),
[fileA]: `export default ${value}`,
[fileB]: `export default ${value}`,
});
const tsxProcess = tsx({
cwd: fixture.path,
args: [
'watch',
'--clear-screen=false',
`--ignore=${fileA}`,
`--ignore=${path.join(fixture.path, 'directory/*')}`,
entryFile,
],
});
tsxProcess.stdout!.on('data', async (data: Buffer) => {
const chunkString = data.toString();
if (chunkString === `${value} ${value}\n`) {
value = Date.now();
await Promise.all([
fixture.writeFile(fileA, `export default ${value}`),
fixture.writeFile(fileB, `export default ${value}`),
]);
await setTimeout(500);
await fixture.writeFile(entryFile, 'console.log("TERMINATE")');
}
if (chunkString === 'TERMINATE\n') {
tsxProcess.kill();
}
});
const tsxProcessResolved = await tsxProcess;
await fixture.rm();
expect(tsxProcessResolved.stdout).not.toMatch(`${value} ${value}`);
expect(tsxProcessResolved.stderr).toBe('');
}, 5000);
});
});
});