Skip to content

Commit

Permalink
feat: add ignoreModules to skip certain modules forcefully (#1117)
Browse files Browse the repository at this point in the history
* feat: add ignoreModules to skip certain modules forcefully

* fix: more timeout in test
  • Loading branch information
MarshallOfSound authored Nov 30, 2023
1 parent 20271d8 commit 0e934ad
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface RebuildOptions {
forceABI?: number;
disablePreGypCopy?: boolean;
buildFromSource?: boolean;
ignoreModules?: string[];
}

export interface RebuilderOptions extends RebuildOptions {
Expand Down Expand Up @@ -62,6 +63,7 @@ export class Rebuilder implements IRebuilder {
public useElectronClang: boolean;
public disablePreGypCopy: boolean;
public buildFromSource: boolean;
public ignoreModules: string[];

constructor(options: RebuilderOptions) {
this.lifecycle = options.lifecycle;
Expand All @@ -79,6 +81,8 @@ export class Rebuilder implements IRebuilder {
this.msvsVersion = process.env.GYP_MSVS_VERSION;
this.disablePreGypCopy = options.disablePreGypCopy || false;
this.buildFromSource = options.buildFromSource || false;
this.ignoreModules = options.ignoreModules || [];
d('ignoreModules', this.ignoreModules);

if (this.useCache && this.force) {
console.warn('[WARNING]: Electron Rebuild has force enabled and cache enabled, force take precedence and the cache will not be used.');
Expand Down Expand Up @@ -170,17 +174,31 @@ export class Rebuilder implements IRebuilder {

const moduleRebuilder = new ModuleRebuilder(this, modulePath);

this.lifecycle.emit('module-found', path.basename(modulePath));
let moduleName = path.basename(modulePath);
const parentName = path.basename(path.dirname(modulePath));
if (parentName !== 'node_modules') {
moduleName = `${parentName}/${moduleName}`;
}

this.lifecycle.emit('module-found', moduleName);

if (!this.force && await moduleRebuilder.alreadyBuiltByRebuild()) {
d(`skipping: ${path.basename(modulePath)} as it is already built`);
this.lifecycle.emit('module-done');
this.lifecycle.emit('module-skip');
d(`skipping: ${moduleName} as it is already built`);
this.lifecycle.emit('module-done', moduleName);
this.lifecycle.emit('module-skip', moduleName);
return;
}

d('checking', moduleName, 'against', this.ignoreModules);
if (this.ignoreModules.includes(moduleName)) {
d(`skipping: ${moduleName} as it is in the ignoreModules array`);
this.lifecycle.emit('module-done', moduleName);
this.lifecycle.emit('module-skip', moduleName);
return;
}

if (await moduleRebuilder.prebuildInstallNativeModuleExists()) {
d(`skipping: ${path.basename(modulePath)} as it was prebuilt`);
d(`skipping: ${moduleName} as it was prebuilt`);
return;
}

Expand All @@ -198,13 +216,13 @@ export class Rebuilder implements IRebuilder {
const applyDiffFn = await lookupModuleState(this.cachePath, cacheKey);
if (typeof applyDiffFn === 'function') {
await applyDiffFn(modulePath);
this.lifecycle.emit('module-done');
this.lifecycle.emit('module-done', moduleName);
return;
}
}

if (await moduleRebuilder.rebuild(cacheKey)) {
this.lifecycle.emit('module-done');
this.lifecycle.emit('module-done', moduleName);
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/rebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ describe('rebuilder', () => {
});
});

describe('ignore rebuild', function() {
this.timeout(2 * MINUTES_IN_MILLISECONDS);

before(async () => await resetTestModule(testModulePath));
after(async () => await cleanupTestModule(testModulePath));
afterEach(resetMSVSVersion);

const buildPath = testModulePath;
const electronVersion = testElectronVersion;
const arch = process.arch;

it('should rebuild all modules again when enabled', async function() {
if (process.platform === 'win32') {
this.timeout(5 * MINUTES_IN_MILLISECONDS);
}
await rebuild({ buildPath, electronVersion, arch });
resetMSVSVersion();
const rebuilder = rebuild({ buildPath, electronVersion, arch, ignoreModules: ['native-hello-world'], force: true });
let skipped = 0;
rebuilder.lifecycle.on('module-skip', () => {
skipped++;
});
await rebuilder;
expect(skipped).to.equal(1);
});
});

describe('only rebuild', function() {
this.timeout(2 * MINUTES_IN_MILLISECONDS);

Expand Down

0 comments on commit 0e934ad

Please sign in to comment.