fix(build): modernize project configuration and tighten Node.js typing support
This commit is contained in:
@@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartgit',
|
||||
version: '3.3.1',
|
||||
version: '3.3.2',
|
||||
description: 'A smart wrapper for nodegit that simplifies Git operations in Node.js.'
|
||||
}
|
||||
|
||||
@@ -17,8 +17,8 @@ export class GitRepo {
|
||||
const dirArg = plugins.path.resolve(toArg);
|
||||
await plugins.isomorphicGit.clone({
|
||||
dir: toArg,
|
||||
fs: smartgitRefArg.envDeps.fs,
|
||||
http: smartgitRefArg.envDeps.http,
|
||||
fs: smartgitRefArg.fs,
|
||||
http: smartgitRefArg.http,
|
||||
url: fromArg,
|
||||
});
|
||||
return new GitRepo(smartgitRefArg, toArg);
|
||||
@@ -31,12 +31,12 @@ export class GitRepo {
|
||||
dirArg = plugins.path.resolve(dirArg);
|
||||
await plugins.isomorphicGit.init({
|
||||
dir: dirArg,
|
||||
fs: smartgitRefArg.envDeps.fs,
|
||||
fs: smartgitRefArg.fs,
|
||||
});
|
||||
return new GitRepo(smartgitRefArg, dirArg);
|
||||
}
|
||||
|
||||
public static async fromOpeningRepoDir(smartgitRefArg: Smartgit, dirArg: string) {
|
||||
public static async fromOpeningRepoDir(smartgitRefArg: Smartgit, dirArg: string): Promise<GitRepo> {
|
||||
dirArg = plugins.path.resolve(dirArg);
|
||||
return new GitRepo(smartgitRefArg, dirArg);
|
||||
}
|
||||
@@ -60,7 +60,7 @@ export class GitRepo {
|
||||
}[]
|
||||
> {
|
||||
const remotes = await plugins.isomorphicGit.listRemotes({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
});
|
||||
return remotes;
|
||||
@@ -78,7 +78,7 @@ export class GitRepo {
|
||||
if (existingRemote.url !== remoteUrlArg) {
|
||||
await plugins.isomorphicGit.deleteRemote({
|
||||
remote: remoteNameArg,
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
});
|
||||
} else {
|
||||
@@ -87,7 +87,7 @@ export class GitRepo {
|
||||
}
|
||||
await plugins.isomorphicGit.addRemote({
|
||||
remote: remoteNameArg,
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
url: remoteUrlArg,
|
||||
});
|
||||
}
|
||||
@@ -95,16 +95,16 @@ export class GitRepo {
|
||||
/**
|
||||
* gets the url for a specific remote
|
||||
*/
|
||||
public async getUrlForRemote(remoteName: string): Promise<string> {
|
||||
public async getUrlForRemote(remoteName: string): Promise<string | undefined> {
|
||||
const remotes = await this.listRemotes();
|
||||
const existingRemote = remotes.find((remoteArg) => remoteArg.remote === remoteName);
|
||||
return existingRemote?.url;
|
||||
}
|
||||
|
||||
public async pushBranchToRemote(branchName: string, remoteName: string) {
|
||||
public async pushBranchToRemote(branchName: string, remoteName: string): Promise<void> {
|
||||
await plugins.isomorphicGit.push({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
http: this.smartgitRef.envDeps.http,
|
||||
fs: this.smartgitRef.fs,
|
||||
http: this.smartgitRef.http,
|
||||
ref: branchName,
|
||||
remote: remoteName,
|
||||
});
|
||||
@@ -115,7 +115,7 @@ export class GitRepo {
|
||||
*/
|
||||
public async getUncommittedDiff(excludeFiles: string[] = []): Promise<string[]> {
|
||||
const statusMatrix = await plugins.isomorphicGit.statusMatrix({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
});
|
||||
|
||||
@@ -133,10 +133,10 @@ export class GitRepo {
|
||||
if (head !== 0 && workdir !== 0 && head !== workdir) {
|
||||
headContent = await plugins.isomorphicGit
|
||||
.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
oid: await plugins.isomorphicGit.resolveRef({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
ref: 'HEAD',
|
||||
}),
|
||||
@@ -144,7 +144,7 @@ export class GitRepo {
|
||||
})
|
||||
.then((result) => new TextDecoder().decode(result.blob));
|
||||
|
||||
workdirContent = await this.smartgitRef.envDeps.fs.promises.readFile(
|
||||
workdirContent = await this.smartgitRef.fs.promises.readFile(
|
||||
plugins.path.join(this.repoDir, filepath),
|
||||
'utf8'
|
||||
);
|
||||
@@ -152,7 +152,7 @@ export class GitRepo {
|
||||
|
||||
// Handle added files
|
||||
if (head === 0 && workdir !== 0) {
|
||||
workdirContent = await this.smartgitRef.envDeps.fs.promises.readFile(
|
||||
workdirContent = await this.smartgitRef.fs.promises.readFile(
|
||||
plugins.path.join(this.repoDir, filepath),
|
||||
'utf8'
|
||||
);
|
||||
@@ -162,10 +162,10 @@ export class GitRepo {
|
||||
try {
|
||||
headContent = await plugins.isomorphicGit
|
||||
.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
oid: await plugins.isomorphicGit.resolveRef({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
ref: 'HEAD',
|
||||
}),
|
||||
@@ -175,7 +175,7 @@ export class GitRepo {
|
||||
} catch (err) {
|
||||
// Check if this is a symlink false positive
|
||||
// Error: "was anticipated to be a tree but it is a blob" means parent path is a symlink
|
||||
if (err.message && err.message.includes('anticipated to be a tree but it is a blob')) {
|
||||
if (err instanceof Error && err.message.includes('anticipated to be a tree but it is a blob')) {
|
||||
// This file is inside a symlinked directory - skip it entirely
|
||||
continue;
|
||||
}
|
||||
@@ -187,10 +187,10 @@ export class GitRepo {
|
||||
if (head !== 0 && workdir === 0) {
|
||||
headContent = await plugins.isomorphicGit
|
||||
.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
oid: await plugins.isomorphicGit.resolveRef({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
ref: 'HEAD',
|
||||
}),
|
||||
@@ -225,23 +225,25 @@ export class GitRepo {
|
||||
{ date: string; version: string; message: string }[]
|
||||
> {
|
||||
const commits = await plugins.isomorphicGit.log({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
});
|
||||
|
||||
const results = [];
|
||||
const results: { date: string; version: string; message: string }[] = [];
|
||||
|
||||
for (const commit of commits) {
|
||||
let version = 'unknown';
|
||||
try {
|
||||
const packageJsonBlob = await plugins.isomorphicGit.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
fs: this.smartgitRef.fs,
|
||||
dir: this.repoDir,
|
||||
oid: commit.oid,
|
||||
filepath: 'package.json',
|
||||
});
|
||||
const packageJson = JSON.parse(new TextDecoder().decode(packageJsonBlob.blob));
|
||||
version = packageJson.version;
|
||||
const packageJson = JSON.parse(new TextDecoder().decode(packageJsonBlob.blob)) as {
|
||||
version?: string;
|
||||
};
|
||||
version = packageJson.version ?? 'unknown';
|
||||
} catch (error) {
|
||||
// If package.json does not exist or any error occurs, leave version as 'unknown'
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import * as plugins from './smartgit.plugins.js';
|
||||
import { GitRepo } from './smartgit.classes.gitrepo.js';
|
||||
|
||||
type TNodeFs = typeof import('node:fs');
|
||||
|
||||
interface IEnvDeps {
|
||||
fs: typeof import('fs') | null;
|
||||
http: any | null; // isomorphic-git http interface
|
||||
fs?: TNodeFs;
|
||||
http?: plugins.HttpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -13,8 +15,8 @@ interface IEnvDeps {
|
||||
export class Smartgit {
|
||||
public smartenvInstance = new plugins.smartenv.Smartenv();
|
||||
public envDeps: IEnvDeps = {
|
||||
fs: null,
|
||||
http: null,
|
||||
fs: undefined,
|
||||
http: undefined,
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -24,8 +26,8 @@ export class Smartgit {
|
||||
public async init(): Promise<void> {
|
||||
try {
|
||||
if (this.smartenvInstance.isNode) {
|
||||
this.envDeps.fs = await this.smartenvInstance.getSafeNodeModule('fs');
|
||||
this.envDeps.http = await this.smartenvInstance.getSafeNodeModule(
|
||||
this.envDeps.fs = await this.smartenvInstance.getSafeNodeModule<TNodeFs>('fs');
|
||||
this.envDeps.http = await this.smartenvInstance.getSafeNodeModule<plugins.HttpClient>(
|
||||
'isomorphic-git/http/node'
|
||||
);
|
||||
} else {
|
||||
@@ -42,6 +44,22 @@ export class Smartgit {
|
||||
}
|
||||
}
|
||||
|
||||
public get fs(): TNodeFs {
|
||||
const fs = this.envDeps.fs;
|
||||
if (!fs) {
|
||||
throw new Error('Smartgit must be initialized before use. Call init() first.');
|
||||
}
|
||||
return fs;
|
||||
}
|
||||
|
||||
public get http(): plugins.HttpClient {
|
||||
const http = this.envDeps.http;
|
||||
if (!http) {
|
||||
throw new Error('Smartgit must be initialized before use. Call init() first.');
|
||||
}
|
||||
return http;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a new GitRepo instance by cloning from a remote URL
|
||||
* @param fromUrlArg the URL to clone from
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// node native
|
||||
import * as path from 'path';
|
||||
import * as path from 'node:path';
|
||||
|
||||
export { path };
|
||||
|
||||
@@ -14,7 +14,7 @@ export { smartenv, smartfile, smartpath, smartpromise, smartstring, smarttime };
|
||||
|
||||
// third party
|
||||
import * as diff from 'diff';
|
||||
import isomorphicGit from 'isomorphic-git';
|
||||
import isomorphicGit, { type HttpClient } from 'isomorphic-git';
|
||||
import { minimatch } from 'minimatch';
|
||||
|
||||
export { diff, isomorphicGit, minimatch };
|
||||
export { diff, isomorphicGit, minimatch, type HttpClient };
|
||||
|
||||
Reference in New Issue
Block a user