|
9 | 9 | * `pgpm import` all emit byte-identical module layouts instead of each |
10 | 10 | * carrying its own copy of the writer. |
11 | 11 | */ |
12 | | -import { PgpmRow, SqlWriteOptions, writePgpmFiles, writePgpmPlan } from '@pgpmjs/ast'; |
| 12 | +import { |
| 13 | + parseAuthor, |
| 14 | + parsePlanFile, |
| 15 | + PgpmRow, |
| 16 | + SqlWriteOptions, |
| 17 | + writePgpmFiles, |
| 18 | + writePgpmPlan, |
| 19 | + writePlanFile |
| 20 | +} from '@pgpmjs/ast'; |
13 | 21 | import * as fs from 'fs'; |
14 | 22 | import * as path from 'path'; |
15 | 23 |
|
@@ -71,6 +79,101 @@ export const writeModule = ( |
71 | 79 | return dir; |
72 | 80 | }; |
73 | 81 |
|
| 82 | +/** Result of appending changes into an existing module (see {@link appendModule}). */ |
| 83 | +export interface AppendModuleResult { |
| 84 | + /** The module directory that was appended to. */ |
| 85 | + dir: string; |
| 86 | + /** Change names that were added to the plan. */ |
| 87 | + added: string[]; |
| 88 | + /** Change names skipped because they already exist in the plan. */ |
| 89 | + skipped: string[]; |
| 90 | + /** Non-fatal notices (skips, dropped dangling dependencies). */ |
| 91 | + warnings: string[]; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Append plan-ordered {@link PgpmRow}s into an *existing* module rather than |
| 96 | + * writing a fresh package. Existing changes, their scripts, and the `.control` |
| 97 | + * file are left untouched; only the new changes are written (deploy/revert/ |
| 98 | + * verify) and appended to `pgpm.plan` after the current changes. |
| 99 | + * |
| 100 | + * Rows whose change name already exists are skipped (never overwritten). |
| 101 | + * A new change's dependency bracket is filtered to names that resolve within |
| 102 | + * the plan (existing changes, other appended changes, or `pkg:`-external |
| 103 | + * references); a dangling internal dependency is dropped with a warning |
| 104 | + * (plan order still sequences it after the current changes). |
| 105 | + */ |
| 106 | +export const appendModule = ( |
| 107 | + moduleDir: string, |
| 108 | + rows: PgpmRow[], |
| 109 | + options: { author?: string } = {} |
| 110 | +): AppendModuleResult => { |
| 111 | + const planPath = path.join(moduleDir, 'pgpm.plan'); |
| 112 | + if (!fs.existsSync(planPath)) { |
| 113 | + throw new Error( |
| 114 | + `No pgpm.plan found at ${planPath}; append mode expects an existing pgpm module directory.` |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + const parsed = parsePlanFile(planPath); |
| 119 | + if (!parsed.data) { |
| 120 | + throw new Error( |
| 121 | + `Failed to parse ${planPath}: ${parsed.errors |
| 122 | + .map(e => `line ${e.line}: ${e.message}`) |
| 123 | + .join('; ')}` |
| 124 | + ); |
| 125 | + } |
| 126 | + const plan = parsed.data; |
| 127 | + |
| 128 | + const existingNames = new Set(plan.changes.map(c => c.name)); |
| 129 | + const incomingNames = new Set(rows.map(r => r.deploy)); |
| 130 | + const appended = new Set<string>(); |
| 131 | + const added: string[] = []; |
| 132 | + const skipped: string[] = []; |
| 133 | + const warnings: string[] = []; |
| 134 | + const newRows: PgpmRow[] = []; |
| 135 | + |
| 136 | + const { fullName, email } = parseAuthor(options.author || 'constructive'); |
| 137 | + const timestamp = new Date().toISOString().replace(/\.\d{3}Z$/, 'Z'); |
| 138 | + |
| 139 | + for (const row of rows) { |
| 140 | + if (existingNames.has(row.deploy) || appended.has(row.deploy)) { |
| 141 | + skipped.push(row.deploy); |
| 142 | + warnings.push(`change ${row.deploy} already exists in ${plan.package}; left untouched`); |
| 143 | + continue; |
| 144 | + } |
| 145 | + const deps = (row.deps ?? []).filter(dep => { |
| 146 | + if (dep.includes(':')) return true; // cross-package (pkg:change) external |
| 147 | + if (existingNames.has(dep) || incomingNames.has(dep)) return true; |
| 148 | + warnings.push(`change ${row.deploy}: dropped dependency ${dep} (not present in ${plan.package})`); |
| 149 | + return false; |
| 150 | + }); |
| 151 | + plan.changes.push({ |
| 152 | + name: row.deploy, |
| 153 | + dependencies: deps, |
| 154 | + timestamp, |
| 155 | + planner: fullName, |
| 156 | + email: email || `${fullName}@constructive.io`, |
| 157 | + comment: `add ${row.name ?? row.deploy}` |
| 158 | + }); |
| 159 | + newRows.push({ ...row, deps }); |
| 160 | + appended.add(row.deploy); |
| 161 | + added.push(row.deploy); |
| 162 | + } |
| 163 | + |
| 164 | + if (newRows.length) { |
| 165 | + const opts: SqlWriteOptions = { |
| 166 | + outdir: path.dirname(moduleDir), |
| 167 | + name: path.basename(moduleDir), |
| 168 | + replacer: (str: string) => str |
| 169 | + }; |
| 170 | + writePgpmFiles(newRows, opts); |
| 171 | + writePlanFile(planPath, plan); |
| 172 | + } |
| 173 | + |
| 174 | + return { dir: moduleDir, added, skipped, warnings }; |
| 175 | +}; |
| 176 | + |
74 | 177 | /** |
75 | 178 | * Guard against clobbering: writing into the source directory requires |
76 | 179 | * `--write`, as does overwriting any existing package directory. |
|
0 commit comments