diff --git a/src/storage/project.ts b/src/storage/project.ts index 215f7fcc..1fc22afc 100644 --- a/src/storage/project.ts +++ b/src/storage/project.ts @@ -20,12 +20,12 @@ */ import JSZip from 'jszip'; -import * as semver from 'semver'; import * as commonStorage from './common_storage'; import * as storageModule from './module'; import * as storageModuleContent from './module_content'; import * as storageNames from './names'; +import { upgradeProjectIfNecessary } from './upgrade_project'; // Types, constants, and functions related to projects, regardless of where the projects are stored. @@ -37,7 +37,7 @@ export type Project = { }; const NO_VERSION = '0.0.0'; -const CURRENT_VERSION = '0.0.1'; +export const CURRENT_VERSION = '0.0.1'; type ProjectInfo = { version: string, @@ -62,7 +62,7 @@ export async function listProjectNames(storage: commonStorage.Storage): Promise< */ export async function fetchProject( storage: commonStorage.Storage, projectName: string): Promise { - await updateProjectIfNecessary(storage, projectName); + await upgradeProjectIfNecessary(storage, projectName); const modulePaths: string[] = await storage.listFilePaths( storageNames.makeModulePathRegexPattern(projectName)); @@ -545,7 +545,7 @@ async function deleteProjectInfo( await storage.deleteFile(projectInfoPath); } -async function fetchProjectInfo( +export async function fetchProjectInfo( storage: commonStorage.Storage, projectName: string): Promise { const projectInfoPath = storageNames.makeProjectInfoPath(projectName); let projectInfo: ProjectInfo; @@ -560,19 +560,3 @@ async function fetchProjectInfo( } return projectInfo; } - -// TODO(lizlooney): Move updateProjectIfNecessary to it's own file. -async function updateProjectIfNecessary( - storage: commonStorage.Storage, projectName: string): Promise { - const projectInfo = await fetchProjectInfo(storage, projectName); - if (semver.lt(projectInfo.version, CURRENT_VERSION)) { - switch (projectInfo.version) { - case '0.0.0': - // Project was saved without a project.info.json file. - // Nothing needs to be done to update to '0.0.1'; - projectInfo.version = '0.0.1'; - break; - } - await saveProjectInfo(storage, projectName); - } -} diff --git a/src/storage/upgrade_project.ts b/src/storage/upgrade_project.ts new file mode 100644 index 00000000..8aa6687e --- /dev/null +++ b/src/storage/upgrade_project.ts @@ -0,0 +1,41 @@ +/** + * @license + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @author lizlooney@google.com (Liz Looney) + */ + +import * as semver from 'semver'; + +import * as commonStorage from './common_storage'; +import * as storageProject from './project'; + + +export async function upgradeProjectIfNecessary( + storage: commonStorage.Storage, projectName: string): Promise { + const projectInfo = await storageProject.fetchProjectInfo(storage, projectName); + if (semver.lt(projectInfo.version, storageProject.CURRENT_VERSION)) { + switch (projectInfo.version) { + case '0.0.0': + // Project was saved without a project.info.json file. + // Nothing needs to be done to upgrade to '0.0.1'; + projectInfo.version = '0.0.1'; + break; + } + await storageProject.saveProjectInfo(storage, projectName); + } +}