Skip to content

Commit 7317272

Browse files
authored
Move upgrade project code to its own file. (#212)
1 parent 322e311 commit 7317272

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

src/storage/project.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
*/
2121

2222
import JSZip from 'jszip';
23-
import * as semver from 'semver';
2423

2524
import * as commonStorage from './common_storage';
2625
import * as storageModule from './module';
2726
import * as storageModuleContent from './module_content';
2827
import * as storageNames from './names';
28+
import { upgradeProjectIfNecessary } from './upgrade_project';
2929

3030
// Types, constants, and functions related to projects, regardless of where the projects are stored.
3131

@@ -37,7 +37,7 @@ export type Project = {
3737
};
3838

3939
const NO_VERSION = '0.0.0';
40-
const CURRENT_VERSION = '0.0.1';
40+
export const CURRENT_VERSION = '0.0.1';
4141

4242
type ProjectInfo = {
4343
version: string,
@@ -62,7 +62,7 @@ export async function listProjectNames(storage: commonStorage.Storage): Promise<
6262
*/
6363
export async function fetchProject(
6464
storage: commonStorage.Storage, projectName: string): Promise<Project> {
65-
await updateProjectIfNecessary(storage, projectName);
65+
await upgradeProjectIfNecessary(storage, projectName);
6666

6767
const modulePaths: string[] = await storage.listFilePaths(
6868
storageNames.makeModulePathRegexPattern(projectName));
@@ -545,7 +545,7 @@ async function deleteProjectInfo(
545545
await storage.deleteFile(projectInfoPath);
546546
}
547547

548-
async function fetchProjectInfo(
548+
export async function fetchProjectInfo(
549549
storage: commonStorage.Storage, projectName: string): Promise<ProjectInfo> {
550550
const projectInfoPath = storageNames.makeProjectInfoPath(projectName);
551551
let projectInfo: ProjectInfo;
@@ -560,19 +560,3 @@ async function fetchProjectInfo(
560560
}
561561
return projectInfo;
562562
}
563-
564-
// TODO(lizlooney): Move updateProjectIfNecessary to it's own file.
565-
async function updateProjectIfNecessary(
566-
storage: commonStorage.Storage, projectName: string): Promise<void> {
567-
const projectInfo = await fetchProjectInfo(storage, projectName);
568-
if (semver.lt(projectInfo.version, CURRENT_VERSION)) {
569-
switch (projectInfo.version) {
570-
case '0.0.0':
571-
// Project was saved without a project.info.json file.
572-
// Nothing needs to be done to update to '0.0.1';
573-
projectInfo.version = '0.0.1';
574-
break;
575-
}
576-
await saveProjectInfo(storage, projectName);
577-
}
578-
}

src/storage/upgrade_project.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @author [email protected] (Liz Looney)
20+
*/
21+
22+
import * as semver from 'semver';
23+
24+
import * as commonStorage from './common_storage';
25+
import * as storageProject from './project';
26+
27+
28+
export async function upgradeProjectIfNecessary(
29+
storage: commonStorage.Storage, projectName: string): Promise<void> {
30+
const projectInfo = await storageProject.fetchProjectInfo(storage, projectName);
31+
if (semver.lt(projectInfo.version, storageProject.CURRENT_VERSION)) {
32+
switch (projectInfo.version) {
33+
case '0.0.0':
34+
// Project was saved without a project.info.json file.
35+
// Nothing needs to be done to upgrade to '0.0.1';
36+
projectInfo.version = '0.0.1';
37+
break;
38+
}
39+
await storageProject.saveProjectInfo(storage, projectName);
40+
}
41+
}

0 commit comments

Comments
 (0)