@propel-code/codex-mover 0.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 +100 -0
- package/dist/bin.js +2 -0
- package/dist/cli.js +113 -0
- package/dist/migration.js +690 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# codex-mover
|
|
2
|
+
|
|
3
|
+
`codex-mover` packages portable Codex and Claude Code history/context into a tarball and merges that tarball into another machine's local data.
|
|
4
|
+
|
|
5
|
+
The package installs a primary `codex-mover` CLI and a `codeport` alias for compatibility with the old `codegraph`-embedded command.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
Use without installing:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npx @propel-code/codex-mover --help
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Install globally:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install -g @propel-code/codex-mover
|
|
19
|
+
codex-mover --help
|
|
20
|
+
codeport --help
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Use with Bun:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
bunx @propel-code/codex-mover --help
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
codex-mover pack [--output ./codex-mover-archive.tar.gz] [--provider codex|claude|all]
|
|
33
|
+
codex-mover merge --input ./codex-mover-archive.tar.gz [--provider codex|claude|all] [--path-map /old=/new]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Examples:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
codex-mover pack --output ./tony-context.tar.gz
|
|
40
|
+
codex-mover merge --input ./tony-context.tar.gz
|
|
41
|
+
codex-mover merge --input ./tony-context.tar.gz --path-map /Users/tony/dev=/Users/alice/dev
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Options:
|
|
45
|
+
|
|
46
|
+
- `--input`
|
|
47
|
+
Archive path to merge in `merge` mode.
|
|
48
|
+
- `--output`
|
|
49
|
+
Archive path to create in `pack` mode. Defaults to `./codex-mover-<timestamp>.tar.gz`.
|
|
50
|
+
- `--provider codex|claude|all`
|
|
51
|
+
Package or merge a single provider or both. Default is `all`.
|
|
52
|
+
- `--codex-home PATH`
|
|
53
|
+
Override the Codex home directory.
|
|
54
|
+
- `--claude-config-dir PATH`
|
|
55
|
+
Override the Claude config directory.
|
|
56
|
+
- `--path-map OLD=NEW`
|
|
57
|
+
Rewrite stored project paths during merge. Repeat this flag when multiple roots moved.
|
|
58
|
+
- `--help`
|
|
59
|
+
Print usage information.
|
|
60
|
+
|
|
61
|
+
## What It Copies
|
|
62
|
+
|
|
63
|
+
By default, `codex-mover` includes portable history and context:
|
|
64
|
+
|
|
65
|
+
- Codex: `sessions/`, `archived_sessions/`, `history.jsonl`, `session_index.jsonl`
|
|
66
|
+
- Codex context: `memories/`, `automations/`, `AGENTS.md`, `rules/`, `policy/`, `skills/`
|
|
67
|
+
- Claude Code: `projects/`, `history.jsonl`, `todos/`
|
|
68
|
+
|
|
69
|
+
## What It Excludes
|
|
70
|
+
|
|
71
|
+
By default, `codex-mover` excludes machine- and account-local state:
|
|
72
|
+
|
|
73
|
+
- Codex: `auth.json`, `config.toml`, caches, logs, sqlite state, shell snapshots, temp files, vendor imports, worktrees
|
|
74
|
+
- Claude Code: backups, caches, debug logs, plugins, `settings.json`, shell snapshots, stats caches
|
|
75
|
+
|
|
76
|
+
## Merge Behavior
|
|
77
|
+
|
|
78
|
+
- JSONL history files are merged by unique line content. Session JSONL files prefer the imported copy when both sides identify the same session.
|
|
79
|
+
- Claude project roots can be rewritten with `--path-map`, which updates both file paths and stored `cwd` and `project` references.
|
|
80
|
+
- Non-appendable conflicting files are preserved as `*.imported-<bundle>` siblings instead of overwriting local data.
|
|
81
|
+
|
|
82
|
+
## Development
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
cd /Users/tony/dev/codex-mover
|
|
86
|
+
bun install
|
|
87
|
+
bun test
|
|
88
|
+
bun run typecheck
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Verification
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
bun run typecheck
|
|
95
|
+
bun test
|
|
96
|
+
bun run build
|
|
97
|
+
npx @propel-code/codex-mover --help
|
|
98
|
+
node dist/bin.js pack --provider codex --output /tmp/codex-mover.tar.gz
|
|
99
|
+
node dist/bin.js merge --input /tmp/codex-mover.tar.gz
|
|
100
|
+
```
|
package/dist/bin.js
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { basename, resolve } from "node:path";
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { mergeMigrationArchive, packMigrationArchive, parsePathMap, } from "./migration.js";
|
|
4
|
+
const DEFAULT_COMMAND_NAME = "codex-mover";
|
|
5
|
+
const PROVIDERS = ["codex", "claude", "all"];
|
|
6
|
+
function getCommandName() {
|
|
7
|
+
const entryPath = process.argv[1];
|
|
8
|
+
if (!entryPath) {
|
|
9
|
+
return DEFAULT_COMMAND_NAME;
|
|
10
|
+
}
|
|
11
|
+
const entryName = basename(entryPath);
|
|
12
|
+
if (entryName.endsWith(".js") || entryName.endsWith(".ts")) {
|
|
13
|
+
return DEFAULT_COMMAND_NAME;
|
|
14
|
+
}
|
|
15
|
+
return entryName;
|
|
16
|
+
}
|
|
17
|
+
function buildHelpText(commandName) {
|
|
18
|
+
return `${commandName}
|
|
19
|
+
|
|
20
|
+
Package and merge local Codex and Claude Code history/context between machines.
|
|
21
|
+
|
|
22
|
+
Usage:
|
|
23
|
+
${commandName} pack [--output ./${commandName}-archive.tar.gz] [--provider codex|claude|all]
|
|
24
|
+
${commandName} merge --input ./${commandName}-archive.tar.gz [--provider codex|claude|all] [--path-map /old=/new]
|
|
25
|
+
|
|
26
|
+
Options:
|
|
27
|
+
--input Archive path to merge
|
|
28
|
+
--output, -o Archive path to create
|
|
29
|
+
--provider Provider selection: codex, claude, or all
|
|
30
|
+
--codex-home Override the Codex home directory
|
|
31
|
+
--claude-config-dir Override the Claude config directory
|
|
32
|
+
--path-map Rewrite stored project paths during merge; repeatable
|
|
33
|
+
--help, -h Show this help
|
|
34
|
+
`;
|
|
35
|
+
}
|
|
36
|
+
function printHelp(commandName) {
|
|
37
|
+
process.stdout.write(buildHelpText(commandName));
|
|
38
|
+
}
|
|
39
|
+
function parseProvider(value) {
|
|
40
|
+
const normalized = value?.trim().toLowerCase() ?? "all";
|
|
41
|
+
if (PROVIDERS.includes(normalized)) {
|
|
42
|
+
return normalized;
|
|
43
|
+
}
|
|
44
|
+
throw new Error(`Unsupported provider "${value}". Use codex, claude, or all.`);
|
|
45
|
+
}
|
|
46
|
+
function getDefaultArchiveName(commandName) {
|
|
47
|
+
const timestamp = new Date().toISOString().replaceAll(/[:.]/g, "-");
|
|
48
|
+
return `./${commandName}-${timestamp}.tar.gz`;
|
|
49
|
+
}
|
|
50
|
+
async function main() {
|
|
51
|
+
const commandName = getCommandName();
|
|
52
|
+
const { values, positionals } = parseArgs({
|
|
53
|
+
options: {
|
|
54
|
+
input: { type: "string" },
|
|
55
|
+
output: { type: "string", short: "o" },
|
|
56
|
+
provider: { type: "string" },
|
|
57
|
+
"codex-home": { type: "string" },
|
|
58
|
+
"claude-config-dir": { type: "string" },
|
|
59
|
+
"path-map": { type: "string", multiple: true },
|
|
60
|
+
help: { type: "boolean", short: "h" },
|
|
61
|
+
},
|
|
62
|
+
allowPositionals: true,
|
|
63
|
+
});
|
|
64
|
+
const command = positionals[0]?.trim().toLowerCase();
|
|
65
|
+
if (values.help || !command) {
|
|
66
|
+
printHelp(commandName);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
const provider = parseProvider(values.provider);
|
|
70
|
+
if (command === "pack") {
|
|
71
|
+
const result = await packMigrationArchive({
|
|
72
|
+
outputPath: resolve(values.output ?? getDefaultArchiveName(commandName)),
|
|
73
|
+
provider,
|
|
74
|
+
...(values["codex-home"] ? { codexHome: values["codex-home"] } : {}),
|
|
75
|
+
...(values["claude-config-dir"]
|
|
76
|
+
? { claudeConfigDir: values["claude-config-dir"] }
|
|
77
|
+
: {}),
|
|
78
|
+
});
|
|
79
|
+
process.stdout.write(`${JSON.stringify({
|
|
80
|
+
mode: "pack",
|
|
81
|
+
output: result.outputPath,
|
|
82
|
+
providers: [...new Set(result.manifest.files.map((file) => file.provider))],
|
|
83
|
+
files: result.manifest.files.length,
|
|
84
|
+
bytes: result.manifest.files.reduce((total, file) => total + file.size, 0),
|
|
85
|
+
bundleId: result.manifest.bundleId,
|
|
86
|
+
}, null, 2)}\n`);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (command === "merge") {
|
|
90
|
+
if (!values.input) {
|
|
91
|
+
throw new Error("merge requires --input PATH.");
|
|
92
|
+
}
|
|
93
|
+
const result = await mergeMigrationArchive({
|
|
94
|
+
inputPath: values.input,
|
|
95
|
+
provider,
|
|
96
|
+
...(values["codex-home"] ? { codexHome: values["codex-home"] } : {}),
|
|
97
|
+
...(values["claude-config-dir"]
|
|
98
|
+
? { claudeConfigDir: values["claude-config-dir"] }
|
|
99
|
+
: {}),
|
|
100
|
+
...((values["path-map"] ?? []).length > 0
|
|
101
|
+
? { pathMaps: (values["path-map"] ?? []).map(parsePathMap) }
|
|
102
|
+
: {}),
|
|
103
|
+
});
|
|
104
|
+
process.stdout.write(`${JSON.stringify({ mode: "merge", ...result }, null, 2)}\n`);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
throw new Error(`Unsupported command "${command}". Use pack or merge.`);
|
|
108
|
+
}
|
|
109
|
+
main().catch((error) => {
|
|
110
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
111
|
+
process.stderr.write(`${message}\n`);
|
|
112
|
+
process.exitCode = 1;
|
|
113
|
+
});
|
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { chmod, copyFile, mkdtemp, mkdir, readFile, readdir, rm, stat, writeFile, } from "node:fs/promises";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { basename, dirname, extname, join, relative, resolve, } from "node:path";
|
|
6
|
+
const MANIFEST_VERSION = "1";
|
|
7
|
+
const PAYLOAD_DIRECTORY = "payload";
|
|
8
|
+
const PORTABLE_NOTES = [
|
|
9
|
+
"Portable history and context are included by default.",
|
|
10
|
+
"Account auth, caches, debug logs, sqlite state, shell snapshots, and worktrees are excluded.",
|
|
11
|
+
"Use --path-map OLD=NEW during merge if project roots moved between machines.",
|
|
12
|
+
];
|
|
13
|
+
const CODEX_EXCLUSIONS = [
|
|
14
|
+
"auth.json",
|
|
15
|
+
"config.toml",
|
|
16
|
+
".codex-global-state.json",
|
|
17
|
+
".personality_migration",
|
|
18
|
+
"cache/**",
|
|
19
|
+
"log/**",
|
|
20
|
+
"logs_*.sqlite*",
|
|
21
|
+
"models_cache.json",
|
|
22
|
+
"shell_snapshots/**",
|
|
23
|
+
"sqlite/**",
|
|
24
|
+
"state_*.sqlite*",
|
|
25
|
+
"tmp/**",
|
|
26
|
+
"vendor_imports/**",
|
|
27
|
+
"version.json",
|
|
28
|
+
"worktrees/**",
|
|
29
|
+
];
|
|
30
|
+
const CLAUDE_EXCLUSIONS = [
|
|
31
|
+
"backups/**",
|
|
32
|
+
"cache/**",
|
|
33
|
+
"debug/**",
|
|
34
|
+
"plugins/**",
|
|
35
|
+
"settings.json",
|
|
36
|
+
"shell-snapshots/**",
|
|
37
|
+
"stats-cache.json",
|
|
38
|
+
"statsig/**",
|
|
39
|
+
];
|
|
40
|
+
const PATH_FIELD_NAMES = new Set(["cwd", "originalPath", "project"]);
|
|
41
|
+
function expandProviders(provider) {
|
|
42
|
+
if (provider === "all") {
|
|
43
|
+
return ["codex", "claude"];
|
|
44
|
+
}
|
|
45
|
+
return [provider];
|
|
46
|
+
}
|
|
47
|
+
function normalizeRelativePath(value) {
|
|
48
|
+
return value.split("\\").join("/");
|
|
49
|
+
}
|
|
50
|
+
function resolveCodexHome(codexHome) {
|
|
51
|
+
if (codexHome?.trim()) {
|
|
52
|
+
return resolve(codexHome);
|
|
53
|
+
}
|
|
54
|
+
if (process.env.CODEX_HOME?.trim()) {
|
|
55
|
+
return resolve(process.env.CODEX_HOME);
|
|
56
|
+
}
|
|
57
|
+
return join(homedir(), ".codex");
|
|
58
|
+
}
|
|
59
|
+
async function ensureParentDirectory(filePath) {
|
|
60
|
+
await mkdir(dirname(filePath), { recursive: true });
|
|
61
|
+
}
|
|
62
|
+
async function pathExists(targetPath) {
|
|
63
|
+
try {
|
|
64
|
+
await stat(targetPath);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function resolveClaudeConfigDir(claudeConfigDir) {
|
|
72
|
+
if (claudeConfigDir?.trim()) {
|
|
73
|
+
return resolve(claudeConfigDir);
|
|
74
|
+
}
|
|
75
|
+
if (process.env.CLAUDE_CONFIG_DIR?.trim()) {
|
|
76
|
+
return resolve(process.env.CLAUDE_CONFIG_DIR);
|
|
77
|
+
}
|
|
78
|
+
const dotClaude = join(homedir(), ".claude");
|
|
79
|
+
if (await pathExists(dotClaude)) {
|
|
80
|
+
return dotClaude;
|
|
81
|
+
}
|
|
82
|
+
return join(homedir(), ".config", "claude");
|
|
83
|
+
}
|
|
84
|
+
async function listAllFilesRecursive(rootDirectory) {
|
|
85
|
+
const files = [];
|
|
86
|
+
const stack = [rootDirectory];
|
|
87
|
+
while (stack.length > 0) {
|
|
88
|
+
const currentDirectory = stack.pop();
|
|
89
|
+
if (!currentDirectory) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
let entries;
|
|
93
|
+
try {
|
|
94
|
+
entries = await readdir(currentDirectory, { withFileTypes: true });
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
for (const entry of entries) {
|
|
100
|
+
const fullPath = join(currentDirectory, entry.name);
|
|
101
|
+
if (entry.isDirectory()) {
|
|
102
|
+
stack.push(fullPath);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (entry.isFile()) {
|
|
106
|
+
files.push(fullPath);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return files.sort((left, right) => left.localeCompare(right));
|
|
111
|
+
}
|
|
112
|
+
async function describeFile(descriptor) {
|
|
113
|
+
const fileStats = await stat(descriptor.absolutePath);
|
|
114
|
+
const content = await readFile(descriptor.absolutePath);
|
|
115
|
+
return {
|
|
116
|
+
provider: descriptor.provider,
|
|
117
|
+
category: descriptor.category,
|
|
118
|
+
relativePath: descriptor.relativePath,
|
|
119
|
+
archivePath: `${PAYLOAD_DIRECTORY}/${descriptor.provider}/${descriptor.relativePath}`,
|
|
120
|
+
mergeStrategy: descriptor.relativePath.endsWith(".jsonl") ? "jsonl" : "copy",
|
|
121
|
+
size: fileStats.size,
|
|
122
|
+
sha256: createHash("sha256").update(content).digest("hex"),
|
|
123
|
+
mode: fileStats.mode & 0o777,
|
|
124
|
+
...(descriptor.projectPath ? { projectPath: descriptor.projectPath } : {}),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
async function addFileIfExists(descriptors, provider, providerRoot, relativePath, category, projectPath) {
|
|
128
|
+
const absolutePath = join(providerRoot, ...relativePath.split("/"));
|
|
129
|
+
if (!(await pathExists(absolutePath))) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
descriptors.push({
|
|
133
|
+
provider,
|
|
134
|
+
category,
|
|
135
|
+
relativePath,
|
|
136
|
+
absolutePath,
|
|
137
|
+
...(projectPath ? { projectPath } : {}),
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
async function addTreeIfExists(descriptors, provider, providerRoot, relativeRoot, category, projectPath) {
|
|
141
|
+
const absoluteRoot = join(providerRoot, ...relativeRoot.split("/"));
|
|
142
|
+
if (!(await pathExists(absoluteRoot))) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const files = await listAllFilesRecursive(absoluteRoot);
|
|
146
|
+
for (const absolutePath of files) {
|
|
147
|
+
const relativePath = normalizeRelativePath(relative(providerRoot, absolutePath));
|
|
148
|
+
descriptors.push({
|
|
149
|
+
provider,
|
|
150
|
+
category,
|
|
151
|
+
relativePath,
|
|
152
|
+
absolutePath,
|
|
153
|
+
...(projectPath ? { projectPath } : {}),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
function applyPathMapsToString(value, pathMaps) {
|
|
158
|
+
const normalizedValue = value.replaceAll("\\", "/");
|
|
159
|
+
for (const pathMap of pathMaps) {
|
|
160
|
+
if (normalizedValue === pathMap.from) {
|
|
161
|
+
return pathMap.to;
|
|
162
|
+
}
|
|
163
|
+
if (normalizedValue.startsWith(`${pathMap.from}/`)) {
|
|
164
|
+
return `${pathMap.to}${normalizedValue.slice(pathMap.from.length)}`;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return value;
|
|
168
|
+
}
|
|
169
|
+
function rewriteJsonPaths(value, pathMaps, currentKey) {
|
|
170
|
+
if (Array.isArray(value)) {
|
|
171
|
+
return value.map((item) => rewriteJsonPaths(item, pathMaps));
|
|
172
|
+
}
|
|
173
|
+
if (!value || typeof value !== "object") {
|
|
174
|
+
if (typeof value === "string" && currentKey && PATH_FIELD_NAMES.has(currentKey)) {
|
|
175
|
+
return applyPathMapsToString(value, pathMaps);
|
|
176
|
+
}
|
|
177
|
+
return value;
|
|
178
|
+
}
|
|
179
|
+
return Object.fromEntries(Object.entries(value).map(([key, entryValue]) => [
|
|
180
|
+
key,
|
|
181
|
+
rewriteJsonPaths(entryValue, pathMaps, key),
|
|
182
|
+
]));
|
|
183
|
+
}
|
|
184
|
+
function getJsonSortKey(value) {
|
|
185
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
const candidate = ["timestamp", "ts", "updated_at"]
|
|
189
|
+
.map((key) => value[key])
|
|
190
|
+
.find((entry) => typeof entry === "string" || typeof entry === "number");
|
|
191
|
+
if (typeof candidate === "number") {
|
|
192
|
+
return `n:${String(candidate).padStart(20, "0")}`;
|
|
193
|
+
}
|
|
194
|
+
if (typeof candidate !== "string") {
|
|
195
|
+
return null;
|
|
196
|
+
}
|
|
197
|
+
const parsed = Date.parse(candidate);
|
|
198
|
+
if (!Number.isNaN(parsed)) {
|
|
199
|
+
return `n:${String(parsed).padStart(20, "0")}`;
|
|
200
|
+
}
|
|
201
|
+
return `s:${candidate}`;
|
|
202
|
+
}
|
|
203
|
+
function transformJsonContent(content, pathMaps) {
|
|
204
|
+
const parsed = JSON.parse(content);
|
|
205
|
+
const rewritten = rewriteJsonPaths(parsed, pathMaps);
|
|
206
|
+
return `${JSON.stringify(rewritten, null, 2)}\n`;
|
|
207
|
+
}
|
|
208
|
+
function transformJsonlContent(content, pathMaps) {
|
|
209
|
+
const lines = content.split("\n");
|
|
210
|
+
const output = [];
|
|
211
|
+
for (const rawLine of lines) {
|
|
212
|
+
if (!rawLine.trim()) {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const parsed = JSON.parse(rawLine);
|
|
217
|
+
const rewritten = rewriteJsonPaths(parsed, pathMaps);
|
|
218
|
+
output.push(JSON.stringify(rewritten));
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
output.push(rawLine);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return output.length === 0 ? "" : `${output.join("\n")}\n`;
|
|
225
|
+
}
|
|
226
|
+
async function readTransformedContent(sourcePath, relativePath, pathMaps) {
|
|
227
|
+
const content = await readFile(sourcePath);
|
|
228
|
+
if (pathMaps.length === 0) {
|
|
229
|
+
return content;
|
|
230
|
+
}
|
|
231
|
+
if (relativePath.endsWith(".json")) {
|
|
232
|
+
return Buffer.from(transformJsonContent(content.toString("utf8"), pathMaps), "utf8");
|
|
233
|
+
}
|
|
234
|
+
if (relativePath.endsWith(".jsonl")) {
|
|
235
|
+
return Buffer.from(transformJsonlContent(content.toString("utf8"), pathMaps), "utf8");
|
|
236
|
+
}
|
|
237
|
+
return content;
|
|
238
|
+
}
|
|
239
|
+
function parseJsonlRecords(content) {
|
|
240
|
+
const records = [];
|
|
241
|
+
for (const rawLine of content.split("\n")) {
|
|
242
|
+
if (!rawLine.trim()) {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const parsed = JSON.parse(rawLine);
|
|
247
|
+
records.push({
|
|
248
|
+
line: JSON.stringify(parsed),
|
|
249
|
+
sortKey: getJsonSortKey(parsed),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
records.push({
|
|
254
|
+
line: rawLine,
|
|
255
|
+
sortKey: null,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return records;
|
|
260
|
+
}
|
|
261
|
+
function getSessionRecordIdentity(value) {
|
|
262
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
const record = value;
|
|
266
|
+
if (typeof record.sessionId === "string" && record.sessionId.trim()) {
|
|
267
|
+
return `sessionId:${record.sessionId}`;
|
|
268
|
+
}
|
|
269
|
+
if (typeof record.session_id === "string" && record.session_id.trim()) {
|
|
270
|
+
return `session_id:${record.session_id}`;
|
|
271
|
+
}
|
|
272
|
+
if (record.payload &&
|
|
273
|
+
typeof record.payload === "object" &&
|
|
274
|
+
!Array.isArray(record.payload)) {
|
|
275
|
+
const payload = record.payload;
|
|
276
|
+
if (typeof payload.sessionId === "string" && payload.sessionId.trim()) {
|
|
277
|
+
return `payload.sessionId:${payload.sessionId}`;
|
|
278
|
+
}
|
|
279
|
+
if (record.type === "session_meta" &&
|
|
280
|
+
typeof payload.id === "string" &&
|
|
281
|
+
payload.id.trim()) {
|
|
282
|
+
return `payload.id:${payload.id}`;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
if (record.type === "session_meta" &&
|
|
286
|
+
typeof record.id === "string" &&
|
|
287
|
+
record.id.trim()) {
|
|
288
|
+
return `id:${record.id}`;
|
|
289
|
+
}
|
|
290
|
+
return null;
|
|
291
|
+
}
|
|
292
|
+
function extractSessionFileIdentity(content) {
|
|
293
|
+
for (const rawLine of content.split("\n")) {
|
|
294
|
+
if (!rawLine.trim()) {
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
try {
|
|
298
|
+
const parsed = JSON.parse(rawLine);
|
|
299
|
+
const identity = getSessionRecordIdentity(parsed);
|
|
300
|
+
if (identity) {
|
|
301
|
+
return identity;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
function isSessionJsonlPath(relativePath) {
|
|
311
|
+
return (relativePath.endsWith(".jsonl") &&
|
|
312
|
+
(relativePath.startsWith("sessions/") ||
|
|
313
|
+
relativePath.startsWith("archived_sessions/") ||
|
|
314
|
+
relativePath.startsWith("projects/")));
|
|
315
|
+
}
|
|
316
|
+
function mergeJsonlContent(targetContent, sourceContent) {
|
|
317
|
+
const targetRecords = parseJsonlRecords(targetContent);
|
|
318
|
+
const sourceRecords = parseJsonlRecords(sourceContent);
|
|
319
|
+
const seen = new Set(targetRecords.map((record) => record.line));
|
|
320
|
+
const newRecords = sourceRecords.filter((record) => {
|
|
321
|
+
if (seen.has(record.line)) {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
seen.add(record.line);
|
|
325
|
+
return true;
|
|
326
|
+
});
|
|
327
|
+
if (newRecords.length === 0) {
|
|
328
|
+
return { mergedContent: targetContent, changed: false };
|
|
329
|
+
}
|
|
330
|
+
const mergedRecords = [...targetRecords, ...newRecords];
|
|
331
|
+
const canSort = mergedRecords.every((record) => record.sortKey !== null);
|
|
332
|
+
if (canSort) {
|
|
333
|
+
mergedRecords.sort((left, right) => (left.sortKey ?? "").localeCompare(right.sortKey ?? ""));
|
|
334
|
+
}
|
|
335
|
+
return {
|
|
336
|
+
mergedContent: `${mergedRecords.map((record) => record.line).join("\n")}\n`,
|
|
337
|
+
changed: true,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
function mergeSessionJsonlContent(targetContent, sourceContent) {
|
|
341
|
+
const targetIdentity = extractSessionFileIdentity(targetContent);
|
|
342
|
+
const sourceIdentity = extractSessionFileIdentity(sourceContent);
|
|
343
|
+
if (targetIdentity && sourceIdentity && targetIdentity === sourceIdentity) {
|
|
344
|
+
return {
|
|
345
|
+
mergedContent: sourceContent,
|
|
346
|
+
changed: targetContent !== sourceContent,
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
return mergeJsonlContent(targetContent, sourceContent);
|
|
350
|
+
}
|
|
351
|
+
function stripTrailingSlash(value) {
|
|
352
|
+
if (value.length <= 1) {
|
|
353
|
+
return value;
|
|
354
|
+
}
|
|
355
|
+
return value.replace(/\/+$/, "");
|
|
356
|
+
}
|
|
357
|
+
export function parsePathMap(value) {
|
|
358
|
+
const separatorIndex = value.indexOf("=");
|
|
359
|
+
if (separatorIndex <= 0 || separatorIndex === value.length - 1) {
|
|
360
|
+
throw new Error(`Invalid --path-map "${value}". Use OLD=NEW.`);
|
|
361
|
+
}
|
|
362
|
+
const from = stripTrailingSlash(resolve(value.slice(0, separatorIndex)));
|
|
363
|
+
const to = stripTrailingSlash(resolve(value.slice(separatorIndex + 1)));
|
|
364
|
+
return { from, to };
|
|
365
|
+
}
|
|
366
|
+
function encodeClaudeProjectDirectory(projectPath) {
|
|
367
|
+
return projectPath.replaceAll("\\", "/").replaceAll("/", "-");
|
|
368
|
+
}
|
|
369
|
+
async function inferProjectPathFromJsonl(sessionPath) {
|
|
370
|
+
const content = await readFile(sessionPath, "utf8");
|
|
371
|
+
for (const rawLine of content.split("\n")) {
|
|
372
|
+
if (!rawLine.trim()) {
|
|
373
|
+
continue;
|
|
374
|
+
}
|
|
375
|
+
try {
|
|
376
|
+
const parsed = JSON.parse(rawLine);
|
|
377
|
+
const directProject = typeof parsed.project === "string"
|
|
378
|
+
? parsed.project
|
|
379
|
+
: typeof parsed.cwd === "string"
|
|
380
|
+
? parsed.cwd
|
|
381
|
+
: null;
|
|
382
|
+
if (directProject) {
|
|
383
|
+
return directProject;
|
|
384
|
+
}
|
|
385
|
+
if (parsed.payload &&
|
|
386
|
+
typeof parsed.payload === "object" &&
|
|
387
|
+
!Array.isArray(parsed.payload) &&
|
|
388
|
+
typeof parsed.payload.cwd === "string") {
|
|
389
|
+
const payload = parsed.payload;
|
|
390
|
+
return payload.cwd;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
return null;
|
|
398
|
+
}
|
|
399
|
+
async function getClaudeProjectPaths(claudeRoot) {
|
|
400
|
+
const projectRoot = join(claudeRoot, "projects");
|
|
401
|
+
const projectPaths = new Map();
|
|
402
|
+
let projectEntries;
|
|
403
|
+
try {
|
|
404
|
+
projectEntries = await readdir(projectRoot, { withFileTypes: true });
|
|
405
|
+
}
|
|
406
|
+
catch {
|
|
407
|
+
return projectPaths;
|
|
408
|
+
}
|
|
409
|
+
for (const entry of projectEntries) {
|
|
410
|
+
if (!entry.isDirectory()) {
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
const projectDirectory = join(projectRoot, entry.name);
|
|
414
|
+
const sessionsIndexPath = join(projectDirectory, "sessions-index.json");
|
|
415
|
+
if (await pathExists(sessionsIndexPath)) {
|
|
416
|
+
try {
|
|
417
|
+
const sessionsIndex = JSON.parse(await readFile(sessionsIndexPath, "utf8"));
|
|
418
|
+
if (typeof sessionsIndex.originalPath === "string") {
|
|
419
|
+
projectPaths.set(entry.name, sessionsIndex.originalPath);
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
catch {
|
|
424
|
+
// Ignore malformed indexes and fall back to session inspection.
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
const files = await listAllFilesRecursive(projectDirectory);
|
|
428
|
+
const sessionFile = files.find((filePath) => filePath.endsWith(".jsonl"));
|
|
429
|
+
if (!sessionFile) {
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
const projectPath = await inferProjectPathFromJsonl(sessionFile);
|
|
433
|
+
if (projectPath) {
|
|
434
|
+
projectPaths.set(entry.name, projectPath);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return projectPaths;
|
|
438
|
+
}
|
|
439
|
+
async function collectCodexDescriptors(codexRoot) {
|
|
440
|
+
const descriptors = [];
|
|
441
|
+
await addTreeIfExists(descriptors, "codex", codexRoot, "sessions", "history");
|
|
442
|
+
await addTreeIfExists(descriptors, "codex", codexRoot, "archived_sessions", "history");
|
|
443
|
+
await addFileIfExists(descriptors, "codex", codexRoot, "history.jsonl", "history");
|
|
444
|
+
await addFileIfExists(descriptors, "codex", codexRoot, "session_index.jsonl", "history");
|
|
445
|
+
await addTreeIfExists(descriptors, "codex", codexRoot, "memories", "context");
|
|
446
|
+
await addTreeIfExists(descriptors, "codex", codexRoot, "automations", "context");
|
|
447
|
+
await addFileIfExists(descriptors, "codex", codexRoot, "AGENTS.md", "context");
|
|
448
|
+
await addTreeIfExists(descriptors, "codex", codexRoot, "rules", "context");
|
|
449
|
+
await addTreeIfExists(descriptors, "codex", codexRoot, "policy", "context");
|
|
450
|
+
await addTreeIfExists(descriptors, "codex", codexRoot, "skills", "skills");
|
|
451
|
+
return descriptors;
|
|
452
|
+
}
|
|
453
|
+
async function collectClaudeDescriptors(claudeRoot) {
|
|
454
|
+
const descriptors = [];
|
|
455
|
+
const projectPaths = await getClaudeProjectPaths(claudeRoot);
|
|
456
|
+
const projectRoot = join(claudeRoot, "projects");
|
|
457
|
+
if (await pathExists(projectRoot)) {
|
|
458
|
+
let projectEntries = [];
|
|
459
|
+
try {
|
|
460
|
+
projectEntries = await readdir(projectRoot, { withFileTypes: true });
|
|
461
|
+
}
|
|
462
|
+
catch {
|
|
463
|
+
// Ignore unreadable project roots and fall back to top-level files.
|
|
464
|
+
}
|
|
465
|
+
for (const entry of projectEntries) {
|
|
466
|
+
if (!entry.isDirectory()) {
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
await addTreeIfExists(descriptors, "claude", claudeRoot, `projects/${entry.name}`, "history", projectPaths.get(entry.name));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
await addFileIfExists(descriptors, "claude", claudeRoot, "history.jsonl", "history");
|
|
473
|
+
await addTreeIfExists(descriptors, "claude", claudeRoot, "todos", "history");
|
|
474
|
+
return descriptors;
|
|
475
|
+
}
|
|
476
|
+
async function collectManifest(providers, roots) {
|
|
477
|
+
const descriptors = (await Promise.all(providers.map((provider) => provider === "codex"
|
|
478
|
+
? collectCodexDescriptors(roots.codex)
|
|
479
|
+
: collectClaudeDescriptors(roots.claude)))).flat();
|
|
480
|
+
const files = await Promise.all(descriptors.map((descriptor) => describeFile(descriptor)));
|
|
481
|
+
files.sort((left, right) => left.archivePath.localeCompare(right.archivePath));
|
|
482
|
+
return {
|
|
483
|
+
version: MANIFEST_VERSION,
|
|
484
|
+
bundleId: `${new Date().toISOString().replaceAll(/[:.]/g, "-")}-${randomUUID()}`,
|
|
485
|
+
createdAt: new Date().toISOString(),
|
|
486
|
+
notes: PORTABLE_NOTES,
|
|
487
|
+
exclusions: {
|
|
488
|
+
codex: CODEX_EXCLUSIONS,
|
|
489
|
+
claude: CLAUDE_EXCLUSIONS,
|
|
490
|
+
},
|
|
491
|
+
files,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
async function runTar(args, workingDirectory) {
|
|
495
|
+
await new Promise((resolvePromise, rejectPromise) => {
|
|
496
|
+
const child = spawn("tar", args, {
|
|
497
|
+
cwd: workingDirectory,
|
|
498
|
+
env: {
|
|
499
|
+
...process.env,
|
|
500
|
+
LANG: process.env.LANG ?? "C",
|
|
501
|
+
LC_ALL: process.env.LC_ALL ?? "C",
|
|
502
|
+
},
|
|
503
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
504
|
+
});
|
|
505
|
+
const stderrChunks = [];
|
|
506
|
+
child.stderr.on("data", (chunk) => {
|
|
507
|
+
stderrChunks.push(chunk);
|
|
508
|
+
});
|
|
509
|
+
child.on("error", rejectPromise);
|
|
510
|
+
child.on("exit", (code) => {
|
|
511
|
+
if (code === 0) {
|
|
512
|
+
resolvePromise();
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const stderr = Buffer.concat(stderrChunks).toString("utf8").trim();
|
|
516
|
+
rejectPromise(new Error(stderr || `tar exited with status ${code ?? "unknown"}.`));
|
|
517
|
+
});
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
function getArchiveCompression(archivePath) {
|
|
521
|
+
const normalized = archivePath.toLowerCase();
|
|
522
|
+
if (normalized.endsWith(".tar")) {
|
|
523
|
+
return "none";
|
|
524
|
+
}
|
|
525
|
+
return "gz";
|
|
526
|
+
}
|
|
527
|
+
async function writeBundleArchive(stagingDirectory, outputPath) {
|
|
528
|
+
const compression = getArchiveCompression(outputPath);
|
|
529
|
+
const args = compression === "gz"
|
|
530
|
+
? ["-czf", outputPath, "-C", stagingDirectory, "."]
|
|
531
|
+
: ["-cf", outputPath, "-C", stagingDirectory, "."];
|
|
532
|
+
await runTar(args, stagingDirectory);
|
|
533
|
+
}
|
|
534
|
+
async function extractBundleArchive(inputPath, extractDirectory) {
|
|
535
|
+
const compression = getArchiveCompression(inputPath);
|
|
536
|
+
const args = compression === "gz"
|
|
537
|
+
? ["-xzf", inputPath, "-C", extractDirectory]
|
|
538
|
+
: ["-xf", inputPath, "-C", extractDirectory];
|
|
539
|
+
await runTar(args, extractDirectory);
|
|
540
|
+
}
|
|
541
|
+
function resolveProviderRoot(provider, roots) {
|
|
542
|
+
return provider === "codex" ? roots.codex : roots.claude;
|
|
543
|
+
}
|
|
544
|
+
function getTargetRelativePath(entry, pathMaps) {
|
|
545
|
+
if (entry.provider !== "claude" ||
|
|
546
|
+
pathMaps.length === 0 ||
|
|
547
|
+
!entry.relativePath.startsWith("projects/") ||
|
|
548
|
+
!entry.projectPath) {
|
|
549
|
+
return entry.relativePath;
|
|
550
|
+
}
|
|
551
|
+
const mappedProjectPath = applyPathMapsToString(entry.projectPath, pathMaps);
|
|
552
|
+
if (mappedProjectPath === entry.projectPath) {
|
|
553
|
+
return entry.relativePath;
|
|
554
|
+
}
|
|
555
|
+
const parts = entry.relativePath.split("/");
|
|
556
|
+
if (parts.length < 2) {
|
|
557
|
+
return entry.relativePath;
|
|
558
|
+
}
|
|
559
|
+
const remainder = parts.slice(2);
|
|
560
|
+
return ["projects", encodeClaudeProjectDirectory(mappedProjectPath), ...remainder].join("/");
|
|
561
|
+
}
|
|
562
|
+
function getConflictPath(targetPath, bundleId) {
|
|
563
|
+
const extension = extname(targetPath);
|
|
564
|
+
const stem = extension ? basename(targetPath, extension) : basename(targetPath);
|
|
565
|
+
const suffix = bundleId.slice(0, 12);
|
|
566
|
+
return join(dirname(targetPath), extension
|
|
567
|
+
? `${stem}.imported-${suffix}${extension}`
|
|
568
|
+
: `${stem}.imported-${suffix}`);
|
|
569
|
+
}
|
|
570
|
+
async function writeTargetFile(targetPath, content, mode) {
|
|
571
|
+
await ensureParentDirectory(targetPath);
|
|
572
|
+
await writeFile(targetPath, content);
|
|
573
|
+
await chmod(targetPath, mode);
|
|
574
|
+
}
|
|
575
|
+
async function withTemporaryDirectory(prefix, fn) {
|
|
576
|
+
const directory = await mkdtemp(join(process.env.TMPDIR ?? "/tmp", prefix));
|
|
577
|
+
try {
|
|
578
|
+
return await fn(directory);
|
|
579
|
+
}
|
|
580
|
+
finally {
|
|
581
|
+
await rm(directory, { recursive: true, force: true });
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
export async function packMigrationArchive(options) {
|
|
585
|
+
const providers = expandProviders(options.provider);
|
|
586
|
+
const roots = {
|
|
587
|
+
codex: resolveCodexHome(options.codexHome),
|
|
588
|
+
claude: await resolveClaudeConfigDir(options.claudeConfigDir),
|
|
589
|
+
};
|
|
590
|
+
const manifest = await collectManifest(providers, roots);
|
|
591
|
+
if (manifest.files.length === 0) {
|
|
592
|
+
throw new Error("No portable Codex or Claude Code data was found to package.");
|
|
593
|
+
}
|
|
594
|
+
const outputPath = resolve(options.outputPath);
|
|
595
|
+
await ensureParentDirectory(outputPath);
|
|
596
|
+
await withTemporaryDirectory("codex-mover-pack-", async (stagingDirectory) => {
|
|
597
|
+
await mkdir(join(stagingDirectory, PAYLOAD_DIRECTORY), { recursive: true });
|
|
598
|
+
for (const entry of manifest.files) {
|
|
599
|
+
const providerRoot = resolveProviderRoot(entry.provider, roots);
|
|
600
|
+
const sourcePath = join(providerRoot, ...entry.relativePath.split("/"));
|
|
601
|
+
const destinationPath = join(stagingDirectory, ...entry.archivePath.split("/"));
|
|
602
|
+
await ensureParentDirectory(destinationPath);
|
|
603
|
+
await copyFile(sourcePath, destinationPath);
|
|
604
|
+
}
|
|
605
|
+
await writeFile(join(stagingDirectory, "manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
|
|
606
|
+
await writeBundleArchive(stagingDirectory, outputPath);
|
|
607
|
+
});
|
|
608
|
+
return {
|
|
609
|
+
outputPath,
|
|
610
|
+
manifest,
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
async function loadManifest(extractedDirectory) {
|
|
614
|
+
const manifestPath = join(extractedDirectory, "manifest.json");
|
|
615
|
+
if (!(await pathExists(manifestPath))) {
|
|
616
|
+
throw new Error("Archive is missing manifest.json.");
|
|
617
|
+
}
|
|
618
|
+
const manifest = JSON.parse(await readFile(manifestPath, "utf8"));
|
|
619
|
+
if (manifest.version !== MANIFEST_VERSION || !Array.isArray(manifest.files)) {
|
|
620
|
+
throw new Error("Archive manifest is invalid or unsupported.");
|
|
621
|
+
}
|
|
622
|
+
return manifest;
|
|
623
|
+
}
|
|
624
|
+
export async function mergeMigrationArchive(options) {
|
|
625
|
+
const providers = expandProviders(options.provider);
|
|
626
|
+
const pathMaps = [...(options.pathMaps ?? [])].sort((left, right) => right.from.length - left.from.length);
|
|
627
|
+
const roots = {
|
|
628
|
+
codex: resolveCodexHome(options.codexHome),
|
|
629
|
+
claude: await resolveClaudeConfigDir(options.claudeConfigDir),
|
|
630
|
+
};
|
|
631
|
+
const inputPath = resolve(options.inputPath);
|
|
632
|
+
return withTemporaryDirectory("codex-mover-merge-", async (extractDirectory) => {
|
|
633
|
+
await extractBundleArchive(inputPath, extractDirectory);
|
|
634
|
+
const manifest = await loadManifest(extractDirectory);
|
|
635
|
+
const result = {
|
|
636
|
+
inputPath,
|
|
637
|
+
bundleId: manifest.bundleId,
|
|
638
|
+
providers,
|
|
639
|
+
copied: 0,
|
|
640
|
+
merged: 0,
|
|
641
|
+
skipped: 0,
|
|
642
|
+
conflicts: 0,
|
|
643
|
+
filesProcessed: 0,
|
|
644
|
+
};
|
|
645
|
+
for (const entry of manifest.files) {
|
|
646
|
+
if (!providers.includes(entry.provider)) {
|
|
647
|
+
continue;
|
|
648
|
+
}
|
|
649
|
+
const providerRoot = resolveProviderRoot(entry.provider, roots);
|
|
650
|
+
const targetRelativePath = getTargetRelativePath(entry, pathMaps);
|
|
651
|
+
const sourcePath = join(extractDirectory, ...entry.archivePath.split("/"));
|
|
652
|
+
const targetPath = join(providerRoot, ...targetRelativePath.split("/"));
|
|
653
|
+
const sourceContent = await readTransformedContent(sourcePath, targetRelativePath, pathMaps);
|
|
654
|
+
result.filesProcessed += 1;
|
|
655
|
+
if (!(await pathExists(targetPath))) {
|
|
656
|
+
await writeTargetFile(targetPath, sourceContent, entry.mode);
|
|
657
|
+
result.copied += 1;
|
|
658
|
+
continue;
|
|
659
|
+
}
|
|
660
|
+
const targetContent = await readFile(targetPath);
|
|
661
|
+
if (targetContent.equals(sourceContent)) {
|
|
662
|
+
result.skipped += 1;
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
if (entry.mergeStrategy === "jsonl") {
|
|
666
|
+
const merged = isSessionJsonlPath(targetRelativePath)
|
|
667
|
+
? mergeSessionJsonlContent(targetContent.toString("utf8"), sourceContent.toString("utf8"))
|
|
668
|
+
: mergeJsonlContent(targetContent.toString("utf8"), sourceContent.toString("utf8"));
|
|
669
|
+
if (!merged.changed) {
|
|
670
|
+
result.skipped += 1;
|
|
671
|
+
continue;
|
|
672
|
+
}
|
|
673
|
+
await writeTargetFile(targetPath, Buffer.from(merged.mergedContent, "utf8"), entry.mode);
|
|
674
|
+
result.merged += 1;
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
const conflictPath = getConflictPath(targetPath, manifest.bundleId);
|
|
678
|
+
if (await pathExists(conflictPath)) {
|
|
679
|
+
const existingConflict = await readFile(conflictPath);
|
|
680
|
+
if (existingConflict.equals(sourceContent)) {
|
|
681
|
+
result.skipped += 1;
|
|
682
|
+
continue;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
await writeTargetFile(conflictPath, sourceContent, entry.mode);
|
|
686
|
+
result.conflicts += 1;
|
|
687
|
+
}
|
|
688
|
+
return result;
|
|
689
|
+
});
|
|
690
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@propel-code/codex-mover",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Package and merge portable Codex and Claude Code history/context between machines.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"packageManager": "bun@1.3.9",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md",
|
|
10
|
+
"LICENSE",
|
|
11
|
+
"package.json"
|
|
12
|
+
],
|
|
13
|
+
"bin": {
|
|
14
|
+
"codex-mover": "./dist/bin.js",
|
|
15
|
+
"codeport": "./dist/bin.js"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsc -p tsconfig.build.json",
|
|
19
|
+
"dev": "bun --watch ./src/cli.ts",
|
|
20
|
+
"start": "bun ./src/cli.ts",
|
|
21
|
+
"test": "bun test --timeout 20000",
|
|
22
|
+
"typecheck": "tsc --noEmit",
|
|
23
|
+
"check": "bun test --timeout 20000 && bun run typecheck",
|
|
24
|
+
"prepack": "npm run build"
|
|
25
|
+
},
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=20"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"codex",
|
|
31
|
+
"claude",
|
|
32
|
+
"cli",
|
|
33
|
+
"migration",
|
|
34
|
+
"portable",
|
|
35
|
+
"backup"
|
|
36
|
+
],
|
|
37
|
+
"license": "Apache-2.0",
|
|
38
|
+
"repository": {
|
|
39
|
+
"type": "git",
|
|
40
|
+
"url": "git+https://github.com/propel-gtm/codex-mover.git"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/propel-gtm/codex-mover#readme",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/propel-gtm/codex-mover/issues"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^24.12.0",
|
|
51
|
+
"typescript": "^5.9.3"
|
|
52
|
+
}
|
|
53
|
+
}
|