@prenta/import 0.5.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/CHANGELOG.md +686 -0
- package/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/adapter.d.ts +47 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +60 -0
- package/dist/adapter.js.map +1 -0
- package/dist/adapters/contentful/asset.d.ts +47 -0
- package/dist/adapters/contentful/asset.d.ts.map +1 -0
- package/dist/adapters/contentful/asset.js +88 -0
- package/dist/adapters/contentful/asset.js.map +1 -0
- package/dist/adapters/contentful/index.d.ts +24 -0
- package/dist/adapters/contentful/index.d.ts.map +1 -0
- package/dist/adapters/contentful/index.js +242 -0
- package/dist/adapters/contentful/index.js.map +1 -0
- package/dist/adapters/contentful/locale.d.ts +66 -0
- package/dist/adapters/contentful/locale.d.ts.map +1 -0
- package/dist/adapters/contentful/locale.js +125 -0
- package/dist/adapters/contentful/locale.js.map +1 -0
- package/dist/adapters/contentful/map.d.ts +69 -0
- package/dist/adapters/contentful/map.d.ts.map +1 -0
- package/dist/adapters/contentful/map.js +174 -0
- package/dist/adapters/contentful/map.js.map +1 -0
- package/dist/adapters/contentful/rich-text.d.ts +52 -0
- package/dist/adapters/contentful/rich-text.d.ts.map +1 -0
- package/dist/adapters/contentful/rich-text.js +95 -0
- package/dist/adapters/contentful/rich-text.js.map +1 -0
- package/dist/adapters/contentful/types.d.ts +189 -0
- package/dist/adapters/contentful/types.d.ts.map +1 -0
- package/dist/adapters/contentful/types.js +26 -0
- package/dist/adapters/contentful/types.js.map +1 -0
- package/dist/adapters/identity.d.ts +27 -0
- package/dist/adapters/identity.d.ts.map +1 -0
- package/dist/adapters/identity.js +116 -0
- package/dist/adapters/identity.js.map +1 -0
- package/dist/adapters/sanity/asset.d.ts +45 -0
- package/dist/adapters/sanity/asset.d.ts.map +1 -0
- package/dist/adapters/sanity/asset.js +79 -0
- package/dist/adapters/sanity/asset.js.map +1 -0
- package/dist/adapters/sanity/index.d.ts +19 -0
- package/dist/adapters/sanity/index.d.ts.map +1 -0
- package/dist/adapters/sanity/index.js +165 -0
- package/dist/adapters/sanity/index.js.map +1 -0
- package/dist/adapters/sanity/map.d.ts +75 -0
- package/dist/adapters/sanity/map.d.ts.map +1 -0
- package/dist/adapters/sanity/map.js +245 -0
- package/dist/adapters/sanity/map.js.map +1 -0
- package/dist/adapters/sanity/parse.d.ts +64 -0
- package/dist/adapters/sanity/parse.d.ts.map +1 -0
- package/dist/adapters/sanity/parse.js +71 -0
- package/dist/adapters/sanity/parse.js.map +1 -0
- package/dist/adapters/sanity/portable-text.d.ts +64 -0
- package/dist/adapters/sanity/portable-text.d.ts.map +1 -0
- package/dist/adapters/sanity/portable-text.js +112 -0
- package/dist/adapters/sanity/portable-text.js.map +1 -0
- package/dist/adapters/sanity/types.d.ts +182 -0
- package/dist/adapters/sanity/types.d.ts.map +1 -0
- package/dist/adapters/sanity/types.js +17 -0
- package/dist/adapters/sanity/types.js.map +1 -0
- package/dist/adapters/strapi/asset.d.ts +56 -0
- package/dist/adapters/strapi/asset.d.ts.map +1 -0
- package/dist/adapters/strapi/asset.js +94 -0
- package/dist/adapters/strapi/asset.js.map +1 -0
- package/dist/adapters/strapi/index.d.ts +20 -0
- package/dist/adapters/strapi/index.d.ts.map +1 -0
- package/dist/adapters/strapi/index.js +205 -0
- package/dist/adapters/strapi/index.js.map +1 -0
- package/dist/adapters/strapi/links.d.ts +79 -0
- package/dist/adapters/strapi/links.d.ts.map +1 -0
- package/dist/adapters/strapi/links.js +98 -0
- package/dist/adapters/strapi/links.js.map +1 -0
- package/dist/adapters/strapi/loader.d.ts +68 -0
- package/dist/adapters/strapi/loader.d.ts.map +1 -0
- package/dist/adapters/strapi/loader.js +177 -0
- package/dist/adapters/strapi/loader.js.map +1 -0
- package/dist/adapters/strapi/map.d.ts +53 -0
- package/dist/adapters/strapi/map.d.ts.map +1 -0
- package/dist/adapters/strapi/map.js +193 -0
- package/dist/adapters/strapi/map.js.map +1 -0
- package/dist/adapters/strapi/types.d.ts +174 -0
- package/dist/adapters/strapi/types.d.ts.map +1 -0
- package/dist/adapters/strapi/types.js +31 -0
- package/dist/adapters/strapi/types.js.map +1 -0
- package/dist/adapters/strapi/uid.d.ts +44 -0
- package/dist/adapters/strapi/uid.d.ts.map +1 -0
- package/dist/adapters/strapi/uid.js +58 -0
- package/dist/adapters/strapi/uid.js.map +1 -0
- package/dist/adapters/wordpress/attachments.d.ts +37 -0
- package/dist/adapters/wordpress/attachments.d.ts.map +1 -0
- package/dist/adapters/wordpress/attachments.js +134 -0
- package/dist/adapters/wordpress/attachments.js.map +1 -0
- package/dist/adapters/wordpress/authors.d.ts +24 -0
- package/dist/adapters/wordpress/authors.d.ts.map +1 -0
- package/dist/adapters/wordpress/authors.js +40 -0
- package/dist/adapters/wordpress/authors.js.map +1 -0
- package/dist/adapters/wordpress/index.d.ts +19 -0
- package/dist/adapters/wordpress/index.d.ts.map +1 -0
- package/dist/adapters/wordpress/index.js +281 -0
- package/dist/adapters/wordpress/index.js.map +1 -0
- package/dist/adapters/wordpress/map.d.ts +61 -0
- package/dist/adapters/wordpress/map.d.ts.map +1 -0
- package/dist/adapters/wordpress/map.js +265 -0
- package/dist/adapters/wordpress/map.js.map +1 -0
- package/dist/adapters/wordpress/parse.d.ts +50 -0
- package/dist/adapters/wordpress/parse.d.ts.map +1 -0
- package/dist/adapters/wordpress/parse.js +141 -0
- package/dist/adapters/wordpress/parse.js.map +1 -0
- package/dist/adapters/wordpress/terms.d.ts +46 -0
- package/dist/adapters/wordpress/terms.d.ts.map +1 -0
- package/dist/adapters/wordpress/terms.js +87 -0
- package/dist/adapters/wordpress/terms.js.map +1 -0
- package/dist/adapters/wordpress/types.d.ts +201 -0
- package/dist/adapters/wordpress/types.d.ts.map +1 -0
- package/dist/adapters/wordpress/types.js +24 -0
- package/dist/adapters/wordpress/types.js.map +1 -0
- package/dist/cli.d.ts +16 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +266 -0
- package/dist/cli.js.map +1 -0
- package/dist/formatter.d.ts +12 -0
- package/dist/formatter.d.ts.map +1 -0
- package/dist/formatter.js +48 -0
- package/dist/formatter.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/migrate.d.ts +151 -0
- package/dist/migrate.d.ts.map +1 -0
- package/dist/migrate.js +255 -0
- package/dist/migrate.js.map +1 -0
- package/dist/registry.d.ts +39 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +62 -0
- package/dist/registry.js.map +1 -0
- package/dist/runner.d.ts +33 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +61 -0
- package/dist/runner.js.map +1 -0
- package/dist/types.d.ts +173 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +17 -0
- package/dist/types.js.map +1 -0
- package/package.json +59 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@prenta/import` — public entry.
|
|
3
|
+
*
|
|
4
|
+
* Programmatic API for migrating content from external CMSs into
|
|
5
|
+
* Prenta. The CLI (`prenta-import`) is a thin wrapper around
|
|
6
|
+
* {@link runImport} + {@link writeImportResult}; build / migration
|
|
7
|
+
* scripts can call those directly.
|
|
8
|
+
*
|
|
9
|
+
* Authors of third-party adapters should import from
|
|
10
|
+
* `@prenta/import/adapter` to get the narrow set of helpers
|
|
11
|
+
* an adapter implementation needs — that submodule is intentionally
|
|
12
|
+
* dependency-light and CLI-free, so adapter packages don't ship the
|
|
13
|
+
* full framework surface.
|
|
14
|
+
*/
|
|
15
|
+
export { buildResult, buildSummary, isImportedDocument } from './adapter.js';
|
|
16
|
+
export { contentfulAdapter } from './adapters/contentful/index.js';
|
|
17
|
+
export { identityAdapter } from './adapters/identity.js';
|
|
18
|
+
export { sanityAdapter } from './adapters/sanity/index.js';
|
|
19
|
+
export { strapiAdapter } from './adapters/strapi/index.js';
|
|
20
|
+
export { wordpressAdapter } from './adapters/wordpress/index.js';
|
|
21
|
+
export { formatImportSummary } from './formatter.js';
|
|
22
|
+
export { MigrationLoadError, MigrationOutputError, MigrationRunError, applyMigrations, loadMigration, } from './migrate.js';
|
|
23
|
+
export { getAdapter, listAdapters, registerAdapter, unregisterAdapter } from './registry.js';
|
|
24
|
+
export { UnknownAdapterError, runImport, writeImportResult } from './runner.js';
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAC5E,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAElE,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAE1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAEhE,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AACpD,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,eAAe,EACf,aAAa,GACd,MAAM,cAAc,CAAA;AAErB,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AAC5F,OAAO,EAAE,mBAAmB,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic post-import migration pipeline.
|
|
3
|
+
*
|
|
4
|
+
* Adapters produce `ImportedDocument`s that are faithful to the
|
|
5
|
+
* source CMS. Migrations transform those documents toward the
|
|
6
|
+
* destination — field renames, URL rewrites, schema reshaping,
|
|
7
|
+
* HTML → portable text, component-prop migrations.
|
|
8
|
+
*
|
|
9
|
+
* A migration is a pure function from documents to documents
|
|
10
|
+
* (plus optional warnings + unresolved fields). The orchestrator
|
|
11
|
+
* composes them sequentially after the adapter run and merges
|
|
12
|
+
* results into the existing {@link ImportResult} envelope, so
|
|
13
|
+
* downstream consumers (the write step, the formatter, the
|
|
14
|
+
* exit-code gate) don't have to know whether a warning came from
|
|
15
|
+
* the adapter or a migration.
|
|
16
|
+
*
|
|
17
|
+
* The CLI exposes this via the `--migrate <path>` flag (repeatable)
|
|
18
|
+
* on the existing `prenta-import <adapter> <input>` command.
|
|
19
|
+
* Library consumers can compose programmatically via
|
|
20
|
+
* {@link loadMigration} + {@link applyMigrations}.
|
|
21
|
+
*
|
|
22
|
+
* Migration scripts may export the migration in any of three
|
|
23
|
+
* shapes — all three are supported so operators can choose the
|
|
24
|
+
* style that fits their script:
|
|
25
|
+
*
|
|
26
|
+
* 1. **Default export, function** —
|
|
27
|
+
* ```ts
|
|
28
|
+
* export default function migrate(documents) {
|
|
29
|
+
* return documents.map(...)
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* 2. **Named `migrate` export** —
|
|
34
|
+
* ```ts
|
|
35
|
+
* export function migrate(documents) {
|
|
36
|
+
* return { documents: documents.map(...) }
|
|
37
|
+
* }
|
|
38
|
+
* export const name = 'rename-hero-cta'
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* 3. **Default export, object** —
|
|
42
|
+
* ```ts
|
|
43
|
+
* export default {
|
|
44
|
+
* name: 'rename-hero-cta',
|
|
45
|
+
* migrate(documents) { return { documents: documents.map(...) } },
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* The orchestrator picks a name in this order: explicit `name`
|
|
50
|
+
* field → script basename (sans extension).
|
|
51
|
+
*/
|
|
52
|
+
import type { ImportResult, ImportWarning, ImportedDocument, UnresolvedField } from './types.js';
|
|
53
|
+
/**
|
|
54
|
+
* What a migration returns. Either a plain array (a quick rewrite
|
|
55
|
+
* with no warnings) or a partial {@link ImportResult}-shaped
|
|
56
|
+
* object the orchestrator can merge into the envelope.
|
|
57
|
+
*/
|
|
58
|
+
export type MigrationOutput = ImportedDocument[] | {
|
|
59
|
+
documents: ImportedDocument[];
|
|
60
|
+
warnings?: ImportWarning[];
|
|
61
|
+
unresolved?: UnresolvedField[];
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* A migration: pure function from documents to documents (+
|
|
65
|
+
* optional warnings / unresolved). Can be sync or async.
|
|
66
|
+
*/
|
|
67
|
+
export type Migration = (documents: readonly ImportedDocument[]) => MigrationOutput | Promise<MigrationOutput>;
|
|
68
|
+
/**
|
|
69
|
+
* Loaded migration ready to apply. Carries the resolved name so
|
|
70
|
+
* the orchestrator can attribute downstream warnings.
|
|
71
|
+
*/
|
|
72
|
+
export interface NamedMigration {
|
|
73
|
+
/** Operator-facing label (basename or `name` export). */
|
|
74
|
+
name: string;
|
|
75
|
+
/** Source path the migration was loaded from. Diagnostic only. */
|
|
76
|
+
source: string;
|
|
77
|
+
/** The migration function. */
|
|
78
|
+
migrate: Migration;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Error thrown when a migration script can't be loaded or
|
|
82
|
+
* doesn't expose a recognizable migration shape.
|
|
83
|
+
*/
|
|
84
|
+
export declare class MigrationLoadError extends Error {
|
|
85
|
+
readonly source: string;
|
|
86
|
+
readonly cause?: unknown;
|
|
87
|
+
constructor(source: string, reason: string, cause?: unknown);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Error thrown when a migration runs but blows up while applied
|
|
91
|
+
* to the documents. Preserves the migration name so the CLI can
|
|
92
|
+
* tell the operator which script broke.
|
|
93
|
+
*/
|
|
94
|
+
export declare class MigrationRunError extends Error {
|
|
95
|
+
readonly migration: string;
|
|
96
|
+
readonly cause: unknown;
|
|
97
|
+
constructor(migration: string, cause: unknown);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Error thrown when a migration ran to completion but returned a
|
|
101
|
+
* value the orchestrator can't safely merge. The most common cause
|
|
102
|
+
* is a script that forgot to `return` (so `out` is `undefined`),
|
|
103
|
+
* but it also fires for non-array `documents`, non-array
|
|
104
|
+
* `warnings`, non-array `unresolved`, etc.
|
|
105
|
+
*
|
|
106
|
+
* Carries the migration name like {@link MigrationRunError} so the
|
|
107
|
+
* CLI can attribute the failure rather than letting a downstream
|
|
108
|
+
* `TypeError: Cannot read properties of undefined (reading 'documents')`
|
|
109
|
+
* escape with no operator context.
|
|
110
|
+
*/
|
|
111
|
+
export declare class MigrationOutputError extends Error {
|
|
112
|
+
readonly migration: string;
|
|
113
|
+
constructor(migration: string, reason: string);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Dynamically import a migration script and resolve it into a
|
|
117
|
+
* {@link NamedMigration}. Accepts the three export shapes
|
|
118
|
+
* documented at the top of this file.
|
|
119
|
+
*
|
|
120
|
+
* `source` is the path the operator passed on the command line
|
|
121
|
+
* (or to the programmatic API). Relative paths resolve against
|
|
122
|
+
* `process.cwd()`.
|
|
123
|
+
*/
|
|
124
|
+
export declare function loadMigration(source: string): Promise<NamedMigration>;
|
|
125
|
+
/**
|
|
126
|
+
* Apply a sequence of migrations to an {@link ImportResult},
|
|
127
|
+
* returning a new result with transformed documents and merged
|
|
128
|
+
* warnings / unresolved fields.
|
|
129
|
+
*
|
|
130
|
+
* Migrations run **in order** — `migrations[0]` first, then
|
|
131
|
+
* `migrations[1]` against `migrations[0]`'s output, etc. A
|
|
132
|
+
* migration's warnings + unresolved fields are appended to what
|
|
133
|
+
* the adapter (and prior migrations) already produced; nothing
|
|
134
|
+
* is dropped.
|
|
135
|
+
*
|
|
136
|
+
* The summary's `converted` count is updated to reflect the
|
|
137
|
+
* post-migration document count (a migration that drops
|
|
138
|
+
* documents lowers `converted`).
|
|
139
|
+
*
|
|
140
|
+
* A migration that throws short-circuits the pipeline with a
|
|
141
|
+
* {@link MigrationRunError} — partial migration output is unsafe
|
|
142
|
+
* to write because downstream migrations might assume a shape the
|
|
143
|
+
* failed migration was supposed to produce. A migration that
|
|
144
|
+
* returns an unrecognizable value short-circuits with a
|
|
145
|
+
* {@link MigrationOutputError} for the same reason; without
|
|
146
|
+
* attribution, a `TypeError: Cannot read properties of undefined`
|
|
147
|
+
* would escape with no signal as to which script in the chain
|
|
148
|
+
* misbehaved.
|
|
149
|
+
*/
|
|
150
|
+
export declare function applyMigrations(result: ImportResult, migrations: readonly NamedMigration[]): Promise<ImportResult>;
|
|
151
|
+
//# sourceMappingURL=migrate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.d.ts","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAKH,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEhG;;;;GAIG;AACH,MAAM,MAAM,eAAe,GACvB,gBAAgB,EAAE,GAClB;IACE,SAAS,EAAE,gBAAgB,EAAE,CAAA;IAC7B,QAAQ,CAAC,EAAE,aAAa,EAAE,CAAA;IAC1B,UAAU,CAAC,EAAE,eAAe,EAAE,CAAA;CAC/B,CAAA;AAEL;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,CACtB,SAAS,EAAE,SAAS,gBAAgB,EAAE,KACnC,eAAe,GAAG,OAAO,CAAC,eAAe,CAAC,CAAA;AAE/C;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,yDAAyD;IACzD,IAAI,EAAE,MAAM,CAAA;IACZ,kEAAkE;IAClE,MAAM,EAAE,MAAM,CAAA;IACd,8BAA8B;IAC9B,OAAO,EAAE,SAAS,CAAA;CACnB;AAED;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAA;gBAEZ,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO;CAM5D;AAED;;;;GAIG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;IAC1B,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAA;gBAEX,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO;CAO9C;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;gBAEd,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAK9C;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAoB3E;AAkCD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,YAAY,EACpB,UAAU,EAAE,SAAS,cAAc,EAAE,GACpC,OAAO,CAAC,YAAY,CAAC,CAiCvB"}
|
package/dist/migrate.js
ADDED
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic post-import migration pipeline.
|
|
3
|
+
*
|
|
4
|
+
* Adapters produce `ImportedDocument`s that are faithful to the
|
|
5
|
+
* source CMS. Migrations transform those documents toward the
|
|
6
|
+
* destination — field renames, URL rewrites, schema reshaping,
|
|
7
|
+
* HTML → portable text, component-prop migrations.
|
|
8
|
+
*
|
|
9
|
+
* A migration is a pure function from documents to documents
|
|
10
|
+
* (plus optional warnings + unresolved fields). The orchestrator
|
|
11
|
+
* composes them sequentially after the adapter run and merges
|
|
12
|
+
* results into the existing {@link ImportResult} envelope, so
|
|
13
|
+
* downstream consumers (the write step, the formatter, the
|
|
14
|
+
* exit-code gate) don't have to know whether a warning came from
|
|
15
|
+
* the adapter or a migration.
|
|
16
|
+
*
|
|
17
|
+
* The CLI exposes this via the `--migrate <path>` flag (repeatable)
|
|
18
|
+
* on the existing `prenta-import <adapter> <input>` command.
|
|
19
|
+
* Library consumers can compose programmatically via
|
|
20
|
+
* {@link loadMigration} + {@link applyMigrations}.
|
|
21
|
+
*
|
|
22
|
+
* Migration scripts may export the migration in any of three
|
|
23
|
+
* shapes — all three are supported so operators can choose the
|
|
24
|
+
* style that fits their script:
|
|
25
|
+
*
|
|
26
|
+
* 1. **Default export, function** —
|
|
27
|
+
* ```ts
|
|
28
|
+
* export default function migrate(documents) {
|
|
29
|
+
* return documents.map(...)
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* 2. **Named `migrate` export** —
|
|
34
|
+
* ```ts
|
|
35
|
+
* export function migrate(documents) {
|
|
36
|
+
* return { documents: documents.map(...) }
|
|
37
|
+
* }
|
|
38
|
+
* export const name = 'rename-hero-cta'
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* 3. **Default export, object** —
|
|
42
|
+
* ```ts
|
|
43
|
+
* export default {
|
|
44
|
+
* name: 'rename-hero-cta',
|
|
45
|
+
* migrate(documents) { return { documents: documents.map(...) } },
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* The orchestrator picks a name in this order: explicit `name`
|
|
50
|
+
* field → script basename (sans extension).
|
|
51
|
+
*/
|
|
52
|
+
import { isAbsolute, resolve, basename } from 'node:path';
|
|
53
|
+
import { pathToFileURL } from 'node:url';
|
|
54
|
+
/**
|
|
55
|
+
* Error thrown when a migration script can't be loaded or
|
|
56
|
+
* doesn't expose a recognizable migration shape.
|
|
57
|
+
*/
|
|
58
|
+
export class MigrationLoadError extends Error {
|
|
59
|
+
source;
|
|
60
|
+
cause;
|
|
61
|
+
constructor(source, reason, cause) {
|
|
62
|
+
super(`Could not load migration '${source}': ${reason}`);
|
|
63
|
+
this.name = 'MigrationLoadError';
|
|
64
|
+
this.source = source;
|
|
65
|
+
this.cause = cause;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Error thrown when a migration runs but blows up while applied
|
|
70
|
+
* to the documents. Preserves the migration name so the CLI can
|
|
71
|
+
* tell the operator which script broke.
|
|
72
|
+
*/
|
|
73
|
+
export class MigrationRunError extends Error {
|
|
74
|
+
migration;
|
|
75
|
+
cause;
|
|
76
|
+
constructor(migration, cause) {
|
|
77
|
+
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
78
|
+
super(`Migration '${migration}' threw: ${reason}`);
|
|
79
|
+
this.name = 'MigrationRunError';
|
|
80
|
+
this.migration = migration;
|
|
81
|
+
this.cause = cause;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Error thrown when a migration ran to completion but returned a
|
|
86
|
+
* value the orchestrator can't safely merge. The most common cause
|
|
87
|
+
* is a script that forgot to `return` (so `out` is `undefined`),
|
|
88
|
+
* but it also fires for non-array `documents`, non-array
|
|
89
|
+
* `warnings`, non-array `unresolved`, etc.
|
|
90
|
+
*
|
|
91
|
+
* Carries the migration name like {@link MigrationRunError} so the
|
|
92
|
+
* CLI can attribute the failure rather than letting a downstream
|
|
93
|
+
* `TypeError: Cannot read properties of undefined (reading 'documents')`
|
|
94
|
+
* escape with no operator context.
|
|
95
|
+
*/
|
|
96
|
+
export class MigrationOutputError extends Error {
|
|
97
|
+
migration;
|
|
98
|
+
constructor(migration, reason) {
|
|
99
|
+
super(`Migration '${migration}' returned an invalid output: ${reason}`);
|
|
100
|
+
this.name = 'MigrationOutputError';
|
|
101
|
+
this.migration = migration;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Dynamically import a migration script and resolve it into a
|
|
106
|
+
* {@link NamedMigration}. Accepts the three export shapes
|
|
107
|
+
* documented at the top of this file.
|
|
108
|
+
*
|
|
109
|
+
* `source` is the path the operator passed on the command line
|
|
110
|
+
* (or to the programmatic API). Relative paths resolve against
|
|
111
|
+
* `process.cwd()`.
|
|
112
|
+
*/
|
|
113
|
+
export async function loadMigration(source) {
|
|
114
|
+
const absPath = isAbsolute(source) ? source : resolve(process.cwd(), source);
|
|
115
|
+
let mod;
|
|
116
|
+
try {
|
|
117
|
+
// The `file://` URL form is required on Windows — bare
|
|
118
|
+
// paths trip ERR_UNSUPPORTED_ESM_URL_SCHEME there.
|
|
119
|
+
mod = (await import(pathToFileURL(absPath).href));
|
|
120
|
+
}
|
|
121
|
+
catch (err) {
|
|
122
|
+
throw new MigrationLoadError(source, err.message, err);
|
|
123
|
+
}
|
|
124
|
+
const fallbackName = basename(source).replace(/\.[^.]+$/, '') || source;
|
|
125
|
+
const resolved = pickMigration(mod, fallbackName);
|
|
126
|
+
if (!resolved) {
|
|
127
|
+
throw new MigrationLoadError(source, 'no migrate function found. Expected a default export, a named `migrate` export, or a default-exported { name, migrate } object.');
|
|
128
|
+
}
|
|
129
|
+
return { ...resolved, source };
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Try the three supported export shapes in order. Returns
|
|
133
|
+
* `undefined` when none match — the loader turns that into a
|
|
134
|
+
* `MigrationLoadError`.
|
|
135
|
+
*/
|
|
136
|
+
function pickMigration(mod, fallbackName) {
|
|
137
|
+
// Shape 1: default export is a function
|
|
138
|
+
if (typeof mod.default === 'function') {
|
|
139
|
+
const explicitName = typeof mod.name === 'string' ? mod.name : fallbackName;
|
|
140
|
+
return { name: explicitName, migrate: mod.default };
|
|
141
|
+
}
|
|
142
|
+
// Shape 3: default export is { name, migrate }
|
|
143
|
+
if (mod.default !== null &&
|
|
144
|
+
typeof mod.default === 'object' &&
|
|
145
|
+
typeof mod.default.migrate === 'function') {
|
|
146
|
+
const def = mod.default;
|
|
147
|
+
const name = typeof def.name === 'string' ? def.name : fallbackName;
|
|
148
|
+
return { name, migrate: def.migrate };
|
|
149
|
+
}
|
|
150
|
+
// Shape 2: named `migrate` export
|
|
151
|
+
if (typeof mod.migrate === 'function') {
|
|
152
|
+
const name = typeof mod.name === 'string' ? mod.name : fallbackName;
|
|
153
|
+
return { name, migrate: mod.migrate };
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Apply a sequence of migrations to an {@link ImportResult},
|
|
159
|
+
* returning a new result with transformed documents and merged
|
|
160
|
+
* warnings / unresolved fields.
|
|
161
|
+
*
|
|
162
|
+
* Migrations run **in order** — `migrations[0]` first, then
|
|
163
|
+
* `migrations[1]` against `migrations[0]`'s output, etc. A
|
|
164
|
+
* migration's warnings + unresolved fields are appended to what
|
|
165
|
+
* the adapter (and prior migrations) already produced; nothing
|
|
166
|
+
* is dropped.
|
|
167
|
+
*
|
|
168
|
+
* The summary's `converted` count is updated to reflect the
|
|
169
|
+
* post-migration document count (a migration that drops
|
|
170
|
+
* documents lowers `converted`).
|
|
171
|
+
*
|
|
172
|
+
* A migration that throws short-circuits the pipeline with a
|
|
173
|
+
* {@link MigrationRunError} — partial migration output is unsafe
|
|
174
|
+
* to write because downstream migrations might assume a shape the
|
|
175
|
+
* failed migration was supposed to produce. A migration that
|
|
176
|
+
* returns an unrecognizable value short-circuits with a
|
|
177
|
+
* {@link MigrationOutputError} for the same reason; without
|
|
178
|
+
* attribution, a `TypeError: Cannot read properties of undefined`
|
|
179
|
+
* would escape with no signal as to which script in the chain
|
|
180
|
+
* misbehaved.
|
|
181
|
+
*/
|
|
182
|
+
export async function applyMigrations(result, migrations) {
|
|
183
|
+
if (migrations.length === 0)
|
|
184
|
+
return result;
|
|
185
|
+
let documents = [...result.documents];
|
|
186
|
+
const warnings = [...result.warnings];
|
|
187
|
+
const unresolved = [...result.unresolved];
|
|
188
|
+
for (const m of migrations) {
|
|
189
|
+
let out;
|
|
190
|
+
try {
|
|
191
|
+
out = await m.migrate(documents);
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
throw new MigrationRunError(m.name, err);
|
|
195
|
+
}
|
|
196
|
+
// Validate-and-merge sits inside the same per-migration block
|
|
197
|
+
// as the call. An invalid return shape is a malformed
|
|
198
|
+
// *migration*, not a generic JS error — it deserves the same
|
|
199
|
+
// operator-facing attribution as a throw.
|
|
200
|
+
const validated = validateMigrationOutput(m.name, out);
|
|
201
|
+
documents = [...validated.documents];
|
|
202
|
+
if (validated.warnings.length > 0)
|
|
203
|
+
warnings.push(...validated.warnings);
|
|
204
|
+
if (validated.unresolved.length > 0)
|
|
205
|
+
unresolved.push(...validated.unresolved);
|
|
206
|
+
}
|
|
207
|
+
return {
|
|
208
|
+
documents,
|
|
209
|
+
warnings,
|
|
210
|
+
unresolved,
|
|
211
|
+
summary: {
|
|
212
|
+
...result.summary,
|
|
213
|
+
converted: documents.length,
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Normalize and validate a migration's return value. Throws
|
|
219
|
+
* {@link MigrationOutputError} when the shape doesn't match the
|
|
220
|
+
* {@link MigrationOutput} contract.
|
|
221
|
+
*
|
|
222
|
+
* Liberal about the two array contents (warnings / unresolved are
|
|
223
|
+
* passed through verbatim; the formatter handles malformed
|
|
224
|
+
* entries cosmetically). Strict about the documents array because
|
|
225
|
+
* the next migration in the chain receives it as input — a
|
|
226
|
+
* non-array would crash the loop on the next iteration with no
|
|
227
|
+
* attribution.
|
|
228
|
+
*/
|
|
229
|
+
function validateMigrationOutput(name, out) {
|
|
230
|
+
if (Array.isArray(out)) {
|
|
231
|
+
return { documents: out, warnings: [], unresolved: [] };
|
|
232
|
+
}
|
|
233
|
+
if (out === null || out === undefined) {
|
|
234
|
+
throw new MigrationOutputError(name, `expected an ImportedDocument[] or { documents } object, got ${out === null ? 'null' : 'undefined'} (did the migration forget to return?)`);
|
|
235
|
+
}
|
|
236
|
+
if (typeof out !== 'object') {
|
|
237
|
+
throw new MigrationOutputError(name, `expected an ImportedDocument[] or { documents } object, got ${typeof out}.`);
|
|
238
|
+
}
|
|
239
|
+
const obj = out;
|
|
240
|
+
if (!Array.isArray(obj.documents)) {
|
|
241
|
+
throw new MigrationOutputError(name, `output object is missing a 'documents' array (got ${obj.documents === undefined ? 'undefined' : typeof obj.documents}).`);
|
|
242
|
+
}
|
|
243
|
+
if (obj.warnings !== undefined && !Array.isArray(obj.warnings)) {
|
|
244
|
+
throw new MigrationOutputError(name, `'warnings' must be an array when present, got ${typeof obj.warnings}.`);
|
|
245
|
+
}
|
|
246
|
+
if (obj.unresolved !== undefined && !Array.isArray(obj.unresolved)) {
|
|
247
|
+
throw new MigrationOutputError(name, `'unresolved' must be an array when present, got ${typeof obj.unresolved}.`);
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
documents: obj.documents,
|
|
251
|
+
warnings: obj.warnings ?? [],
|
|
252
|
+
unresolved: obj.unresolved ?? [],
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
//# sourceMappingURL=migrate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrate.js","sourceRoot":"","sources":["../src/migrate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkDG;AAEH,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAsCxC;;;GAGG;AACH,MAAM,OAAO,kBAAmB,SAAQ,KAAK;IAClC,MAAM,CAAQ;IACd,KAAK,CAAU;IAExB,YAAY,MAAc,EAAE,MAAc,EAAE,KAAe;QACzD,KAAK,CAAC,6BAA6B,MAAM,MAAM,MAAM,EAAE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,qDACE,GAAG,CAAC,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,OAAO,GAAG,CAAC,SACzD,IAAI,CACL,CAAA;IACH,CAAC;IACD,IAAI,GAAG,CAAC,QAAQ,KAAK,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC/D,MAAM,IAAI,oBAAoB,CAC5B,IAAI,EACJ,iDAAiD,OAAO,GAAG,CAAC,QAAQ,GAAG,CACxE,CAAA;IACH,CAAC;IACD,IAAI,GAAG,CAAC,UAAU,KAAK,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,CAAC;QACnE,MAAM,IAAI,oBAAoB,CAC5B,IAAI,EACJ,mDAAmD,OAAO,GAAG,CAAC,UAAU,GAAG,CAC5E,CAAA;IACH,CAAC;IACD,OAAO;QACL,SAAS,EAAE,GAAG,CAAC,SAA+B;QAC9C,QAAQ,EAAG,GAAG,CAAC,QAAwC,IAAI,EAAE;QAC7D,UAAU,EAAG,GAAG,CAAC,UAA4C,IAAI,EAAE;KACpE,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter registry.
|
|
3
|
+
*
|
|
4
|
+
* The framework keeps a small mutable registry so the CLI can route
|
|
5
|
+
* `prenta-import <name> ...` invocations to the correct adapter
|
|
6
|
+
* without each adapter having to re-implement command parsing. The
|
|
7
|
+
* registry starts populated with built-in adapters (currently just
|
|
8
|
+
* `identity`); future PRs add `sanity`, `contentful`, `strapi`,
|
|
9
|
+
* `wordpress` here.
|
|
10
|
+
*
|
|
11
|
+
* Third parties can register adapters at runtime via
|
|
12
|
+
* {@link registerAdapter}, which makes external adapter packages
|
|
13
|
+
* (e.g. `@my-org/prenta-import-prismic`) ship as one-line
|
|
14
|
+
* registrations from their own bin or programmatic entry.
|
|
15
|
+
*/
|
|
16
|
+
import type { ImportAdapter } from './types.js';
|
|
17
|
+
/**
|
|
18
|
+
* Add (or replace) an adapter in the registry. Returns the previous
|
|
19
|
+
* adapter under the same name (or `null`), so callers can save and
|
|
20
|
+
* restore the registry in tests if they need to.
|
|
21
|
+
*/
|
|
22
|
+
export declare function registerAdapter(adapter: ImportAdapter): ImportAdapter | null;
|
|
23
|
+
/**
|
|
24
|
+
* Remove an adapter by name. Returns `true` if something was
|
|
25
|
+
* actually removed. Used by tests; not exposed in the CLI.
|
|
26
|
+
*/
|
|
27
|
+
export declare function unregisterAdapter(name: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Look up an adapter by name. Returns `null` for unknown names; the
|
|
30
|
+
* CLI prints the available names from {@link listAdapters} when this
|
|
31
|
+
* happens.
|
|
32
|
+
*/
|
|
33
|
+
export declare function getAdapter(name: string): ImportAdapter | null;
|
|
34
|
+
/**
|
|
35
|
+
* Snapshot of every currently-registered adapter, sorted by name for
|
|
36
|
+
* deterministic CLI output.
|
|
37
|
+
*/
|
|
38
|
+
export declare function listAdapters(): ImportAdapter[];
|
|
39
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAOH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAc/C;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,aAAa,GAAG,aAAa,GAAG,IAAI,CAI5E;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAEvD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI,CAE7D;AAED;;;GAGG;AACH,wBAAgB,YAAY,IAAI,aAAa,EAAE,CAE9C"}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapter registry.
|
|
3
|
+
*
|
|
4
|
+
* The framework keeps a small mutable registry so the CLI can route
|
|
5
|
+
* `prenta-import <name> ...` invocations to the correct adapter
|
|
6
|
+
* without each adapter having to re-implement command parsing. The
|
|
7
|
+
* registry starts populated with built-in adapters (currently just
|
|
8
|
+
* `identity`); future PRs add `sanity`, `contentful`, `strapi`,
|
|
9
|
+
* `wordpress` here.
|
|
10
|
+
*
|
|
11
|
+
* Third parties can register adapters at runtime via
|
|
12
|
+
* {@link registerAdapter}, which makes external adapter packages
|
|
13
|
+
* (e.g. `@my-org/prenta-import-prismic`) ship as one-line
|
|
14
|
+
* registrations from their own bin or programmatic entry.
|
|
15
|
+
*/
|
|
16
|
+
import { contentfulAdapter } from './adapters/contentful/index.js';
|
|
17
|
+
import { identityAdapter } from './adapters/identity.js';
|
|
18
|
+
import { sanityAdapter } from './adapters/sanity/index.js';
|
|
19
|
+
import { strapiAdapter } from './adapters/strapi/index.js';
|
|
20
|
+
import { wordpressAdapter } from './adapters/wordpress/index.js';
|
|
21
|
+
const registry = new Map();
|
|
22
|
+
function register(adapter) {
|
|
23
|
+
registry.set(adapter.name, adapter);
|
|
24
|
+
}
|
|
25
|
+
register(identityAdapter);
|
|
26
|
+
register(sanityAdapter);
|
|
27
|
+
register(contentfulAdapter);
|
|
28
|
+
register(strapiAdapter);
|
|
29
|
+
register(wordpressAdapter);
|
|
30
|
+
/**
|
|
31
|
+
* Add (or replace) an adapter in the registry. Returns the previous
|
|
32
|
+
* adapter under the same name (or `null`), so callers can save and
|
|
33
|
+
* restore the registry in tests if they need to.
|
|
34
|
+
*/
|
|
35
|
+
export function registerAdapter(adapter) {
|
|
36
|
+
const previous = registry.get(adapter.name) ?? null;
|
|
37
|
+
register(adapter);
|
|
38
|
+
return previous;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Remove an adapter by name. Returns `true` if something was
|
|
42
|
+
* actually removed. Used by tests; not exposed in the CLI.
|
|
43
|
+
*/
|
|
44
|
+
export function unregisterAdapter(name) {
|
|
45
|
+
return registry.delete(name);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Look up an adapter by name. Returns `null` for unknown names; the
|
|
49
|
+
* CLI prints the available names from {@link listAdapters} when this
|
|
50
|
+
* happens.
|
|
51
|
+
*/
|
|
52
|
+
export function getAdapter(name) {
|
|
53
|
+
return registry.get(name) ?? null;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Snapshot of every currently-registered adapter, sorted by name for
|
|
57
|
+
* deterministic CLI output.
|
|
58
|
+
*/
|
|
59
|
+
export function listAdapters() {
|
|
60
|
+
return [...registry.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../src/registry.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAA;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAA;AACxD,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAGhE,MAAM,QAAQ,GAAG,IAAI,GAAG,EAAyB,CAAA;AAEjD,SAAS,QAAQ,CAAC,OAAsB;IACtC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;AACrC,CAAC;AAED,QAAQ,CAAC,eAAe,CAAC,CAAA;AACzB,QAAQ,CAAC,aAAa,CAAC,CAAA;AACvB,QAAQ,CAAC,iBAAiB,CAAC,CAAA;AAC3B,QAAQ,CAAC,aAAa,CAAC,CAAA;AACvB,QAAQ,CAAC,gBAAgB,CAAC,CAAA;AAE1B;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAAC,OAAsB;IACpD,MAAM,QAAQ,GAAG,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAA;IACnD,QAAQ,CAAC,OAAO,CAAC,CAAA;IACjB,OAAO,QAAQ,CAAA;AACjB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;AAC9B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,IAAY;IACrC,OAAO,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,IAAI,CAAA;AACnC,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,YAAY;IAC1B,OAAO,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAA;AAC5E,CAAC"}
|
package/dist/runner.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Top-level orchestrator for adapter runs.
|
|
3
|
+
*
|
|
4
|
+
* Resolves the adapter, invokes it with the provided options, and
|
|
5
|
+
* returns the result. Doesn't touch disk on its own — disk writes
|
|
6
|
+
* live in {@link writeImportResult} so the CLI's `--dry-run` vs
|
|
7
|
+
* `--write` semantics can compose cleanly.
|
|
8
|
+
*/
|
|
9
|
+
import type { ImportInvocation, ImportResult } from './types.js';
|
|
10
|
+
/**
|
|
11
|
+
* Look up the named adapter and run it. Throws on unknown adapter
|
|
12
|
+
* names — the CLI catches and prints the available list. We use a
|
|
13
|
+
* throw rather than returning `null` because the alternative would
|
|
14
|
+
* be a third return-shape consumers always have to discriminate.
|
|
15
|
+
*/
|
|
16
|
+
export declare function runImport(invocation: ImportInvocation): Promise<ImportResult>;
|
|
17
|
+
/**
|
|
18
|
+
* Write the result to disk using the standard envelope. Always wraps
|
|
19
|
+
* documents in `{ documents: [...] }` even for a single document, so
|
|
20
|
+
* the file shape is stable across imports (downstream tooling can
|
|
21
|
+
* key on the envelope without first sniffing for "is it an array").
|
|
22
|
+
*
|
|
23
|
+
* The summary + warnings + unresolved fields tag along so the file
|
|
24
|
+
* is self-describing — operators don't need to re-run the import to
|
|
25
|
+
* see what the adapter did.
|
|
26
|
+
*/
|
|
27
|
+
export declare function writeImportResult(path: string, result: ImportResult): void;
|
|
28
|
+
export declare class UnknownAdapterError extends Error {
|
|
29
|
+
readonly adapter: string;
|
|
30
|
+
readonly available: string;
|
|
31
|
+
constructor(adapter: string, available: string);
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=runner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner.d.ts","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAEhE;;;;;GAKG;AACH,wBAAsB,SAAS,CAAC,UAAU,EAAE,gBAAgB,GAAG,OAAO,CAAC,YAAY,CAAC,CAenF;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CAQ1E;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IAC5C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;gBAEd,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM;CAM/C"}
|
package/dist/runner.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Top-level orchestrator for adapter runs.
|
|
3
|
+
*
|
|
4
|
+
* Resolves the adapter, invokes it with the provided options, and
|
|
5
|
+
* returns the result. Doesn't touch disk on its own — disk writes
|
|
6
|
+
* live in {@link writeImportResult} so the CLI's `--dry-run` vs
|
|
7
|
+
* `--write` semantics can compose cleanly.
|
|
8
|
+
*/
|
|
9
|
+
import { writeFileSync } from 'node:fs';
|
|
10
|
+
import { getAdapter, listAdapters } from './registry.js';
|
|
11
|
+
/**
|
|
12
|
+
* Look up the named adapter and run it. Throws on unknown adapter
|
|
13
|
+
* names — the CLI catches and prints the available list. We use a
|
|
14
|
+
* throw rather than returning `null` because the alternative would
|
|
15
|
+
* be a third return-shape consumers always have to discriminate.
|
|
16
|
+
*/
|
|
17
|
+
export async function runImport(invocation) {
|
|
18
|
+
const adapter = getAdapter(invocation.adapter);
|
|
19
|
+
if (!adapter) {
|
|
20
|
+
const available = listAdapters()
|
|
21
|
+
.map((a) => a.name)
|
|
22
|
+
.join(', ') || '<none registered>';
|
|
23
|
+
throw new UnknownAdapterError(invocation.adapter, available);
|
|
24
|
+
}
|
|
25
|
+
const result = await adapter.run({
|
|
26
|
+
input: invocation.input,
|
|
27
|
+
config: invocation.config,
|
|
28
|
+
shouldInclude: invocation.shouldInclude,
|
|
29
|
+
});
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Write the result to disk using the standard envelope. Always wraps
|
|
34
|
+
* documents in `{ documents: [...] }` even for a single document, so
|
|
35
|
+
* the file shape is stable across imports (downstream tooling can
|
|
36
|
+
* key on the envelope without first sniffing for "is it an array").
|
|
37
|
+
*
|
|
38
|
+
* The summary + warnings + unresolved fields tag along so the file
|
|
39
|
+
* is self-describing — operators don't need to re-run the import to
|
|
40
|
+
* see what the adapter did.
|
|
41
|
+
*/
|
|
42
|
+
export function writeImportResult(path, result) {
|
|
43
|
+
const envelope = {
|
|
44
|
+
summary: result.summary,
|
|
45
|
+
documents: result.documents,
|
|
46
|
+
warnings: result.warnings,
|
|
47
|
+
unresolved: result.unresolved,
|
|
48
|
+
};
|
|
49
|
+
writeFileSync(path, JSON.stringify(envelope, null, 2) + '\n', 'utf-8');
|
|
50
|
+
}
|
|
51
|
+
export class UnknownAdapterError extends Error {
|
|
52
|
+
adapter;
|
|
53
|
+
available;
|
|
54
|
+
constructor(adapter, available) {
|
|
55
|
+
super(`Unknown adapter '${adapter}'. Available adapters: ${available}.`);
|
|
56
|
+
this.name = 'UnknownAdapterError';
|
|
57
|
+
this.adapter = adapter;
|
|
58
|
+
this.available = available;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner.js","sourceRoot":"","sources":["../src/runner.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAEvC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AAGxD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAAC,UAA4B;IAC1D,MAAM,OAAO,GAAG,UAAU,CAAC,UAAU,CAAC,OAAO,CAAC,CAAA;IAC9C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,SAAS,GACb,YAAY,EAAE;aACX,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;aAClB,IAAI,CAAC,IAAI,CAAC,IAAI,mBAAmB,CAAA;QACtC,MAAM,IAAI,mBAAmB,CAAC,UAAU,CAAC,OAAO,EAAE,SAAS,CAAC,CAAA;IAC9D,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QAC/B,KAAK,EAAE,UAAU,CAAC,KAAK;QACvB,MAAM,EAAE,UAAU,CAAC,MAAM;QACzB,aAAa,EAAE,UAAU,CAAC,aAAa;KACxC,C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