git-push-time 1.0.1 → 1.0.3

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/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "git-push-time",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "CLI tool to push project to Git with custom repo URL and custom commit timestamp",
5
5
  "main": "bin/index.js",
6
6
  "type": "module",
7
7
  "bin": {
8
- "githubcli": "./bin/index.js",
9
- "git-push-time": "./bin/index.js",
10
- "git-custom-push": "./bin/index.js"
8
+ "githubcli": "bin/index.js",
9
+ "git-push-time": "bin/index.js",
10
+ "git-custom-push": "bin/index.js"
11
11
  },
12
12
  "scripts": {
13
13
  "start": "node bin/index.js"
package/lib/npm.js DELETED
@@ -1,82 +0,0 @@
1
- import { spawnSync, execSync } from 'child_process';
2
- import fs from 'fs';
3
- import path from 'path';
4
-
5
- export function checkNpmInstalled() {
6
- try {
7
- execSync('npm --version', { stdio: 'ignore' });
8
- return true;
9
- } catch {
10
- return false;
11
- }
12
- }
13
-
14
- export function getPackageInfo(cwd = process.cwd()) {
15
- try {
16
- const raw = fs.readFileSync(path.join(cwd, 'package.json'), 'utf-8');
17
- const pkg = JSON.parse(raw);
18
- return { name: pkg.name || '(chưa đặt tên)', version: pkg.version || '0.0.0', hasPublishConfig: !!pkg.publishConfig };
19
- } catch {
20
- return null;
21
- }
22
- }
23
-
24
- /** Đang login npm chưa? (không in token ra) */
25
- export function checkNpmLogin(cwd = process.cwd()) {
26
- const r = spawnSync('npm', ['whoami'], { cwd, encoding: 'utf-8' });
27
- if (r.status === 0) return { loggedIn: true, user: r.stdout.trim() };
28
- return { loggedIn: false, user: null };
29
- }
30
-
31
- /**
32
- * Cảnh báo nếu .npmrc chứa token đang bị track trong git.
33
- * Chỉ kiểm tra tên file, KHÔNG đọc/in token.
34
- */
35
- export function isNpmrcTracked(cwd = process.cwd()) {
36
- try {
37
- const out = execSync('git ls-files', { cwd, encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] });
38
- return out.split('\n').map((s) => s.trim()).includes('.npmrc');
39
- } catch {
40
- return false;
41
- }
42
- }
43
-
44
- /**
45
- * Publish lên npm. Token lấy từ `npm login` hoặc env NPM_TOKEN,
46
- * KHÔNG nhận token qua flag để tránh lộ trong shell history.
47
- */
48
- export function publishToNpm({ access = 'public', dryRun = false, cwd = process.cwd() } = {}) {
49
- const args = ['publish'];
50
- if (access) args.push('--access', access);
51
- if (dryRun) args.push('--dry-run');
52
-
53
- const env = { ...process.env };
54
- // Ưu tiên NPM_TOKEN từ env nếu có (CI). Không log giá trị này.
55
- if (env.NPM_TOKEN && !env.NODE_AUTH_TOKEN) {
56
- env.NODE_AUTH_TOKEN = env.NPM_TOKEN;
57
- }
58
-
59
- const r = spawnSync('npm', args, { cwd, encoding: 'utf-8', env });
60
- const output = ((r.stdout || '') + '\n' + (r.stderr || '')).trim();
61
- if (r.status === 0) {
62
- return { ok: true, output };
63
- }
64
- return { ok: false, output, parsed: parseNpmError(output) };
65
- }
66
-
67
- export function parseNpmError(output = '') {
68
- const lower = output.toLowerCase();
69
- if (lower.includes('eneedauth') || lower.includes('need auth') || lower.includes('401') || lower.includes('you must be logged in')) {
70
- return { code: 'NEED_AUTH', hint: 'Chưa login npm. Chạy: npm login (hoặc set env NPM_TOKEN khi chạy CI)' };
71
- }
72
- if (lower.includes('e403') || lower.includes('403 forbidden')) {
73
- return { code: 'FORBIDDEN', hint: 'Không có quyền publish tên này (bị trùng tên hoặc chưa được add collaborator). Đổi name trong package.json hoặc xin quyền.' };
74
- }
75
- if (lower.includes('epublishconflict') || lower.includes('cannot publish over previously published')) {
76
- return { code: 'VERSION_EXISTS', hint: 'Version này đã tồn tại. Tăng version: npm version patch rồi publish lại.' };
77
- }
78
- if (lower.includes('e400') || lower.includes('invalid package name') || lower.includes('invalid name')) {
79
- return { code: 'BAD_NAME', hint: 'Tên package chưa hợp lệ trong package.json (chữ thường, không dấu cách).' };
80
- }
81
- return { code: 'UNKNOWN', hint: 'Xem log npm phía trên để biết chi tiết.' };
82
- }