tsz-cli 0.1.9

CLI binaries for the tsz TypeScript compiler
Documentation
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
//! Tests for build mode orchestrator and project references

use clap::Parser;
use std::path::{Path, PathBuf};
use tempfile::TempDir;

use crate::args::CliArgs;
use crate::build;
use crate::project_refs::ResolvedProject;

/// Create a test project with tsconfig.json
fn create_test_project(dir: &Path, name: &str, config: &str) -> PathBuf {
    let project_dir = dir.join(name);
    std::fs::create_dir_all(&project_dir).unwrap();

    let config_path = project_dir.join("tsconfig.json");
    std::fs::write(&config_path, config).unwrap();

    project_dir
}

/// Create a test source file
fn create_source_file(project_dir: &Path, name: &str, content: &str) -> PathBuf {
    let src_dir = project_dir.join("src");
    std::fs::create_dir_all(&src_dir).unwrap();

    let file_path = src_dir.join(name);
    std::fs::write(&file_path, content).unwrap();

    file_path
}

#[test]
fn test_is_project_up_to_date_no_buildinfo() {
    let temp_dir = TempDir::new().unwrap();

    // Create a project without .tsbuildinfo
    let project_dir = create_test_project(
        temp_dir.path(),
        "test",
        r#"
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }
}
"#,
    );

    let project = ResolvedProject {
        config_path: project_dir.join("tsconfig.json"),
        root_dir: project_dir.clone(),
        config: serde_json::from_str("{}").unwrap(),
        resolved_references: vec![],
        is_composite: true,
        out_dir: Some(project_dir.join("dist")),
        declaration_dir: None,
    };

    let args = CliArgs::try_parse_from(["tsz"]).unwrap();

    // Project without .tsbuildinfo should not be up-to-date
    assert!(!build::is_project_up_to_date(&project, &args));
}

#[test]
#[ignore = "is_project_up_to_date implementation incomplete - needs .tsbuildinfo parsing and validation"]
fn test_is_project_up_to_date_with_buildinfo() {
    let temp_dir = TempDir::new().unwrap();

    // Create a project with .tsbuildinfo
    let project_dir = create_test_project(
        temp_dir.path(),
        "test",
        r#"
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }
}
"#,
    );

    // Create a minimal .tsbuildinfo file
    let buildinfo_path = project_dir.join("tsconfig.tsbuildinfo");
    let compiler_version = env!("CARGO_PKG_VERSION");
    let buildinfo_content = format!(
        r#"{{
  "version": "0.1.0",
  "compilerVersion": "{compiler_version}",
  "rootFiles": [],
  "fileInfos": {{}},
  "dependencies": {{}},
  "semanticDiagnosticsPerFile": {{}},
  "emitSignatures": {{}},
  "latestChangedDtsFile": null,
  "options": {{}},
  "buildTime": 1234567890
}}"#
    );
    std::fs::write(&buildinfo_path, buildinfo_content).unwrap();

    let project = ResolvedProject {
        config_path: project_dir.join("tsconfig.json"),
        root_dir: project_dir.clone(),
        config: serde_json::from_str("{}").unwrap(),
        resolved_references: vec![],
        is_composite: true,
        out_dir: Some(project_dir.join("dist")),
        declaration_dir: None,
    };

    let args = CliArgs::try_parse_from(["tsz"]).unwrap();

    // Project with valid .tsbuildinfo should be up-to-date (for now)
    // TODO: This should check source file changes too
    assert!(build::is_project_up_to_date(&project, &args));
}

