@warpgogol/repo-extract 1.1.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/LICENSE +201 -0
- package/README.md +155 -0
- package/dist/changelog.d.ts +9 -0
- package/dist/changelog.d.ts.map +1 -0
- package/dist/changelog.js +41 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +61 -0
- package/dist/config.d.ts +69 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +90 -0
- package/dist/errors.d.ts +16 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +39 -0
- package/dist/extract.d.ts +3 -0
- package/dist/extract.d.ts.map +1 -0
- package/dist/extract.js +407 -0
- package/dist/fs/copy.d.ts +16 -0
- package/dist/fs/copy.d.ts.map +1 -0
- package/dist/fs/copy.js +224 -0
- package/dist/fs/discover.d.ts +2 -0
- package/dist/fs/discover.d.ts.map +1 -0
- package/dist/fs/discover.js +126 -0
- package/dist/fs/git.d.ts +10 -0
- package/dist/fs/git.d.ts.map +1 -0
- package/dist/fs/git.js +252 -0
- package/dist/fs/transform.d.ts +16 -0
- package/dist/fs/transform.d.ts.map +1 -0
- package/dist/fs/transform.js +539 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/log.d.ts +6 -0
- package/dist/log.d.ts.map +1 -0
- package/dist/log.js +23 -0
- package/dist/package-manager.d.ts +4 -0
- package/dist/package-manager.d.ts.map +1 -0
- package/dist/package-manager.js +30 -0
- package/dist/postprocess/index.d.ts +4 -0
- package/dist/postprocess/index.d.ts.map +1 -0
- package/dist/postprocess/index.js +70 -0
- package/dist/scan.d.ts +4 -0
- package/dist/scan.d.ts.map +1 -0
- package/dist/scan.js +78 -0
- package/dist/types.d.ts +124 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +17 -0
- package/package.json +82 -0
package/dist/fs/copy.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>Filtered filesystem copy utilities for monorepo export.</purpose>
|
|
4
|
+
<non-goals>
|
|
5
|
+
<item>Does not transform package.json (see transform.ts).</item>
|
|
6
|
+
<item>Does not discover dependencies (see discover.ts).</item>
|
|
7
|
+
</non-goals>
|
|
8
|
+
</MODULE_CONTRACT>
|
|
9
|
+
<CHANGE_SUMMARY>
|
|
10
|
+
<item>Ported copyFiltered, copyStandalonePackage, removeDest from scripts/export-clients-helpers.ts for RFC-0070.</item>
|
|
11
|
+
<item>RFC-0071: Split IGNORE_DIRS into BASE_IGNORE_DIRS (org-neutral) and config-driven ignoreDirs. MONOREPO_CONFIG_FILES kept as auto-detect candidate list.</item>
|
|
12
|
+
<item>RFC-0072: Extended IGNORE_FILES with package-lock.json and yarn.lock. Added getMonorepoConfigFiles(pm) to filter pnpm-workspace.yaml for npm/yarn.</item>
|
|
13
|
+
<item>RFC-0075: Consolidated inline import in truncateCsv to top-level. Made isKeyMaterial/isDbFile configurable via excludePathSegments/excludeExtensions.</item>
|
|
14
|
+
</CHANGE_SUMMARY>
|
|
15
|
+
*/
|
|
16
|
+
import { cp, mkdir, readdir, rm, readFile, writeFile } from "node:fs/promises";
|
|
17
|
+
import * as fsSync from "node:fs";
|
|
18
|
+
import * as path from "node:path";
|
|
19
|
+
const BASE_IGNORE_DIRS = new Set([
|
|
20
|
+
"node_modules",
|
|
21
|
+
".turbo",
|
|
22
|
+
"dist",
|
|
23
|
+
".env",
|
|
24
|
+
".DS_Store",
|
|
25
|
+
"Thumbs.db",
|
|
26
|
+
]);
|
|
27
|
+
const IGNORE_FILES = new Set([
|
|
28
|
+
".env",
|
|
29
|
+
".env.local",
|
|
30
|
+
".env.production",
|
|
31
|
+
".env.production.local",
|
|
32
|
+
"pnpm-lock.yaml",
|
|
33
|
+
"package-lock.json",
|
|
34
|
+
"yarn.lock",
|
|
35
|
+
"changelog.config.yaml",
|
|
36
|
+
]);
|
|
37
|
+
const MONOREPO_CONFIG_FILES = [
|
|
38
|
+
"turbo.json",
|
|
39
|
+
"pnpm-workspace.yaml",
|
|
40
|
+
"tsconfig.base.json",
|
|
41
|
+
"tsconfig.app.json",
|
|
42
|
+
"eslint.config.mjs",
|
|
43
|
+
".prettierrc.mjs",
|
|
44
|
+
".prettierignore",
|
|
45
|
+
".markdownlint.json",
|
|
46
|
+
".nvmrc",
|
|
47
|
+
".dockerignore",
|
|
48
|
+
];
|
|
49
|
+
export function getMonorepoConfigFiles(pm) {
|
|
50
|
+
if (pm !== "pnpm") {
|
|
51
|
+
return MONOREPO_CONFIG_FILES.filter((f) => f !== "pnpm-workspace.yaml");
|
|
52
|
+
}
|
|
53
|
+
return [...MONOREPO_CONFIG_FILES];
|
|
54
|
+
}
|
|
55
|
+
export function isIgnored(name, extraIgnoreDirs) {
|
|
56
|
+
if (BASE_IGNORE_DIRS.has(name) || IGNORE_FILES.has(name) || name.endsWith(".tsbuildinfo")) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
if (extraIgnoreDirs && extraIgnoreDirs.includes(name)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
export function isKeyMaterial(relPath, excludePathSegments) {
|
|
65
|
+
const segments = excludePathSegments ?? [
|
|
66
|
+
".input" + path.sep + "signing-key",
|
|
67
|
+
".input" + path.sep + "batches",
|
|
68
|
+
];
|
|
69
|
+
return segments.some((seg) => relPath.includes(seg));
|
|
70
|
+
}
|
|
71
|
+
export function isDbFile(name, excludeExtensions) {
|
|
72
|
+
const extensions = excludeExtensions ?? [".db"];
|
|
73
|
+
return extensions.some((ext) => name.endsWith(ext));
|
|
74
|
+
}
|
|
75
|
+
export async function ensureDir(p) {
|
|
76
|
+
await mkdir(p, { recursive: true });
|
|
77
|
+
}
|
|
78
|
+
export async function removeDest(dest) {
|
|
79
|
+
let entries;
|
|
80
|
+
try {
|
|
81
|
+
entries = await readdir(dest, { withFileTypes: true });
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
for (const e of entries) {
|
|
87
|
+
if (e.name === ".git" || e.name === "LICENSE")
|
|
88
|
+
continue;
|
|
89
|
+
await rm(path.join(dest, e.name), { recursive: true, force: true });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export async function copyFile(src, dest) {
|
|
93
|
+
await ensureDir(path.dirname(dest));
|
|
94
|
+
await cp(src, dest);
|
|
95
|
+
}
|
|
96
|
+
export async function copyFiltered(srcRoot, destRoot, subDir = "", extraIgnoreDirs, excludePathSegments, excludeExtensions) {
|
|
97
|
+
const srcDir = path.join(srcRoot, subDir);
|
|
98
|
+
const destDir = path.join(destRoot, subDir);
|
|
99
|
+
let count = 0;
|
|
100
|
+
let entries;
|
|
101
|
+
try {
|
|
102
|
+
entries = await readdir(srcDir, { withFileTypes: true });
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
107
|
+
for (const entry of entries) {
|
|
108
|
+
const name = entry.name;
|
|
109
|
+
const relPath = subDir ? `${subDir}${path.sep}${name}` : name;
|
|
110
|
+
if (isIgnored(name, extraIgnoreDirs) ||
|
|
111
|
+
isKeyMaterial(relPath, excludePathSegments) ||
|
|
112
|
+
isDbFile(name, excludeExtensions))
|
|
113
|
+
continue;
|
|
114
|
+
const srcPath = path.join(srcDir, name);
|
|
115
|
+
const destPath = path.join(destDir, name);
|
|
116
|
+
if (entry.isDirectory()) {
|
|
117
|
+
count += await copyFiltered(srcRoot, destRoot, relPath, extraIgnoreDirs, excludePathSegments, excludeExtensions);
|
|
118
|
+
}
|
|
119
|
+
else if (entry.isFile()) {
|
|
120
|
+
await copyFile(srcPath, destPath);
|
|
121
|
+
count++;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return count;
|
|
125
|
+
}
|
|
126
|
+
export async function copyStandalonePackage(srcRoot, dest, packageDir, ignoreDirs, excludePathSegments, excludeExtensions) {
|
|
127
|
+
const srcDir = path.join(srcRoot, packageDir);
|
|
128
|
+
const skipNames = new Set(["node_modules", ".turbo"]);
|
|
129
|
+
if (ignoreDirs) {
|
|
130
|
+
for (const d of ignoreDirs)
|
|
131
|
+
skipNames.add(d);
|
|
132
|
+
}
|
|
133
|
+
let count = 0;
|
|
134
|
+
await cp(srcDir, dest, {
|
|
135
|
+
recursive: true,
|
|
136
|
+
filter: (src) => {
|
|
137
|
+
if (src === srcDir)
|
|
138
|
+
return true;
|
|
139
|
+
const name = path.basename(src);
|
|
140
|
+
if (skipNames.has(name) || name.endsWith(".tsbuildinfo"))
|
|
141
|
+
return false;
|
|
142
|
+
const relPath = path.relative(srcDir, src);
|
|
143
|
+
if (isKeyMaterial(relPath, excludePathSegments) || isDbFile(name, excludeExtensions))
|
|
144
|
+
return false;
|
|
145
|
+
try {
|
|
146
|
+
if (fsSync.statSync(src).isFile())
|
|
147
|
+
count++;
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
/* no-op */
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
return count;
|
|
156
|
+
}
|
|
157
|
+
export async function copyBatchSample(srcRoot, destRoot, htmlLimit, logger) {
|
|
158
|
+
let htmlCount = 0;
|
|
159
|
+
async function walk(dirRel) {
|
|
160
|
+
if (htmlCount >= htmlLimit)
|
|
161
|
+
return false;
|
|
162
|
+
const srcDir = path.join(srcRoot, dirRel);
|
|
163
|
+
const destDir = path.join(destRoot, dirRel);
|
|
164
|
+
let entries;
|
|
165
|
+
try {
|
|
166
|
+
entries = await readdir(srcDir, { withFileTypes: true });
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
const subdirs = [];
|
|
172
|
+
for (const entry of entries) {
|
|
173
|
+
const name = entry.name;
|
|
174
|
+
if (name === "." || name === "..")
|
|
175
|
+
continue;
|
|
176
|
+
const relPath = dirRel ? `${dirRel}/${name}` : name;
|
|
177
|
+
const srcPath = path.join(srcDir, name);
|
|
178
|
+
const destPath = path.join(destDir, name);
|
|
179
|
+
if (entry.isDirectory()) {
|
|
180
|
+
subdirs.push(relPath);
|
|
181
|
+
}
|
|
182
|
+
else if (entry.isFile()) {
|
|
183
|
+
const isHtml = /\.html?$/i.test(name);
|
|
184
|
+
const isCsv = /\.csv$/i.test(name);
|
|
185
|
+
if (isHtml && htmlCount >= htmlLimit)
|
|
186
|
+
continue;
|
|
187
|
+
await ensureDir(path.dirname(destPath));
|
|
188
|
+
if (isHtml) {
|
|
189
|
+
htmlCount++;
|
|
190
|
+
await cp(srcPath, destPath);
|
|
191
|
+
}
|
|
192
|
+
else if (isCsv) {
|
|
193
|
+
await truncateCsv(srcPath, destPath, CSV_LIMIT);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
await cp(srcPath, destPath);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
for (const sd of subdirs) {
|
|
201
|
+
if (htmlCount >= htmlLimit)
|
|
202
|
+
break;
|
|
203
|
+
if (!(await walk(sd)))
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
await walk("");
|
|
209
|
+
const files = htmlCount <= htmlLimit ? htmlCount : htmlLimit;
|
|
210
|
+
(logger?.log ?? console.log)(` batch HTML files sampled: ${files}`);
|
|
211
|
+
}
|
|
212
|
+
const CSV_LIMIT = 50;
|
|
213
|
+
export async function truncateCsv(srcPath, destPath, csvLimit) {
|
|
214
|
+
const content = await readFile(srcPath, "utf-8");
|
|
215
|
+
const lines = content.split("\n");
|
|
216
|
+
const header = lines[0] || "";
|
|
217
|
+
const dataLines = lines.slice(1).filter((l) => l.trim().length > 0);
|
|
218
|
+
const sampled = dataLines.slice(0, csvLimit);
|
|
219
|
+
let result = [header, ...sampled].join("\n");
|
|
220
|
+
if (!result.endsWith("\n"))
|
|
221
|
+
result += "\n";
|
|
222
|
+
await writeFile(destPath, result, "utf-8");
|
|
223
|
+
}
|
|
224
|
+
export { MONOREPO_CONFIG_FILES };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discover.d.ts","sourceRoot":"","sources":["../../src/fs/discover.ts"],"names":[],"mappings":"AAkBA,wBAAsB,mBAAmB,CACvC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EAAE,EACjB,iBAAiB,GAAE,MAAM,EAAO,GAC/B,OAAO,CAAC,MAAM,EAAE,CAAC,CAyCnB"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>Auto-discovery of transitive workspace package dependencies.</purpose>
|
|
4
|
+
<non-goals>
|
|
5
|
+
<item>Does not copy or transform files (see copy.ts, transform.ts).</item>
|
|
6
|
+
</non-goals>
|
|
7
|
+
</MODULE_CONTRACT>
|
|
8
|
+
<CHANGE_SUMMARY>
|
|
9
|
+
<item>Ported discoverPackageDeps from scripts/export-clients-helpers.ts for RFC-0070. Supports both @syrokomskyi/ and @warpgogol/ prefixes.</item>
|
|
10
|
+
<item>RFC-0071: Default workspacePrefixes changed from ["@syrokomskyi/", "@warpgogol/"] to [] (empty). Callers must pass explicit prefixes.
|
|
11
|
+
</CHANGE_SUMMARY>
|
|
12
|
+
*/
|
|
13
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
14
|
+
import * as path from "node:path";
|
|
15
|
+
import { isIgnored } from "./copy.js";
|
|
16
|
+
export async function discoverPackageDeps(root, appDirs, workspacePrefixes = []) {
|
|
17
|
+
const visited = new Set();
|
|
18
|
+
const result = new Set();
|
|
19
|
+
async function resolvePkg(pkgName) {
|
|
20
|
+
if (visited.has(pkgName))
|
|
21
|
+
return;
|
|
22
|
+
visited.add(pkgName);
|
|
23
|
+
const pkgDir = await findPackageDir(root, pkgName);
|
|
24
|
+
if (!pkgDir)
|
|
25
|
+
return;
|
|
26
|
+
const relDir = path.relative(root, pkgDir).replace(/\\/g, "/");
|
|
27
|
+
result.add(relDir);
|
|
28
|
+
const pkgJsonPath = path.join(pkgDir, "package.json");
|
|
29
|
+
try {
|
|
30
|
+
const raw = await readFile(pkgJsonPath, "utf-8");
|
|
31
|
+
const pkg = JSON.parse(raw);
|
|
32
|
+
const allDeps = {
|
|
33
|
+
...pkg.dependencies,
|
|
34
|
+
...pkg.devDependencies,
|
|
35
|
+
...pkg.peerDependencies,
|
|
36
|
+
};
|
|
37
|
+
for (const depName of Object.keys(allDeps)) {
|
|
38
|
+
for (const prefix of workspacePrefixes) {
|
|
39
|
+
if (depName.startsWith(prefix)) {
|
|
40
|
+
await resolvePkg(depName);
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
/* no-op */
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
for (const appDir of appDirs) {
|
|
51
|
+
await resolveAppDeps(root, appDir, resolvePkg, workspacePrefixes);
|
|
52
|
+
}
|
|
53
|
+
return Array.from(result).sort();
|
|
54
|
+
}
|
|
55
|
+
async function resolveAppDeps(root, appDir, resolvePkg, workspacePrefixes) {
|
|
56
|
+
const appPkgPath = path.join(root, appDir, "package.json");
|
|
57
|
+
try {
|
|
58
|
+
const raw = await readFile(appPkgPath, "utf-8");
|
|
59
|
+
const pkg = JSON.parse(raw);
|
|
60
|
+
const allDeps = { ...pkg.dependencies, ...pkg.devDependencies, ...pkg.peerDependencies };
|
|
61
|
+
for (const depName of Object.keys(allDeps)) {
|
|
62
|
+
for (const prefix of workspacePrefixes) {
|
|
63
|
+
if (depName.startsWith(prefix)) {
|
|
64
|
+
await resolvePkg(depName);
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
/* no-op */
|
|
72
|
+
}
|
|
73
|
+
const fullDir = path.join(root, appDir);
|
|
74
|
+
try {
|
|
75
|
+
const entries = await readdir(fullDir, { withFileTypes: true });
|
|
76
|
+
for (const entry of entries) {
|
|
77
|
+
if (entry.isDirectory() && !isIgnored(entry.name)) {
|
|
78
|
+
const subDir = path.join(appDir, entry.name);
|
|
79
|
+
const subPkg = path.join(fullDir, entry.name, "package.json");
|
|
80
|
+
try {
|
|
81
|
+
await readFile(subPkg, "utf-8");
|
|
82
|
+
await resolveAppDeps(root, subDir, resolvePkg, workspacePrefixes);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
/* no package.json in this subdir, skip */
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
/* no-op */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async function findPackageDir(root, pkgName) {
|
|
95
|
+
const packagesDir = path.join(root, "packages");
|
|
96
|
+
async function searchDir(dir) {
|
|
97
|
+
let entries;
|
|
98
|
+
try {
|
|
99
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
for (const entry of entries) {
|
|
105
|
+
if (!entry.isDirectory() || isIgnored(entry.name))
|
|
106
|
+
continue;
|
|
107
|
+
const subDir = path.join(dir, entry.name);
|
|
108
|
+
const pkgJsonPath = path.join(subDir, "package.json");
|
|
109
|
+
try {
|
|
110
|
+
const raw = await readFile(pkgJsonPath, "utf-8");
|
|
111
|
+
const pkg = JSON.parse(raw);
|
|
112
|
+
if (pkg.name === pkgName) {
|
|
113
|
+
return subDir;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
/* no package.json here — recurse into subdirectory */
|
|
118
|
+
}
|
|
119
|
+
const found = await searchDir(subDir);
|
|
120
|
+
if (found)
|
|
121
|
+
return found;
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
return searchDir(packagesDir);
|
|
126
|
+
}
|
package/dist/fs/git.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { GitConfig } from "../types.js";
|
|
2
|
+
import type { Logger } from "../log.js";
|
|
3
|
+
export declare function commitExport(dest: string, commitMessage: string, gitConfig?: GitConfig, logger?: Logger): Promise<{
|
|
4
|
+
committed: boolean;
|
|
5
|
+
pushed: boolean;
|
|
6
|
+
}>;
|
|
7
|
+
export declare function getLastExportDate(dest: string): string | null;
|
|
8
|
+
export declare function transferGitHistory(root: string, dest: string, pathPrefixes: string[], logger?: Logger): Promise<boolean>;
|
|
9
|
+
export declare function diffSummary(dest: string): string | null;
|
|
10
|
+
//# sourceMappingURL=git.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"git.d.ts","sourceRoot":"","sources":["../../src/fs/git.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAGxC,wBAAsB,YAAY,CAChC,IAAI,EAAE,MAAM,EACZ,aAAa,EAAE,MAAM,EACrB,SAAS,CAAC,EAAE,SAAS,EACrB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC;IAAE,SAAS,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,CAAC,CAsGlD;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAW7D;AAED,wBAAsB,kBAAkB,CACtC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,EAAE,EACtB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,OAAO,CAAC,CAsGlB;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAoCvD"}
|
package/dist/fs/git.js
ADDED
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
/*
|
|
2
|
+
<MODULE_CONTRACT>
|
|
3
|
+
<purpose>Git init, commit, and push helpers for exported repos.</purpose>
|
|
4
|
+
<non-goals>
|
|
5
|
+
<item>Does not implement AI commit message generation (see changelog.ts).</item>
|
|
6
|
+
</non-goals>
|
|
7
|
+
</MODULE_CONTRACT>
|
|
8
|
+
<CHANGE_SUMMARY>
|
|
9
|
+
<item>Initial git helpers for RFC-0070. Ported commitExport from scripts/export-clients.ts.</item>
|
|
10
|
+
<item>RFC-0073: replaced all execSync with execFileSync (argument arrays) to eliminate shell injection.</item>
|
|
11
|
+
<item>RFC-0074: Wrapped execFileSync calls with GitOperationError. Changed commitExport return to { committed, pushed }.</item>
|
|
12
|
+
<item>RFC-0075: Added Windows multi-prefix guard in transferGitHistory().</item>
|
|
13
|
+
</CHANGE_SUMMARY>
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, rmSync } from "node:fs";
|
|
16
|
+
import { cp, mkdtemp, rm } from "node:fs/promises";
|
|
17
|
+
import * as path from "node:path";
|
|
18
|
+
import * as os from "node:os";
|
|
19
|
+
import { execFileSync } from "node:child_process";
|
|
20
|
+
import { GitOperationError } from "../errors.js";
|
|
21
|
+
export async function commitExport(dest, commitMessage, gitConfig, logger) {
|
|
22
|
+
const log = logger?.log ?? console.log;
|
|
23
|
+
const error = logger?.error ?? console.error;
|
|
24
|
+
log("");
|
|
25
|
+
log("Committing changes in destination repository...");
|
|
26
|
+
let committed = false;
|
|
27
|
+
let pushed = false;
|
|
28
|
+
try {
|
|
29
|
+
const gitDir = path.join(dest, ".git");
|
|
30
|
+
if (!existsSync(gitDir)) {
|
|
31
|
+
log(" initializing git repository...");
|
|
32
|
+
try {
|
|
33
|
+
execFileSync("git", ["init", "-b", "main"], { cwd: dest, stdio: "pipe" });
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
throw new GitOperationError("init", (err instanceof Error ? err.message : String(err)).slice(0, 200), err instanceof Error ? err : undefined);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const branch = execFileSync("git", ["branch", "--show-current"], { cwd: dest, stdio: "pipe" })
|
|
41
|
+
.toString()
|
|
42
|
+
.trim();
|
|
43
|
+
if (branch && branch !== "main") {
|
|
44
|
+
execFileSync("git", ["branch", "-m", "main"], { cwd: dest, stdio: "pipe" });
|
|
45
|
+
log(` renamed branch: ${branch} → main`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (gitConfig?.remote) {
|
|
49
|
+
try {
|
|
50
|
+
const remoteUrl = execFileSync("git", ["remote", "get-url", "origin"], {
|
|
51
|
+
cwd: dest,
|
|
52
|
+
stdio: "pipe",
|
|
53
|
+
})
|
|
54
|
+
.toString()
|
|
55
|
+
.trim();
|
|
56
|
+
if (remoteUrl !== gitConfig.remote) {
|
|
57
|
+
execFileSync("git", ["remote", "set-url", "origin", gitConfig.remote], {
|
|
58
|
+
cwd: dest,
|
|
59
|
+
stdio: "pipe",
|
|
60
|
+
});
|
|
61
|
+
log(` updated origin remote: ${gitConfig.remote}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
execFileSync("git", ["remote", "add", "origin", gitConfig.remote], {
|
|
66
|
+
cwd: dest,
|
|
67
|
+
stdio: "pipe",
|
|
68
|
+
});
|
|
69
|
+
log(` added origin remote: ${gitConfig.remote}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
execFileSync("git", ["add", "-A"], { cwd: dest, stdio: "pipe" });
|
|
73
|
+
const status = execFileSync("git", ["status", "--porcelain"], { cwd: dest, stdio: "pipe" })
|
|
74
|
+
.toString()
|
|
75
|
+
.trim();
|
|
76
|
+
if (status) {
|
|
77
|
+
execFileSync("git", ["commit", "-m", commitMessage], { cwd: dest, stdio: "inherit" });
|
|
78
|
+
log(` committed: ${commitMessage}`);
|
|
79
|
+
committed = true;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
log(" nothing to commit");
|
|
83
|
+
}
|
|
84
|
+
if (gitConfig?.remote && gitConfig.autoPush) {
|
|
85
|
+
log("Pushing to origin...");
|
|
86
|
+
try {
|
|
87
|
+
const branch = execFileSync("git", ["branch", "--show-current"], {
|
|
88
|
+
cwd: dest,
|
|
89
|
+
stdio: "pipe",
|
|
90
|
+
})
|
|
91
|
+
.toString()
|
|
92
|
+
.trim();
|
|
93
|
+
if (!branch) {
|
|
94
|
+
log(" skipped (no branch to push)");
|
|
95
|
+
return { committed, pushed };
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
execFileSync("git", ["push", "-u", "origin", branch], { cwd: dest, stdio: "inherit" });
|
|
99
|
+
log(` pushed to origin/${branch}`);
|
|
100
|
+
pushed = true;
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
log(" regular push rejected, retrying with --force...");
|
|
104
|
+
execFileSync("git", ["push", "-u", "--force", "origin", branch], {
|
|
105
|
+
cwd: dest,
|
|
106
|
+
stdio: "inherit",
|
|
107
|
+
});
|
|
108
|
+
log(` force-pushed to origin/${branch}`);
|
|
109
|
+
pushed = true;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (pushErr) {
|
|
113
|
+
error(` push failed: ${pushErr instanceof Error ? pushErr.message : pushErr}`);
|
|
114
|
+
error(" commit is saved locally — push manually when SSH is available");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
if (err instanceof GitOperationError)
|
|
120
|
+
throw err;
|
|
121
|
+
log(" git commit skipped (not a git repository or no changes)");
|
|
122
|
+
}
|
|
123
|
+
return { committed, pushed };
|
|
124
|
+
}
|
|
125
|
+
export function getLastExportDate(dest) {
|
|
126
|
+
try {
|
|
127
|
+
const dateStr = execFileSync("git", ["log", "-1", "--format=%ad", "--date=format:%Y-%m-%d"], {
|
|
128
|
+
cwd: dest,
|
|
129
|
+
encoding: "utf-8",
|
|
130
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
131
|
+
}).trim();
|
|
132
|
+
return dateStr || null;
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export async function transferGitHistory(root, dest, pathPrefixes, logger) {
|
|
139
|
+
const log = logger?.log ?? console.log;
|
|
140
|
+
try {
|
|
141
|
+
execFileSync("git", ["rev-parse", "--git-dir"], { cwd: root, stdio: "pipe" });
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
log(" source is not a git repository, skipping history transfer");
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
log(`Transferring git history for paths: ${pathPrefixes.join(", ")}...`);
|
|
148
|
+
if (process.platform === "win32" && pathPrefixes.length > 1) {
|
|
149
|
+
throw new Error("Multi-prefix git history transfer is not supported on Windows. " +
|
|
150
|
+
"Use WSL or Git Bash, or export with a single path prefix.");
|
|
151
|
+
}
|
|
152
|
+
const tmpDir = await mkdtemp(path.join(os.tmpdir(), "repo-extract-history-"));
|
|
153
|
+
try {
|
|
154
|
+
try {
|
|
155
|
+
execFileSync("git", ["clone", "--no-hardlinks", root, tmpDir], {
|
|
156
|
+
stdio: "pipe",
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
throw new GitOperationError("clone", (err instanceof Error ? err.message : String(err)).slice(0, 200), err instanceof Error ? err : undefined);
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
execFileSync("git", ["reset", "--hard"], { cwd: tmpDir, stdio: "pipe" });
|
|
164
|
+
}
|
|
165
|
+
catch (err) {
|
|
166
|
+
throw new GitOperationError("reset", (err instanceof Error ? err.message : String(err)).slice(0, 200), err instanceof Error ? err : undefined);
|
|
167
|
+
}
|
|
168
|
+
const env = { ...process.env, FILTER_BRANCH_SQUELCH_WARNING: "1" };
|
|
169
|
+
if (pathPrefixes.length === 1) {
|
|
170
|
+
try {
|
|
171
|
+
execFileSync("git", [
|
|
172
|
+
"filter-branch",
|
|
173
|
+
"-f",
|
|
174
|
+
"--prune-empty",
|
|
175
|
+
"--subdirectory-filter",
|
|
176
|
+
pathPrefixes[0],
|
|
177
|
+
"--",
|
|
178
|
+
"--all",
|
|
179
|
+
], { cwd: tmpDir, stdio: "pipe", env });
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
throw new GitOperationError("filter-branch", (err instanceof Error ? err.message : String(err)).slice(0, 200), err instanceof Error ? err : undefined);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
const keepPattern = pathPrefixes
|
|
187
|
+
.map((p) => `^${p.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}/`)
|
|
188
|
+
.join("|");
|
|
189
|
+
const indexFilter = `git ls-files | grep -vE '${keepPattern.replace(/'/g, "'\\''")}' | xargs -r git rm -r --cached --ignore-unmatch --quiet --`;
|
|
190
|
+
try {
|
|
191
|
+
execFileSync("git", ["filter-branch", "-f", "--prune-empty", "--index-filter", indexFilter, "--", "--all"], { cwd: tmpDir, stdio: "pipe", env });
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
throw new GitOperationError("filter-branch", (err instanceof Error ? err.message : String(err)).slice(0, 200), err instanceof Error ? err : undefined);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const destGit = path.join(dest, ".git");
|
|
198
|
+
if (existsSync(destGit)) {
|
|
199
|
+
rmSync(destGit, { recursive: true, force: true });
|
|
200
|
+
}
|
|
201
|
+
await cp(path.join(tmpDir, ".git"), destGit, { recursive: true });
|
|
202
|
+
log(" git history transferred");
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
catch (err) {
|
|
206
|
+
log(" git history transfer failed, continuing with fresh repo");
|
|
207
|
+
log(` ${(err instanceof Error ? err.message : String(err)).slice(0, 200)}`);
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
finally {
|
|
211
|
+
await rm(tmpDir, { recursive: true, force: true }).catch(() => { });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
export function diffSummary(dest) {
|
|
215
|
+
try {
|
|
216
|
+
const nameStatus = execFileSync("git", ["diff", "--cached", "--name-status"], {
|
|
217
|
+
cwd: dest,
|
|
218
|
+
stdio: "pipe",
|
|
219
|
+
})
|
|
220
|
+
.toString()
|
|
221
|
+
.trim();
|
|
222
|
+
if (!nameStatus)
|
|
223
|
+
return null;
|
|
224
|
+
const lines = nameStatus.split("\n");
|
|
225
|
+
const added = lines.filter((l) => l.startsWith("A")).length;
|
|
226
|
+
const modified = lines.filter((l) => l.startsWith("M")).length;
|
|
227
|
+
const deleted = lines.filter((l) => l.startsWith("D")).length;
|
|
228
|
+
const renamed = lines.filter((l) => l.startsWith("R")).length;
|
|
229
|
+
const paths = lines.slice(0, 20).map((l) => {
|
|
230
|
+
const parts = l.split("\t");
|
|
231
|
+
const filePath = parts[parts.length - 1];
|
|
232
|
+
const segments = filePath.split("/");
|
|
233
|
+
return segments.slice(0, 3).join("/");
|
|
234
|
+
});
|
|
235
|
+
const uniquePaths = [...new Set(paths)].slice(0, 5);
|
|
236
|
+
const parts = [];
|
|
237
|
+
if (added)
|
|
238
|
+
parts.push(`+${added}`);
|
|
239
|
+
if (modified)
|
|
240
|
+
parts.push(`~${modified}`);
|
|
241
|
+
if (deleted)
|
|
242
|
+
parts.push(`-${deleted}`);
|
|
243
|
+
if (renamed)
|
|
244
|
+
parts.push(`→${renamed}`);
|
|
245
|
+
const statPart = parts.join(" ");
|
|
246
|
+
const pathPart = uniquePaths.join(", ");
|
|
247
|
+
return pathPart ? `${statPart} files (${pathPart})` : `${statPart} files`;
|
|
248
|
+
}
|
|
249
|
+
catch {
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ExtractConfig, PackageManager, CiConfig } from "../types.js";
|
|
2
|
+
import type { Logger } from "../log.js";
|
|
3
|
+
export declare function transformPackageJson(dest: string, relPath: string, config: ExtractConfig, logger?: Logger): Promise<void>;
|
|
4
|
+
export declare function fixPackageTsconfigs(dest: string, packageDirs: string[]): Promise<void>;
|
|
5
|
+
export declare function fixAppTsconfigs(dest: string, appTsconfigPaths: string[]): Promise<void>;
|
|
6
|
+
export declare function regenerateTsconfigBase(dest: string, config: ExtractConfig, logger?: Logger): Promise<void>;
|
|
7
|
+
export declare function findAppTsconfigs(dest: string, appDirs: string[]): Promise<string[]>;
|
|
8
|
+
export declare function findPackageJsonFiles(dest: string, dirs: string[]): Promise<string[]>;
|
|
9
|
+
export declare function buildGitignore(extraLines?: string[], config?: ExtractConfig): string;
|
|
10
|
+
export declare function buildRootPackageJson(destName: string, config: ExtractConfig, pm: PackageManager, root?: string): object;
|
|
11
|
+
export declare function generateRootFiles(dest: string, root: string, config: ExtractConfig, packageDirs: string[], appDirs: string[], pm: PackageManager, logger?: Logger): Promise<void>;
|
|
12
|
+
export declare function cleanStrayArtifacts(dest: string): Promise<void>;
|
|
13
|
+
export declare function generateCiWorkflow(pm: PackageManager, ci?: CiConfig): string | null;
|
|
14
|
+
export declare function fixStandalonePackageJson(dest: string, config: ExtractConfig, pm: PackageManager, root?: string, logger?: Logger): Promise<void>;
|
|
15
|
+
export declare function fixStandaloneVitestConfig(dest: string, logger?: Logger): Promise<void>;
|
|
16
|
+
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/fs/transform.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAGxC,wBAAsB,oBAAoB,CACxC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,aAAa,EACrB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC,CA2Df;AAED,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA0D5F;AAED,wBAAsB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAY7F;AAED,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,aAAa,EACrB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC,CA8Bf;AAED,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAOzF;AAmBD,wBAAsB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAO1F;AAmBD,wBAAgB,cAAc,CAAC,UAAU,GAAE,MAAM,EAAO,EAAE,MAAM,CAAC,EAAE,aAAa,GAAG,MAAM,CA4BxF;AAoCD,wBAAgB,oBAAoB,CAClC,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,cAAc,EAClB,IAAI,CAAC,EAAE,MAAM,GACZ,MAAM,CAuBR;AAED,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,aAAa,EACrB,WAAW,EAAE,MAAM,EAAE,EACrB,OAAO,EAAE,MAAM,EAAE,EACjB,EAAE,EAAE,cAAc,EAClB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC,CAoFf;AAED,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA8BrE;AAED,wBAAgB,kBAAkB,CAAC,EAAE,EAAE,cAAc,EAAE,EAAE,CAAC,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,CA+DnF;AAED,wBAAsB,wBAAwB,CAC5C,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,cAAc,EAClB,IAAI,CAAC,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC,CAiEf;AAED,wBAAsB,yBAAyB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAe5F"}
|