Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: block dependencies need to be installed at the same time when adding blocks #2711

Merged
merged 1 commit into from
Aug 15, 2019
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
31 changes: 16 additions & 15 deletions packages/iceworks-server/src/lib/adapter/modules/page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ export default class Page implements IPageModule {
const pages = await Promise.all(subDirectories.map(async(dir) => {
const pagePath = path.join(dirPath, dir);
const { atime, birthtime, ctime, mtime } = await lstatAsync(pagePath);
const blocks = await this.getBlocks(dir);
const pageName = path.basename(dir);
const blocks = await this.getBlocks(pageName);
return {
name: path.basename(pagePath),
path: pagePath,
Expand Down Expand Up @@ -172,10 +173,6 @@ export default class Page implements IPageModule {
const { name, blocks } = page;
const { socket, i18n } = ctx;

if (this.checkBlocksName(blocks)) {
throw new Error(i18n.format('baseAdapter.page.blocks.exist'));
}

// create page dir
socket.emit('adapter.page.create.status', { text: i18n.format('baseAdapter.page.create.createMenu'), percent: 10 });
const pageName = upperCamelCase(name);
Expand All @@ -188,16 +185,12 @@ export default class Page implements IPageModule {
throw error;
}

// download blocks
// add blocks
socket.emit('adapter.page.create.status', { text: i18n.format('baseAdapter.page.create.download'), percent: 40 });
await this.downloadBlocksToPage(blocks, pageName, ctx);

// install block dependencies
socket.emit('adapter.page.create.status', { text: i18n.format('baseAdapter.page.create.installDep'), percent: 80 });
await this.installBlocksDependencies(blocks, ctx);
await this.addBlocks({ blocks, name: pageName }, ctx);

// create page file
socket.emit('adapter.page.create.status', { text: i18n.format('baseAdapter.page.create.createFile'), percent: 90 });
socket.emit('adapter.page.create.status', { text: i18n.format('baseAdapter.page.create.createFile'), percent: 80 });

const template = await loadTemplate(this.templateFileName, this.templateFilePath);
const fileContent = template.compile({
Expand Down Expand Up @@ -234,7 +227,13 @@ export default class Page implements IPageModule {
public async getBlocks(name: string): Promise<IProjectBlock[]> {
const pagePath = path.join(this.path, name);
const blocksPath = path.join(pagePath, this.componentDirName);
const blockDirectroies = await scanDirectory(blocksPath);
let blockDirectroies = [];
try {
blockDirectroies = await scanDirectory(blocksPath);
} catch (err) {
// ignore error
}

const blocks = blockDirectroies.map((blockDir) => {
return {
name: blockDir,
Expand All @@ -246,18 +245,20 @@ export default class Page implements IPageModule {

public async addBlocks(params: {blocks: IMaterialBlock[]; name?: string }, ctx: IContext): Promise<void> {
const {blocks, name} = params;
const {i18n } = ctx;
const {i18n} = ctx;

const existBlocks = await this.getBlocks(name);
if (this.checkBlocksName(existBlocks.concat(blocks))) {
throw new Error(i18n.format('baseAdapter.page.blocks.exist'));
}

await this.downloadBlocksToPage(blocks, name, ctx);
await this.installBlocksDependencies(blocks, ctx);
}

public async addBlock(params: {block: IMaterialBlock; name?: string }, ctx: IContext): Promise<void> {
const {block, name} = params;
await this.downloadBlockToPage(block, name, ctx);
await this.installBlocksDependencies([block], ctx);
}
}
2 changes: 1 addition & 1 deletion packages/iceworks-server/src/lib/scanDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const accessAsync = util.promisify(fs.access);
export default async (directoryPath: string): Promise<string[]> => {
const isExist = await pathExists(directoryPath)
if (!isExist) {
throw new Error('Directory is not exist.');
throw new Error(`${directoryPath} is not exist.`);
}

const files = await readdirAsync(directoryPath);
Expand Down