@mxpicture/build-api 0.2.13 → 0.2.14

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.
@@ -0,0 +1,5 @@
1
+ import { SourceFile } from "../types/types.code.js";
2
+ export declare const formatCode: (...lines: string[]) => Promise<string>;
3
+ export declare const formatJson: (...lines: string[]) => Promise<string>;
4
+ export declare const formatJson2Spaces: (...lines: string[]) => Promise<string>;
5
+ export declare const createSourceFiles: (filePaths: string[]) => SourceFile[];
@@ -0,0 +1,21 @@
1
+ import ts from "typescript";
2
+ import { format } from "prettier";
3
+ export const formatCode = async (...lines) => format(lines.join("\n"), { parser: "typescript" });
4
+ export const formatJson = async (...lines) => format(lines.join("\n"), { parser: "json" });
5
+ export const formatJson2Spaces = async (...lines) => format(lines.join("\n"), { parser: "json", tabWidth: 2, useTabs: false });
6
+ export const createSourceFiles = (filePaths) => {
7
+ const program = ts.createProgram(filePaths, {
8
+ target: ts.ScriptTarget.ES2023,
9
+ module: ts.ModuleKind.Node20,
10
+ strict: true,
11
+ });
12
+ const checker = program.getTypeChecker();
13
+ const results = [];
14
+ for (const filePath of filePaths) {
15
+ const sourceFile = program.getSourceFile(filePath);
16
+ if (!sourceFile)
17
+ continue;
18
+ results.push({ sourceFile, filePath, checker });
19
+ }
20
+ return results;
21
+ };
@@ -0,0 +1 @@
1
+ export * from "./format.code.js";
@@ -0,0 +1,2 @@
1
+ // This file is auto-generated. Do not edit manually.
2
+ export * from "./format.code.js";
@@ -0,0 +1,33 @@
1
+ import { GitChangedResult, GitChangedContentsResult } from "../types/types.git.js";
2
+ export declare class GitChanges {
3
+ readonly rootDir: string;
4
+ protected _sinceCommit: string | null;
5
+ constructor(rootDir: string);
6
+ set sinceCommit(sinceCommit: string);
7
+ get sinceCommit(): string;
8
+ /**
9
+ * Checks if a specific file or directory has been changed since a given git commit.
10
+ * Includes both committed and uncommitted changes (staged + unstaged).
11
+ *
12
+ * @param target - Relative path to the file or directory to check.
13
+ * @param sinceCommit - The git commit hash (or ref like a tag/branch) to compare against.
14
+ * @returns A result object indicating whether changes were detected.
15
+ */
16
+ hasChanged(path: string): Promise<GitChangedResult>;
17
+ /**
18
+ * Reads the current content of all files that have changed since a given git commit
19
+ * within a specific file or directory scope.
20
+ * Includes both committed and uncommitted changes (staged + unstaged).
21
+ *
22
+ * Deleted files are excluded from the result since they no longer exist on disk.
23
+ *
24
+ * @param target - Relative path to the file or directory to check.
25
+ * @param sinceCommit - The git commit hash (or ref like a tag/branch) to compare against.
26
+ * @param cwd - The working directory of the git repository (defaults to process.cwd()).
27
+ * @returns A result object containing the current content of each changed file.
28
+ */
29
+ readChangedFiles(readContent?: boolean): Promise<GitChangedContentsResult>;
30
+ }
31
+ export declare class GitChangesCommit extends GitChanges {
32
+ constructor(rootDir: string, sinceCommit: string);
33
+ }
@@ -0,0 +1,85 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { existsSync } from "node:fs";
3
+ import { resolve } from "node:path";
4
+ import { verifiedGit } from "./git.util.js";
5
+ export class GitChanges {
6
+ rootDir;
7
+ _sinceCommit = null;
8
+ constructor(rootDir) {
9
+ this.rootDir = rootDir;
10
+ }
11
+ set sinceCommit(sinceCommit) {
12
+ this._sinceCommit = sinceCommit;
13
+ }
14
+ get sinceCommit() {
15
+ if (!this._sinceCommit)
16
+ throw new Error("SinceCommit missing");
17
+ return this._sinceCommit;
18
+ }
19
+ /**
20
+ * Checks if a specific file or directory has been changed since a given git commit.
21
+ * Includes both committed and uncommitted changes (staged + unstaged).
22
+ *
23
+ * @param target - Relative path to the file or directory to check.
24
+ * @param sinceCommit - The git commit hash (or ref like a tag/branch) to compare against.
25
+ * @returns A result object indicating whether changes were detected.
26
+ */
27
+ async hasChanged(path) {
28
+ const sinceCommit = this.sinceCommit;
29
+ const resolvedTarget = resolve(this.rootDir, path);
30
+ if (!existsSync(resolvedTarget)) {
31
+ throw new Error(`Target path does not exist: ${resolvedTarget}`);
32
+ }
33
+ const git = await verifiedGit(this.rootDir, sinceCommit);
34
+ // Compare commit to working directory (includes uncommitted changes)
35
+ const diff = await git.diffSummary([sinceCommit, "--", path]);
36
+ const changedFiles = diff.files.map((file) => file.file);
37
+ return {
38
+ path,
39
+ sinceCommit,
40
+ hasChanged: changedFiles.length > 0,
41
+ changedFiles,
42
+ };
43
+ }
44
+ /**
45
+ * Reads the current content of all files that have changed since a given git commit
46
+ * within a specific file or directory scope.
47
+ * Includes both committed and uncommitted changes (staged + unstaged).
48
+ *
49
+ * Deleted files are excluded from the result since they no longer exist on disk.
50
+ *
51
+ * @param target - Relative path to the file or directory to check.
52
+ * @param sinceCommit - The git commit hash (or ref like a tag/branch) to compare against.
53
+ * @param cwd - The working directory of the git repository (defaults to process.cwd()).
54
+ * @returns A result object containing the current content of each changed file.
55
+ */
56
+ async readChangedFiles(readContent) {
57
+ const sinceCommit = this.sinceCommit;
58
+ const git = await verifiedGit(this.rootDir, sinceCommit);
59
+ // Compare commit to working directory (includes uncommitted changes)
60
+ const diff = await git.diffSummary([sinceCommit, "--", this.rootDir]);
61
+ const files = [];
62
+ for (const entry of diff.files) {
63
+ const repoFilePath = entry.file;
64
+ const absFilePath = resolve(this.rootDir, repoFilePath);
65
+ // Skip deleted files — they no longer exist on disk
66
+ if (!existsSync(absFilePath)) {
67
+ continue;
68
+ }
69
+ // Read from the file system to capture uncommitted changes
70
+ const content = readContent ? await readFile(absFilePath, "utf-8") : null;
71
+ files.push({ absFilePath, content, repoFilePath });
72
+ }
73
+ return {
74
+ path: this.rootDir,
75
+ sinceCommit,
76
+ files,
77
+ };
78
+ }
79
+ }
80
+ export class GitChangesCommit extends GitChanges {
81
+ constructor(rootDir, sinceCommit) {
82
+ super(rootDir);
83
+ this.sinceCommit = sinceCommit;
84
+ }
85
+ }
@@ -0,0 +1,10 @@
1
+ import { SimpleGit } from "simple-git";
2
+ import { GitCommitHash } from "../types/types.git.js";
3
+ /**
4
+ * Creates a SimpleGit instance and verifies the cwd is a repo
5
+ * and the commit ref is valid.
6
+ */
7
+ export declare const verifiedGit: (rootDir: string, commitRef?: string) => Promise<SimpleGit>;
8
+ export declare const lastCommitHash: (rootDir: string) => Promise<string | null>;
9
+ export declare const commitHashs: (rootDir: string) => Promise<GitCommitHash[]>;
10
+ export declare const ensureCommitRef: (ref: string, fallback: string, rootDir: string) => Promise<string>;
@@ -0,0 +1,43 @@
1
+ import { simpleGit } from "simple-git";
2
+ const __entries = [];
3
+ /**
4
+ * Creates a SimpleGit instance and verifies the cwd is a repo
5
+ * and the commit ref is valid.
6
+ */
7
+ export const verifiedGit = async (rootDir, commitRef) => {
8
+ let found = __entries.find((entry) => entry.rootDir === rootDir && entry.commitRef === commitRef);
9
+ if (!found) {
10
+ found = await verifyGit(simpleGit(rootDir), rootDir, commitRef);
11
+ __entries.push(found);
12
+ }
13
+ return found.git;
14
+ };
15
+ const verifyGit = async (git, rootDir, commitRef) => {
16
+ if (!(await git.checkIsRepo()))
17
+ throw new Error(`Not a git repository: ${rootDir}`);
18
+ if (!commitRef)
19
+ return { git, rootDir, commitRef };
20
+ try {
21
+ await git.revparse(["--verify", commitRef]);
22
+ }
23
+ catch {
24
+ throw new Error(`Invalid git commit reference: "${commitRef}"`);
25
+ }
26
+ return { git, rootDir, commitRef };
27
+ };
28
+ export const lastCommitHash = async (rootDir) => (await (await verifiedGit(rootDir)).log()).latest?.hash ?? null;
29
+ export const commitHashs = async (rootDir) => (await (await verifiedGit(rootDir)).log()).all.map((a) => ({
30
+ date: a.date,
31
+ hash: a.hash,
32
+ diff: a.diff,
33
+ }));
34
+ export const ensureCommitRef = async (ref, fallback, rootDir) => {
35
+ try {
36
+ await verifiedGit(rootDir, ref);
37
+ return ref;
38
+ }
39
+ catch {
40
+ await verifiedGit(rootDir, fallback);
41
+ return fallback;
42
+ }
43
+ };
@@ -0,0 +1,2 @@
1
+ export * from "./GitChanges.js";
2
+ export * from "./git.util.js";
@@ -0,0 +1,3 @@
1
+ // This file is auto-generated. Do not edit manually.
2
+ export * from "./GitChanges.js";
3
+ export * from "./git.util.js";
@@ -1,7 +1,11 @@
1
1
  export * from "./types.barrel.js";