#[test]
fn test_is_project_up_to_date_force_rebuild() {
    let temp_dir = TempDir::new().unwrap();

    let project_dir = create_test_project(
        temp_dir.path(),
        "test",
        r#"
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }
}
"#,
    );

    // Create .tsbuildinfo
    let buildinfo_path = project_dir.join("tsconfig.tsbuildinfo");
    std::fs::write(&buildinfo_path, "{}").unwrap();

    let project = ResolvedProject {
        config_path: project_dir.join("tsconfig.json"),
        root_dir: project_dir.clone(),
        config: serde_json::from_str("{}").unwrap(),
        resolved_references: vec![],
        is_composite: true,
        out_dir: Some(project_dir.join("dist")),
        declaration_dir: None,
    };

    let args = CliArgs::try_parse_from(["tsz", "--force"]).unwrap();

    // Even with .tsbuildinfo, --force should cause rebuild
    assert!(!build::is_project_up_to_date(&project, &args));
}

#[test]
fn test_get_build_info_path() {
    let temp_dir = TempDir::new().unwrap();

    let project_dir = create_test_project(temp_dir.path(), "myproject", "{}");

    let project = ResolvedProject {
        config_path: project_dir.join("tsconfig.json"),
        root_dir: project_dir.clone(),
        config: serde_json::from_str("{}").unwrap(),
        resolved_references: vec![],
        is_composite: false,
        out_dir: None,
        declaration_dir: None,
    };

    // This is an internal test, so we need to make get_build_info_path public or test indirectly
    // For now, we'll just verify the project structure
    assert!(project.config_path.exists());
    assert_eq!(project.config_path.file_name().unwrap(), "tsconfig.json");
}

#[test]
fn test_is_project_up_to_date_with_source_changes() {
    let temp_dir = TempDir::new().unwrap();

    // Create a project with .tsbuildinfo and source files
    let project_dir = create_test_project(
        temp_dir.path(),
        "test",
        r#"
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }
}
"#,
    );

    // Create initial source file
    let _source_file = create_source_file(&project_dir, "index.ts", "export const x = 1;");

    // Create a .tsbuildinfo that lists the source file
    let buildinfo_path = project_dir.join("tsconfig.tsbuildinfo");
    let compiler_version = env!("CARGO_PKG_VERSION");
    let buildinfo_content = format!(
        r#"{{
  "version": "0.1.0",
  "compilerVersion": "{compiler_version}",
  "rootFiles": ["src/index.ts"],
  "fileInfos": {{
    "src/index.ts": {{
      "version": "oldhash",
      "signature": null
    }}
  }},
  "dependencies": {{}},
  "semanticDiagnosticsPerFile": {{}},
  "emitSignatures": {{}},
  "latestChangedDtsFile": null,
  "options": {{}},
  "buildTime": 1234567890
}}"#
    );
    std::fs::write(&buildinfo_path, buildinfo_content).unwrap();

    let project = ResolvedProject {
        config_path: project_dir.join("tsconfig.json"),
        root_dir: project_dir.clone(),
        config: serde_json::from_str("{}").unwrap(),
        resolved_references: vec![],
        is_composite: true,
        out_dir: Some(project_dir.join("dist")),
        declaration_dir: None,
    };

    let args = CliArgs::try_parse_from(["tsz"]).unwrap();

    // Project should need rebuild due to source file change
    assert!(!build::is_project_up_to_date(&project, &args));
}

#[test]
fn test_is_project_up_to_date_with_new_source_files() {
    let temp_dir = TempDir::new().unwrap();

    let project_dir = create_test_project(
        temp_dir.path(),
        "test",
        r#"
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }
}
"#,
    );

    // Create initial source file
    create_source_file(&project_dir, "index.ts", "export const x = 1;");

    // Create .tsbuildinfo that doesn't list the new file
    let buildinfo_path = project_dir.join("tsconfig.tsbuildinfo");
    let compiler_version = env!("CARGO_PKG_VERSION");
    let buildinfo_content = format!(
        r#"{{
  "version": "0.1.0",
  "compilerVersion": "{compiler_version}",
  "rootFiles": [],
  "fileInfos": {{}},
  "dependencies": {{}},
  "semanticDiagnosticsPerFile": {{}},
  "emitSignatures": {{}},
  "latestChangedDtsFile": null,
  "options": {{}},
  "buildTime": 1234567890
}}"#
    );
    std::fs::write(&buildinfo_path, buildinfo_content).unwrap();

    let project = ResolvedProject {
        config_path: project_dir.join("tsconfig.json"),
        root_dir: project_dir.clone(),
        config: serde_json::from_str("{}").unwrap(),
        resolved_references: vec![],
        is_composite: true,
        out_dir: Some(project_dir.join("dist")),
        declaration_dir: None,
    };

    let args = CliArgs::try_parse_from(["tsz"]).unwrap();

    // Project should need rebuild due to new source file
    assert!(!build::is_project_up_to_date(&project, &args));
}

