Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unexpected file changes when editing #2786

Merged
merged 16 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ describe('dialog operation', () => {
const mockReq = {
params: { projectId },
query: {},
body: { name: 'test.dialog', content: '' },
zhixzhan marked this conversation as resolved.
Show resolved Hide resolved
body: { name: 'test2.dialog', content: '' },
} as Request;
await ProjectController.createFile(mockReq, mockRes);
expect(mockRes.status).toHaveBeenCalledWith(200);
});

it('should remove dialog', async () => {
const mockReq = {
params: { name: 'test.dialog', projectId },
params: { name: 'test2.dialog', projectId },
query: {},
body: {},
} as Request;
Expand Down
4 changes: 0 additions & 4 deletions Composer/packages/server/src/controllers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async function createProject(req: Request, res: Response) {
const currentProject = await BotProjectService.getProjectById(id, user);
if (currentProject !== undefined) {
await currentProject.updateBotInfo(name, description);
await currentProject.init();
zhixzhan marked this conversation as resolved.
Show resolved Hide resolved
const project = currentProject.getProject();
log('Project created successfully.');
res.status(200).json({
Expand All @@ -72,7 +71,6 @@ async function getProjectById(req: Request, res: Response) {
const currentProject = await BotProjectService.getProjectById(projectId, user);

if (currentProject !== undefined && (await currentProject.exists())) {
await currentProject.init();
const project = currentProject.getProject();
res.status(200).json({
id: projectId,
Expand Down Expand Up @@ -109,7 +107,6 @@ async function openProject(req: Request, res: Response) {
const id = await BotProjectService.openProject(location, user);
const currentProject = await BotProjectService.getProjectById(id, user);
if (currentProject !== undefined) {
await currentProject.init();
const project = currentProject.getProject();
res.status(200).json({
id: currentProject.id,
Expand Down Expand Up @@ -151,7 +148,6 @@ async function saveProjectAs(req: Request, res: Response) {
const currentProject = await BotProjectService.getProjectById(id, user);
if (currentProject !== undefined) {
await currentProject.updateBotInfo(name, description);
await currentProject.init();
const project = currentProject.getProject();
res.status(200).json({
id,
Expand Down
10 changes: 5 additions & 5 deletions Composer/packages/server/src/models/bot/botProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class BotProject {
};

public deleteFile = async (name: string) => {
if (Path.resolve(name) === 'Main') {
if (Path.basename(name, '.dialog') === this.name) {
zhixzhan marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Main dialog can't be removed`);
}

Expand Down Expand Up @@ -313,7 +313,7 @@ export class BotProject {
try {
await this.fileStorage.rmDir(folderPath);
} catch (e) {
console.log(e);
// pass
}
}
};
Expand Down Expand Up @@ -383,6 +383,8 @@ export class BotProject {
if (index === -1) {
throw new Error(`no such file at ${relativePath}`);
}
this.files[index].content = content;

const absolutePath = `${this.dir}/${relativePath}`;

// only write if the file has actually changed
Expand All @@ -394,8 +396,6 @@ export class BotProject {
// instead of calling stat again which could be expensive
const stats = await this.fileStorage.stat(absolutePath);

this.files[index].content = content;

return stats.lastModified;
};

Expand All @@ -406,10 +406,10 @@ export class BotProject {
if (index === -1) {
throw new Error(`no such file at ${relativePath}`);
}
this.files.splice(index, 1);

const absolutePath = `${this.dir}/${relativePath}`;
await this.fileStorage.removeFile(absolutePath);
this.files.splice(index, 1);
zhixzhan marked this conversation as resolved.
Show resolved Hide resolved
};

// ensure dir exist, dir is a absolute dir path
Expand Down
24 changes: 19 additions & 5 deletions Composer/packages/server/src/services/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import merge from 'lodash/merge';
import find from 'lodash/find';
import { importResolverGenerator, ResolverResource } from '@bfc/shared';
import extractMemoryPaths from '@bfc/indexers/lib/dialogUtils/extractMemoryPaths';
import { UserIdentity } from '@bfc/plugin-loader';

import { BotProject } from '../models/bot/botProject';
Expand Down Expand Up @@ -35,7 +36,7 @@ export class BotProjectService {

public static lgImportResolver(source: string, id: string, projectId: string): ResolverResource {
BotProjectService.initialize();
const project = BotProjectService.currentBotProjects.find(({ id }) => id === projectId);
const project = BotProjectService.getIndexedProjectById(projectId);
if (!project) throw new Error('project not found');
const resource = project.files.reduce((result: ResolverResource[], file) => {
const { name, content } = file;
Expand All @@ -50,7 +51,7 @@ export class BotProjectService {

public static luImportResolver(source: string, id: string, projectId: string): ResolverResource {
BotProjectService.initialize();
const project = BotProjectService.currentBotProjects.find(({ id }) => id === projectId);
const project = BotProjectService.getIndexedProjectById(projectId);
if (!project) throw new Error('project not found');
const resource = project.files.reduce((result: ResolverResource[], file) => {
const { name, content } = file;
Expand Down Expand Up @@ -88,9 +89,13 @@ export class BotProjectService {
'turn.repeatedIds',
'turn.activityProcessed',
];
const projectVariables = BotProjectService.getIndexedProjectById(projectId)
?.files.filter(file => file.name.endsWith('.dialog'))
.map(({ content }) => extractMemoryPaths(content));

const userDefined: string[] =
BotProjectService.currentBotProjects[projectId]?.dialogs.reduce((result: string[], dialog) => {
zhixzhan marked this conversation as resolved.
Show resolved Hide resolved
result = [...dialog.userDefinedVariables, ...result];
projectVariables?.reduce((result: string[], variables) => {
result = [...variables, ...result];
return result;
}, []) || [];
return [...defaultProperties, ...userDefined];
Expand Down Expand Up @@ -180,9 +185,18 @@ export class BotProjectService {
Store.set('projectLocationMap', BotProjectService.projectLocationMap);
};

public static getProjectById = async (projectId: string, user?: UserIdentity) => {
public static getIndexedProjectById(projectId): BotProject | undefined {
// use indexed project
const indexedCurrentProject = BotProjectService.currentBotProjects.find(({ id }) => id === projectId);
if (indexedCurrentProject) return indexedCurrentProject;
}

public static getProjectById = async (projectId: string, user?: UserIdentity): Promise<BotProject> => {
BotProjectService.initialize();

const cachedProject = BotProjectService.getIndexedProjectById(projectId);
if (cachedProject) return cachedProject;
zhixzhan marked this conversation as resolved.
Show resolved Hide resolved

if (!BotProjectService.projectLocationMap?.[projectId]) {
throw new Error('project not found in cache');
} else {
Expand Down