Skip to content

Commit 32fc8dd

Browse files
committed
Install npm package semver.
In project.ts: Added functions fetchProjectInfo and updateProjectIfNecessary. Call updateProjectIfNecessary in fetchProject.
1 parent 2a60d38 commit 32fc8dd

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

package-lock.json

Lines changed: 35 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"react-dom": "^19.1.0",
2525
"react-i18next": "^15.6.0",
2626
"react-syntax-highlighter": "^15.6.1",
27+
"semver": "^7.7.2",
2728
"web-vitals": "^5.0.3"
2829
},
2930
"scripts": {
@@ -56,6 +57,7 @@
5657
"@shadcn/ui": "^0.0.4",
5758
"@types/node": "^24.0.15",
5859
"@types/react-syntax-highlighter": "^15.5.13",
60+
"@types/semver": "^7.7.0",
5961
"@vitejs/plugin-react": "^4.7.0",
6062
"autoprefixer": "^10.4.21",
6163
"playwright": "^1.54.1",

src/storage/project.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
import JSZip from 'jszip';
23+
import * as semver from 'semver';
2324

2425
import * as commonStorage from './common_storage';
2526
import * as storageModule from './module';
@@ -35,6 +36,7 @@ export type Project = {
3536
opModes: storageModule.OpMode[],
3637
};
3738

39+
const NO_VERSION = '0.0.0';
3840
const CURRENT_VERSION = '0.0.1';
3941

4042
type ProjectInfo = {
@@ -60,6 +62,8 @@ export async function listProjectNames(storage: commonStorage.Storage): Promise<
6062
*/
6163
export async function fetchProject(
6264
storage: commonStorage.Storage, projectName: string): Promise<Project> {
65+
await updateProjectIfNecessary(storage, projectName);
66+
6367
const modulePaths: string[] = await storage.listFilePaths(
6468
storageNames.makeModulePathRegexPattern(projectName));
6569

@@ -540,3 +544,34 @@ async function deleteProjectInfo(
540544
const projectInfoPath = storageNames.makeProjectInfoPath(projectName);
541545
await storage.deleteFile(projectInfoPath);
542546
}
547+
548+
async function fetchProjectInfo(
549+
storage: commonStorage.Storage, projectName: string): Promise<ProjectInfo> {
550+
const projectInfoPath = storageNames.makeProjectInfoPath(projectName);
551+
let projectInfo: ProjectInfo;
552+
try {
553+
const projectInfoContentText = await storage.fetchFileContentText(projectInfoPath);
554+
projectInfo = parseProjectInfoContentText(projectInfoContentText);
555+
} catch (error) {
556+
// The file doesn't exist.
557+
projectInfo = {
558+
version: NO_VERSION,
559+
};
560+
}
561+
return projectInfo;
562+
}
563+
564+
async function updateProjectIfNecessary(
565+
storage: commonStorage.Storage, projectName: string): Promise<void> {
566+
const projectInfo = await fetchProjectInfo(storage, projectName);
567+
if (semver.lt(projectInfo.version, CURRENT_VERSION)) {
568+
switch (projectInfo.version) {
569+
case '0.0.0':
570+
// Project was saved without a project.info.json file.
571+
// Nothing needs to be done to update to '0.0.1';
572+
projectInfo.version = '0.0.1';
573+
break;
574+
}
575+
await saveProjectInfo(storage, projectName);
576+
}
577+
}

0 commit comments

Comments
 (0)