#[test]
fn test_is_project_up_to_date_cross_project_invalidation() {
    let temp_dir = TempDir::new().unwrap();

    // Create main project
    let main_dir = create_test_project(
        temp_dir.path(),
        "main",
        r#"
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }
}
"#,
    );

    // Create referenced project
    let ref_dir = create_test_project(
        temp_dir.path(),
        "ref",
        r#"
{
  "compilerOptions": {
    "composite": true,
    "declaration": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }
}
"#,
    );

    // Create .d.ts file in referenced project
    let ref_dist_dir = ref_dir.join("dist");
    std::fs::create_dir_all(&ref_dist_dir).unwrap();
    let ref_dts_path = ref_dist_dir.join("index.d.ts");
    std::fs::write(&ref_dts_path, "export const x = 1;").unwrap();

    // Create .tsbuildinfo for referenced project with recent .d.ts
    let ref_buildinfo_path = ref_dir.join("tsconfig.tsbuildinfo");
    let current_time = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs();
    let compiler_version = env!("CARGO_PKG_VERSION");
    let ref_buildinfo_content = format!(
        r#"{{
  "version": "0.1.0",
  "compilerVersion": "{compiler_version}",
  "rootFiles": [],
  "fileInfos": {{}},
  "dependencies": {{}},
  "semanticDiagnosticsPerFile": {{}},
  "emitSignatures": {{}},
  "latestChangedDtsFile": "dist/index.d.ts",
  "options": {{}},
  "buildTime": {current_time}
}}"#
    );
    std::fs::write(&ref_buildinfo_path, ref_buildinfo_content).unwrap();

    // Create .tsbuildinfo for main project with older timestamp
    let main_buildinfo_path = main_dir.join("tsconfig.tsbuildinfo");
    let old_time = current_time - 3600; // 1 hour ago
    let main_buildinfo_content = format!(
        r#"{{
  "version": "0.1.0",
  "compilerVersion": "{compiler_version}",
  "rootFiles": [],
  "fileInfos": {{}},
  "dependencies": {{}},
  "semanticDiagnosticsPerFile": {{}},
  "emitSignatures": {{}},
  "latestChangedDtsFile": null,
  "options": {{}},
  "buildTime": {old_time}
}}"#
    );
    std::fs::write(&main_buildinfo_path, main_buildinfo_content).unwrap();

    // Create resolved reference
    use crate::project_refs::{ProjectReference, ResolvedProjectReference};
    let resolved_ref = ResolvedProjectReference {
        config_path: ref_dir.join("tsconfig.json"),
        original: ProjectReference {
            path: "../ref".to_string(),
            prepend: false,
            circular: false,
        },
        is_valid: true,
        error: None,
    };

    let project = ResolvedProject {
        config_path: main_dir.join("tsconfig.json"),
        root_dir: main_dir.clone(),
        config: serde_json::from_str("{}").unwrap(),
        resolved_references: vec![resolved_ref],
        is_composite: true,
        out_dir: Some(main_dir.join("dist")),
        declaration_dir: None,
    };

    let args = CliArgs::try_parse_from(["tsz"]).unwrap();

    // Main project should need rebuild because referenced .d.ts is newer
    assert!(!build::is_project_up_to_date(&project, &args));
}