-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add node-pre-gyp support (#1095)
* add NodePreGyp module * chore: update node-abi * fix: node-pre-gyp args * feat: add napi support * test: add initial node-pre-gyp tests * fix: some tests * test: skip on m1 * chore: remove unused code * feat: add skipPrebuilds option * fix: napi build version test * fix: spawning node-pre-gyp on windows * use fs.rmdir maxRetries Co-authored-by: Keeley Hammond <[email protected]> * rename 'skipPreloads' to 'buildFromSource' --------- Co-authored-by: George Xu <[email protected]> Co-authored-by: Keeley Hammond <[email protected]>
- Loading branch information
1 parent
6b485d6
commit e71316b
Showing
13 changed files
with
167 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import debug from 'debug'; | ||
import { spawn } from '@malept/cross-spawn-promise'; | ||
|
||
import { locateBinary, NativeModule } from '.'; | ||
const d = debug('electron-rebuild'); | ||
|
||
export class NodePreGyp extends NativeModule { | ||
async usesTool(): Promise<boolean> { | ||
const dependencies = await this.packageJSONFieldWithDefault('dependencies', {}); | ||
// eslint-disable-next-line no-prototype-builtins | ||
return dependencies.hasOwnProperty('@mapbox/node-pre-gyp'); | ||
} | ||
|
||
async locateBinary(): Promise<string | null> { | ||
return locateBinary(this.modulePath, 'node_modules/@mapbox/node-pre-gyp/bin/node-pre-gyp'); | ||
} | ||
|
||
async run(nodePreGypPath: string): Promise<void> { | ||
await spawn( | ||
process.execPath, | ||
[ | ||
nodePreGypPath, | ||
'reinstall', | ||
'--fallback-to-build', | ||
`--arch=${this.rebuilder.arch}`, | ||
`--platform=${this.rebuilder.platform}`, | ||
...await this.getNodePreGypRuntimeArgs(), | ||
], | ||
{ | ||
cwd: this.modulePath, | ||
} | ||
); | ||
} | ||
|
||
async findPrebuiltModule(): Promise<boolean> { | ||
const nodePreGypPath = await this.locateBinary(); | ||
if (nodePreGypPath) { | ||
d(`triggering prebuild download step: ${this.moduleName}`); | ||
try { | ||
await this.run(nodePreGypPath); | ||
return true; | ||
} catch (err) { | ||
d('failed to use node-pre-gyp:', err); | ||
|
||
if (err?.message?.includes('requires Node-API but Electron')) { | ||
throw err; | ||
} | ||
} | ||
} else { | ||
d(`could not find node-pre-gyp relative to: ${this.modulePath}`); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
async getNodePreGypRuntimeArgs(): Promise<string[]> { | ||
const moduleNapiVersions = await this.getSupportedNapiVersions(); | ||
if (moduleNapiVersions) { | ||
return []; | ||
} else { | ||
return [ | ||
'--runtime=electron', | ||
`--target=${this.rebuilder.electronVersion}`, | ||
`--dist-url=${this.rebuilder.headerURL}`, | ||
]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import chai, { expect } from 'chai'; | ||
import chaiAsPromised from 'chai-as-promised'; | ||
import { EventEmitter } from 'events'; | ||
import path from 'path'; | ||
|
||
import { cleanupTestModule, resetTestModule, TIMEOUT_IN_MILLISECONDS, TEST_MODULE_PATH as testModulePath } from './helpers/module-setup'; | ||
import { NodePreGyp } from '../lib/module-type/node-pre-gyp'; | ||
import { Rebuilder } from '../lib/rebuild'; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
describe('node-pre-gyp', () => { | ||
const modulePath = path.join(testModulePath, 'node_modules', 'sqlite3'); | ||
const rebuilderArgs = { | ||
buildPath: testModulePath, | ||
electronVersion: '8.0.0', | ||
arch: process.arch, | ||
lifecycle: new EventEmitter() | ||
}; | ||
|
||
describe('Node-API support', function() { | ||
this.timeout(TIMEOUT_IN_MILLISECONDS); | ||
|
||
before(async () => await resetTestModule(testModulePath)); | ||
after(async () => await cleanupTestModule(testModulePath)); | ||
|
||
it('should find correct napi version and select napi args', async () => { | ||
const rebuilder = new Rebuilder(rebuilderArgs); | ||
const nodePreGyp = new NodePreGyp(rebuilder, modulePath); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
expect(nodePreGyp.nodeAPI.getNapiVersion((await nodePreGyp.getSupportedNapiVersions())!)).to.equal(3); | ||
expect(await nodePreGyp.getNodePreGypRuntimeArgs()).to.deep.equal([]) | ||
}); | ||
|
||
it('should not fail running node-pre-gyp', async () => { | ||
const rebuilder = new Rebuilder(rebuilderArgs); | ||
const nodePreGyp = new NodePreGyp(rebuilder, modulePath); | ||
expect(await nodePreGyp.findPrebuiltModule()).to.equal(true); | ||
}); | ||
|
||
it('should throw error with unsupported Electron version', async () => { | ||
const rebuilder = new Rebuilder({ | ||
...rebuilderArgs, | ||
electronVersion: '2.0.0', | ||
}); | ||
const nodePreGyp = new NodePreGyp(rebuilder, modulePath); | ||
expect(nodePreGyp.findPrebuiltModule()).to.eventually.be.rejectedWith("Native module 'sqlite3' requires Node-API but Electron v2.0.0 does not support Node-API"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters