GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — next-forge/scripts/update.ts at main · zgcoding/next-forge

Expand file tree

/

update.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
214 lines (169 loc) · 5.41 KB
/

update.ts

Copy path

File metadata and controls

214 lines (169 loc) · 5.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { copyFile, mkdir, readFile, rm } from "node:fs/promises";
import { dirname, join } from "node:path";
import {
cancel,
intro,
isCancel,
log,
outro,
select,
spinner,
} from "@clack/prompts";
import {
allInternalContent,
cleanFileName,
exec,
getAvailableVersions,
tempDirName,
url,
} from "./utils.js";
const compareVersions = (a: string, b: string) => {
const [aMajor, aMinor, aPatch] = a.split(".").map(Number);
const [bMajor, bMinor, bPatch] = b.split(".").map(Number);
if (aMajor !== bMajor) {
return aMajor - bMajor;
}
if (aMinor !== bMinor) {
return aMinor - bMinor;
}
return aPatch - bPatch;
};
const createTemporaryDirectory = async (name: string) => {
const cwd = process.cwd();
const tempDir = join(cwd, name);
await rm(tempDir, { recursive: true, force: true });
await mkdir(tempDir, { recursive: true });
};
const cloneRepository = async (name: string) =>
await exec(`git clone ${url} ${name}`);
const getFiles = async (version: string) => {
await exec(`git checkout ${version}`);
const response = await exec("git ls-files");
const files = response.stdout.toString().trim().split("\n");
return files;
};
const updateFiles = async (files: string[]) => {
const cwd = process.cwd();
const tempDir = join(cwd, tempDirName);
for (const file of files) {
const sourcePath = join(tempDir, file);
const destPath = join(cwd, file);
// Ensure destination directory exists
await mkdir(dirname(destPath), { recursive: true });
await copyFile(sourcePath, destPath);
}
};
const deleteTemporaryDirectory = async () =>
await rm(tempDirName, { recursive: true, force: true });
const getCurrentVersion = async (): Promise<string | undefined> => {
const packageJsonPath = join(process.cwd(), "package.json");
const packageJsonContents = await readFile(packageJsonPath, "utf-8");
const packageJson = JSON.parse(packageJsonContents) as { version?: string };
return packageJson.version;
};
const selectVersion = async (
label: string,
availableVersions: string[],
initialValue: string | undefined
) => {
const version = await select({
message: `Select a version to update ${label}:`,
options: availableVersions.map((v) => ({ value: v, label: `v${v}` })),
initialValue,
maxItems: 10,
});
if (isCancel(version)) {
cancel("Operation cancelled.");
process.exit(0);
}
return version.toString();
};
const getDiff = async (
from: { version: string; files: string[] },
to: { version: string; files: string[] }
) => {
const filesToUpdate: string[] = [];
for (const file of to.files) {
// Skip internal content that is meant to be deleted during init
if (allInternalContent.some((ic) => file.startsWith(ic))) {
continue;
}
const hasChanged =
!from.files.includes(file) ||
(
await exec(
`git diff ${from.version} ${to.version} -- "${cleanFileName(file)}"`,
{ maxBuffer: 1024 * 1024 * 1024 }
)
)
.toString()
.trim() !== "";
if (hasChanged) {
filesToUpdate.push(file);
}
}
return filesToUpdate;
};
export const update = async (options: { from?: string; to?: string }) => {
try {
intro("Let's update your next-forge project!");
const cwd = process.cwd();
const availableVersions = await getAvailableVersions();
let currentVersion = await getCurrentVersion();
// Ditch the project version if it is not in the available versions
if (currentVersion && !availableVersions.includes(currentVersion)) {
currentVersion = undefined;
}
const fromVersion =
options.from ||
(await selectVersion("from", availableVersions, currentVersion));
if (fromVersion === availableVersions[0]) {
outro("You are already on the latest version!");
return;
}
const upgradeableVersions = availableVersions.filter(
(v) => compareVersions(v, fromVersion) > 0
);
const [nextVersion] = upgradeableVersions;
const toVersion =
options.to ||
(await selectVersion("to", upgradeableVersions, nextVersion));
const from = `v${fromVersion}`;
const to = `v${toVersion}`;
const s = spinner();
s.start(`Preparing to update from ${from} to ${to}...`);
s.message("Creating temporary directory...");
await createTemporaryDirectory(tempDirName);
s.message("Cloning next-forge...");
await cloneRepository(tempDirName);
s.message("Moving into repository...");
process.chdir(tempDirName);
s.message(`Getting files from version ${from}...`);
const fromFiles = await getFiles(from);
s.message(`Getting files from version ${to}...`);
const toFiles = await getFiles(to);
s.message(`Computing diff between versions ${from} and ${to}...`);
const diff = await getDiff(
{
version: from,
files: fromFiles,
},
{
version: to,
files: toFiles,
}
);
s.message("Moving back to original directory...");
process.chdir(cwd);
s.message(`Updating ${diff.length} files...`);
await updateFiles(diff);
s.message("Cleaning up...");
await deleteTemporaryDirectory();
s.stop(`Successfully updated project from ${from} to ${to}!`);
outro("Please review and test the changes carefully.");
} catch (error) {
const message = error instanceof Error ? error.message : `${error}`;
log.error(`Failed to update project: ${message}`);
process.exit(1);
}
};
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.