tsdown-migrate 0.17.0-beta.2 → 0.17.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ //#region src/index.d.ts
2
+ interface MigrateOptions {
3
+ cwd?: string;
4
+ dryRun?: boolean;
5
+ }
6
+ declare function migrate({
7
+ cwd,
8
+ dryRun
9
+ }: MigrateOptions): Promise<void>;
10
+ //#endregion
11
+ export { MigrateOptions, migrate };
package/dist/index.mjs ADDED
@@ -0,0 +1,3 @@
1
+ import { t as migrate } from "./src-CeLaPmVr.mjs";
2
+
3
+ export { migrate };
package/dist/run.d.mts ADDED
@@ -0,0 +1 @@
1
+ export { };
package/dist/run.mjs ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+ import { n as name, r as version, t as migrate } from "./src-CeLaPmVr.mjs";
3
+ import process from "node:process";
4
+ import consola from "consola";
5
+ import { cac } from "cac";
6
+
7
+ //#region src/cli.ts
8
+ const cli = cac(name).version(version).help();
9
+ cli.command("", "Migrate a project to tsdown").option("-c, --cwd <path>", "Current working directory to run the migration in").option("-d, --dry-run", "Perform a dry run without making changes").action((options) => migrate(options));
10
+ async function runCLI() {
11
+ cli.parse(process.argv, { run: false });
12
+ try {
13
+ await cli.runMatchedCommand();
14
+ } catch (error) {
15
+ consola.error(error);
16
+ process.exit(1);
17
+ }
18
+ }
19
+
20
+ //#endregion
21
+ //#region src/run.ts
22
+ runCLI();
23
+
24
+ //#endregion
25
+ export { };
@@ -0,0 +1,140 @@
1
+ import { existsSync } from "node:fs";
2
+ import { readFile, unlink, writeFile } from "node:fs/promises";
3
+ import process from "node:process";
4
+ import { getCliCommand, parseNi, run } from "@antfu/ni";
5
+ import { dim, green, red, underline } from "ansis";
6
+ import consola from "consola";
7
+ import { createPatch, createTwoFilesPatch } from "diff";
8
+
9
+ //#region ../../src/utils/format.ts
10
+ function detectIndentation(jsonText) {
11
+ const lines = jsonText.split(/\r?\n/);
12
+ for (const line of lines) {
13
+ const match = line.match(/^(\s+)\S/);
14
+ if (!match) continue;
15
+ if (match[1].includes(" ")) return " ";
16
+ return match[1].length;
17
+ }
18
+ return 2;
19
+ }
20
+
21
+ //#endregion
22
+ //#region package.json
23
+ var name = "tsdown-migrate";
24
+ var version = "0.17.0-beta.3";
25
+
26
+ //#endregion
27
+ //#region src/index.ts
28
+ async function migrate({ cwd, dryRun }) {
29
+ if (dryRun) consola.info("Dry run enabled. No changes were made.");
30
+ else if (!await consola.prompt(`Before proceeding, review the migration guide at ${underline`https://tsdown.dev/guide/migrate-from-tsup`}, as this process will modify your files.\nUncommitted changes will be lost. Use the ${green`--dry-run`} flag to preview changes without applying them.\n\nContinue?`, { type: "confirm" })) {
31
+ consola.warn("Migration cancelled.");
32
+ process.exitCode = 1;
33
+ return;
34
+ }
35
+ if (cwd) process.chdir(cwd);
36
+ let migrated = await migratePackageJson(dryRun);
37
+ if (await migrateTsupConfig(dryRun)) migrated = true;
38
+ if (!migrated) {
39
+ consola.error("No migration performed.");
40
+ process.exitCode = 1;
41
+ return;
42
+ }
43
+ consola.info("Migration completed. Installing dependencies...");
44
+ if (dryRun) consola.info("[dry-run] would run:", await getCliCommand(parseNi, []));
45
+ else {
46
+ await run(parseNi, [], { cwd });
47
+ consola.success("Dependencies installed.");
48
+ }
49
+ }
50
+ const DEP_FIELDS = {
51
+ dependencies: `^${version}`,
52
+ devDependencies: `^${version}`,
53
+ peerDependencies: "*",
54
+ peerDependenciesMeta: null
55
+ };
56
+ async function migratePackageJson(dryRun) {
57
+ if (!existsSync("package.json")) {
58
+ consola.error("No package.json found");
59
+ return false;
60
+ }
61
+ const pkgRaw = await readFile("package.json", "utf8");
62
+ let pkg = JSON.parse(pkgRaw);
63
+ let found = false;
64
+ for (const [field, semver] of Object.entries(DEP_FIELDS)) if (pkg[field]?.tsup) {
65
+ consola.info(`Migrating \`${field}\` to tsdown.`);
66
+ found = true;
67
+ pkg[field] = renameKey(pkg[field], "tsup", "tsdown", semver);
68
+ }
69
+ if (pkg.scripts) {
70
+ for (const key of Object.keys(pkg.scripts)) if (pkg.scripts[key].includes("tsup")) {
71
+ consola.info(`Migrating \`${key}\` script to tsdown`);
72
+ found = true;
73
+ pkg.scripts[key] = pkg.scripts[key].replaceAll(/tsup(?:-node)?/g, "tsdown");
74
+ }
75
+ }
76
+ if (pkg.tsup) {
77
+ consola.info("Migrating `tsup` field in package.json to `tsdown`.");
78
+ found = true;
79
+ pkg = renameKey(pkg, "tsup", "tsdown");
80
+ }
81
+ if (!found) {
82
+ consola.warn("No tsup-related fields found in package.json");
83
+ return false;
84
+ }
85
+ const eol = pkgRaw.endsWith("\n") ? "\n" : "";
86
+ const newPkgRaw = `${JSON.stringify(pkg, null, detectIndentation(pkgRaw))}${eol}`;
87
+ if (dryRun) {
88
+ consola.info("[dry-run] package.json:");
89
+ outputDiff(createPatch("package.json", pkgRaw, newPkgRaw));
90
+ } else {
91
+ await writeFile("package.json", newPkgRaw);
92
+ consola.success("Migrated `package.json`");
93
+ }
94
+ return true;
95
+ }
96
+ const TSUP_FILES = [
97
+ "tsup.config.ts",
98
+ "tsup.config.cts",
99
+ "tsup.config.mts",
100
+ "tsup.config.js",
101
+ "tsup.config.cjs",
102
+ "tsup.config.mjs",
103
+ "tsup.config.json"
104
+ ];
105
+ async function migrateTsupConfig(dryRun) {
106
+ let found = false;
107
+ for (const file of TSUP_FILES) {
108
+ if (!existsSync(file)) continue;
109
+ consola.info(`Found \`${file}\``);
110
+ found = true;
111
+ const tsupConfigRaw = await readFile(file, "utf8");
112
+ const tsupConfig = tsupConfigRaw.replaceAll(/\btsup\b/g, "tsdown").replaceAll(/\bTSUP\b/g, "TSDOWN");
113
+ const renamed = file.replaceAll("tsup", "tsdown");
114
+ if (dryRun) {
115
+ consola.info(`[dry-run] ${file} -> ${renamed}:`);
116
+ outputDiff(createTwoFilesPatch(file, renamed, tsupConfigRaw, tsupConfig));
117
+ } else {
118
+ await writeFile(renamed, tsupConfig, "utf8");
119
+ await unlink(file);
120
+ consola.success(`Migrated \`${file}\` to \`${renamed}\``);
121
+ }
122
+ }
123
+ if (!found) consola.warn("No tsup config found");
124
+ return found;
125
+ }
126
+ function renameKey(obj, oldKey, newKey, newValue) {
127
+ const newObj = {};
128
+ for (const key of Object.keys(obj)) if (key === oldKey) newObj[newKey] = newValue || obj[oldKey];
129
+ else newObj[key] = obj[key];
130
+ return newObj;
131
+ }
132
+ function outputDiff(text) {
133
+ for (const line of text.split("\n")) {
134
+ const color = line[0] === "+" ? green : line[0] === "-" ? red : dim;
135
+ console.info(color(line));
136
+ }
137
+ }
138
+
139
+ //#endregion
140
+ export { name as n, version as r, migrate as t };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsdown-migrate",
3
- "version": "0.17.0-beta.2",
3
+ "version": "0.17.0-beta.3",
4
4
  "description": "A CLI tool to help migrate your project to tsdown.",
5
5
  "type": "module",
6
6
  "license": "MIT",