2
+ export * from "./types.changelog.js";
2
3
  export * from "./types.changeset.js";
3
4
  export * from "./types.cleanup.js";
5
+ export * from "./types.code.js";
4
6
  export * from "./types.deps.js";
7
+ export * from "./types.documents.js";
8
+ export * from "./types.git.js";
5
9
  export * from "./types.npm.js";
6
10
  export * from "./types.os.js";
7
11
  export * from "./types.package.js";
@@ -1,8 +1,12 @@
1
1
  // This file is auto-generated. Do not edit manually.
2
2
  export * from "./types.barrel.js";
3
+ export * from "./types.changelog.js";
3
4
  export * from "./types.changeset.js";
4
5
  export * from "./types.cleanup.js";
6
+ export * from "./types.code.js";
5
7
  export * from "./types.deps.js";
8
+ export * from "./types.documents.js";
9
+ export * from "./types.git.js";
6
10
  export * from "./types.npm.js";
7
11
  export * from "./types.os.js";
8
12
  export * from "./types.package.js";
@@ -0,0 +1,13 @@
1
+ import { DocumentData } from "./types.documents.js";
2
+ export declare enum ChangeWatchType {
3
+ changelog = "changelog",
4
+ gitHistory = "gitHistory"
5
+ }
6
+ export interface ChangeWatch {
7
+ pattern: string;
8
+ type: ChangeWatchType;
9
+ }
10
+ export type ChangeEventHandler<T extends DocumentData> = (paths: string[], watch: ChangeWatch) => Promise<T | null>;
11
+ export interface ChangeWatchHandler<T extends DocumentData> extends ChangeWatch {
12
+ handler: ChangeEventHandler<T>;
13
+ }
@@ -0,0 +1,5 @@
1
+ export var ChangeWatchType;
2
+ (function (ChangeWatchType) {
3
+ ChangeWatchType["changelog"] = "changelog";
4
+ ChangeWatchType["gitHistory"] = "gitHistory";
5
+ })(ChangeWatchType || (ChangeWatchType = {}));
@@ -0,0 +1,6 @@
1
+ import ts from "typescript";
2
+ export interface SourceFile {
3
+ filePath: string;
4
+ checker: ts.TypeChecker;
5
+ sourceFile: ts.SourceFile;
6
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export type DocumentFieldValue = any;
2
+ export type DocumentData = {
3
+ [field: string]: DocumentFieldValue;
4
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import type { DiffResult } from "simple-git";
2
+ export interface GitChangedResult {
3
+ path: string;
4
+ sinceCommit: string;
5
+ hasChanged: boolean;
6
+ changedFiles: string[];
7
+ }
8
+ export interface GitChangedContent {
9
+ absFilePath: string;
10
+ repoFilePath: string;
11
+ content: string | null;
12
+ }
13
+ export interface GitChangedContentsResult {
14
+ path: string;
15
+ sinceCommit: string;
16
+ files: GitChangedContent[];
17
+ }
18
+ export interface GitCommitHash {
19
+ hash: string;
20
+ date: string;
21
+ diff?: DiffResult;
22
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mxpicture/build-api",
3
- "version": "0.2.13",
3
+ "version": "0.2.14",
4
4
  "description": "Build utilities API",
5
5
  "type": "module",
6
6
  "author": "MXPicture",
@@ -15,6 +15,8 @@
15
15
  "./cleanup": "./dist/cleanup/index.js",
16
16
  "./common": "./dist/common/index.js",
17
17
  "./deps": "./dist/deps/index.js",
18
+ "./format": "./dist/format/index.js",
19
+ "./git": "./dist/git/index.js",
18
20
  "./logger": "./dist/logger/index.js",
19
21
  "./npmPublish": "./dist/npmPublish/index.js",
20
22
  "./osInfo": "./dist/osInfo/index.js",
@@ -42,7 +44,11 @@
42
44
  "typescript": "^5.9.3"
43
45
  },
44
46
  "dependencies": {
47
+ "@types/micromatch": "^4.0.10",
45
48
  "json5": "^2.2.3",
49
+ "micromatch": "^4.0.8",
50
+ "prettier": "^3.8.1",
51
+ "simple-git": "^3.32.3",
46
52
  "yaml": "^2.8.2",
47
53
  "zod": "^4.3.6"
48
54
  }