Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/reactComponents/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export function Component(props: MenuProps): React.JSX.Element {
}

try {
const blobUrl = await storageProject.downloadProject(props.storage, props.project.projectName);
const blobUrl = await storageProject.downloadProject(props.storage, props.project);
const filename = props.project.projectName + storageNames.UPLOAD_DOWNLOAD_FILE_EXTENSION;

// Create a temporary link to download the file
Expand Down
10 changes: 7 additions & 3 deletions src/storage/client_side_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,12 @@ class ClientSideStorage implements commonStorage.Storage {
}

async listModules(
opt_modulePathFilter?: commonStorage.ModulePathFilter):
Promise<{[path: string]: storageModuleContent.ModuleContent}> {
opt_modulePathRegexPattern?: string
): Promise<{[path: string]: storageModuleContent.ModuleContent}> {

const regExp = opt_modulePathRegexPattern
? new RegExp(opt_modulePathRegexPattern)
: null;
return new Promise((resolve, reject) => {
const pathToModuleContent: {[path: string]: storageModuleContent.ModuleContent} = {};
const openCursorRequest = this.db.transaction([MODULES_STORE_NAME], 'readonly')
Expand All @@ -144,7 +148,7 @@ class ClientSideStorage implements commonStorage.Storage {
const value = cursor.value;
// TODO(lizlooney): do we need value.path? Is there another way to get the path?
const modulePath = value.path;
if (!opt_modulePathFilter || opt_modulePathFilter(modulePath)) {
if (!regExp || regExp.test(modulePath)) {
const moduleContent = storageModuleContent.parseModuleContentText(value.content);
pathToModuleContent[modulePath] = moduleContent;
}
Expand Down
12 changes: 9 additions & 3 deletions src/storage/common_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@

import * as storageModuleContent from './module_content';

export type ModulePathFilter = (modulePath: string) => boolean;

export interface Storage {
saveEntry(entryKey: string, entryValue: string): Promise<void>;

fetchEntry(entryKey: string, defaultValue: string): Promise<string>;
listModules(opt_modulePathFilter?: ModulePathFilter): Promise<{[path: string]: storageModuleContent.ModuleContent}>;

listModules(
opt_modulePathRegexPattern?: string
): Promise<{[path: string]: storageModuleContent.ModuleContent}>;

fetchModuleDateModifiedMillis(modulePath: string): Promise<number>;

fetchModuleContentText(modulePath: string): Promise<string>;

saveModule(modulePath: string, moduleContentText: string): Promise<void>;

deleteModule(modulePath: string): Promise<void>;
}
12 changes: 9 additions & 3 deletions src/storage/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ export function snakeCaseToPascalCase(snakeCaseName: string): string {
}

/**
* Returns the module path prefix for the given project name.
* Returns the module path regex pattern for modules in the given project.
*/
export function makeModulePathPrefix(projectName: string): string {
return projectName + '/';
export function makeModulePathRegexPattern(projectName: string): string {
const prefix = projectName + '/';
const suffix = JSON_FILE_EXTENSION;
return '^' + escapeRegExp(prefix) + '.*' + escapeRegExp(suffix) + '$';
}

function escapeRegExp(text: string): string {
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

/**
Expand Down
20 changes: 6 additions & 14 deletions src/storage/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,8 @@ export async function copyProject(
async function renameOrCopyProject(
storage: commonStorage.Storage, project: Project, newProjectName: string,
rename: boolean): Promise<void> {
const modulePathPrefix = storageNames.makeModulePathPrefix(project.projectName);
const pathToModuleContent = await storage.listModules(
(modulePath: string) => modulePath.startsWith(modulePathPrefix));
storageNames.makeModulePathRegexPattern(project.projectName));

for (const modulePath in pathToModuleContent) {
const className = storageNames.getClassName(modulePath);
Expand All @@ -182,9 +181,9 @@ async function renameOrCopyProject(
*/
export async function deleteProject(
storage: commonStorage.Storage, project: Project): Promise<void> {
const modulePathPrefix = storageNames.makeModulePathPrefix(project.projectName);
const pathToModuleContent = await storage.listModules(
(modulePath: string) => modulePath.startsWith(modulePathPrefix));
storageNames.makeModulePathRegexPattern(project.projectName));

for (const modulePath in pathToModuleContent) {
await storage.deleteModule(modulePath);
}
Expand Down Expand Up @@ -286,14 +285,8 @@ export async function copyModuleInProject(
async function renameOrCopyModule(
storage: commonStorage.Storage, project: Project, newClassName: string,
oldModule: storageModule.Module, rename: boolean): Promise<string> {
const pathToModuleContent = await storage.listModules(
(modulePath: string) => modulePath === oldModule.modulePath);
if (! (oldModule.modulePath in pathToModuleContent)) {
throw new Error('Failed to find module with path ' + oldModule.modulePath);
}

const newModulePath = storageNames.makeModulePath(project.projectName, newClassName);
const moduleContentText = pathToModuleContent[oldModule.modulePath].getModuleContentText();
const moduleContentText = await storage.fetchModuleContentText(oldModule.modulePath);
await storage.saveModule(newModulePath, moduleContentText);
if (rename) {
// For rename, delete the old module.
Expand Down Expand Up @@ -403,10 +396,9 @@ export function findModuleByModulePath(project: Project, modulePath: string): st
* Produce the blob for downloading a project.
*/
export async function downloadProject(
storage: commonStorage.Storage, projectName: string): Promise<string> {
const modulePathPrefix = storageNames.makeModulePathPrefix(projectName);
storage: commonStorage.Storage, project: Project): Promise<string> {
const pathToModuleContent = await storage.listModules(
(modulePath: string) => modulePath.startsWith(modulePathPrefix));
storageNames.makeModulePathRegexPattern(project.projectName));

const classNameToModuleContentText: {[className: string]: string} = {}; // value is module content text
for (const modulePath in pathToModuleContent) {
Expand Down