Build Extension, Catch Build Fail

Hello, I am a Korean developer.
I created a plug-in that uses build extensions to erase files when building a project and then restore them to their original state after the build is complete.

My problem is, my plug-in works perfectly, but if the build fails, I won’t return the files I deleted.

Browsing the document does not provide a way to handle the build failure.

What should I do?

Maybe you want this?

my cocos creator version is 3.6.1

i can’t found onError callback

image
cocos-build-template.zip (219.4 KB)
I created the plugin directly through here directly

yeah i know, but, I clicked this button to create a plug-in, but there is still no onError in the BuildHook namespace

Can you share part of the code to see where things are messed up?

Hooks.ts

import * as fs from 'fs';

let data: { [path: string]: { contents: string; meta: string } } = {};

async function remove(path: string): Promise<{ contents: string; meta: string }> {
    let full_path = path.replace('db://', Editor.Project.path + '/');
    let str = fs.readFileSync(full_path, { encoding: 'utf8' });
    let str_meta = fs.readFileSync(full_path + '.meta', { encoding: 'utf8' });
    console.log(`remove the ${path}`);
    await Editor.Message.request('asset-db', 'delete-asset', path);

    return { contents: str, meta: str_meta };
}

async function create(path: string, data: { contents: string; meta: string }) {
    console.log(`create the ${path}`);
    let full_path = path.replace('db://', Editor.Project.path + '/');
    await Editor.Message.request('asset-db', 'create-asset', path, data.contents);
    fs.writeFileSync(full_path + '.meta', data.meta);
}

export async function onBeforeBuild(options) {
    data = {};
    let pack = options['packages'] || {};
    let module = pack['build-ignore-files'] || {};
    let list = (module['ignoreList'] || '').split(';');

    for (let path of list) {
        let exist_search_path = path.replace('db://', Editor.Project.path + '/');
        let is_exist = path !== '' && fs.existsSync(exist_search_path);
        console.log(path + ' is exist : ' + is_exist.toString());
        if (!is_exist) {
            continue;
        }
        let contents = await remove(path);
        data[path] = contents;
    }
}

export async function onAfterBuildAssets(options) {
    for (let path in data) {
        await create(path, data[path]);
    }
}

export function load() {
    console.log('build-ignore-files load');
}

export function unload() {
    console.log('build-ignore-files unload');
}

this is full hooks code


You can check if the content of the template file under the engine path is correct first

Oops… Im so sorry…

I thought I was using version 3.6.1 until now, but the version I am using was version 3.5.0

I upgraded the project to version 3.6.0 and there is an onError in BuildHook.

I solved my problem using this. Thank you so much