@cyberalien/git-utils 0.0.1
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/lib/git/branch.d.ts +5 -0
- package/lib/git/branch.js +13 -0
- package/lib/git/clone.d.ts +6 -0
- package/lib/git/clone.js +21 -0
- package/lib/git/commit.d.ts +2 -0
- package/lib/git/commit.js +17 -0
- package/lib/git/exists.d.ts +5 -0
- package/lib/git/exists.js +12 -0
- package/lib/git/types.d.ts +6 -0
- package/lib/git/types.js +1 -0
- package/lib/git/updated.d.ts +2 -0
- package/lib/git/updated.js +10 -0
- package/lib/misc/exec.d.ts +10 -0
- package/lib/misc/exec.js +21 -0
- package/lib/misc/json.d.ts +9 -0
- package/lib/misc/json.js +16 -0
- package/lib/misc/version.d.ts +5 -0
- package/lib/misc/version.js +24 -0
- package/package.json +38 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { execAsync } from "../misc/exec.js";
|
|
2
|
+
/**
|
|
3
|
+
* Get current branch at target directory
|
|
4
|
+
*/
|
|
5
|
+
async function getCurrentGitBranch(target) {
|
|
6
|
+
try {
|
|
7
|
+
const { stdout } = await execAsync(`git rev-parse --abbrev-ref HEAD`, { cwd: target });
|
|
8
|
+
return stdout.trim();
|
|
9
|
+
} catch {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export { getCurrentGitBranch };
|
package/lib/git/clone.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { execAsync } from "../misc/exec.js";
|
|
2
|
+
import { doesGitRepoExist } from "./exists.js";
|
|
3
|
+
import { rm } from "node:fs/promises";
|
|
4
|
+
/**
|
|
5
|
+
* Clone Git repository to target directory
|
|
6
|
+
*/
|
|
7
|
+
async function cloneGitRepo(repo, target) {
|
|
8
|
+
try {
|
|
9
|
+
await rm(target, {
|
|
10
|
+
recursive: true,
|
|
11
|
+
force: true
|
|
12
|
+
});
|
|
13
|
+
} catch {}
|
|
14
|
+
try {
|
|
15
|
+
await execAsync(`git clone ${repo.shallow ? "--depth 1" : ""} --branch ${repo.branch} ${repo.repo} ${target}`);
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return doesGitRepoExist(target);
|
|
20
|
+
}
|
|
21
|
+
export { cloneGitRepo };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { execAsync } from "../misc/exec.js";
|
|
2
|
+
import { getCurrentGitBranch } from "./branch.js";
|
|
3
|
+
async function commitGitRepoChanges(target, message, push = true) {
|
|
4
|
+
try {
|
|
5
|
+
await execAsync(`git add -A`, { cwd: target });
|
|
6
|
+
await execAsync(`git commit -m "${message}"`, { cwd: target });
|
|
7
|
+
if (!(await execAsync(`git status`, { cwd: target })).stdout.includes("Your branch is ahead of")) {
|
|
8
|
+
console.error("Failed to commit changes");
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
if (push) await execAsync(`git push origin "${await getCurrentGitBranch(target)}"`, { cwd: target });
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
export { commitGitRepoChanges };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { lstat } from "node:fs/promises";
|
|
2
|
+
/**
|
|
3
|
+
* Check if Git repository exists in target directory
|
|
4
|
+
*/
|
|
5
|
+
async function doesGitRepoExist(target) {
|
|
6
|
+
try {
|
|
7
|
+
return (await lstat(`${target}/.git`)).isDirectory();
|
|
8
|
+
} catch {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export { doesGitRepoExist };
|
package/lib/git/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { execAsync } from "../misc/exec.js";
|
|
2
|
+
async function isGitRepoUpdated(target) {
|
|
3
|
+
try {
|
|
4
|
+
return !(await execAsync(`git status`, { cwd: target })).stdout.includes("nothing to commit, working tree clean");
|
|
5
|
+
} catch {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
export { isGitRepoUpdated };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ExecOptions } from "node:child_process";
|
|
2
|
+
interface ExecResult {
|
|
3
|
+
stdout: string;
|
|
4
|
+
stderr: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Exec as Promise
|
|
8
|
+
*/
|
|
9
|
+
declare function execAsync(cmd: string, options?: ExecOptions): Promise<ExecResult>;
|
|
10
|
+
export { ExecResult, execAsync };
|
package/lib/misc/exec.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { exec } from "node:child_process";
|
|
3
|
+
/**
|
|
4
|
+
* Exec as Promise
|
|
5
|
+
*/
|
|
6
|
+
function execAsync(cmd, options) {
|
|
7
|
+
return new Promise((fulfill, reject) => {
|
|
8
|
+
if (typeof options?.cwd === "string") options = {
|
|
9
|
+
...options,
|
|
10
|
+
cwd: resolve(options.cwd)
|
|
11
|
+
};
|
|
12
|
+
exec(cmd, options, (error, stdout, stderr) => {
|
|
13
|
+
if (error) reject(error);
|
|
14
|
+
else fulfill({
|
|
15
|
+
stdout,
|
|
16
|
+
stderr
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
export { execAsync };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read and parse JSON file
|
|
3
|
+
*/
|
|
4
|
+
declare function readJsonFile<T>(target: string, filename: string): Promise<any>;
|
|
5
|
+
/**
|
|
6
|
+
* Write JSON file
|
|
7
|
+
*/
|
|
8
|
+
declare function writeJsonFile<T>(target: string, filename: string, data: T): Promise<void>;
|
|
9
|
+
export { readJsonFile, writeJsonFile };
|
package/lib/misc/json.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { readFile, writeFile } from "node:fs/promises";
|
|
3
|
+
/**
|
|
4
|
+
* Read and parse JSON file
|
|
5
|
+
*/
|
|
6
|
+
async function readJsonFile(target, filename) {
|
|
7
|
+
const data = await readFile(join(target, filename), "utf-8");
|
|
8
|
+
return JSON.parse(data);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Write JSON file
|
|
12
|
+
*/
|
|
13
|
+
async function writeJsonFile(target, filename, data) {
|
|
14
|
+
await writeFile(join(target, filename), JSON.stringify(data, null, 2) + "\n", "utf-8");
|
|
15
|
+
}
|
|
16
|
+
export { readJsonFile, writeJsonFile };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { readJsonFile, writeJsonFile } from "./json.js";
|
|
2
|
+
function update(oldVersion, newVersion) {
|
|
3
|
+
if (typeof newVersion === "string") return newVersion;
|
|
4
|
+
if (newVersion) return newVersion(oldVersion);
|
|
5
|
+
const parts = oldVersion.split(".");
|
|
6
|
+
const lastPart = parseInt(parts.pop() || "");
|
|
7
|
+
if (isNaN(lastPart)) throw new Error(`Invalid version: ${oldVersion}`);
|
|
8
|
+
parts.push((lastPart + 1).toString());
|
|
9
|
+
return parts.join(".");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Bump version in package.json file, return new version
|
|
13
|
+
*/
|
|
14
|
+
async function bumpPackageVersion(target, newVersion) {
|
|
15
|
+
const content = await readJsonFile(target, "package.json");
|
|
16
|
+
const oldVersion = content.version;
|
|
17
|
+
const version = update(oldVersion, newVersion);
|
|
18
|
+
if (version !== oldVersion) {
|
|
19
|
+
content.version = version;
|
|
20
|
+
writeJsonFile(target, "package.json", content);
|
|
21
|
+
}
|
|
22
|
+
return version;
|
|
23
|
+
}
|
|
24
|
+
export { bumpPackageVersion };
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cyberalien/git-utils",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"description": "Functions for working with git repos.",
|
|
5
|
+
"author": "Vjacheslav Trushkin",
|
|
6
|
+
"version": "0.0.1",
|
|
7
|
+
"license": "UNLICENSED",
|
|
8
|
+
"bugs": "https://github.com/cyberalien/svg-utils/issues",
|
|
9
|
+
"homepage": "https://cyberalien.dev/",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/cyberalien/svg-utils.git"
|
|
13
|
+
},
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"main": "lib/index.js",
|
|
16
|
+
"module": "lib/index.js",
|
|
17
|
+
"types": "lib/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"lib",
|
|
20
|
+
"*.d.ts"
|
|
21
|
+
],
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@iconify/types": "^2.0.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^25.6.0",
|
|
27
|
+
"oxlint": "^1.60.0",
|
|
28
|
+
"tsdown": "^0.21.9",
|
|
29
|
+
"typescript": "^5.9.3",
|
|
30
|
+
"vitest": "^4.1.4"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"lint": "oxlint --fix src/**/*.ts && tsc --noemit",
|
|
34
|
+
"prebuild": "pnpm run lint",
|
|
35
|
+
"build": "tsdown",
|
|
36
|
+
"test": "vitest"
|
|
37
|
+
}
|
|
38
|
+
}
|