ts-repo-utils 7.3.0 → 7.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -0
- package/dist/cmd/assert-repo-is-clean.mjs +1 -1
- package/dist/cmd/check-should-run-type-checks.mjs +1 -1
- package/dist/cmd/format-diff-from.mjs +1 -1
- package/dist/cmd/format-uncommitted.mjs +1 -1
- package/dist/cmd/gen-index-ts.mjs +1 -1
- package/dist/functions/should-run.d.mts +1 -1
- package/dist/functions/should-run.d.mts.map +1 -1
- package/dist/functions/should-run.mjs +1 -0
- package/dist/functions/should-run.mjs.map +1 -1
- package/dist/node-global.d.mts +2 -0
- package/dist/node-global.d.mts.map +1 -1
- package/package.json +1 -1
- package/src/cmd/assert-repo-is-clean.mts +1 -1
- package/src/cmd/check-should-run-type-checks.mts +1 -1
- package/src/cmd/format-diff-from.mts +1 -1
- package/src/cmd/format-uncommitted.mts +1 -1
- package/src/cmd/gen-index-ts.mts +1 -1
- package/src/functions/should-run.mts +3 -1
- package/src/node-global.mts +1 -1
package/README.md
CHANGED
|
@@ -236,6 +236,39 @@ type Ret = Promise<
|
|
|
236
236
|
|
|
237
237
|
### Script Execution Utilities
|
|
238
238
|
|
|
239
|
+
#### `isDirectlyExecuted(fileUrl: string): boolean`
|
|
240
|
+
|
|
241
|
+
Determines whether a script is being executed directly via CLI or imported as a module. This is useful for creating scripts that can both be imported as libraries and executed directly.
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
import { isDirectlyExecuted } from 'ts-repo-utils';
|
|
245
|
+
|
|
246
|
+
// or
|
|
247
|
+
// import "ts-repo-utils"; // isDirectlyExecuted is globally defined in ts-repo-utils
|
|
248
|
+
|
|
249
|
+
// calculator.mjs
|
|
250
|
+
export const add = (a: number, b: number): number => a + b;
|
|
251
|
+
export const multiply = (a: number, b: number): number => a * b;
|
|
252
|
+
|
|
253
|
+
// Only run main logic when executed directly: node calculator.mjs (or tsx calculator.mts)
|
|
254
|
+
// When imported elsewhere, only the functions are available
|
|
255
|
+
if (isDirectlyExecuted(import.meta.url)) {
|
|
256
|
+
console.log('Calculator CLI');
|
|
257
|
+
console.log('2 + 3 =', add(2, 3));
|
|
258
|
+
console.log('4 × 5 =', multiply(4, 5));
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
When executed directly (`node calculator.mjs`), it runs the main function and prints the results. When imported (`import { add } from './calculator.mjs'`), it only provides the functions without executing the main logic.
|
|
263
|
+
|
|
264
|
+
NOTE: If you use [tsx](https://www.npmjs.com/package/tsx) or [ts-node](https://www.npmjs.com/package/ts-node), run your scripts with the extension `.(m)ts` instead of `.(m)js` so that `isDirectlyExecuted` can correctly determine if the script is executed directly.
|
|
265
|
+
|
|
266
|
+
**Use Cases:**
|
|
267
|
+
|
|
268
|
+
- Creating CLI tools that can also be used as libraries
|
|
269
|
+
- Preventing automatic execution when a file is imported
|
|
270
|
+
- Running initialization code only during direct execution
|
|
271
|
+
|
|
239
272
|
### Path and File System Utilities
|
|
240
273
|
|
|
241
274
|
#### `pathExists(filePath: string): Promise<boolean>`
|
|
@@ -361,6 +394,39 @@ type Ret = Result<
|
|
|
361
394
|
>;
|
|
362
395
|
```
|
|
363
396
|
|
|
397
|
+
#### Build Optimization Utilities
|
|
398
|
+
|
|
399
|
+
##### `checkShouldRunTypeChecks(options?): Promise<boolean>`
|
|
400
|
+
|
|
401
|
+
Checks whether TypeScript type checks should run based on file changes from the base branch. Optimizes CI/CD pipelines by skipping type checks when only non-TypeScript files have changed.
|
|
402
|
+
(Function version of the `check-should-run-type-checks` command)
|
|
403
|
+
|
|
404
|
+
```typescript
|
|
405
|
+
import { checkShouldRunTypeChecks } from 'ts-repo-utils';
|
|
406
|
+
|
|
407
|
+
// Use default settings (compare against origin/main)
|
|
408
|
+
const shouldRun = await checkShouldRunTypeChecks();
|
|
409
|
+
|
|
410
|
+
if (shouldRun) {
|
|
411
|
+
await $('npm run type-check');
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// Custom ignore patterns and base branch
|
|
415
|
+
const shouldRun2 = await checkShouldRunTypeChecks({
|
|
416
|
+
pathsIgnore: ['.eslintrc.json', 'docs/', '**.md', 'scripts/'],
|
|
417
|
+
baseBranch: 'origin/develop',
|
|
418
|
+
});
|
|
419
|
+
```
|
|
420
|
+
|
|
421
|
+
**Options:**
|
|
422
|
+
|
|
423
|
+
- `pathsIgnore?` - Patterns to ignore when checking if type checks should run:
|
|
424
|
+
- Exact file matches: `.cspell.json`
|
|
425
|
+
- Directory prefixes: `docs/` (matches any file in docs directory)
|
|
426
|
+
- File extensions: `**.md` (matches any markdown file)
|
|
427
|
+
- Default: `['LICENSE', '.editorconfig', '.gitignore', '.cspell.json', '.markdownlint-cli2.mjs', '.npmignore', '.prettierignore', '.prettierrc', 'docs/', '**.md', '**.txt']`
|
|
428
|
+
- `baseBranch?` - Base branch to compare against (default: `origin/main`)
|
|
429
|
+
|
|
364
430
|
### Code Formatting Utilities
|
|
365
431
|
|
|
366
432
|
#### `formatFilesGlob(pathGlob: string, options?): Promise<Result<undefined, unknown>>`
|
|
@@ -668,6 +734,10 @@ const configJson: string = await fs.readFile('./config.json', {
|
|
|
668
734
|
});
|
|
669
735
|
|
|
670
736
|
const files: readonly string[] = await glob('**/*.ts');
|
|
737
|
+
|
|
738
|
+
if (isDirectlyExecuted(import.meta.url)) {
|
|
739
|
+
echo('Running as CLI');
|
|
740
|
+
}
|
|
671
741
|
```
|
|
672
742
|
|
|
673
743
|
- `$` - The command execution utility described above.
|
|
@@ -675,6 +745,7 @@ const files: readonly string[] = await glob('**/*.ts');
|
|
|
675
745
|
- `path` - `node:path`
|
|
676
746
|
- `fs` - `node:fs/promises`
|
|
677
747
|
- `glob` - `fast-glob`
|
|
748
|
+
- `isDirectlyExecuted` - The script execution utility described above.
|
|
678
749
|
|
|
679
750
|
## Common Patterns
|
|
680
751
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"should-run.d.mts","sourceRoot":"","sources":["../../src/functions/should-run.mts"],"names":[],"mappings":"AACA,OAAO,oBAAoB,CAAC;AAG5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,eAAO,MAAM,wBAAwB,GACnC,UAAU,QAAQ,CAAC;IACjB,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC,KACD,OAAO,CAAC,
|
|
1
|
+
{"version":3,"file":"should-run.d.mts","sourceRoot":"","sources":["../../src/functions/should-run.mts"],"names":[],"mappings":"AACA,OAAO,oBAAoB,CAAC;AAG5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,eAAO,MAAM,wBAAwB,GACnC,UAAU,QAAQ,CAAC;IACjB,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC,KACD,OAAO,CAAC,OAAO,CAqDjB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"should-run.mjs","sources":["../../src/functions/should-run.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDG;MACU,wBAAwB,GAAG,OACtC,OAGE,
|
|
1
|
+
{"version":3,"file":"should-run.mjs","sources":["../../src/functions/should-run.mts"],"sourcesContent":[null],"names":[],"mappings":";;;;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDG;MACU,wBAAwB,GAAG,OACtC,OAGE,KACkB;AACpB,IAAA,MAAM,WAAW,GAAG,OAAO,EAAE,WAAW,IAAI;QAC1C,SAAS;QACT,eAAe;QACf,YAAY;QACZ,cAAc;QACd,wBAAwB;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ist/node-global.d.mts
CHANGED
|
@@ -2,11 +2,13 @@ import { default as glob_ } from 'fast-glob';
|
|
|
2
2
|
import * as fs_ from 'node:fs/promises';
|
|
3
3
|
import * as path_ from 'node:path';
|
|
4
4
|
import { $ as $_ } from './functions/exec-async.mjs';
|
|
5
|
+
import { isDirectlyExecuted as isDirectlyExecuted_ } from './functions/is-directly-executed.mjs';
|
|
5
6
|
declare global {
|
|
6
7
|
const $: typeof $_;
|
|
7
8
|
const echo: typeof console.log;
|
|
8
9
|
const path: typeof path_;
|
|
9
10
|
const fs: typeof fs_;
|
|
10
11
|
const glob: typeof glob_;
|
|
12
|
+
const isDirectlyExecuted: typeof isDirectlyExecuted_;
|
|
11
13
|
}
|
|
12
14
|
//# sourceMappingURL=node-global.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"node-global.d.mts","sourceRoot":"","sources":["../src/node-global.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,KAAK,GAAG,MAAM,kBAAkB,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"node-global.d.mts","sourceRoot":"","sources":["../src/node-global.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,WAAW,CAAC;AAC7C,OAAO,KAAK,GAAG,MAAM,kBAAkB,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,MAAM,4BAA4B,CAAC;AACrD,OAAO,EAAE,kBAAkB,IAAI,mBAAmB,EAAE,MAAM,sCAAsC,CAAC;AAejG,OAAO,CAAC,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;IACnB,MAAM,IAAI,EAAE,OAAO,OAAO,CAAC,GAAG,CAAC;IAE/B,MAAM,IAAI,EAAE,OAAO,KAAK,CAAC;IACzB,MAAM,EAAE,EAAE,OAAO,GAAG,CAAC;IACrB,MAAM,IAAI,EAAE,OAAO,KAAK,CAAC;IACzB,MAAM,kBAAkB,EAAE,OAAO,mBAAmB,CAAC;CACtD"}
|
package/package.json
CHANGED
package/src/cmd/gen-index-ts.mts
CHANGED
|
@@ -61,7 +61,7 @@ export const checkShouldRunTypeChecks = async (
|
|
|
61
61
|
pathsIgnore?: readonly string[];
|
|
62
62
|
baseBranch?: string;
|
|
63
63
|
}>,
|
|
64
|
-
): Promise<
|
|
64
|
+
): Promise<boolean> => {
|
|
65
65
|
const pathsIgnore = options?.pathsIgnore ?? [
|
|
66
66
|
'LICENSE',
|
|
67
67
|
'.editorconfig',
|
|
@@ -112,4 +112,6 @@ export const checkShouldRunTypeChecks = async (
|
|
|
112
112
|
if (GITHUB_OUTPUT !== undefined) {
|
|
113
113
|
await fs.appendFile(GITHUB_OUTPUT, `should_run=${shouldRunTsChecks}\n`);
|
|
114
114
|
}
|
|
115
|
+
|
|
116
|
+
return shouldRunTsChecks;
|
|
115
117
|
};
|
package/src/node-global.mts
CHANGED