Initial commit

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
dinlo
2026-05-31 18:44:04 +08:00
commit 436a9631fc
8616 changed files with 1389957 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) Contributors to the Electron project
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+187
View File
@@ -0,0 +1,187 @@
# @electron/rebuild
[![Test](https://github.com/electron/rebuild/actions/workflows/test.yml/badge.svg)](https://github.com/electron/rebuild/actions/workflows/test.yml)
[![NPM](https://img.shields.io/npm/v/@electron/rebuild.svg?style=flat)](https://npm.im/@electron/rebuild)
[![Coverage Status](https://codecov.io/gh/electron/rebuild/branch/main/graph/badge.svg)](https://codecov.io/gh/electron/rebuild)
[![API docs](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fregistry.npmjs.org%2F%40electron%2Frebuild%2Flatest&query=%24.version&logo=typescript&logoColor=white&label=API%20Docs)](https://packages.electronjs.org/rebuild)
This executable rebuilds native Node.js modules against the version of Node.js
that your Electron project is using. This allows you to use native Node.js
modules in Electron apps without your system version of Node.js matching exactly
(which is often not the case, and sometimes not even possible).
### How does it work?
Install the package with `--save-dev`:
```sh
npm install --save-dev @electron/rebuild
```
Then, whenever you install a new npm package, rerun electron-rebuild:
```sh
$(npm bin)/electron-rebuild
```
Or if you're on Windows:
```sh
.\node_modules\.bin\electron-rebuild.cmd
```
If you have a good node-gyp config but you see an error about a missing element on Windows like `Could not load the Visual C++ component "VCBuild.exe"`, try to launch `electron-rebuild` in an npm script:
```json
"scripts": {
"rebuild": "electron-rebuild -f -w yourmodule"
}
```
and then
```sh
npm run rebuild
```
### What are the requirements?
Node v22.12.0 or higher is required. Building native modules from source uses
[`node-gyp`](https://github.com/nodejs/node-gyp#installation). Refer to the link for its
installation/runtime requirements.
### CLI arguments
```
Usage: electron-rebuild --version [version] --module-dir [path]
Options:
-v, --version The version of Electron to build against [string]
-f, --force Force rebuilding modules, even if we would skip
it otherwise [boolean]
-a, --arch Override the target architecture to something
other than your system's [string]
-m, --module-dir The path to the node_modules directory to rebuild
[string]
-w, --which-module A specific module to build, or comma separated
list of modules. Modules will only be rebuilt if
they also match the types of dependencies being
rebuilt (see --types). [string]
-o, --only Only build specified module, or comma separated
list of modules. All others are ignored. [string]
-e, --electron-prebuilt-dir The path to the prebuilt electron module [string]
-d, --dist-url Custom header tarball URL [string]
-t, --types The types of dependencies to rebuild. Comma
separated list of "prod", "dev" and "optional".
Default is "prod,optional" [string]
-p, --parallel Rebuild in parallel, this is enabled by default
on macOS and Linux [boolean]
-s, --sequential Rebuild modules sequentially, this is enabled by
default on Windows [boolean]
-b, --debug Build debug version of modules [boolean]
--prebuild-tag-prefix GitHub tag prefix passed to prebuild-install.
Default is "v" [string]
--force-abi Override the ABI version for the version of
Electron you are targeting. Only use when
targeting Nightly releases. [number]
--use-electron-clang Use the clang executable that Electron used when
building its binary. This will guarantee compiler
compatibility [boolean]
--disable-pre-gyp-copy Disables the pre-gyp copy step [boolean]
--build-from-source Skips prebuild download and rebuilds module from
source. [boolean]
-h, --help Show help [boolean]
```
### How can I use this with [Electron Forge](https://github.com/electron/forge)?
This package is automatically used with Electron Forge when packaging an Electron app.
### How can I integrate this into [Electron Packager](https://github.com/electron/packager)?
electron-rebuild provides a function compatible with the [`afterCopy` hook](https://electron.github.io/packager/main/interfaces/Options.html#afterCopy)
for Electron Packager. For example:
```javascript
import packager from "@electron/packager";
import { rebuild } from "@electron/rebuild";
packager({
// … other options
afterCopy: [
(buildPath, electronVersion, platform, arch, callback) => {
rebuild({ buildPath, electronVersion, arch })
.then(() => callback())
.catch((error) => callback(error));
},
],
// … other options
});
```
### How can I integrate this with [prebuild](https://github.com/prebuild/prebuild)?
If your module uses [prebuild](https://github.com/prebuild/prebuild) for creating prebuilt binaries,
it also uses [prebuild-install](https://github.com/prebuild/prebuild-install) to download them. If
this is the case, then `electron-rebuild` will run `prebuild-install` to download the correct
binaries from the project's GitHub Releases instead of rebuilding them.
### How can I integrate this into my build pipeline?
electron-rebuild is also a library that you can require into your app or
build process. It has a very simple API:
```javascript
import { rebuild } from '@electron/rebuild';
// Public: Rebuilds a node_modules directory with the given Electron version.
//
// options: Object with the following properties
// buildPath - An absolute path to your app's directory. (The directory that contains your node_modules)
// electronVersion - The version of Electron to rebuild for
// arch (optional) - Default: process.arch - The arch to rebuild for
// extraModules (optional) - Default: [] - An array of modules to rebuild as well as the detected modules
// onlyModules (optional) - Default: null - An array of modules to rebuild, ONLY these module names will be rebuilt.
// The "types" property will be ignored if this option is set.
// force (optional) - Default: false - Force a rebuild of modules regardless of their current build state
// headerURL (optional) - Default: https://www.electronjs.org/headers - The URL to download Electron header files from
// types (optional) - Default: ['prod', 'optional'] - The types of modules to rebuild
// mode (optional) - The rebuild mode, either 'sequential' or 'parallel' - Default varies per platform (probably shouldn't mess with this one)
// useElectronClang (optional) - Whether to use the clang executable that Electron used when building its binary. This will guarantee compiler compatibility
// Returns a Promise indicating whether the operation succeeded or not
```
For full API usage, see the [API documentation](https://packages.electronjs.org/rebuild).
A full build process might look something like:
```javascript
// ESM
import { rebuild } from "@electron/rebuild";
rebuild({
buildPath: import.meta.dirname,
electronVersion: "35.1.5",
})
.then(() => console.info("Rebuild Successful"))
.catch((e) => {
console.error("Building modules didn't work!");
console.error(e);
});
// CommonJS
const { rebuild } = require("@electron/rebuild");
rebuild({
buildPath: __dirname,
electronVersion: "35.1.5",
})
.then(() => console.info("Rebuild Successful"))
.catch((e) => {
console.error("Building modules didn't work!");
console.error(e);
});
```
+16
View File
@@ -0,0 +1,16 @@
/**
* Runs the `uname` command and returns the trimmed output.
*
* Copied from `@electron/get`.
*/
export declare function uname(): string;
export type ConfigVariables = {
arm_version?: string;
};
/**
* Generates an architecture name that would be used in an Electron or Node.js
* download file name.
*
* Copied from `@electron/get`.
*/
export declare function getNodeArch(arch: string, configVariables: ConfigVariables): string;
+31
View File
@@ -0,0 +1,31 @@
import { execSync } from 'node:child_process';
/**
* Runs the `uname` command and returns the trimmed output.
*
* Copied from `@electron/get`.
*/
export function uname() {
return execSync('uname -m')
.toString()
.trim();
}
/**
* Generates an architecture name that would be used in an Electron or Node.js
* download file name.
*
* Copied from `@electron/get`.
*/
export function getNodeArch(arch, configVariables) {
if (arch === 'arm') {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
switch (configVariables.arm_version) {
case '6':
return uname();
case '7':
default:
return 'armv7l';
}
}
return arch;
}
//# sourceMappingURL=arch.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"arch.js","sourceRoot":"","sources":["../src/arch.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAE9C;;;;GAIG;AACH,MAAM,UAAU,KAAK;IACnB,OAAO,QAAQ,CAAC,UAAU,CAAC;SACxB,QAAQ,EAAE;SACV,IAAI,EAAE,CAAC;AACZ,CAAC;AAMD;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,eAAgC;IACxE,IAAI,IAAI,KAAK,KAAK,EAAE,CAAC;QACnB,8DAA8D;QAC9D,QAAQ,eAAe,CAAC,WAAW,EAAE,CAAC;YACpC,KAAK,GAAG;gBACN,OAAO,KAAK,EAAE,CAAC;YACjB,KAAK,GAAG,CAAC;YACT;gBACE,OAAO,QAAQ,CAAC;QACpB,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
+14
View File
@@ -0,0 +1,14 @@
type CacheOptions = {
ABI: string;
arch: string;
platform: string;
debug: boolean;
electronVersion: string;
headerURL: string;
modulePath: string;
};
export declare const cacheModuleState: (dir: string, cachePath: string, key: string) => Promise<void>;
type ApplyDiffFunction = (dir: string) => Promise<void>;
export declare const lookupModuleState: (cachePath: string, key: string) => Promise<ApplyDiffFunction | boolean>;
export declare function generateCacheKey(opts: CacheOptions): Promise<string>;
export {};
+148
View File
@@ -0,0 +1,148 @@
import crypto from 'node:crypto';
import debug from 'debug';
import fs from 'graceful-fs';
import path from 'node:path';
import zlib from 'node:zlib';
import { promisifiedGracefulFs } from './promisifiedGracefulFs.js';
const d = debug('electron-rebuild');
// Update this number if you change the caching logic to ensure no bad cache hits
const ELECTRON_REBUILD_CACHE_ID = 1;
class Snap {
hash;
data;
constructor(hash, data) {
this.hash = hash;
this.data = data;
}
}
const takeSnapshot = async (dir, relativeTo = dir) => {
const snap = {};
await Promise.all((await promisifiedGracefulFs.readdir(dir)).map(async (child) => {
if (child === 'node_modules')
return;
const childPath = path.resolve(dir, child);
const relative = path.relative(relativeTo, childPath);
if ((await fs.promises.stat(childPath)).isDirectory()) {
snap[relative] = await takeSnapshot(childPath, relativeTo);
}
else {
const data = await promisifiedGracefulFs.readFile(childPath);
snap[relative] = new Snap(crypto.createHash('SHA256').update(data).digest('hex'), data);
}
}));
return snap;
};
const writeSnapshot = async (diff, dir) => {
for (const key in diff) {
if (diff[key] instanceof Snap) {
await fs.promises.mkdir(path.dirname(path.resolve(dir, key)), { recursive: true });
await promisifiedGracefulFs.writeFile(path.resolve(dir, key), diff[key].data);
}
else {
await fs.promises.mkdir(path.resolve(dir, key), { recursive: true });
await writeSnapshot(diff[key], dir);
}
}
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const serialize = (snap) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const jsonReady = {};
for (const key in snap) {
if (snap[key] instanceof Snap) {
const s = snap[key];
jsonReady[key] = {
__isSnap: true,
hash: s.hash,
data: s.data.toString('base64')
};
}
else {
jsonReady[key] = serialize(snap[key]);
}
}
return jsonReady;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const unserialize = (jsonReady) => {
const snap = {};
for (const key in jsonReady) {
if (jsonReady[key].__isSnap) {
snap[key] = new Snap(jsonReady[key].hash, Buffer.from(jsonReady[key].data, 'base64'));
}
else {
snap[key] = unserialize(jsonReady[key]);
}
}
return snap;
};
export const cacheModuleState = async (dir, cachePath, key) => {
const snap = await takeSnapshot(dir);
const moduleBuffer = Buffer.from(JSON.stringify(serialize(snap)));
const zipped = await new Promise(resolve => zlib.gzip(moduleBuffer, (_, result) => resolve(result)));
await fs.promises.mkdir(cachePath, { recursive: true });
await promisifiedGracefulFs.writeFile(path.resolve(cachePath, key), zipped);
};
export const lookupModuleState = async (cachePath, key) => {
if (fs.existsSync(path.resolve(cachePath, key))) {
return async function applyDiff(dir) {
const zipped = await promisifiedGracefulFs.readFile(path.resolve(cachePath, key));
const unzipped = await new Promise(resolve => { zlib.gunzip(zipped, (_, result) => resolve(result)); });
const diff = unserialize(JSON.parse(unzipped.toString()));
await writeSnapshot(diff, dir);
};
}
return false;
};
function dHashTree(tree, hash) {
for (const key of Object.keys(tree).sort()) {
hash.update(key);
if (typeof tree[key] === 'string') {
hash.update(tree[key]);
}
else {
dHashTree(tree[key], hash);
}
}
}
async function hashDirectory(dir, relativeTo) {
relativeTo ??= dir;
d('hashing dir', dir);
const dirTree = {};
await Promise.all((await promisifiedGracefulFs.readdir(dir)).map(async (child) => {
d('found child', child, 'in dir', dir);
// Ignore output directories
if (dir === relativeTo && (child === 'build' || child === 'bin'))
return;
// Don't hash nested node_modules
if (child === 'node_modules')
return;
const childPath = path.resolve(dir, child);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const relative = path.relative(relativeTo, childPath);
if ((await fs.promises.stat(childPath)).isDirectory()) {
dirTree[relative] = await hashDirectory(childPath, relativeTo);
}
else {
dirTree[relative] = crypto.createHash('SHA256').update(await promisifiedGracefulFs.readFile(childPath)).digest('hex');
}
}));
return dirTree;
}
export async function generateCacheKey(opts) {
const tree = await hashDirectory(opts.modulePath);
const hasher = crypto.createHash('SHA256')
.update(`${ELECTRON_REBUILD_CACHE_ID}`)
.update(path.basename(opts.modulePath))
.update(opts.ABI)
.update(opts.arch)
.update(opts.platform)
.update(opts.debug ? 'debug' : 'not debug')
.update(opts.headerURL)
.update(opts.electronVersion);
dHashTree(tree, hasher);
const hash = hasher.digest('hex');
d('calculated hash of', opts.modulePath, 'to be', hash);
return hash;
}
//# sourceMappingURL=cache.js.map
File diff suppressed because one or more lines are too long
+4
View File
@@ -0,0 +1,4 @@
export declare function getClangEnvironmentVars(electronVersion: string, targetArch: string): Promise<{
env: Record<string, string>;
args: string[];
}>;
+112
View File
@@ -0,0 +1,112 @@
import cp from 'node:child_process';
import debug from 'debug';
import fs from 'graceful-fs';
import path from 'node:path';
import * as tar from 'tar';
import zlib from 'node:zlib';
import { ELECTRON_GYP_DIR } from './constants.js';
import { fetch } from './fetcher.js';
import { downloadLinuxSysroot } from './sysroot-fetcher.js';
import { promisifiedGracefulFs } from './promisifiedGracefulFs.js';
const d = debug('electron-rebuild');
const CDS_URL = 'https://commondatastorage.googleapis.com/chromium-browser-clang';
function getPlatformUrlPrefix(hostOS, hostArch) {
const prefixMap = {
'linux': 'Linux_x64',
'darwin': 'Mac',
'win32': 'Win',
};
let prefix = prefixMap[hostOS];
if (prefix === 'Mac' && hostArch === 'arm64') {
prefix = 'Mac_arm64';
}
return CDS_URL + '/' + prefix + '/';
}
function getClangDownloadURL(packageFile, packageVersion, hostOS, hostArch) {
const cdsFile = `${packageFile}-${packageVersion}.tgz`;
return getPlatformUrlPrefix(hostOS, hostArch) + cdsFile;
}
function getSDKRoot() {
if (process.env.SDKROOT)
return process.env.SDKROOT;
const output = cp.execFileSync('xcrun', ['--sdk', 'macosx', '--show-sdk-path']);
return output.toString().trim();
}
export async function getClangEnvironmentVars(electronVersion, targetArch) {
const clangDownloadDir = await downloadClangVersion(electronVersion);
const clangDir = path.resolve(clangDownloadDir, 'bin');
const clangArgs = [];
if (process.platform === 'darwin') {
clangArgs.push('-isysroot', getSDKRoot());
}
const gypArgs = [];
if (process.platform === 'win32') {
console.log(await promisifiedGracefulFs.readdir(clangDir));
gypArgs.push(`/p:CLToolExe=clang-cl.exe`, `/p:CLToolPath=${clangDir}`);
}
if (process.platform === 'linux') {
const sysrootPath = await downloadLinuxSysroot(electronVersion, targetArch);
clangArgs.push('--sysroot', sysrootPath);
}
return {
env: {
CC: `"${path.resolve(clangDir, 'clang')}" ${clangArgs.join(' ')}`,
CXX: `"${path.resolve(clangDir, 'clang++')}" ${clangArgs.join(' ')}`,
},
args: gypArgs,
};
}
function clangVersionFromRevision(update) {
const regex = /CLANG_REVISION = '([^']+)'\nCLANG_SUB_REVISION = (\d+)\n/g;
const clangVersionMatch = regex.exec(update);
if (!clangVersionMatch)
return null;
const [, clangVersion, clangSubRevision] = clangVersionMatch;
return `${clangVersion}-${clangSubRevision}`;
}
function clangVersionFromSVN(update) {
const regex = /CLANG_REVISION = '([^']+)'\nCLANG_SVN_REVISION = '([^']+)'\nCLANG_SUB_REVISION = (\d+)\n/g;
const clangVersionMatch = regex.exec(update);
if (!clangVersionMatch)
return null;
const [, clangVersion, clangSvn, clangSubRevision] = clangVersionMatch;
return `${clangSvn}-${clangVersion.substr(0, 8)}-${clangSubRevision}`;
}
async function downloadClangVersion(electronVersion) {
d('fetching clang for Electron:', electronVersion);
const clangDirPath = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-clang`);
if (fs.existsSync(path.resolve(clangDirPath, 'bin', 'clang')))
return clangDirPath;
await fs.promises.mkdir(ELECTRON_GYP_DIR, { recursive: true });
const electronDeps = await fetch(`https://raw.githubusercontent.com/electron/electron/v${electronVersion}/DEPS`, 'text');
const chromiumRevisionExtractor = /'chromium_version':\n\s+'([^']+)/g;
const chromiumRevisionMatch = chromiumRevisionExtractor.exec(electronDeps);
if (!chromiumRevisionMatch)
throw new Error('Failed to determine Chromium revision for given Electron version');
const chromiumRevision = chromiumRevisionMatch[1];
d('fetching clang for Chromium:', chromiumRevision);
const base64ClangUpdate = await fetch(`https://chromium.googlesource.com/chromium/src.git/+/${chromiumRevision}/tools/clang/scripts/update.py?format=TEXT`, 'text');
const clangUpdate = Buffer.from(base64ClangUpdate, 'base64').toString('utf8');
const clangVersionString = clangVersionFromRevision(clangUpdate) || clangVersionFromSVN(clangUpdate);
if (!clangVersionString)
throw new Error('Failed to determine Clang revision from Electron version');
d('fetching clang:', clangVersionString);
const clangDownloadURL = getClangDownloadURL('clang', clangVersionString, process.platform, process.arch);
const contents = await fetch(clangDownloadURL, 'buffer');
d('deflating clang');
zlib.deflateSync(contents);
const tarPath = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-clang.tar`);
if (fs.existsSync(tarPath))
await fs.promises.rm(tarPath, { recursive: true, force: true });
await promisifiedGracefulFs.writeFile(tarPath, Buffer.from(contents));
await fs.promises.mkdir(clangDirPath, { recursive: true });
d('tar running on clang');
await tar.x({
file: tarPath,
cwd: clangDirPath,
});
await fs.promises.rm(tarPath, { recursive: true, force: true });
d('cleaning up clang tar file');
return clangDirPath;
}
//# sourceMappingURL=clang-fetcher.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"clang-fetcher.js","sourceRoot":"","sources":["../src/clang-fetcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,oBAAoB,CAAC;AACpC,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,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EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAC5F,MAAM,qBAAqB,CAAC,SAAS,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC;IACtE,MAAM,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,YAAY,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAC3D,CAAC,CAAC,sBAAsB,CAAC,CAAC;IAC1B,MAAM,GAAG,CAAC,CAAC,CAAC;QACV,IAAI,EAAE,OAAO;QACb,GAAG,EAAE,YAAY;KAClB,CAAC,CAAC;IACH,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAChE,CAAC,CAAC,4BAA4B,CAAC,CAAC;IAChC,OAAO,YAAY,CAAC;AACtB,CAAC"}
+2
View File
@@ -0,0 +1,2 @@
#!/usr/bin/env node
export {};
+140
View File
@@ -0,0 +1,140 @@
#!/usr/bin/env node
import fs from 'graceful-fs';
import path from 'node:path';
import util from 'node:util';
import ora from 'ora';
import yargs from 'yargs/yargs';
import { getProjectRootPath } from './search-module.js';
import { locateElectronModule } from './electron-locator.js';
import { rebuild } from './rebuild.js';
import { pathToFileURL } from 'node:url';
const argv = yargs(process.argv.slice(2)).version(false).options({
version: { alias: 'v', type: 'string', description: 'The version of Electron to build against' },
force: { alias: 'f', type: 'boolean', description: 'Force rebuilding modules, even if we would skip it otherwise' },
arch: { alias: 'a', type: 'string', description: "Override the target architecture to something other than your system's" },
'module-dir': { alias: 'm', type: 'string', description: 'The path to the node_modules directory to rebuild' },
// TODO: should be type: array
'which-module': { alias: 'w', type: 'string', description: 'A specific module to build, or comma separated list of modules. Modules will only be rebuilt if they also match the types of dependencies being rebuilt (see --types).' },
// TODO: should be type: array
only: { alias: 'o', type: 'string', description: 'Only build specified module, or comma separated list of modules. All others are ignored.' },
'electron-prebuilt-dir': { alias: 'e', type: 'string', description: 'The path to the prebuilt electron module' },
'dist-url': { alias: 'd', type: 'string', description: 'Custom header tarball URL' },
// TODO: should be type: array
types: { alias: 't', type: 'string', description: 'The types of dependencies to rebuild. Comma separated list of "prod", "dev" and "optional". Default is "prod,optional"' },
parallel: { alias: 'p', type: 'boolean', description: 'Rebuild in parallel, this is enabled by default on macOS and Linux' },
sequential: { alias: 's', type: 'boolean', description: 'Rebuild modules sequentially, this is enabled by default on Windows' },
debug: { alias: 'b', type: 'boolean', description: 'Build debug version of modules' },
'prebuild-tag-prefix': { type: 'string', description: 'GitHub tag prefix passed to prebuild-install. Default is "v"' },
'force-abi': { type: 'number', description: 'Override the ABI version for the version of Electron you are targeting. Only use when targeting Nightly releases.' },
'use-electron-clang': { type: 'boolean', description: 'Use the clang executable that Electron used when building its binary. This will guarantee compiler compatibility' },
'disable-pre-gyp-copy': { type: 'boolean', description: 'Disables the pre-gyp copy step' },
'build-from-source': { type: 'boolean', description: 'Skips prebuild download and rebuilds module from source.' },
}).usage('Usage: $0 --version [version] --module-dir [path]')
.help()
.alias('help', 'h')
.epilog('Copyright 2016-2021')
.parseSync();
if (process.argv.length === 3 && process.argv[2] === '--version') {
try {
console.log('Electron Rebuild Version:', (await import(pathToFileURL(path.resolve(import.meta.dirname, '../../package.json')).toString(), { with: { type: 'json' } })).default.version);
}
catch (err) {
console.log('Electron Rebuild Version:', (await import(pathToFileURL(path.resolve(import.meta.dirname, '../package.json')).toString(), { with: { type: 'json' } })).default.version);
}
process.exit(0);
}
const handler = (err) => {
console.error(util.styleText('red', '\nAn unhandled error occurred inside electron-rebuild'));
console.error(util.styleText('red', `${err.message}\n\n${err.stack}`));
process.exit(-1);
};
process.on('uncaughtException', handler);
process.on('unhandledRejection', handler);
(async () => {
const projectRootPath = await getProjectRootPath(process.cwd());
const electronModulePath = argv.e ? path.resolve(process.cwd(), argv.e) : await locateElectronModule(projectRootPath);
let electronModuleVersion = argv.v;
if (!electronModuleVersion) {
try {
if (!electronModulePath)
throw new Error('Prebuilt electron module not found');
const pkgJson = await import(pathToFileURL(path.join(electronModulePath, 'package.json')).toString(), { with: { type: 'json' } });
electronModuleVersion = pkgJson.default.version;
}
catch (e) {
throw new Error(`Unable to find electron's version number, either install it or specify an explicit version`);
}
}
let rootDirectory = argv.m;
if (!rootDirectory) {
// NB: We assume here that we're going to rebuild the immediate parent's
// node modules, which might not always be the case but it's at least a
// good guess
rootDirectory = path.resolve(import.meta.dirname, '../../..');
if (!fs.existsSync(rootDirectory) || !fs.existsSync(path.resolve(rootDirectory, 'package.json'))) {
// Then we try the CWD
rootDirectory = process.cwd();
if (!fs.existsSync(rootDirectory) || !fs.existsSync(path.resolve(rootDirectory, 'package.json'))) {
throw new Error('Unable to find parent node_modules directory, specify it via --module-dir, E.g. "--module-dir ." for the current directory');
}
}
}
else {
rootDirectory = path.resolve(process.cwd(), rootDirectory);
}
if (argv.forceAbi && typeof argv.forceAbi !== 'number') {
throw new Error('force-abi must be a number');
}
let modulesDone = 0;
let moduleTotal = 0;
const rebuildSpinner = ora('Searching dependency tree').start();
let lastModuleName;
const redraw = (moduleName) => {
if (moduleName)
lastModuleName = moduleName;
if (argv.p) {
rebuildSpinner.text = `Building modules: ${modulesDone}/${moduleTotal}`;
}
else {
rebuildSpinner.text = `Building module: ${lastModuleName}, Completed: ${modulesDone}`;
}
};
const rebuilder = rebuild({
buildPath: rootDirectory,
electronVersion: electronModuleVersion,
arch: argv.a || process.arch,
extraModules: argv.w ? argv.w.split(',') : [],
onlyModules: argv.o ? argv.o.split(',') : null,
force: argv.f,
headerURL: argv.d,
types: argv.t ? argv.t.split(',') : ['prod', 'optional'],
mode: argv.p ? 'parallel' : (argv.s ? 'sequential' : undefined),
debug: argv.debug,
prebuildTagPrefix: argv.prebuildTagPrefix || 'v',
forceABI: argv.forceAbi,
useElectronClang: !!argv.useElectronClang,
disablePreGypCopy: !!argv.disablePreGypCopy,
projectRootPath,
buildFromSource: !!argv.buildFromSource,
});
const lifecycle = rebuilder.lifecycle;
lifecycle.on('module-found', (moduleName) => {
moduleTotal += 1;
redraw(moduleName);
});
lifecycle.on('module-done', () => {
modulesDone += 1;
redraw();
});
try {
await rebuilder;
}
catch (err) {
rebuildSpinner.text = 'Rebuild Failed';
rebuildSpinner.fail();
throw err;
}
rebuildSpinner.text = 'Rebuild Complete';
rebuildSpinner.succeed();
})();
//# sourceMappingURL=cli.js.map
File diff suppressed because one or more lines are too long
+1
View File
@@ -0,0 +1 @@
export declare const ELECTRON_GYP_DIR: string;
+4
View File
@@ -0,0 +1,4 @@
import os from 'node:os';
import path from 'node:path';
export const ELECTRON_GYP_DIR = path.resolve(os.homedir(), '.electron-gyp');
//# sourceMappingURL=constants.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAAM,CAAC,MAAM,gBAAgB,GAAG,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,EAAE,EAAE,eAAe,CAAC,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare function locateElectronModule(projectRootPath?: string | undefined, startDir?: string | undefined): Promise<string | null>;
+30
View File
@@ -0,0 +1,30 @@
import fs from 'graceful-fs';
import path from 'node:path';
import { searchForModule } from './search-module.js';
import { fileURLToPath } from 'node:url';
const electronModuleNames = ['electron', 'electron-prebuilt-compile'];
async function locateModuleByImport() {
for (const moduleName of electronModuleNames) {
try {
const modulePath = path.resolve(fileURLToPath(import.meta.resolve(path.join(moduleName, 'package.json'))), '..');
if (fs.existsSync(path.join(modulePath, 'package.json'))) {
return modulePath;
}
}
catch { // eslint-disable-line no-empty
}
}
return null;
}
export async function locateElectronModule(projectRootPath = undefined, startDir = undefined) {
startDir ??= process.cwd();
for (const moduleName of electronModuleNames) {
const electronPaths = await searchForModule(startDir, moduleName, projectRootPath);
const electronPath = electronPaths.find((ePath) => fs.existsSync(path.join(ePath, 'package.json')));
if (electronPath) {
return electronPath;
}
}
return locateModuleByImport();
}
//# sourceMappingURL=electron-locator.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"electron-locator.js","sourceRoot":"","sources":["../src/electron-locator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,MAAM,mBAAmB,GAAG,CAAC,UAAU,EAAG,2BAA2B,CAAC,CAAC;AAEvE,KAAK,UAAU,oBAAoB;IACjC,KAAK,MAAM,UAAU,IAAI,mBAAmB,EAAE,CAAC;QAC7C,IAAI,CAAC;YACH,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC;YACjH,IAAI,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC;gBACzD,OAAO,UAAU,CAAC;YACpB,CAAC;QACH,CAAC;QAAC,MAAM,CAAC,CAAC,+BAA+B;QACzC,CAAC;IACH,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,kBAAsC,SAAS,EAC/C,WAA+B,SAAS;IAExC,QAAQ,KAAK,OAAO,CAAC,GAAG,EAAE,CAAC;IAE3B,KAAK,MAAM,UAAU,IAAI,mBAAmB,EAAE,CAAC;QAC7C,MAAM,aAAa,GAAG,MAAM,eAAe,CAAC,QAAQ,EAAE,UAAU,EAAE,eAAe,CAAC,CAAC;QACnF,MAAM,YAAY,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,KAAa,EAAE,EAAE,CAAC,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,cAAc,CAAC,CAAC,CAAC,CAAC;QAE5G,IAAI,YAAY,EAAE,CAAC;YACjB,OAAO,YAAY,CAAC;QACtB,CAAC;IACH,CAAC;IAED,OAAO,oBAAoB,EAAE,CAAC;AAChC,CAAC"}
+3
View File
@@ -0,0 +1,3 @@
/// <reference types="node" resolution-mode="require"/>
/// <reference types="node" resolution-mode="require"/>
export declare function fetch<T extends 'buffer' | 'text', RT = T extends 'buffer' ? Buffer : string>(url: string, responseType: T, retries?: number): Promise<RT>;
+27
View File
@@ -0,0 +1,27 @@
import { setTimeout as sleep } from 'node:timers/promises';
import debug from 'debug';
import got from 'got';
const d = debug('electron-rebuild');
export async function fetch(url, responseType, retries = 3) {
if (retries === 0)
throw new Error('Failed to fetch a clang resource, run with DEBUG=electron-rebuild for more information');
d('downloading:', url);
try {
const response = await got.default.get(url, {
responseType,
});
if (response.statusCode !== 200) {
d('got bad status code:', response.statusCode);
await sleep(2000);
return fetch(url, responseType, retries - 1);
}
d('response came back OK');
return response.body;
}
catch (err) {
d('request failed for some reason', err);
await sleep(2000);
return fetch(url, responseType, retries - 1);
}
}
//# sourceMappingURL=fetcher.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"fetcher.js","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE3D,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,GAAG,MAAM,KAAK,CAAC;AAEtB,MAAM,CAAC,GAAG,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAEpC,MAAM,CAAC,KAAK,UAAU,KAAK,CAAyE,GAAW,EAAE,YAAe,EAAE,OAAO,GAAG,CAAC;IAC3I,IAAI,OAAO,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,wFAAwF,CAAC,CAAC;IAC7H,CAAC,CAAC,cAAc,EAAE,GAAG,CAAC,CAAC;IACvB,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE;YAC1C,YAAY;SACb,CAAC,CAAC;QACH,IAAI,QAAQ,CAAC,UAAU,KAAK,GAAG,EAAE,CAAC;YAChC,CAAC,CAAC,sBAAsB,EAAE,QAAQ,CAAC,UAAU,CAAC,CAAC;YAC/C,MAAM,KAAK,CAAC,IAAI,CAAC,CAAC;YAClB,OAAO,KAAK,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;QAC/C,CAAC;QACD,CAAC,CAAC,uBAAuB,CAAC,CAAC;QAC3B,OAAO,QAAQ,CAAC,IAAU,CAAC;IAC7B,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,CAAC,CAAC,gCAAgC,EAAE,GAAG,CAAC,CAAC;QACzC,MAAM,KAAK,CAAC,IAAI,CAAC,CAAC;QAClB,OAAO,KAAK,CAAC,GAAG,EAAE,YAAY,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC"}
+2
View File
@@ -0,0 +1,2 @@
import { rebuild, RebuildOptions } from './rebuild.js';
export { rebuild, RebuildOptions };
+3
View File
@@ -0,0 +1,3 @@
import { rebuild } from './rebuild.js';
export { rebuild };
//# sourceMappingURL=main.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAkB,MAAM,cAAc,CAAC;AAEvD,OAAO,EAAE,OAAO,EAAkB,CAAC"}
+29
View File
@@ -0,0 +1,29 @@
import { IRebuilder } from './types.js';
export declare class ModuleRebuilder {
private modulePath;
private nodeGyp;
private rebuilder;
private prebuildify;
private prebuildInstall;
private nodePreGyp;
constructor(rebuilder: IRebuilder, modulePath: string);
get metaPath(): string;
get metaData(): string;
alreadyBuiltByRebuild(): Promise<boolean>;
cacheModuleState(cacheKey: string): Promise<void>;
/**
* Whether a prebuild-install-generated native module exists.
*/
prebuildInstallNativeModuleExists(): Promise<boolean>;
/**
* If the native module uses prebuildify, check to see if it comes with a prebuilt module for
* the given platform and arch.
*/
findPrebuildifyModule(cacheKey: string): Promise<boolean>;
findPrebuildInstallModule(cacheKey: string): Promise<boolean>;
findNodePreGypInstallModule(cacheKey: string): Promise<boolean>;
rebuildNodeGypModule(cacheKey: string): Promise<boolean>;
replaceExistingNativeModule(): Promise<void>;
writeMetadata(): Promise<void>;
rebuild(cacheKey: string): Promise<boolean>;
}
+127
View File
@@ -0,0 +1,127 @@
import debug from 'debug';
import fs from 'graceful-fs';
import path from 'node:path';
import { cacheModuleState } from './cache.js';
import { NodeGyp } from './module-type/node-gyp/node-gyp.js';
import { Prebuildify } from './module-type/prebuildify.js';
import { PrebuildInstall } from './module-type/prebuild-install.js';
import { NodePreGyp } from './module-type/node-pre-gyp.js';
import { promisifiedGracefulFs } from './promisifiedGracefulFs.js';
const d = debug('electron-rebuild');
export class ModuleRebuilder {
modulePath;
nodeGyp;
rebuilder;
prebuildify;
prebuildInstall;
nodePreGyp;
constructor(rebuilder, modulePath) {
this.modulePath = modulePath;
this.rebuilder = rebuilder;
this.nodeGyp = new NodeGyp(rebuilder, modulePath);
this.prebuildify = new Prebuildify(rebuilder, modulePath);
this.prebuildInstall = new PrebuildInstall(rebuilder, modulePath);
this.nodePreGyp = new NodePreGyp(rebuilder, modulePath);
}
get metaPath() {
return path.resolve(this.modulePath, 'build', this.rebuilder.buildType, '.forge-meta');
}
get metaData() {
return `${this.rebuilder.arch}--${this.rebuilder.ABI}`;
}
async alreadyBuiltByRebuild() {
if (fs.existsSync(this.metaPath)) {
const meta = await promisifiedGracefulFs.readFile(this.metaPath, 'utf8');
return meta === this.metaData;
}
return false;
}
async cacheModuleState(cacheKey) {
if (this.rebuilder.useCache) {
await cacheModuleState(this.modulePath, this.rebuilder.cachePath, cacheKey);
}
}
/**
* Whether a prebuild-install-generated native module exists.
*/
async prebuildInstallNativeModuleExists() {
return this.prebuildInstall.prebuiltModuleExists();
}
/**
* If the native module uses prebuildify, check to see if it comes with a prebuilt module for
* the given platform and arch.
*/
async findPrebuildifyModule(cacheKey) {
if (await this.prebuildify.usesTool()) {
d(`assuming is prebuildify powered: ${this.prebuildify.moduleName}`);
if (await this.prebuildify.findPrebuiltModule()) {
await this.writeMetadata();
await this.cacheModuleState(cacheKey);
return true;
}
}
return false;
}
async findPrebuildInstallModule(cacheKey) {
if (await this.prebuildInstall.usesTool()) {
d(`assuming is prebuild-install powered: ${this.prebuildInstall.moduleName}`);
if (await this.prebuildInstall.findPrebuiltModule()) {
d('installed prebuilt module:', this.prebuildInstall.moduleName);
await this.writeMetadata();
await this.cacheModuleState(cacheKey);
return true;
}
}
return false;
}
async findNodePreGypInstallModule(cacheKey) {
if (await this.nodePreGyp.usesTool()) {
d(`assuming is node-pre-gyp powered: ${this.nodePreGyp.moduleName}`);
if (await this.nodePreGyp.findPrebuiltModule()) {
d('installed prebuilt module:', this.nodePreGyp.moduleName);
await this.writeMetadata();
await this.cacheModuleState(cacheKey);
return true;
}
}
return false;
}
async rebuildNodeGypModule(cacheKey) {
await this.nodeGyp.rebuildModule();
d('built via node-gyp:', this.nodeGyp.moduleName);
await this.writeMetadata();
await this.replaceExistingNativeModule();
await this.cacheModuleState(cacheKey);
return true;
}
async replaceExistingNativeModule() {
const buildLocation = path.resolve(this.modulePath, 'build', this.rebuilder.buildType);
d('searching for .node file', buildLocation);
const buildLocationFiles = await promisifiedGracefulFs.readdir(buildLocation);
d('testing files', buildLocationFiles);
const nodeFile = buildLocationFiles.find((file) => file !== '.node' && file.endsWith('.node'));
const nodePath = nodeFile ? path.resolve(buildLocation, nodeFile) : undefined;
if (nodePath && fs.existsSync(nodePath)) {
d('found .node file', nodePath);
if (!this.rebuilder.disablePreGypCopy) {
const abiPath = path.resolve(this.modulePath, `bin/${this.rebuilder.platform}-${this.rebuilder.arch}-${this.rebuilder.ABI}`);
d('copying to prebuilt place:', abiPath);
await fs.promises.mkdir(abiPath, { recursive: true });
await promisifiedGracefulFs.copyFile(nodePath, path.join(abiPath, `${this.nodeGyp.moduleName}.node`));
}
}
}
async writeMetadata() {
await fs.promises.mkdir(path.dirname(this.metaPath), { recursive: true });
await promisifiedGracefulFs.writeFile(this.metaPath, this.metaData);
}
async rebuild(cacheKey) {
if (!this.rebuilder.buildFromSource && ((await this.findPrebuildifyModule(cacheKey)) ||
(await this.findPrebuildInstallModule(cacheKey)) ||
(await this.findNodePreGypInstallModule(cacheKey)))) {
return true;
}
return await this.rebuildNodeGypModule(cacheKey);
}
}
//# sourceMappingURL=module-rebuilder.js.map
File diff suppressed because one or more lines are too long
+22
View File
@@ -0,0 +1,22 @@
import { NodeAPI } from '../node-api.js';
import { IRebuilder } from '../types.js';
type PackageJSONValue = string | Record<string, unknown>;
export declare class NativeModule {
protected rebuilder: IRebuilder;
private _moduleName;
protected modulePath: string;
nodeAPI: NodeAPI;
private packageJSON;
constructor(rebuilder: IRebuilder, modulePath: string);
get moduleName(): string;
packageJSONFieldWithDefault(key: string, defaultValue: PackageJSONValue): Promise<PackageJSONValue>;
packageJSONField(key: string): Promise<PackageJSONValue | undefined>;
getSupportedNapiVersions(): Promise<number[] | undefined>;
/**
* Search dependencies for package using either `packageName` or
* `@namespace/packageName` in the case of forks.
*/
findPackageInDependencies(packageName: string, packageProperty?: string): Promise<string | null>;
}
export declare function locateBinary(basePath: string, suffix: string): Promise<string | null>;
export {};
+68
View File
@@ -0,0 +1,68 @@
import fs from 'graceful-fs';
import path from 'node:path';
import { NodeAPI } from '../node-api.js';
import { readPackageJson } from '../read-package-json.js';
export class NativeModule {
rebuilder;
_moduleName;
modulePath;
nodeAPI;
packageJSON;
constructor(rebuilder, modulePath) {
this.rebuilder = rebuilder;
this.modulePath = modulePath;
this.nodeAPI = new NodeAPI(this.moduleName, this.rebuilder.electronVersion);
}
get moduleName() {
if (!this._moduleName) {
const basename = path.basename(this.modulePath);
const parentDir = path.basename(path.dirname(this.modulePath));
if (parentDir.startsWith('@')) {
this._moduleName = `${parentDir}/${basename}`;
}
this._moduleName = basename;
}
return this._moduleName;
}
async packageJSONFieldWithDefault(key, defaultValue) {
const result = await this.packageJSONField(key);
return result === undefined ? defaultValue : result;
}
async packageJSONField(key) {
this.packageJSON ||= await readPackageJson(this.modulePath);
return this.packageJSON[key];
}
async getSupportedNapiVersions() {
const binary = (await this.packageJSONFieldWithDefault('binary', {}));
return binary?.napi_versions;
}
/**
* Search dependencies for package using either `packageName` or
* `@namespace/packageName` in the case of forks.
*/
async findPackageInDependencies(packageName, packageProperty = 'dependencies') {
const dependencies = await this.packageJSONFieldWithDefault(packageProperty, {});
if (typeof dependencies !== 'object')
return null;
// Look for direct dependency match
// eslint-disable-next-line no-prototype-builtins
if (dependencies.hasOwnProperty(packageName))
return packageName;
const forkedPackage = Object.keys(dependencies).find(dependency => dependency.startsWith('@') && dependency.endsWith(`/${packageName}`));
return forkedPackage || null;
}
}
export async function locateBinary(basePath, suffix) {
let parentPath = basePath;
let testPath;
while (testPath !== parentPath) {
testPath = parentPath;
const checkPath = path.resolve(testPath, suffix);
if (fs.existsSync(checkPath)) {
return checkPath;
}
parentPath = path.resolve(testPath, '..');
}
return null;
}
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/module-type/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACzC,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAK1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AAI,CAAC,CAAC;IAC5C,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,6 @@
import { NativeModule } from '../index.js';
export declare class NodeGyp extends NativeModule {
buildArgs(prefixedArgs: string[]): Promise<string[]>;
buildArgsFromBinaryField(): Promise<string[]>;
rebuildModule(): Promise<void>;
}
+119
View File
@@ -0,0 +1,119 @@
import debug from 'debug';
import detectLibc from 'detect-libc';
import path from 'node:path';
import semver from 'semver';
import { ELECTRON_GYP_DIR } from '../../constants.js';
import { getClangEnvironmentVars } from '../../clang-fetcher.js';
import { NativeModule } from '../index.js';
import { fork } from 'node:child_process';
const d = debug('electron-rebuild');
export class NodeGyp extends NativeModule {
async buildArgs(prefixedArgs) {
const args = [
'node',
'node-gyp',
'rebuild',
...prefixedArgs,
`--runtime=electron`,
`--target=${this.rebuilder.electronVersion}`,
`--arch=${this.rebuilder.arch}`,
`--dist-url=${this.rebuilder.headerURL}`,
'--build-from-source'
];
args.push(d.enabled ? '--verbose' : '--silent');
if (this.rebuilder.debug) {
args.push('--debug');
}
args.push(...(await this.buildArgsFromBinaryField()));
if (this.rebuilder.msvsVersion) {
args.push(`--msvs_version=${this.rebuilder.msvsVersion}`);
}
// Headers of old Electron versions do not have a valid config.gypi file
// and --force-process-config must be passed to node-gyp >= 8.4.0 to
// correctly build modules for them.
// See also https://github.com/nodejs/node-gyp/pull/2497
if (!semver.satisfies(this.rebuilder.electronVersion, '^14.2.0 || ^15.3.0') && semver.major(this.rebuilder.electronVersion) < 16) {
args.push('--force-process-config');
}
return args;
}
async buildArgsFromBinaryField() {
const binary = await this.packageJSONFieldWithDefault('binary', {});
let napiBuildVersion = undefined;
if (Array.isArray(binary.napi_versions)) {
napiBuildVersion = this.nodeAPI.getNapiVersion(binary.napi_versions.map(str => Number(str)));
}
const flags = await Promise.all(Object.entries(binary).map(async ([binaryKey, binaryValue]) => {
if (binaryKey === 'napi_versions') {
return;
}
let value = binaryValue;
if (binaryKey === 'module_path') {
value = path.resolve(this.modulePath, value);
}
value = value.replace('{configuration}', this.rebuilder.buildType)
.replace('{node_abi}', `electron-v${this.rebuilder.electronVersion.split('.').slice(0, 2).join('.')}`)
.replace('{platform}', this.rebuilder.platform)
.replace('{arch}', this.rebuilder.arch)
.replace('{version}', await this.packageJSONField('version'))
.replace('{libc}', await detectLibc.family() || 'unknown');
if (napiBuildVersion !== undefined) {
value = value.replace('{napi_build_version}', napiBuildVersion.toString());
}
for (const [replaceKey, replaceValue] of Object.entries(binary)) {
value = value.replace(`{${replaceKey}}`, replaceValue);
}
return `--${binaryKey}=${value}`;
}));
return flags.filter(value => value);
}
async rebuildModule() {
if (this.rebuilder.platform !== process.platform) {
throw new Error("node-gyp does not support cross-compiling native modules from source.");
}
if (this.modulePath.includes(' ')) {
console.error('Attempting to build a module with a space in the path');
console.error('See https://github.com/nodejs/node-gyp/issues/65#issuecomment-368820565 for reasons why this may not work');
// FIXME: Re-enable the throw when more research has been done
// throw new Error(`node-gyp does not support building modules with spaces in their path, tried to build: ${modulePath}`);
}
const env = {
...process.env,
};
const extraNodeGypArgs = [];
if (this.rebuilder.useElectronClang) {
const { env: clangEnv, args: clangArgs } = await getClangEnvironmentVars(this.rebuilder.electronVersion, this.rebuilder.arch);
Object.assign(env, clangEnv);
extraNodeGypArgs.push(...clangArgs);
}
const nodeGypArgs = await this.buildArgs(extraNodeGypArgs);
d('rebuilding', this.moduleName, 'with args', nodeGypArgs);
const forkedChild = fork(path.resolve(import.meta.dirname, 'worker.js'), {
env,
cwd: this.modulePath,
stdio: 'pipe',
});
const outputBuffers = [];
forkedChild.stdout?.on('data', (chunk) => {
outputBuffers.push(chunk);
});
forkedChild.stderr?.on('data', (chunk) => {
outputBuffers.push(chunk);
});
forkedChild.send({
moduleName: this.moduleName,
nodeGypArgs,
extraNodeGypArgs,
devDir: this.rebuilder.mode === 'sequential' ? ELECTRON_GYP_DIR : path.resolve(ELECTRON_GYP_DIR, '_p', this.moduleName),
});
await new Promise((resolve, reject) => {
forkedChild.on('exit', (code) => {
if (code === 0)
return resolve();
console.error(Buffer.concat(outputBuffers).toString());
reject(new Error(`node-gyp failed to rebuild '${this.modulePath}'`));
});
});
}
}
//# sourceMappingURL=node-gyp.js.map
@@ -0,0 +1 @@
{"version":3,"file":"node-gyp.js","sourceRoot":"","sources":["../../../src/module-type/node-gyp/node-gyp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,uBAAuB,EAAE,MAAM,wBAAwB,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1C,MAAM,CAAC,GAAG,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAEpC,MAAM,OAAO,OAAQ,SAAQ,YAAY;IACvC,KAAK,CAAC,SAAS,CAAC,YAAsB;QACpC,MAAM,IAAI,GAAG;YACX,MAAM;YACN,UAAU;YACV,SAAS;YACT,GAAG,YAAY;YACf,oBAAoB;YACpB,YAAY,IAAI,CAAC,SAAS,CAAC,eAAe,EAAE;YAC5C,UAAU,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE;YAC/B,cAAc,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE;YACxC,qBAAqB;SACtB,CAAC;QAEF,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;QAEhD,IAAI,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACvB,CAAC;QAED,IAAI,CAAC,I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AAa,GAAa,EAAE,CAAC;QACnC,WAAW,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YACvC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;QACH,WAAW,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE;YACvC,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC5B,CAAC,CAAC,CAAC;QACH,WAAW,CAAC,IAAI,CAAC;YACf,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,WAAW;YACX,gBAAgB;YAChB,MAAM,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,IAAI,EAAE,IAAI,CAAC,UAAU,CAAC;SACxH,CAAC,CAAC;QAEH,MAAM,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC1C,WAAW,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;gBAC9B,IAAI,IAAI,KAAK,CAAC;oBAAE,OAAO,OAAO,EAAE,CAAC;gBACjC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC;gBACvD,MAAM,CAAC,IAAI,KAAK,CAAC,+BAA+B,IAAI,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC;YACvE,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC;CACF"}
+1
View File
@@ -0,0 +1 @@
export {};
+32
View File
@@ -0,0 +1,32 @@
import NodeGypRunner from 'node-gyp';
process.on('message', async ({ nodeGypArgs, devDir, extraNodeGypArgs, }) => {
const nodeGyp = NodeGypRunner();
nodeGyp.parseArgv(nodeGypArgs);
nodeGyp.devDir = devDir;
let command = nodeGyp.todo.shift();
try {
while (command) {
if (command.name === 'configure') {
command.args = command.args.filter((arg) => !extraNodeGypArgs.includes(arg));
}
else if (command.name === 'build' && process.platform === 'win32') {
// This is disgusting but it prevents node-gyp from destroying our MSBuild arguments
command.args.map = (fn) => {
return Array.prototype.map.call(command.args, (arg) => {
if (arg.startsWith('/p:'))
return arg;
return fn(arg);
});
};
}
await nodeGyp.commands[command.name](command.args);
command = nodeGyp.todo.shift();
}
process.exit(0);
}
catch (err) {
console.error(err);
process.exit(1);
}
});
//# sourceMappingURL=worker.js.map
@@ -0,0 +1 @@
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../src/module-type/node-gyp/worker.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,MAAM,UAAU,CAAC;AAErC,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,KAAK,EAAE,EAC3B,WAAW,EACX,MAAM,EACN,gBAAgB,GACjB,EAAE,EAAE;IACH,MAAM,OAAO,GAAG,aAAa,EAAE,CAAC;IAChC,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;IAC/B,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC;IACxB,IAAI,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;IACnC,IAAI,CAAC;QACH,OAAO,OAAO,EAAE,CAAC;YACf,IAAI,OAAO,CAAC,IAAI,KAAK,WAAW,EAAE,CAAC;gBACjC,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAW,EAAE,EAAE,CAAC,CAAC,gBAAgB,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;YACvF,CAAC;iBAAM,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO,IAAI,OAAO,CAAC,QAAQ,KAAK,OAAO,EAAE,CAAC;gBACpE,oFAAoF;gBACpF,OAAO,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,EAA2B,EAAE,EAAE;oBACjD,OAAO,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,GAAW,EAAE,EAAE;wBAC5D,IAAI,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC;4BAAE,OAAO,GAAG,CAAC;wBACtC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC;oBACjB,CAAC,CAAC,CAAC;gBACL,CAAC,CAAC;YACJ,CAAC;YACD,MAAM,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YACnD,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;QACjC,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC;AACH,CAAC,CAAC,CAAC"}
+10
View File
@@ -0,0 +1,10 @@
import { NativeModule } from './index.js';
export declare class NodePreGyp extends NativeModule {
usesTool(): Promise<boolean>;
locateBinary(): Promise<string | null>;
run(nodePreGypPath: string): Promise<void>;
findPrebuiltModule(): Promise<boolean>;
getNodePreGypRuntimeArgs(): Promise<string[]>;
private shouldUpdateBinary;
private getModulePaths;
}
+110
View File
@@ -0,0 +1,110 @@
import debug from 'debug';
import { spawn } from '@malept/cross-spawn-promise';
import { readBinaryFileArch } from 'read-binary-file-arch';
import { locateBinary, NativeModule } from './index.js';
const d = debug('electron-rebuild');
export class NodePreGyp extends NativeModule {
async usesTool() {
const packageName = await this.findPackageInDependencies('node-pre-gyp');
return !!packageName;
}
async locateBinary() {
const packageName = await this.findPackageInDependencies('node-pre-gyp');
if (!packageName)
return null;
return locateBinary(this.modulePath, `node_modules/${packageName}/bin/node-pre-gyp`);
}
async run(nodePreGypPath) {
const redownloadBinary = await this.shouldUpdateBinary(nodePreGypPath);
await spawn(process.execPath, [
nodePreGypPath,
'reinstall',
'--fallback-to-build',
...(redownloadBinary ? ['--update-binary'] : []),
`--arch=${this.rebuilder.arch}`, // fallback build arch
`--target_arch=${this.rebuilder.arch}`, // prebuild arch
`--target_platform=${this.rebuilder.platform}`,
...await this.getNodePreGypRuntimeArgs(),
], {
cwd: this.modulePath,
});
}
async findPrebuiltModule() {
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() {
const moduleNapiVersions = await this.getSupportedNapiVersions();
if (moduleNapiVersions) {
return [];
}
else {
return [
'--runtime=electron',
`--target=${this.rebuilder.electronVersion}`,
`--dist-url=${this.rebuilder.headerURL}`,
];
}
}
async shouldUpdateBinary(nodePreGypPath) {
let shouldUpdate = false;
// Redownload binary only if the existing module arch differs from the
// target arch.
try {
const modulePaths = await this.getModulePaths(nodePreGypPath);
d('module paths:', modulePaths);
for (const modulePath of modulePaths) {
let moduleArch;
try {
moduleArch = await readBinaryFileArch(modulePath);
d('module arch:', moduleArch);
}
catch (error) {
d('failed to read module arch:', error.message);
continue;
}
if (moduleArch && moduleArch !== this.rebuilder.arch) {
shouldUpdate = true;
d('module architecture differs:', `${moduleArch} !== ${this.rebuilder.arch}`);
break;
}
}
}
catch (error) {
d('failed to get existing binary arch:', error.message);
// Assume architecture differs
shouldUpdate = true;
}
return shouldUpdate;
}
async getModulePaths(nodePreGypPath) {
const results = await spawn(process.execPath, [
nodePreGypPath,
'reveal',
'module', // pick property with module path
`--target_arch=${this.rebuilder.arch}`,
`--target_platform=${this.rebuilder.platform}`,
], {
cwd: this.modulePath,
});
// Packages with multiple binaries will output one per line
return results.split('\n').filter(Boolean);
}
}
//# sourceMappingURL=node-pre-gyp.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"node-pre-gyp.js","sourceRoot":"","sources":["../../src/module-type/node-pre-gyp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAE3D,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACxD,MAAM,CAAC,GAAG,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAEpC,MAAM,OAAO,UAAW,SAAQ,YAAY;IAC1C,KAAK,CAAC,QAAQ;QACZ,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,yBAAyB,CAAC,cAAc,CAAC,CAAC;QACzE,OAAO,CAAC,CAAC,WAAW,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,YAAY;QAChB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,yBAAyB,CAAC,cAAc,CAAC,CAAC;QACzE,IAAI,CAAC,WAAW;YAAE,OAAO,IAAI,CAAC;QAC9B,OAAO,YAAY,CACjB,IAAI,CAAC,UAAU,EACf,gBAAgB,WAAW,mBAAmB,CAC/C,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,cAAsB;QAC9B,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,kBAAkB,CAAC,cAAc,CAAC,CAAC;QAEvE,MAAM,KAAK,CACT,OAAO,CAAC,QAAQ,EAChB;YACE,cAAc;YACd,WAAW;YACX,qBAAqB;YACrB,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YAChD,UAAU,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE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AC,QAAQ,EAAE;SAC/C,EAAE;YACD,GAAG,EAAE,IAAI,CAAC,UAAU;SACrB,CAAC,CAAC;QAEH,2DAA2D;QAC3D,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC7C,CAAC;CACF"}
+12
View File
@@ -0,0 +1,12 @@
import { NativeModule } from './index.js';
export declare class PrebuildInstall extends NativeModule {
usesTool(): Promise<boolean>;
locateBinary(): Promise<string | null>;
run(prebuildInstallPath: string): Promise<void>;
findPrebuiltModule(): Promise<boolean>;
/**
* Whether a prebuild-install-based native module exists.
*/
prebuiltModuleExists(): Promise<boolean>;
getPrebuildInstallRuntimeArgs(): Promise<string[]>;
}
+73
View File
@@ -0,0 +1,73 @@
import debug from 'debug';
import fs from 'graceful-fs';
import path from 'node:path';
import { spawn } from '@malept/cross-spawn-promise';
import { locateBinary, NativeModule } from './index.js';
const d = debug('electron-rebuild');
export class PrebuildInstall extends NativeModule {
async usesTool() {
const packageName = await this.findPackageInDependencies('prebuild-install');
return !!packageName;
}
async locateBinary() {
const packageName = await this.findPackageInDependencies('prebuild-install');
if (!packageName)
return null;
return locateBinary(this.modulePath, `node_modules/${packageName}/bin.js`);
}
async run(prebuildInstallPath) {
await spawn(process.execPath, [
path.resolve(import.meta.dirname, '..', `prebuild-shim.js`),
prebuildInstallPath,
`--arch=${this.rebuilder.arch}`,
`--platform=${this.rebuilder.platform}`,
`--tag-prefix=${this.rebuilder.prebuildTagPrefix}`,
...await this.getPrebuildInstallRuntimeArgs(),
], {
cwd: this.modulePath,
});
}
async findPrebuiltModule() {
const prebuildInstallPath = await this.locateBinary();
if (prebuildInstallPath) {
d(`triggering prebuild download step: ${this.moduleName}`);
try {
await this.run(prebuildInstallPath);
return true;
}
catch (err) {
d('failed to use prebuild-install:', err);
if (err?.message?.includes('requires Node-API but Electron')) {
throw err;
}
}
}
else {
d(`could not find prebuild-install relative to: ${this.modulePath}`);
}
return false;
}
/**
* Whether a prebuild-install-based native module exists.
*/
async prebuiltModuleExists() {
return fs.existsSync(path.resolve(this.modulePath, 'prebuilds', `${this.rebuilder.platform}-${this.rebuilder.arch}`, `electron-${this.rebuilder.ABI}.node`));
}
async getPrebuildInstallRuntimeArgs() {
const moduleNapiVersions = await this.getSupportedNapiVersions();
if (moduleNapiVersions) {
const napiVersion = this.nodeAPI.getNapiVersion(moduleNapiVersions);
return [
'--runtime=napi',
`--target=${napiVersion}`,
];
}
else {
return [
'--runtime=electron',
`--target=${this.rebuilder.electronVersion}`,
];
}
}
}
//# sourceMappingURL=prebuild-install.js.map
@@ -0,0 +1 @@
{"version":3,"file":"prebuild-install.js","sourceRoot":"","sources":["../../src/module-type/prebuild-install.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AAEpD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACxD,MAAM,CAAC,GAAG,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAEpC,MAAM,OAAO,eAAgB,SAAQ,YAAY;IAC/C,KAAK,CAAC,QAAQ;QACZ,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,yBAAyB,CAAC,kBAAkB,CAAC,CAAC;QAC7E,OAAO,CAAC,CAAC,WAAW,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,YAAY;QAChB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,yBAAyB,CAAC,kBAAkB,CAAC,CAAC;QAC7E,IAAI,CAAC,WAAW;YAAE,OAAO,IAAI,CAAC;QAC9B,OAAO,YAAY,CACjB,IAAI,CAAC,UAAU,EACf,gBAAgB,WAAW,SAAS,CACrC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,mBAA2B;QACnC,MA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I,CAAC,SAAS,CAAC,IAAI,EAAE,EAAE,YAAY,IAAI,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;IAC/J,CAAC;IAED,KAAK,CAAC,6BAA6B;QACjC,MAAM,kBAAkB,GAAG,MAAM,IAAI,CAAC,wBAAwB,EAAE,CAAC;QACjE,IAAI,kBAAkB,EAAE,CAAC;YACvB,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,kBAAkB,CAAC,CAAC;YACpE,OAAO;gBACL,gBAAgB;gBAChB,YAAY,WAAW,EAAE;aAC1B,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,OAAO;gBACL,oBAAoB;gBACpB,YAAY,IAAI,CAAC,SAAS,CAAC,eAAe,EAAE;aAC7C,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
+11
View File
@@ -0,0 +1,11 @@
import { NativeModule } from './index.js';
export declare function determineNativePrebuildArch(arch: string): string;
/**
* The extension of `prebuildify`-generated native modules, after the last `.`. This value differs
* based on whether the target arch is ARM-based.
*/
export declare function determineNativePrebuildExtension(arch: string): string;
export declare class Prebuildify extends NativeModule {
usesTool(): Promise<boolean>;
findPrebuiltModule(): Promise<boolean>;
}
+58
View File
@@ -0,0 +1,58 @@
import debug from 'debug';
import fs from 'graceful-fs';
import path from 'node:path';
import { getNodeArch } from '../arch.js';
import { NativeModule } from './index.js';
const d = debug('electron-rebuild');
export function determineNativePrebuildArch(arch) {
if (arch === 'armv7l') {
return 'arm';
}
return arch;
}
/**
* The extension of `prebuildify`-generated native modules, after the last `.`. This value differs
* based on whether the target arch is ARM-based.
*/
export function determineNativePrebuildExtension(arch) {
switch (arch) {
case 'arm64':
return 'armv8.node';
case 'armv7l':
return 'armv7.node';
}
return 'node';
}
export class Prebuildify extends NativeModule {
async usesTool() {
const packageName = await this.findPackageInDependencies('prebuildify', 'devDependencies');
return !!packageName;
}
async findPrebuiltModule() {
d(`Checking for prebuilds for "${this.moduleName}"`);
const prebuildsDir = path.join(this.modulePath, 'prebuilds');
if (!(fs.existsSync(prebuildsDir))) {
d(`Could not find the prebuilds directory at "${prebuildsDir}"`);
return false;
}
const nodeArch = getNodeArch(this.rebuilder.arch, process.config.variables);
const prebuiltModuleDir = path.join(prebuildsDir, `${this.rebuilder.platform}-${determineNativePrebuildArch(nodeArch)}`);
const nativeExt = determineNativePrebuildExtension(nodeArch);
const electronNapiModuleFilename = path.join(prebuiltModuleDir, `electron.napi.${nativeExt}`);
const nodejsNapiModuleFilename = path.join(prebuiltModuleDir, `node.napi.${nativeExt}`);
const abiModuleFilename = path.join(prebuiltModuleDir, `electron.abi${this.rebuilder.ABI}.${nativeExt}`);
if (fs.existsSync(electronNapiModuleFilename) || fs.existsSync(nodejsNapiModuleFilename)) {
this.nodeAPI.ensureElectronSupport();
d(`Found prebuilt Node-API module in ${prebuiltModuleDir}"`);
}
else if (fs.existsSync(abiModuleFilename)) {
d(`Found prebuilt module: "${abiModuleFilename}"`);
}
else {
d(`Could not locate "${electronNapiModuleFilename}", "${nodejsNapiModuleFilename}", or "${abiModuleFilename}"`);
return false;
}
return true;
}
}
//# sourceMappingURL=prebuildify.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"prebuildify.js","sourceRoot":"","sources":["../../src/module-type/prebuildify.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAmB,WAAW,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE1C,MAAM,CAAC,GAAG,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAEpC,MAAM,UAAU,2BAA2B,CAAC,IAAY;IACtD,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;QACtB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,IAAI,CAAC;AACd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,CAAC,iBAAiB,EAAE,aAAa,SAAS,EAAE,CAAC,CAAC;QACxF,MAAM,iBAAiB,GAAG,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,eAAe,IAAI,CAAC,SAAS,CAAC,GAAG,IAAI,SAAS,EAAE,CAAC,CAAC;QAEzG,IAAI,EAAE,CAAC,UAAU,CAAC,0BAA0B,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,wBAAwB,CAAC,EAAE,CAAC;YACzF,IAAI,CAAC,OAAO,CAAC,qBAAqB,EAAE,CAAC;YACrC,CAAC,CAAC,qCAAqC,iBAAiB,GAAG,CAAC,CAAC;QAC/D,CAAC;aAAM,IAAI,EAAE,CAAC,UAAU,CAAC,iBAAiB,CAAC,EAAE,CAAC;YAC5C,CAAC,CAAC,2BAA2B,iBAAiB,GAAG,CAAC,CAAC;QACrD,CAAC;aAAM,CAAC;YACN,CAAC,CAAC,qBAAqB,0BAA0B,OAAO,wBAAwB,UAAU,iBAAiB,GAAG,CAAC,CAAC;YAChH,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
+17
View File
@@ -0,0 +1,17 @@
export type ModuleType = 'prod' | 'dev' | 'optional';
export declare class ModuleWalker {
buildPath: string;
modulesToRebuild: string[];
onlyModules: string[] | null;
prodDeps: Set<string>;
projectRootPath?: string;
realModulePaths: Set<string>;
realNodeModulesPaths: Set<string>;
types: ModuleType[];
constructor(buildPath: string, projectRootPath: string | undefined, types: ModuleType[], prodDeps: Set<string>, onlyModules: string[] | null);
get nodeModulesPaths(): Promise<string[]>;
walkModules(): Promise<void>;
findModule(moduleName: string, fromDir: string, foundFn: ((p: string) => Promise<void>)): Promise<void[]>;
markChildrenAsProdDeps(modulePath: string): Promise<void>;
findAllModulesIn(nodeModulesPath: string, prefix?: string): Promise<void>;
}
+129
View File
@@ -0,0 +1,129 @@
import debug from 'debug';
import fs from 'graceful-fs';
import path from 'node:path';
import { readPackageJson } from './read-package-json.js';
import { searchForModule, searchForNodeModules } from './search-module.js';
import { promisifiedGracefulFs } from './promisifiedGracefulFs.js';
const d = debug('electron-rebuild');
export class ModuleWalker {
buildPath;
modulesToRebuild;
onlyModules;
prodDeps;
projectRootPath;
realModulePaths;
realNodeModulesPaths;
types;
constructor(buildPath, projectRootPath, types, prodDeps, onlyModules) {
this.buildPath = buildPath;
this.modulesToRebuild = [];
this.projectRootPath = projectRootPath;
this.types = types;
this.prodDeps = prodDeps;
this.onlyModules = onlyModules;
this.realModulePaths = new Set();
this.realNodeModulesPaths = new Set();
}
get nodeModulesPaths() {
return searchForNodeModules(this.buildPath, this.projectRootPath);
}
async walkModules() {
const rootPackageJson = await readPackageJson(this.buildPath);
const markWaiters = [];
const depKeys = [];
if (this.types.includes('prod') || this.onlyModules) {
depKeys.push(...Object.keys(rootPackageJson.dependencies || {}));
}
if (this.types.includes('optional') || this.onlyModules) {
depKeys.push(...Object.keys(rootPackageJson.optionalDependencies || {}));
}
if (this.types.includes('dev') || this.onlyModules) {
depKeys.push(...Object.keys(rootPackageJson.devDependencies || {}));
}
for (const key of depKeys) {
this.prodDeps.add(key);
const modulePaths = await searchForModule(this.buildPath, key, this.projectRootPath);
for (const modulePath of modulePaths) {
markWaiters.push(this.markChildrenAsProdDeps(modulePath));
}
}
await Promise.all(markWaiters);
d('identified prod deps:', this.prodDeps);
}
async findModule(moduleName, fromDir, foundFn) {
const testPaths = await searchForModule(fromDir, moduleName, this.projectRootPath);
const foundFns = testPaths.map(testPath => foundFn(testPath));
return Promise.all(foundFns);
}
async markChildrenAsProdDeps(modulePath) {
if (!fs.existsSync(modulePath)) {
return;
}
d('exploring', modulePath);
let childPackageJson;
try {
childPackageJson = await readPackageJson(modulePath, true);
}
catch (err) {
return;
}
const moduleWait = [];
const callback = this.markChildrenAsProdDeps.bind(this);
for (const key of Object.keys(childPackageJson.dependencies || {}).concat(Object.keys(childPackageJson.optionalDependencies || {}))) {
if (this.prodDeps.has(key)) {
continue;
}
this.prodDeps.add(key);
moduleWait.push(this.findModule(key, modulePath, callback));
}
await Promise.all(moduleWait);
}
async findAllModulesIn(nodeModulesPath, prefix = '') {
// Some package managers use symbolic links when installing node modules
// we need to be sure we've never tested the a package before by resolving
// all symlinks in the path and testing against a set
const realNodeModulesPath = await fs.promises.realpath(nodeModulesPath);
if (this.realNodeModulesPaths.has(realNodeModulesPath)) {
return;
}
this.realNodeModulesPaths.add(realNodeModulesPath);
d('scanning:', realNodeModulesPath);
for (const modulePath of await promisifiedGracefulFs.readdir(realNodeModulesPath)) {
// Ignore the magical .bin directory
if (modulePath === '.bin')
continue;
const subPath = path.resolve(nodeModulesPath, modulePath);
// Ensure that we don't mark modules as needing to be rebuilt more than once
// by ignoring / resolving symlinks
let realPath;
try {
realPath = await fs.promises.realpath(subPath);
}
catch (error) {
// pnpm leaves dangling symlinks when modules are removed
if (error.code === 'ENOENT') {
const stat = await fs.promises.lstat(subPath);
if (stat.isSymbolicLink()) {
continue;
}
}
throw error;
}
if (this.realModulePaths.has(realPath)) {
continue;
}
this.realModulePaths.add(realPath);
const moduleName = `${prefix}${modulePath}`;
if (this.prodDeps.has(moduleName) && (!this.onlyModules || this.onlyModules.includes(moduleName))) {
this.modulesToRebuild.push(realPath);
}
if (modulePath.startsWith('@')) {
await this.findAllModulesIn(realPath, `${modulePath}/`);
}
if (fs.existsSync(path.resolve(nodeModulesPath, modulePath, 'node_modules'))) {
await this.findAllModulesIn(path.resolve(realPath, 'node_modules'));
}
}
}
}
//# sourceMappingURL=module-walker.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"module-walker.js","sourceRoot":"","sources":["../src/module-walker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,eAAe,EAAE,oB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AAQ,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC;gBAClG,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACvC,CAAC;YAED,IAAI,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC/B,MAAM,IAAI,CAAC,gBAAgB,CAAC,QAAQ,EAAE,GAAG,UAAU,GAAG,CAAC,CAAC;YAC1D,CAAC;YAED,IAAI,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,UAAU,EAAE,cAAc,CAAC,CAAC,EAAE,CAAC;gBAC7E,MAAM,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC,CAAC;YACtE,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
+8
View File
@@ -0,0 +1,8 @@
export declare class NodeAPI {
private moduleName;
private electronVersion;
constructor(moduleName: string, electronVersion: string);
ensureElectronSupport(): void;
getVersionForElectron(): number;
getNapiVersion(moduleNapiVersions: number[]): number;
}
+29
View File
@@ -0,0 +1,29 @@
import { fromElectronVersion as napiVersionFromElectronVersion } from 'node-api-version';
export class NodeAPI {
moduleName;
electronVersion;
constructor(moduleName, electronVersion) {
this.moduleName = moduleName;
this.electronVersion = electronVersion;
}
ensureElectronSupport() {
this.getVersionForElectron();
}
getVersionForElectron() {
const electronNapiVersion = napiVersionFromElectronVersion(this.electronVersion);
if (!electronNapiVersion) {
throw new Error(`Native module '${this.moduleName}' requires Node-API but Electron v${this.electronVersion} does not support Node-API`);
}
return electronNapiVersion;
}
getNapiVersion(moduleNapiVersions) {
const electronNapiVersion = this.getVersionForElectron();
// Filter out Node-API versions that are too high
const filteredVersions = moduleNapiVersions.filter((v) => (v <= electronNapiVersion));
if (filteredVersions.length === 0) {
throw new Error(`Native module '${this.moduleName}' supports Node-API versions ${moduleNapiVersions} but Electron v${this.electronVersion} only supports Node-API v${electronNapiVersion}`);
}
return Math.max(...filteredVersions);
}
}
//# sourceMappingURL=node-api.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"node-api.js","sourceRoot":"","sources":["../src/node-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,IAAI,8BAA8B,EAAE,MAAM,kBAAkB,CAAC;AAEzF,MAAM,OAAO,OAAO;IACV,UAAU,CAAS;IACnB,eAAe,CAAS;IAEhC,YAAY,UAAkB,EAAE,eAAuB;QACrD,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;IACzC,CAAC;IAED,qBAAqB;QACnB,IAAI,CAAC,qBAAqB,EAAE,CAAC;IAC/B,CAAC;IAED,qBAAqB;QACnB,MAAM,mBAAmB,GAAG,8BAA8B,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;QAEjF,IAAI,CAAC,mBAAmB,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,kBAAkB,IAAI,CAAC,UAAU,qCAAqC,IAAI,CAAC,eAAe,4BAA4B,CAAC,CAAC;QAC1I,CAAC;QAED,OAAO,mBAAmB,CAAC;IAC7B,CAAC;IAED,cAAc,CAAC,kBAA4B;QACzC,MAAM,mBAAmB,GAAG,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAEzD,iDAAiD;QACjD,MAAM,gBAAgB,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,mBAAmB,CAAC,CAAC,CAAC;QAEtF,IAAI,gBAAgB,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,kBAAkB,IAAI,CAAC,UAAU,gCAAgC,kBAAkB,kBAAkB,IAAI,CAAC,eAAe,4BAA4B,mBAAmB,EAAE,CAAC,CAAC;QAC9L,CAAC;QAED,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,gBAAgB,CAAC,CAAC;IACvC,CAAC;CACF"}
+1
View File
@@ -0,0 +1 @@
export {};
+7
View File
@@ -0,0 +1,7 @@
import { pathToFileURL } from 'node:url';
process.argv.splice(1, 1);
// This tricks prebuild-install into not validating on the
// 1.8.x and 8.x ABI collision
Object.defineProperty(process.versions, 'modules', { value: '-1', writable: false });
import(pathToFileURL(process.argv[1]).toString());
//# sourceMappingURL=prebuild-shim.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"prebuild-shim.js","sourceRoot":"","sources":["../src/prebuild-shim.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAE1B,0DAA0D;AAC1D,8BAA8B;AAC9B,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,QAAQ,EAAE,SAAS,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAC;AAErF,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC"}
+3
View File
@@ -0,0 +1,3 @@
/// <reference types="node" resolution-mode="require"/>
import gracefulFS from 'graceful-fs';
export declare const promisifiedGracefulFs: Pick<typeof gracefulFS.promises, "copyFile" | "readFile" | "readdir" | "writeFile">;
+9
View File
@@ -0,0 +1,9 @@
import gracefulFS from 'graceful-fs';
import { promisify } from 'node:util';
export const promisifiedGracefulFs = {
copyFile: promisify(gracefulFS.copyFile),
readFile: promisify(gracefulFS.readFile),
readdir: promisify(gracefulFS.readdir),
writeFile: promisify(gracefulFS.writeFile),
};
//# sourceMappingURL=promisifiedGracefulFs.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"promisifiedGracefulFs.js","sourceRoot":"","sources":["../src/promisifiedGracefulFs.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,QAAQ,EAAE,SAAS,CAAC,UAAU,CAAC,QAAQ,CAAC;IACxC,QAAQ,EAAE,SAAS,CAAC,UAAU,CAAC,QAAQ,CAAC;IACxC,OAAO,EAAE,SAAS,CAAC,UAAU,CAAC,OAAO,CAAC;IACtC,SAAS,EAAE,SAAS,CAAC,UAAU,CAAC,SAAS,CAAC;CAC+C,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare function readPackageJson(dir: string, safe?: boolean): Promise<any>;
+19
View File
@@ -0,0 +1,19 @@
import { promisifiedGracefulFs } from './promisifiedGracefulFs.js';
import path from 'node:path';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function readPackageJson(dir, safe = false) {
try {
return JSON.parse(await promisifiedGracefulFs.readFile(path.resolve(dir, 'package.json'), {
encoding: 'utf-8',
}));
}
catch (err) {
if (safe) {
return {};
}
else {
throw err;
}
}
}
//# sourceMappingURL=read-package-json.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"read-package-json.js","sourceRoot":"","sources":["../src/read-package-json.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,8DAA8D;AAC9D,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,GAAW,EAAE,IAAI,GAAG,KAAK;IAC7D,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,qBAAqB,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,cAAc,CAAC,EAAE;YACxF,QAAQ,EAAE,OAAO;SAClB,CAAC,CAAC,CAAC;IACN,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,IAAI,EAAE,CAAC;YACT,OAAO,EAAE,CAAC;QACZ,CAAC;aAAM,CAAC;YACN,MAAM,GAAG,CAAC;QACZ,CAAC;IACH,CAAC;AACH,CAAC"}
+150
View File
@@ -0,0 +1,150 @@
/// <reference types="node" resolution-mode="require"/>
/// <reference types="node" resolution-mode="require"/>
import { EventEmitter } from 'node:events';
import { BuildType, IRebuilder, RebuildMode } from './types.js';
import { ModuleType } from './module-walker.js';
export interface RebuildOptions {
/**
* The path to the `node_modules` directory to rebuild.
*/
buildPath: string;
/**
* The version of Electron to build against.
*/
electronVersion: string;
/**
* Override the target platform to something other than the host system platform.
* Note: This only applies to downloading prebuilt binaries. **It is not possible to cross-compile native modules.**
*
* @defaultValue The system {@link https://nodejs.org/api/process.html#processplatform | `process.platform`} value
*/
platform?: NodeJS.Platform;
/**
* Override the target rebuild architecture to something other than the host system architecture.
*
* @defaultValue The system {@link https://nodejs.org/api/process.html#processarch | `process.arch`} value
*/
arch?: string;
/**
* An array of module names to rebuild in addition to detected modules
* @default []
*/
extraModules?: string[];
/**
* An array of module names to rebuild. **Only** these modules will be rebuilt.
*/
onlyModules?: string[] | null;
/**
* Force a rebuild of modules regardless of their current build state.
*/
force?: boolean;
/**
* URL to download Electron header files from.
* @defaultValue `https://www.electronjs.org/headers`
*/
headerURL?: string;
/**
* Array of types of dependencies to rebuild. Possible values are `prod`, `dev`, and `optional`.
*
* @defaultValue `['prod', 'optional']`
*/
types?: ModuleType[];
/**
* Whether to rebuild modules sequentially or in parallel.
*
* @defaultValue `sequential`
*/
mode?: RebuildMode;
/**
* Rebuilds a Debug build of target modules. If this is `false`, a Release build will be generated instead.
*
* @defaultValue false
*/
debug?: boolean;
/**
* Enables hash-based caching to speed up local rebuilds.
*
* @experimental
* @defaultValue false
*/
useCache?: boolean;
/**
* Whether to use the `clang` executable that Electron uses when building.
* This will guarantee compiler compatibility.
*
* @defaultValue false
*/
useElectronClang?: boolean;
/**
* Sets a custom cache path for the {@link useCache} option.
* @experimental
* @defaultValue a `.electron-rebuild-cache` folder in the `os.homedir()` directory
*/
cachePath?: string;
/**
* GitHub tag prefix passed to {@link https://www.npmjs.com/package/prebuild-install | `prebuild-install`}.
* @defaultValue `v`
*/
prebuildTagPrefix?: string;
/**
* Path to the root of the project if using npm or yarn workspaces.
*/
projectRootPath?: string;
/**
* Override the Application Binary Interface (ABI) version for the version of Electron you are targeting.
* Only use when targeting nightly releases.
*
* @see the {@link https://github.com/electron/node-abi | electron/node-abi} repository for a list of Electron and Node.js ABIs
*/
forceABI?: number;
/**
* Disables the copying of `.node` files if not needed.
* @defaultValue false
*/
disablePreGypCopy?: boolean;
/**
* Skip prebuild download and rebuild module from source.
*
* @defaultValue false
*/
buildFromSource?: boolean;
/**
* Array of module names to ignore during the rebuild process.
*/
ignoreModules?: string[];
}
export interface RebuilderOptions extends RebuildOptions {
lifecycle: EventEmitter;
}
export declare class Rebuilder implements IRebuilder {
private ABIVersion;
private moduleWalker;
rebuilds: (() => Promise<void>)[];
lifecycle: EventEmitter;
buildPath: string;
electronVersion: string;
platform: NodeJS.Platform;
arch: string;
force: boolean;
headerURL: string;
mode: RebuildMode;
debug: boolean;
useCache: boolean;
cachePath: string;
prebuildTagPrefix: string;
msvsVersion?: string;
useElectronClang: boolean;
disablePreGypCopy: boolean;
buildFromSource: boolean;
ignoreModules: string[];
constructor(options: RebuilderOptions);
get ABI(): string;
get buildType(): BuildType;
rebuild(): Promise<void>;
modulesToRebuild(): Promise<string[]>;
rebuildModuleAt(modulePath: string): Promise<void>;
}
export type RebuildResult = Promise<void> & {
lifecycle: EventEmitter;
};
export declare function rebuild(options: RebuildOptions): RebuildResult;
+173
View File
@@ -0,0 +1,173 @@
import debug from 'debug';
import { EventEmitter } from 'node:events';
import fs from 'graceful-fs';
import { getAbi } from 'node-abi';
import os from 'node:os';
import path from 'node:path';
import { generateCacheKey, lookupModuleState } from './cache.js';
import { BuildType } from './types.js';
import { ModuleRebuilder } from './module-rebuilder.js';
import { ModuleWalker } from './module-walker.js';
const d = debug('electron-rebuild');
const defaultMode = 'sequential';
const defaultTypes = ['prod', 'optional'];
export class Rebuilder {
ABIVersion;
moduleWalker;
rebuilds;
lifecycle;
buildPath;
electronVersion;
platform;
arch;
force;
headerURL;
mode;
debug;
useCache;
cachePath;
prebuildTagPrefix;
msvsVersion;
useElectronClang;
disablePreGypCopy;
buildFromSource;
ignoreModules;
constructor(options) {
this.lifecycle = options.lifecycle;
this.buildPath = options.buildPath;
this.electronVersion = options.electronVersion;
this.platform = options.platform || process.platform;
this.arch = options.arch || process.arch;
this.force = options.force || false;
this.headerURL = options.headerURL || 'https://www.electronjs.org/headers';
this.mode = options.mode || defaultMode;
this.debug = options.debug || false;
this.useCache = options.useCache || false;
this.useElectronClang = options.useElectronClang || false;
this.cachePath = options.cachePath || path.resolve(os.homedir(), '.electron-rebuild-cache');
this.prebuildTagPrefix = options.prebuildTagPrefix || 'v';
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.');
this.useCache = false;
}
if (typeof this.electronVersion === 'number') {
if (`${this.electronVersion}`.split('.').length === 1) {
this.electronVersion = `${this.electronVersion}.0.0`;
}
else {
this.electronVersion = `${this.electronVersion}.0`;
}
}
if (typeof this.electronVersion !== 'string') {
throw new Error(`Expected a string version for electron version, got a "${typeof this.electronVersion}"`);
}
this.ABIVersion = options.forceABI?.toString();
const onlyModules = options.onlyModules || null;
const extraModules = new Set(options.extraModules);
const types = options.types || defaultTypes;
this.moduleWalker = new ModuleWalker(this.buildPath, options.projectRootPath, types, extraModules, onlyModules);
this.rebuilds = [];
d('rebuilding with args:', this.buildPath, this.electronVersion, this.platform, this.arch, extraModules, this.force, this.headerURL, types, this.debug);
}
get ABI() {
if (this.ABIVersion === undefined) {
this.ABIVersion = getAbi(this.electronVersion, 'electron');
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.ABIVersion;
}
get buildType() {
return this.debug ? BuildType.Debug : BuildType.Release;
}
async rebuild() {
if (!path.isAbsolute(this.buildPath)) {
throw new Error('Expected buildPath to be an absolute path');
}
this.lifecycle.emit('start');
for (const modulePath of await this.modulesToRebuild()) {
this.rebuilds.push(() => this.rebuildModuleAt(modulePath));
}
this.rebuilds.push(() => this.rebuildModuleAt(this.buildPath));
if (this.mode !== 'sequential') {
await Promise.all(this.rebuilds.map(fn => fn()));
}
else {
for (const rebuildFn of this.rebuilds) {
await rebuildFn();
}
}
}
async modulesToRebuild() {
await this.moduleWalker.walkModules();
for (const nodeModulesPath of await this.moduleWalker.nodeModulesPaths) {
await this.moduleWalker.findAllModulesIn(nodeModulesPath);
}
return this.moduleWalker.modulesToRebuild;
}
async rebuildModuleAt(modulePath) {
if (!(fs.existsSync(path.resolve(modulePath, 'binding.gyp')))) {
return;
}
const moduleRebuilder = new ModuleRebuilder(this, 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: ${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: ${moduleName} as it was prebuilt`);
return;
}
let cacheKey;
if (this.useCache) {
cacheKey = await generateCacheKey({
ABI: this.ABI,
arch: this.arch,
platform: this.platform,
debug: this.debug,
electronVersion: this.electronVersion,
headerURL: this.headerURL,
modulePath,
});
const applyDiffFn = await lookupModuleState(this.cachePath, cacheKey);
if (typeof applyDiffFn === 'function') {
await applyDiffFn(modulePath);
this.lifecycle.emit('module-done', moduleName);
return;
}
}
if (await moduleRebuilder.rebuild(cacheKey)) {
this.lifecycle.emit('module-done', moduleName);
}
}
}
export function rebuild(options) {
// eslint-disable-next-line prefer-rest-params
d('rebuilding with args:', arguments);
const lifecycle = new EventEmitter();
const rebuilderOptions = { ...options, lifecycle };
const rebuilder = new Rebuilder(rebuilderOptions);
const ret = rebuilder.rebuild();
ret.lifecycle = lifecycle;
return ret;
}
//# sourceMappingURL=rebuild.js.map
File diff suppressed because one or more lines are too long
+23
View File
@@ -0,0 +1,23 @@
/**
* Find all instances of a given module in node_modules subdirectories while traversing up
* ancestor directories.
*
* @param cwd the initial directory to traverse
* @param moduleName the Node module name (should work for scoped modules as well)
* @param rootPath the project's root path. If provided, the traversal will stop at this path.
*/
export declare function searchForModule(cwd: string, moduleName: string, rootPath?: string): Promise<string[]>;
/**
* Find all instances of node_modules subdirectories while traversing up ancestor directories.
*
* @param cwd the initial directory to traverse
* @param rootPath the project's root path. If provided, the traversal will stop at this path.
*/
export declare function searchForNodeModules(cwd: string, rootPath?: string): Promise<string[]>;
/**
* Determine the root directory of a given project, by looking for a directory with an
* NPM or yarn lockfile or pnpm lockfile.
*
* @param cwd the initial directory to traverse
*/
export declare function getProjectRootPath(cwd: string): Promise<string>;
+68
View File
@@ -0,0 +1,68 @@
import fs from 'graceful-fs';
import path from 'node:path';
async function shouldContinueSearch(traversedPath, rootPath, stopAtPackageJSON) {
if (rootPath) {
return Promise.resolve(traversedPath !== path.dirname(rootPath));
}
else if (stopAtPackageJSON) {
return fs.existsSync(path.join(traversedPath, 'package.json'));
}
else {
return true;
}
}
async function traverseAncestorDirectories(cwd, pathGenerator, rootPath, maxItems, stopAtPackageJSON) {
const paths = [];
let traversedPath = path.resolve(cwd);
while (await shouldContinueSearch(traversedPath, rootPath, stopAtPackageJSON)) {
const generatedPath = pathGenerator(traversedPath);
if (fs.existsSync(generatedPath)) {
paths.push(generatedPath);
}
const parentPath = path.dirname(traversedPath);
if (parentPath === traversedPath || (maxItems && paths.length >= maxItems)) {
break;
}
traversedPath = parentPath;
}
return paths;
}
/**
* Find all instances of a given module in node_modules subdirectories while traversing up
* ancestor directories.
*
* @param cwd the initial directory to traverse
* @param moduleName the Node module name (should work for scoped modules as well)
* @param rootPath the project's root path. If provided, the traversal will stop at this path.
*/
export async function searchForModule(cwd, moduleName, rootPath) {
const pathGenerator = (traversedPath) => path.join(traversedPath, 'node_modules', moduleName);
return traverseAncestorDirectories(cwd, pathGenerator, rootPath, undefined, true);
}
/**
* Find all instances of node_modules subdirectories while traversing up ancestor directories.
*
* @param cwd the initial directory to traverse
* @param rootPath the project's root path. If provided, the traversal will stop at this path.
*/
export async function searchForNodeModules(cwd, rootPath) {
const pathGenerator = (traversedPath) => path.join(traversedPath, 'node_modules');
return traverseAncestorDirectories(cwd, pathGenerator, rootPath, undefined, true);
}
/**
* Determine the root directory of a given project, by looking for a directory with an
* NPM or yarn lockfile or pnpm lockfile.
*
* @param cwd the initial directory to traverse
*/
export async function getProjectRootPath(cwd) {
for (const lockFilename of ['yarn.lock', 'package-lock.json', 'pnpm-lock.yaml']) {
const pathGenerator = (traversedPath) => path.join(traversedPath, lockFilename);
const lockPaths = await traverseAncestorDirectories(cwd, pathGenerator, undefined, 1);
if (lockPaths.length > 0) {
return path.dirname(lockPaths[0]);
}
}
return cwd;
}
//# sourceMappingURL=search-module.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"search-module.js","sourceRoot":"","sources":["../src/search-module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,KAAK,UAAU,oBAAoB,CAAC,a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E,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,cAAc,EAAE,UAAU,CAAC,CAAC;IACrH,OAAO,2BAA2B,CAAC,GAAG,EAAE,aAAa,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;AACpF,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,GAAW,EAAE,QAAiB;IACvE,MAAM,aAAa,GAA0B,CAAC,aAAa,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,cAAc,CAAC,CAAC;IACzG,OAAO,2BAA2B,CAAC,GAAG,EAAE,aAAa,EAAE,QAAQ,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;AACpF,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,GAAW;IAClD,KAAK,MAAM,YAAY,IAAI,CAAC,WAAW,EAAE,mBAAmB,EAAE,gBAAgB,CAAC,EAAE,CAAC;QAChF,MAAM,aAAa,GAA0B,CAAC,aAAa,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,YAAY,CAAC,CAAC;QACvG,MAAM,SAAS,GAAG,MAAM,2BAA2B,CAAC,GAAG,EAAE,aAAa,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;QACtF,IAAI,SAAS,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACzB,OAAO,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
+1
View File
@@ -0,0 +1 @@
export declare function downloadLinuxSysroot(electronVersion: string, targetArch: string): Promise<string>;
+40
View File
@@ -0,0 +1,40 @@
import { spawn } from '@malept/cross-spawn-promise';
import crypto from 'node:crypto';
import debug from 'debug';
import fs from 'graceful-fs';
import path from 'node:path';
import { ELECTRON_GYP_DIR } from './constants.js';
import { fetch } from './fetcher.js';
import { promisifiedGracefulFs } from './promisifiedGracefulFs.js';
const d = debug('electron-rebuild');
const sysrootArchAliases = {
x64: 'amd64',
ia32: 'i386',
};
const SYSROOT_BASE_URL = 'https://dev-cdn.electronjs.org/linux-sysroots';
export async function downloadLinuxSysroot(electronVersion, targetArch) {
d('fetching sysroot for Electron:', electronVersion);
const sysrootDir = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-sysroot`);
if (fs.existsSync(path.resolve(sysrootDir, 'lib')))
return sysrootDir;
await fs.promises.mkdir(sysrootDir, { recursive: true });
const linuxArch = sysrootArchAliases[targetArch] || targetArch;
const electronSysroots = JSON.parse(await fetch(`https://raw.githubusercontent.com/electron/electron/v${electronVersion}/script/sysroots.json`, 'text'));
const { Sha1Sum: sha, Tarball: fileName } = electronSysroots[`sid_${linuxArch}`] || electronSysroots[`bullseye_${linuxArch}`];
const sysrootURL = `${SYSROOT_BASE_URL}/${sha}/${fileName}`;
const sysrootBuffer = await fetch(sysrootURL, 'buffer');
const actualSha = crypto.createHash('SHA1').update(sysrootBuffer).digest('hex');
d('expected sha:', sha);
d('actual sha:', actualSha);
if (sha !== actualSha)
throw new Error(`Attempted to download the linux sysroot for ${electronVersion} but the SHA checksum did not match`);
d('writing sysroot to disk');
const tmpTarFile = path.resolve(ELECTRON_GYP_DIR, `${electronVersion}-${fileName}`);
if (fs.existsSync(tmpTarFile))
await fs.promises.rm(tmpTarFile, { recursive: true, force: true });
await promisifiedGracefulFs.writeFile(tmpTarFile, sysrootBuffer);
d('decompressing sysroot');
await spawn('tar', ['-xf', tmpTarFile, '-C', sysrootDir], { stdio: 'ignore' });
return sysrootDir;
}
//# sourceMappingURL=sysroot-fetcher.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"sysroot-fetcher.js","sourceRoot":"","sources":["../src/sysroot-fetcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAC;AACpD,O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AC,YAAY,SAAS,EAAE,CAAC,CAAC;IAC9H,MAAM,UAAU,GAAG,GAAG,gBAAgB,IAAI,GAAG,IAAI,QAAQ,EAAE,CAAC;IAC5D,MAAM,aAAa,GAAG,MAAM,KAAK,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;IAExD,MAAM,SAAS,GAAG,MAAM,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAChF,CAAC,CAAC,eAAe,EAAE,GAAG,CAAC,CAAC;IACxB,CAAC,CAAC,aAAa,EAAE,SAAS,CAAC,CAAC;IAC5B,IAAI,GAAG,KAAK,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,+CAA+C,eAAe,qCAAqC,CAAC,CAAC;IAE5I,CAAC,CAAC,yBAAyB,CAAC,CAAC;IAC7B,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,GAAG,eAAe,IAAI,QAAQ,EAAE,CAAC,CAAC;IACpF,IAAI,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC;QAAE,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,UAAU,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;IAClG,MAAM,qBAAqB,CAAC,SAAS,CAAC,UAAU,EAAE,aAAa,CAAC,CAAC;IAEjE,CAAC,CAAC,uBAAuB,CAAC,CAAC;IAC3B,MAAM,KAAK,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,CAAC,EAAE,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC,CAAC;IAE/E,OAAO,UAAU,CAAC;AACpB,CAAC"}
+28
View File
@@ -0,0 +1,28 @@
/// <reference types="node" resolution-mode="require"/>
/// <reference types="node" resolution-mode="require"/>
import { EventEmitter } from 'node:events';
export declare enum BuildType {
Debug = "Debug",
Release = "Release"
}
export type RebuildMode = 'sequential' | 'parallel';
export interface IRebuilder {
ABI: string;
arch: string;
buildPath: string;
buildType: BuildType;
cachePath: string;
debug: boolean;
disablePreGypCopy: boolean;
electronVersion: string;
force: boolean;
headerURL: string;
lifecycle: EventEmitter;
mode: RebuildMode;
msvsVersion?: string;
platform: NodeJS.Platform;
prebuildTagPrefix: string;
buildFromSource: boolean;
useCache: boolean;
useElectronClang: boolean;
}
+6
View File
@@ -0,0 +1,6 @@
export var BuildType;
(function (BuildType) {
BuildType["Debug"] = "Debug";
BuildType["Release"] = "Release";
})(BuildType || (BuildType = {}));
//# sourceMappingURL=types.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAEA,MAAM,CAAN,IAAY,SAGX;AAHD,WAAY,SAAS;IACnB,4BAAe,CAAA;IACf,gCAAmB,CAAA;AACrB,CAAC,EAHW,SAAS,KAAT,SAAS,QAGpB"}
+16
View File
@@ -0,0 +1,16 @@
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case `uname` in
*CYGWIN*|*MINGW*|*MSYS*)
if command -v cygpath > /dev/null 2>&1; then
basedir=`cygpath -w "$basedir"`
fi
;;
esac
if [ -x "$basedir/node" ]; then
exec "$basedir/node" "$basedir/../semver/bin/semver.js" "$@"
else
exec node "$basedir/../semver/bin/semver.js" "$@"
fi
+17
View File
@@ -0,0 +1,17 @@
@ECHO off
GOTO start
:find_dp0
SET dp0=%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\semver\bin\semver.js" %*
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
# Fix case when both the Windows and Linux builds of Node
# are installed in the same directory
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "$basedir/node$exe" "$basedir/../semver/bin/semver.js" $args
} else {
& "$basedir/node$exe" "$basedir/../semver/bin/semver.js" $args
}
$ret=$LASTEXITCODE
} else {
# Support pipeline input
if ($MyInvocation.ExpectingInput) {
$input | & "node$exe" "$basedir/../semver/bin/semver.js" $args
} else {
& "node$exe" "$basedir/../semver/bin/semver.js" $args
}
$ret=$LASTEXITCODE
}
exit $ret
+15
View File
@@ -0,0 +1,15 @@
The ISC License
Copyright (c) Isaac Z. Schlueter and Contributors
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+665
View File
@@ -0,0 +1,665 @@
semver(1) -- The semantic versioner for npm
===========================================
## Install
```bash
npm install semver
````
## Usage
As a node module:
```js
const semver = require('semver')
semver.valid('1.2.3') // '1.2.3'
semver.valid('a.b.c') // null
semver.clean(' =v1.2.3 ') // '1.2.3'
semver.satisfies('1.2.3', '1.x || >=2.5.0 || 5.0.0 - 7.2.3') // true
semver.gt('1.2.3', '9.8.7') // false
semver.lt('1.2.3', '9.8.7') // true
semver.minVersion('>=1.0.0') // '1.0.0'
semver.valid(semver.coerce('v2')) // '2.0.0'
semver.valid(semver.coerce('42.6.7.9.3-alpha')) // '42.6.7'
```
You can also just load the module for the function that you care about if
you'd like to minimize your footprint.
```js
// load the whole API at once in a single object
const semver = require('semver')
// or just load the bits you need
// all of them listed here, just pick and choose what you want
// classes
const SemVer = require('semver/classes/semver')
const Comparator = require('semver/classes/comparator')
const Range = require('semver/classes/range')
// functions for working with versions
const semverParse = require('semver/functions/parse')
const semverValid = require('semver/functions/valid')
const semverClean = require('semver/functions/clean')
const semverInc = require('semver/functions/inc')
const semverDiff = require('semver/functions/diff')
const semverMajor = require('semver/functions/major')
const semverMinor = require('semver/functions/minor')
const semverPatch = require('semver/functions/patch')
const semverPrerelease = require('semver/functions/prerelease')
const semverCompare = require('semver/functions/compare')
const semverRcompare = require('semver/functions/rcompare')
const semverCompareLoose = require('semver/functions/compare-loose')
const semverCompareBuild = require('semver/functions/compare-build')
const semverSort = require('semver/functions/sort')
const semverRsort = require('semver/functions/rsort')
// low-level comparators between versions
const semverGt = require('semver/functions/gt')
const semverLt = require('semver/functions/lt')
const semverEq = require('semver/functions/eq')
const semverNeq = require('semver/functions/neq')
const semverGte = require('semver/functions/gte')
const semverLte = require('semver/functions/lte')
const semverCmp = require('semver/functions/cmp')
const semverCoerce = require('semver/functions/coerce')
// working with ranges
const semverSatisfies = require('semver/functions/satisfies')
const semverMaxSatisfying = require('semver/ranges/max-satisfying')
const semverMinSatisfying = require('semver/ranges/min-satisfying')
const semverToComparators = require('semver/ranges/to-comparators')
const semverMinVersion = require('semver/ranges/min-version')
const semverValidRange = require('semver/ranges/valid')
const semverOutside = require('semver/ranges/outside')
const semverGtr = require('semver/ranges/gtr')
const semverLtr = require('semver/ranges/ltr')
const semverIntersects = require('semver/ranges/intersects')
const semverSimplifyRange = require('semver/ranges/simplify')
const semverRangeSubset = require('semver/ranges/subset')
```
As a command-line utility:
```
$ semver -h
A JavaScript implementation of the https://semver.org/ specification
Copyright Isaac Z. Schlueter
Usage: semver [options] <version> [<version> [...]]
Prints valid versions sorted by SemVer precedence
Options:
-r --range <range>
Print versions that match the specified range.
-i --increment [<level>]
Increment a version by the specified level. Level can
be one of: major, minor, patch, premajor, preminor,
prepatch, prerelease, or release. Default level is 'patch'.
Only one version may be specified.
--preid <identifier>
Identifier to be used to prefix premajor, preminor,
prepatch or prerelease version increments.
-l --loose
Interpret versions and ranges loosely
-n <0|1|false>
Base number for prerelease identifier (default: 0).
Use false to omit the number altogether.
-p --include-prerelease
Always include prerelease versions in range matching
-c --coerce
Coerce a string into SemVer if possible
(does not imply --loose)
--rtl
Coerce version strings right to left
--ltr
Coerce version strings left to right (default)
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
If no satisfying versions are found, then exits failure.
Versions are printed in ascending order, so supplying
multiple versions to the utility will just sort them.
```
## Versions
A "version" is described by the `v2.0.0` specification found at
<https://semver.org/>.
A leading `"="` or `"v"` character is stripped off and ignored.
Support for stripping a leading "v" is kept for compatibility with `v1.0.0` of the SemVer
specification but should not be used anymore.
## Ranges
A `version range` is a set of `comparators` that specify versions
that satisfy the range.
A `comparator` is composed of an `operator` and a `version`. The set
of primitive `operators` is:
* `<` Less than
* `<=` Less than or equal to
* `>` Greater than
* `>=` Greater than or equal to
* `=` Equal. If no operator is specified, then equality is assumed,
so this operator is optional but MAY be included.
For example, the comparator `>=1.2.7` would match the versions
`1.2.7`, `1.2.8`, `2.5.3`, and `1.3.9`, but not the versions `1.2.6`
or `1.1.0`. The comparator `>1` is equivalent to `>=2.0.0` and
would match the versions `2.0.0` and `3.1.0`, but not the versions
`1.0.1` or `1.1.0`.
Comparators can be joined by whitespace to form a `comparator set`,
which is satisfied by the **intersection** of all of the comparators
it includes.
A range is composed of one or more comparator sets, joined by `||`. A
version matches a range if and only if every comparator in at least
one of the `||`-separated comparator sets is satisfied by the version.
For example, the range `>=1.2.7 <1.3.0` would match the versions
`1.2.7`, `1.2.8`, and `1.2.99`, but not the versions `1.2.6`, `1.3.0`,
or `1.1.0`.
The range `1.2.7 || >=1.2.9 <2.0.0` would match the versions `1.2.7`,
`1.2.9`, and `1.4.6`, but not the versions `1.2.8` or `2.0.0`.
### Prerelease Tags
If a version has a prerelease tag (for example, `1.2.3-alpha.3`) then
it will only be allowed to satisfy comparator sets if at least one
comparator with the same `[major, minor, patch]` tuple also has a
prerelease tag.
For example, the range `>1.2.3-alpha.3` would be allowed to match the
version `1.2.3-alpha.7`, but it would *not* be satisfied by
`3.4.5-alpha.9`, even though `3.4.5-alpha.9` is technically "greater
than" `1.2.3-alpha.3` according to the SemVer sort rules. The version
range only accepts prerelease tags on the `1.2.3` version.
Version `3.4.5` *would* satisfy the range because it does not have a
prerelease flag, and `3.4.5` is greater than `1.2.3-alpha.7`.
The purpose of this behavior is twofold. First, prerelease versions
frequently are updated very quickly, and contain many breaking changes
that are (by the author's design) not yet fit for public consumption.
Therefore, by default, they are excluded from range-matching
semantics.
Second, a user who has opted into using a prerelease version has
indicated the intent to use *that specific* set of
alpha/beta/rc versions. By including a prerelease tag in the range,
the user is indicating that they are aware of the risk. However, it
is still not appropriate to assume that they have opted into taking a
similar risk on the *next* set of prerelease versions.
Note that this behavior can be suppressed (treating all prerelease
versions as if they were normal versions, for range-matching)
by setting the `includePrerelease` flag on the options
object to any
[functions](https://github.com/npm/node-semver#functions) that do
range matching.
#### Prerelease Identifiers
The method `.inc` takes an additional `identifier` string argument that
will append the value of the string as a prerelease identifier:
```javascript
semver.inc('1.2.3', 'prerelease', 'beta')
// '1.2.4-beta.0'
```
command-line example:
```bash
$ semver 1.2.3 -i prerelease --preid beta
1.2.4-beta.0
```
Which then can be used to increment further:
```bash
$ semver 1.2.4-beta.0 -i prerelease
1.2.4-beta.1
```
To get out of the prerelease phase, use the `release` option:
```bash
$ semver 1.2.4-beta.1 -i release
1.2.4
```
#### Prerelease Identifier Base
The method `.inc` takes an optional parameter 'identifierBase' string
that will let you let your prerelease number as zero-based or one-based.
Set to `false` to omit the prerelease number altogether.
If you do not specify this parameter, it will default to zero-based.
```javascript
semver.inc('1.2.3', 'prerelease', 'beta', '1')
// '1.2.4-beta.1'
```
```javascript
semver.inc('1.2.3', 'prerelease', 'beta', false)
// '1.2.4-beta'
```
command-line example:
```bash
$ semver 1.2.3 -i prerelease --preid beta -n 1
1.2.4-beta.1
```
```bash
$ semver 1.2.3 -i prerelease --preid beta -n false
1.2.4-beta
```
### Advanced Range Syntax
Advanced range syntax desugars to primitive comparators in
deterministic ways.
Advanced ranges may be combined in the same way as primitive
comparators using white space or `||`.
#### Hyphen Ranges `X.Y.Z - A.B.C`
Specifies an inclusive set.
* `1.2.3 - 2.3.4` := `>=1.2.3 <=2.3.4`
If a partial version is provided as the first version in the inclusive
range, then the missing pieces are replaced with zeroes.
* `1.2 - 2.3.4` := `>=1.2.0 <=2.3.4`
If a partial version is provided as the second version in the
inclusive range, then all versions that start with the supplied parts
of the tuple are accepted, but nothing that would be greater than the
provided tuple parts.
* `1.2.3 - 2.3` := `>=1.2.3 <2.4.0-0`
* `1.2.3 - 2` := `>=1.2.3 <3.0.0-0`
#### X-Ranges `1.2.x` `1.X` `1.2.*` `*`
Any of `X`, `x`, or `*` may be used to "stand in" for one of the
numeric values in the `[major, minor, patch]` tuple.
* `*` := `>=0.0.0` (Any non-prerelease version satisfies, unless
`includePrerelease` is specified, in which case any version at all
satisfies)
* `1.x` := `>=1.0.0 <2.0.0-0` (Matching major version)
* `1.2.x` := `>=1.2.0 <1.3.0-0` (Matching major and minor versions)
A partial version range is treated as an X-Range, so the special
character is in fact optional.
* `""` (empty string) := `*` := `>=0.0.0`
* `1` := `1.x.x` := `>=1.0.0 <2.0.0-0`
* `1.2` := `1.2.x` := `>=1.2.0 <1.3.0-0`
#### Tilde Ranges `~1.2.3` `~1.2` `~1`
Allows patch-level changes if a minor version is specified on the
comparator. Allows minor-level changes if not.
* `~1.2.3` := `>=1.2.3 <1.(2+1).0` := `>=1.2.3 <1.3.0-0`
* `~1.2` := `>=1.2.0 <1.(2+1).0` := `>=1.2.0 <1.3.0-0` (Same as `1.2.x`)
* `~1` := `>=1.0.0 <(1+1).0.0` := `>=1.0.0 <2.0.0-0` (Same as `1.x`)
* `~0.2.3` := `>=0.2.3 <0.(2+1).0` := `>=0.2.3 <0.3.0-0`
* `~0.2` := `>=0.2.0 <0.(2+1).0` := `>=0.2.0 <0.3.0-0` (Same as `0.2.x`)
* `~0` := `>=0.0.0 <(0+1).0.0` := `>=0.0.0 <1.0.0-0` (Same as `0.x`)
* `~1.2.3-beta.2` := `>=1.2.3-beta.2 <1.3.0-0` Note that prereleases in
the `1.2.3` version will be allowed, if they are greater than or
equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
`1.2.4-beta.2` would not, because it is a prerelease of a
different `[major, minor, patch]` tuple.
#### Caret Ranges `^1.2.3` `^0.2.5` `^0.0.4`
Allows changes that do not modify the left-most non-zero element in the
`[major, minor, patch]` tuple. In other words, this allows patch and
minor updates for versions `1.0.0` and above, patch updates for
versions `0.X >=0.1.0`, and *no* updates for versions `0.0.X`.
Many authors treat a `0.x` version as if the `x` were the major
"breaking-change" indicator.
Caret ranges are ideal when an author may make breaking changes
between `0.2.4` and `0.3.0` releases, which is a common practice.
However, it presumes that there will *not* be breaking changes between
`0.2.4` and `0.2.5`. It allows for changes that are presumed to be
additive (but non-breaking), according to commonly observed practices.
* `^1.2.3` := `>=1.2.3 <2.0.0-0`
* `^0.2.3` := `>=0.2.3 <0.3.0-0`
* `^0.0.3` := `>=0.0.3 <0.0.4-0`
* `^1.2.3-beta.2` := `>=1.2.3-beta.2 <2.0.0-0` Note that prereleases in
the `1.2.3` version will be allowed, if they are greater than or
equal to `beta.2`. So, `1.2.3-beta.4` would be allowed, but
`1.2.4-beta.2` would not, because it is a prerelease of a
different `[major, minor, patch]` tuple.
* `^0.0.3-beta` := `>=0.0.3-beta <0.0.4-0` Note that prereleases in the
`0.0.3` version *only* will be allowed, if they are greater than or
equal to `beta`. So, `0.0.3-pr.2` would be allowed.
When parsing caret ranges, a missing `patch` value desugars to the
number `0`, but will allow flexibility within that value, even if the
major and minor versions are both `0`.
* `^1.2.x` := `>=1.2.0 <2.0.0-0`
* `^0.0.x` := `>=0.0.0 <0.1.0-0`
* `^0.0` := `>=0.0.0 <0.1.0-0`
A missing `minor` and `patch` values will desugar to zero, but also
allow flexibility within those values, even if the major version is
zero.
* `^1.x` := `>=1.0.0 <2.0.0-0`
* `^0.x` := `>=0.0.0 <1.0.0-0`
### Range Grammar
Putting all this together, here is a Backus-Naur grammar for ranges,
for the benefit of parser authors:
```bnf
range-set ::= range ( logical-or range ) *
logical-or ::= ( ' ' ) * '||' ( ' ' ) *
range ::= hyphen | simple ( ' ' simple ) * | ''
hyphen ::= partial ' - ' partial
simple ::= primitive | partial | tilde | caret
primitive ::= ( '<' | '>' | '>=' | '<=' | '=' ) partial
partial ::= xr ( '.' xr ( '.' xr qualifier ? )? )?
xr ::= 'x' | 'X' | '*' | nr
nr ::= '0' | ['1'-'9'] ( ['0'-'9'] ) *
tilde ::= '~' partial
caret ::= '^' partial
qualifier ::= ( '-' pre )? ( '+' build )?
pre ::= parts
build ::= parts
parts ::= part ( '.' part ) *
part ::= nr | [-0-9A-Za-z]+
```
## Functions
All methods and classes take a final `options` object argument. All
options in this object are `false` by default. The options supported
are:
- `loose`: Be more forgiving about not-quite-valid semver strings.
(Any resulting output will always be 100% strict compliant, of
course.) For backwards compatibility reasons, if the `options`
argument is a boolean value instead of an object, it is interpreted
to be the `loose` param.
- `includePrerelease`: Set to suppress the [default
behavior](https://github.com/npm/node-semver#prerelease-tags) of
excluding prerelease tagged versions from ranges unless they are
explicitly opted into.
Strict-mode Comparators and Ranges will be strict about the SemVer
strings that they parse.
* `valid(v)`: Return the parsed version, or null if it's not valid.
* `inc(v, releaseType, options, identifier, identifierBase)`:
Return the version incremented by the release
type (`major`, `premajor`, `minor`, `preminor`, `patch`,
`prepatch`, `prerelease`, or `release`), or null if it's not valid
* `premajor` in one call will bump the version up to the next major
version and down to a prerelease of that major version.
`preminor`, and `prepatch` work the same way.
* If called from a non-prerelease version, `prerelease` will work the
same as `prepatch`. It increments the patch version and then makes a
prerelease. If the input version is already a prerelease it simply
increments it.
* `release` will remove any prerelease part of the version.
* `identifier` can be used to prefix `premajor`, `preminor`,
`prepatch`, or `prerelease` version increments. `identifierBase`
is the base to be used for the `prerelease` identifier.
* `prerelease(v)`: Returns an array of prerelease components, or null
if none exist. Example: `prerelease('1.2.3-alpha.1') -> ['alpha', 1]`
* `major(v)`: Return the major version number.
* `minor(v)`: Return the minor version number.
* `patch(v)`: Return the patch version number.
* `intersects(r1, r2, loose)`: Return true if the two supplied ranges
or comparators intersect.
* `parse(v)`: Attempt to parse a string as a semantic version, returning either
a `SemVer` object or `null`.
### Comparison
* `gt(v1, v2)`: `v1 > v2`
* `gte(v1, v2)`: `v1 >= v2`
* `lt(v1, v2)`: `v1 < v2`
* `lte(v1, v2)`: `v1 <= v2`
* `eq(v1, v2)`: `v1 == v2` This is true if they're logically equivalent,
even if they're not the same string. You already know how to
compare strings.
* `neq(v1, v2)`: `v1 != v2` The opposite of `eq`.
* `cmp(v1, comparator, v2)`: Pass in a comparison string, and it'll call
the corresponding function above. `"==="` and `"!=="` do simple
string comparison, but are included for completeness. Throws if an
invalid comparison string is provided.
* `compare(v1, v2)`: Return `0` if `v1 == v2`, or `1` if `v1` is greater, or `-1` if
`v2` is greater. Sorts in ascending order if passed to `Array.sort()`.
* `rcompare(v1, v2)`: The reverse of `compare`. Sorts an array of versions
in descending order when passed to `Array.sort()`.
* `compareBuild(v1, v2)`: The same as `compare` but considers `build` when two versions
are equal. Sorts in ascending order if passed to `Array.sort()`.
* `compareLoose(v1, v2)`: Short for `compare(v1, v2, { loose: true })`.
* `diff(v1, v2)`: Returns the difference between two versions by the release type
(`major`, `premajor`, `minor`, `preminor`, `patch`, `prepatch`, or `prerelease`),
or null if the versions are the same.
### Sorting
* `sort(versions)`: Returns a sorted array of versions based on the `compareBuild`
function.
* `rsort(versions)`: The reverse of `sort`. Returns an array of versions based on
the `compareBuild` function in descending order.
### Comparators
* `intersects(comparator)`: Return true if the comparators intersect
### Ranges
* `validRange(range)`: Return the valid range or null if it's not valid.
* `satisfies(version, range)`: Return true if the version satisfies the
range.
* `maxSatisfying(versions, range)`: Return the highest version in the list
that satisfies the range, or `null` if none of them do.
* `minSatisfying(versions, range)`: Return the lowest version in the list
that satisfies the range, or `null` if none of them do.
* `minVersion(range)`: Return the lowest version that can match
the given range.
* `gtr(version, range)`: Return `true` if the version is greater than all the
versions possible in the range.
* `ltr(version, range)`: Return `true` if the version is less than all the
versions possible in the range.
* `outside(version, range, hilo)`: Return true if the version is outside
the bounds of the range in either the high or low direction. The
`hilo` argument must be either the string `'>'` or `'<'`. (This is
the function called by `gtr` and `ltr`.)
* `intersects(range)`: Return true if any of the range comparators intersect.
* `simplifyRange(versions, range)`: Return a "simplified" range that
matches the same items in the `versions` list as the range specified. Note
that it does *not* guarantee that it would match the same versions in all
cases, only for the set of versions provided. This is useful when
generating ranges by joining together multiple versions with `||`
programmatically, to provide the user with something a bit more
ergonomic. If the provided range is shorter in string-length than the
generated range, then that is returned.
* `subset(subRange, superRange)`: Return `true` if the `subRange` range is
entirely contained by the `superRange` range.
Note that, since ranges may be non-contiguous, a version might not be
greater than a range, less than a range, *or* satisfy a range! For
example, the range `1.2 <1.2.9 || >2.0.0` would have a hole from `1.2.9`
until `2.0.0`, so version `1.2.10` would not be greater than the
range (because `2.0.1` satisfies, which is higher), nor less than the
range (since `1.2.8` satisfies, which is lower), and it also does not
satisfy the range.
If you want to know if a version satisfies or does not satisfy a
range, use the `satisfies(version, range)` function.
### Coercion
* `coerce(version, options)`: Coerces a string to semver if possible
This aims to provide a very forgiving translation of a non-semver string to
semver. It looks for the first digit in a string and consumes all
remaining characters which satisfy at least a partial semver (e.g., `1`,
`1.2`, `1.2.3`) up to the max permitted length (256 characters). Longer
versions are simply truncated (`4.6.3.9.2-alpha2` becomes `4.6.3`). All
surrounding text is simply ignored (`v3.4 replaces v3.3.1` becomes
`3.4.0`). Only text which lacks digits will fail coercion (`version one`
is not valid). The maximum length for any semver component considered for
coercion is 16 characters; longer components will be ignored
(`10000000000000000.4.7.4` becomes `4.7.4`). The maximum value for any
semver component is `Number.MAX_SAFE_INTEGER || (2**53 - 1)`; higher value
components are invalid (`9999999999999999.4.7.4` is likely invalid).
If the `options.rtl` flag is set, then `coerce` will return the right-most
coercible tuple that does not share an ending index with a longer coercible
tuple. For example, `1.2.3.4` will return `2.3.4` in rtl mode, not
`4.0.0`. `1.2.3/4` will return `4.0.0`, because the `4` is not a part of
any other overlapping SemVer tuple.
If the `options.includePrerelease` flag is set, then the `coerce` result will contain
prerelease and build parts of a version. For example, `1.2.3.4-rc.1+rev.2`
will preserve prerelease `rc.1` and build `rev.2` in the result.
### Clean
* `clean(version)`: Clean a string to be a valid semver if possible
This will return a cleaned and trimmed semver version. If the provided
version is not valid a null will be returned. This does not work for
ranges.
ex.
* `s.clean(' = v 2.1.5foo')`: `null`
* `s.clean(' = v 2.1.5foo', { loose: true })`: `'2.1.5-foo'`
* `s.clean(' = v 2.1.5-foo')`: `null`
* `s.clean(' = v 2.1.5-foo', { loose: true })`: `'2.1.5-foo'`
* `s.clean('=v2.1.5')`: `'2.1.5'`
* `s.clean(' =v2.1.5')`: `'2.1.5'`
* `s.clean(' 2.1.5 ')`: `'2.1.5'`
* `s.clean('~1.0.0')`: `null`
## Constants
As a convenience, helper constants are exported to provide information about what `node-semver` supports:
### `RELEASE_TYPES`
- major
- premajor
- minor
- preminor
- patch
- prepatch
- prerelease
```
const semver = require('semver');
if (semver.RELEASE_TYPES.includes(arbitraryUserInput)) {
console.log('This is a valid release type!');
} else {
console.warn('This is NOT a valid release type!');
}
```
### `SEMVER_SPEC_VERSION`
2.0.0
```
const semver = require('semver');
console.log('We are currently using the semver specification version:', semver.SEMVER_SPEC_VERSION);
```
## Exported Modules
<!--
TODO: Make sure that all of these items are documented (classes aren't,
eg), and then pull the module name into the documentation for that specific
thing.
-->
You may pull in just the part of this semver utility that you need if you
are sensitive to packing and tree-shaking concerns. The main
`require('semver')` export uses getter functions to lazily load the parts
of the API that are used.
The following modules are available:
* `require('semver')`
* `require('semver/classes')`
* `require('semver/classes/comparator')`
* `require('semver/classes/range')`
* `require('semver/classes/semver')`
* `require('semver/functions/clean')`
* `require('semver/functions/cmp')`
* `require('semver/functions/coerce')`
* `require('semver/functions/compare')`
* `require('semver/functions/compare-build')`
* `require('semver/functions/compare-loose')`
* `require('semver/functions/diff')`
* `require('semver/functions/eq')`
* `require('semver/functions/gt')`
* `require('semver/functions/gte')`
* `require('semver/functions/inc')`
* `require('semver/functions/lt')`
* `require('semver/functions/lte')`
* `require('semver/functions/major')`
* `require('semver/functions/minor')`
* `require('semver/functions/neq')`
* `require('semver/functions/parse')`
* `require('semver/functions/patch')`
* `require('semver/functions/prerelease')`
* `require('semver/functions/rcompare')`
* `require('semver/functions/rsort')`
* `require('semver/functions/satisfies')`
* `require('semver/functions/sort')`
* `require('semver/functions/valid')`
* `require('semver/ranges/gtr')`
* `require('semver/ranges/intersects')`
* `require('semver/ranges/ltr')`
* `require('semver/ranges/max-satisfying')`
* `require('semver/ranges/min-satisfying')`
* `require('semver/ranges/min-version')`
* `require('semver/ranges/outside')`
* `require('semver/ranges/simplify')`
* `require('semver/ranges/subset')`
* `require('semver/ranges/to-comparators')`
* `require('semver/ranges/valid')`
+191
View File
@@ -0,0 +1,191 @@
#!/usr/bin/env node
// Standalone semver comparison program.
// Exits successfully and prints matching version(s) if
// any supplied version is valid and passes all tests.
'use strict'
const argv = process.argv.slice(2)
let versions = []
const range = []
let inc = null
const version = require('../package.json').version
let loose = false
let includePrerelease = false
let coerce = false
let rtl = false
let identifier
let identifierBase
const semver = require('../')
const parseOptions = require('../internal/parse-options')
let reverse = false
let options = {}
const main = () => {
if (!argv.length) {
return help()
}
while (argv.length) {
let a = argv.shift()
const indexOfEqualSign = a.indexOf('=')
if (indexOfEqualSign !== -1) {
const value = a.slice(indexOfEqualSign + 1)
a = a.slice(0, indexOfEqualSign)
argv.unshift(value)
}
switch (a) {
case '-rv': case '-rev': case '--rev': case '--reverse':
reverse = true
break
case '-l': case '--loose':
loose = true
break
case '-p': case '--include-prerelease':
includePrerelease = true
break
case '-v': case '--version':
versions.push(argv.shift())
break
case '-i': case '--inc': case '--increment':
switch (argv[0]) {
case 'major': case 'minor': case 'patch': case 'prerelease':
case 'premajor': case 'preminor': case 'prepatch':
case 'release':
inc = argv.shift()
break
default:
inc = 'patch'
break
}
break
case '--preid':
identifier = argv.shift()
break
case '-r': case '--range':
range.push(argv.shift())
break
case '-n':
identifierBase = argv.shift()
if (identifierBase === 'false') {
identifierBase = false
}
break
case '-c': case '--coerce':
coerce = true
break
case '--rtl':
rtl = true
break
case '--ltr':
rtl = false
break
case '-h': case '--help': case '-?':
return help()
default:
versions.push(a)
break
}
}
options = parseOptions({ loose, includePrerelease, rtl })
versions = versions.map((v) => {
return coerce ? (semver.coerce(v, options) || { version: v }).version : v
}).filter((v) => {
return semver.valid(v, options)
})
if (!versions.length) {
return fail()
}
if (inc && (versions.length !== 1 || range.length)) {
return failInc()
}
for (let i = 0, l = range.length; i < l; i++) {
versions = versions.filter((v) => {
return semver.satisfies(v, range[i], options)
})
if (!versions.length) {
return fail()
}
}
versions
.sort((a, b) => semver[reverse ? 'rcompare' : 'compare'](a, b, options))
.map(v => semver.clean(v, options))
.map(v => inc ? semver.inc(v, inc, options, identifier, identifierBase) : v)
.forEach(v => console.log(v))
}
const failInc = () => {
console.error('--inc can only be used on a single version with no range')
fail()
}
const fail = () => process.exit(1)
const help = () => console.log(
`SemVer ${version}
A JavaScript implementation of the https://semver.org/ specification
Copyright Isaac Z. Schlueter
Usage: semver [options] <version> [<version> [...]]
Prints valid versions sorted by SemVer precedence
Options:
-r --range <range>
Print versions that match the specified range.
-i --increment [<level>]
Increment a version by the specified level. Level can
be one of: major, minor, patch, premajor, preminor,
prepatch, prerelease, or release. Default level is 'patch'.
Only one version may be specified.
--preid <identifier>
Identifier to be used to prefix premajor, preminor,
prepatch or prerelease version increments.
-l --loose
Interpret versions and ranges loosely
-p --include-prerelease
Always include prerelease versions in range matching
-c --coerce
Coerce a string into SemVer if possible
(does not imply --loose)
--rtl
Coerce version strings right to left
--ltr
Coerce version strings left to right (default)
-n <base>
Base number to be used for the prerelease identifier.
Can be either 0 or 1, or false to omit the number altogether.
Defaults to 0.
Program exits successfully if any valid version satisfies
all supplied ranges, and prints all satisfying versions.
If no satisfying versions are found, then exits failure.
Versions are printed in ascending order, so supplying
multiple versions to the utility will just sort them.`)
main()
@@ -0,0 +1,143 @@
'use strict'
const ANY = Symbol('SemVer ANY')
// hoisted class for cyclic dependency
class Comparator {
static get ANY () {
return ANY
}
constructor (comp, options) {
options = parseOptions(options)
if (comp instanceof Comparator) {
if (comp.loose === !!options.loose) {
return comp
} else {
comp = comp.value
}
}
comp = comp.trim().split(/\s+/).join(' ')
debug('comparator', comp, options)
this.options = options
this.loose = !!options.loose
this.parse(comp)
if (this.semver === ANY) {
this.value = ''
} else {
this.value = this.operator + this.semver.version
}
debug('comp', this)
}
parse (comp) {
const r = this.options.loose ? re[t.COMPARATORLOOSE] : re[t.COMPARATOR]
const m = comp.match(r)
if (!m) {
throw new TypeError(`Invalid comparator: ${comp}`)
}
this.operator = m[1] !== undefined ? m[1] : ''
if (this.operator === '=') {
this.operator = ''
}
// if it literally is just '>' or '' then allow anything.
if (!m[2]) {
this.semver = ANY
} else {
this.semver = new SemVer(m[2], this.options.loose)
}
}
toString () {
return this.value
}
test (version) {
debug('Comparator.test', version, this.options.loose)
if (this.semver === ANY || version === ANY) {
return true
}
if (typeof version === 'string') {
try {
version = new SemVer(version, this.options)
} catch (er) {
return false
}
}
return cmp(version, this.operator, this.semver, this.options)
}
intersects (comp, options) {
if (!(comp instanceof Comparator)) {
throw new TypeError('a Comparator is required')
}
if (this.operator === '') {
if (this.value === '') {
return true
}
return new Range(comp.value, options).test(this.value)
} else if (comp.operator === '') {
if (comp.value === '') {
return true
}
return new Range(this.value, options).test(comp.semver)
}
options = parseOptions(options)
// Special cases where nothing can possibly be lower
if (options.includePrerelease &&
(this.value === '<0.0.0-0' || comp.value === '<0.0.0-0')) {
return false
}
if (!options.includePrerelease &&
(this.value.startsWith('<0.0.0') || comp.value.startsWith('<0.0.0'))) {
return false
}
// Same direction increasing (> or >=)
if (this.operator.startsWith('>') && comp.operator.startsWith('>')) {
return true
}
// Same direction decreasing (< or <=)
if (this.operator.startsWith('<') && comp.operator.startsWith('<')) {
return true
}
// same SemVer and both sides are inclusive (<= or >=)
if (
(this.semver.version === comp.semver.version) &&
this.operator.includes('=') && comp.operator.includes('=')) {
return true
}
// opposite directions less than
if (cmp(this.semver, '<', comp.semver, options) &&
this.operator.startsWith('>') && comp.operator.startsWith('<')) {
return true
}
// opposite directions greater than
if (cmp(this.semver, '>', comp.semver, options) &&
this.operator.startsWith('<') && comp.operator.startsWith('>')) {
return true
}
return false
}
}
module.exports = Comparator
const parseOptions = require('../internal/parse-options')
const { safeRe: re, t } = require('../internal/re')
const cmp = require('../functions/cmp')
const debug = require('../internal/debug')
const SemVer = require('./semver')
const Range = require('./range')
+7
View File
@@ -0,0 +1,7 @@
'use strict'
module.exports = {
SemVer: require('./semver.js'),
Range: require('./range.js'),
Comparator: require('./comparator.js'),
}
+557
View File
@@ -0,0 +1,557 @@
'use strict'
const SPACE_CHARACTERS = /\s+/g
// hoisted class for cyclic dependency
class Range {
constructor (range, options) {
options = parseOptions(options)
if (range instanceof Range) {
if (
range.loose === !!options.loose &&
range.includePrerelease === !!options.includePrerelease
) {
return range
} else {
return new Range(range.raw, options)
}
}
if (range instanceof Comparator) {
// just put it in the set and return
this.raw = range.value
this.set = [[range]]
this.formatted = undefined
return this
}
this.options = options
this.loose = !!options.loose
this.includePrerelease = !!options.includePrerelease
// First reduce all whitespace as much as possible so we do not have to rely
// on potentially slow regexes like \s*. This is then stored and used for
// future error messages as well.
this.raw = range.trim().replace(SPACE_CHARACTERS, ' ')
// First, split on ||
this.set = this.raw
.split('||')
// map the range to a 2d array of comparators
.map(r => this.parseRange(r.trim()))
// throw out any comparator lists that are empty
// this generally means that it was not a valid range, which is allowed
// in loose mode, but will still throw if the WHOLE range is invalid.
.filter(c => c.length)
if (!this.set.length) {
throw new TypeError(`Invalid SemVer Range: ${this.raw}`)
}
// if we have any that are not the null set, throw out null sets.
if (this.set.length > 1) {
// keep the first one, in case they're all null sets
const first = this.set[0]
this.set = this.set.filter(c => !isNullSet(c[0]))
if (this.set.length === 0) {
this.set = [first]
} else if (this.set.length > 1) {
// if we have any that are *, then the range is just *
for (const c of this.set) {
if (c.length === 1 && isAny(c[0])) {
this.set = [c]
break
}
}
}
}
this.formatted = undefined
}
get range () {
if (this.formatted === undefined) {
this.formatted = ''
for (let i = 0; i < this.set.length; i++) {
if (i > 0) {
this.formatted += '||'
}
const comps = this.set[i]
for (let k = 0; k < comps.length; k++) {
if (k > 0) {
this.formatted += ' '
}
this.formatted += comps[k].toString().trim()
}
}
}
return this.formatted
}
format () {
return this.range
}
toString () {
return this.range
}
parseRange (range) {
// memoize range parsing for performance.
// this is a very hot path, and fully deterministic.
const memoOpts =
(this.options.includePrerelease && FLAG_INCLUDE_PRERELEASE) |
(this.options.loose && FLAG_LOOSE)
const memoKey = memoOpts + ':' + range
const cached = cache.get(memoKey)
if (cached) {
return cached
}
const loose = this.options.loose
// `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4`
const hr = loose ? re[t.HYPHENRANGELOOSE] : re[t.HYPHENRANGE]
range = range.replace(hr, hyphenReplace(this.options.includePrerelease))
debug('hyphen replace', range)
// `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5`
range = range.replace(re[t.COMPARATORTRIM], comparatorTrimReplace)
debug('comparator trim', range)
// `~ 1.2.3` => `~1.2.3`
range = range.replace(re[t.TILDETRIM], tildeTrimReplace)
debug('tilde trim', range)
// `^ 1.2.3` => `^1.2.3`
range = range.replace(re[t.CARETTRIM], caretTrimReplace)
debug('caret trim', range)
// At this point, the range is completely trimmed and
// ready to be split into comparators.
let rangeList = range
.split(' ')
.map(comp => parseComparator(comp, this.options))
.join(' ')
.split(/\s+/)
// >=0.0.0 is equivalent to *
.map(comp => replaceGTE0(comp, this.options))
if (loose) {
// in loose mode, throw out any that are not valid comparators
rangeList = rangeList.filter(comp => {
debug('loose invalid filter', comp, this.options)
return !!comp.match(re[t.COMPARATORLOOSE])
})
}
debug('range list', rangeList)
// if any comparators are the null set, then replace with JUST null set
// if more than one comparator, remove any * comparators
// also, don't include the same comparator more than once
const rangeMap = new Map()
const comparators = rangeList.map(comp => new Comparator(comp, this.options))
for (const comp of comparators) {
if (isNullSet(comp)) {
return [comp]
}
rangeMap.set(comp.value, comp)
}
if (rangeMap.size > 1 && rangeMap.has('')) {
rangeMap.delete('')
}
const result = [...rangeMap.values()]
cache.set(memoKey, result)
return result
}
intersects (range, options) {
if (!(range instanceof Range)) {
throw new TypeError('a Range is required')
}
return this.set.some((thisComparators) => {
return (
isSatisfiable(thisComparators, options) &&
range.set.some((rangeComparators) => {
return (
isSatisfiable(rangeComparators, options) &&
thisComparators.every((thisComparator) => {
return rangeComparators.every((rangeComparator) => {
return thisComparator.intersects(rangeComparator, options)
})
})
)
})
)
})
}
// if ANY of the sets match ALL of its comparators, then pass
test (version) {
if (!version) {
return false
}
if (typeof version === 'string') {
try {
version = new SemVer(version, this.options)
} catch (er) {
return false
}
}
for (let i = 0; i < this.set.length; i++) {
if (testSet(this.set[i], version, this.options)) {
return true
}
}
return false
}
}
module.exports = Range
const LRU = require('../internal/lrucache')
const cache = new LRU()
const parseOptions = require('../internal/parse-options')
const Comparator = require('./comparator')
const debug = require('../internal/debug')
const SemVer = require('./semver')
const {
safeRe: re,
t,
comparatorTrimReplace,
tildeTrimReplace,
caretTrimReplace,
} = require('../internal/re')
const { FLAG_INCLUDE_PRERELEASE, FLAG_LOOSE } = require('../internal/constants')
const isNullSet = c => c.value === '<0.0.0-0'
const isAny = c => c.value === ''
// take a set of comparators and determine whether there
// exists a version which can satisfy it
const isSatisfiable = (comparators, options) => {
let result = true
const remainingComparators = comparators.slice()
let testComparator = remainingComparators.pop()
while (result && remainingComparators.length) {
result = remainingComparators.every((otherComparator) => {
return testComparator.intersects(otherComparator, options)
})
testComparator = remainingComparators.pop()
}
return result
}
// comprised of xranges, tildes, stars, and gtlt's at this point.
// already replaced the hyphen ranges
// turn into a set of JUST comparators.
const parseComparator = (comp, options) => {
comp = comp.replace(re[t.BUILD], '')
debug('comp', comp, options)
comp = replaceCarets(comp, options)
debug('caret', comp)
comp = replaceTildes(comp, options)
debug('tildes', comp)
comp = replaceXRanges(comp, options)
debug('xrange', comp)
comp = replaceStars(comp, options)
debug('stars', comp)
return comp
}
const isX = id => !id || id.toLowerCase() === 'x' || id === '*'
// ~, ~> --> * (any, kinda silly)
// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0-0
// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0-0
// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0-0
// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0-0
// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0-0
// ~0.0.1 --> >=0.0.1 <0.1.0-0
const replaceTildes = (comp, options) => {
return comp
.trim()
.split(/\s+/)
.map((c) => replaceTilde(c, options))
.join(' ')
}
const replaceTilde = (comp, options) => {
const r = options.loose ? re[t.TILDELOOSE] : re[t.TILDE]
return comp.replace(r, (_, M, m, p, pr) => {
debug('tilde', comp, _, M, m, p, pr)
let ret
if (isX(M)) {
ret = ''
} else if (isX(m)) {
ret = `>=${M}.0.0 <${+M + 1}.0.0-0`
} else if (isX(p)) {
// ~1.2 == >=1.2.0 <1.3.0-0
ret = `>=${M}.${m}.0 <${M}.${+m + 1}.0-0`
} else if (pr) {
debug('replaceTilde pr', pr)
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${+m + 1}.0-0`
} else {
// ~1.2.3 == >=1.2.3 <1.3.0-0
ret = `>=${M}.${m}.${p
} <${M}.${+m + 1}.0-0`
}
debug('tilde return', ret)
return ret
})
}
// ^ --> * (any, kinda silly)
// ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0-0
// ^2.0, ^2.0.x --> >=2.0.0 <3.0.0-0
// ^1.2, ^1.2.x --> >=1.2.0 <2.0.0-0
// ^1.2.3 --> >=1.2.3 <2.0.0-0
// ^1.2.0 --> >=1.2.0 <2.0.0-0
// ^0.0.1 --> >=0.0.1 <0.0.2-0
// ^0.1.0 --> >=0.1.0 <0.2.0-0
const replaceCarets = (comp, options) => {
return comp
.trim()
.split(/\s+/)
.map((c) => replaceCaret(c, options))
.join(' ')
}
const replaceCaret = (comp, options) => {
debug('caret', comp, options)
const r = options.loose ? re[t.CARETLOOSE] : re[t.CARET]
const z = options.includePrerelease ? '-0' : ''
return comp.replace(r, (_, M, m, p, pr) => {
debug('caret', comp, _, M, m, p, pr)
let ret
if (isX(M)) {
ret = ''
} else if (isX(m)) {
ret = `>=${M}.0.0${z} <${+M + 1}.0.0-0`
} else if (isX(p)) {
if (M === '0') {
ret = `>=${M}.${m}.0${z} <${M}.${+m + 1}.0-0`
} else {
ret = `>=${M}.${m}.0${z} <${+M + 1}.0.0-0`
}
} else if (pr) {
debug('replaceCaret pr', pr)
if (M === '0') {
if (m === '0') {
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${m}.${+p + 1}-0`
} else {
ret = `>=${M}.${m}.${p}-${pr
} <${M}.${+m + 1}.0-0`
}
} else {
ret = `>=${M}.${m}.${p}-${pr
} <${+M + 1}.0.0-0`
}
} else {
debug('no pr')
if (M === '0') {
if (m === '0') {
ret = `>=${M}.${m}.${p
}${z} <${M}.${m}.${+p + 1}-0`
} else {
ret = `>=${M}.${m}.${p
}${z} <${M}.${+m + 1}.0-0`
}
} else {
ret = `>=${M}.${m}.${p
} <${+M + 1}.0.0-0`
}
}
debug('caret return', ret)
return ret
})
}
const replaceXRanges = (comp, options) => {
debug('replaceXRanges', comp, options)
return comp
.split(/\s+/)
.map((c) => replaceXRange(c, options))
.join(' ')
}
const replaceXRange = (comp, options) => {
comp = comp.trim()
const r = options.loose ? re[t.XRANGELOOSE] : re[t.XRANGE]
return comp.replace(r, (ret, gtlt, M, m, p, pr) => {
debug('xRange', comp, ret, gtlt, M, m, p, pr)
const xM = isX(M)
const xm = xM || isX(m)
const xp = xm || isX(p)
const anyX = xp
if (gtlt === '=' && anyX) {
gtlt = ''
}
// if we're including prereleases in the match, then we need
// to fix this to -0, the lowest possible prerelease value
pr = options.includePrerelease ? '-0' : ''
if (xM) {
if (gtlt === '>' || gtlt === '<') {
// nothing is allowed
ret = '<0.0.0-0'
} else {
// nothing is forbidden
ret = '*'
}
} else if (gtlt && anyX) {
// we know patch is an x, because we have any x at all.
// replace X with 0
if (xm) {
m = 0
}
p = 0
if (gtlt === '>') {
// >1 => >=2.0.0
// >1.2 => >=1.3.0
gtlt = '>='
if (xm) {
M = +M + 1
m = 0
p = 0
} else {
m = +m + 1
p = 0
}
} else if (gtlt === '<=') {
// <=0.7.x is actually <0.8.0, since any 0.7.x should
// pass. Similarly, <=7.x is actually <8.0.0, etc.
gtlt = '<'
if (xm) {
M = +M + 1
} else {
m = +m + 1
}
}
if (gtlt === '<') {
pr = '-0'
}
ret = `${gtlt + M}.${m}.${p}${pr}`
} else if (xm) {
ret = `>=${M}.0.0${pr} <${+M + 1}.0.0-0`
} else if (xp) {
ret = `>=${M}.${m}.0${pr
} <${M}.${+m + 1}.0-0`
}
debug('xRange return', ret)
return ret
})
}
// Because * is AND-ed with everything else in the comparator,
// and '' means "any version", just remove the *s entirely.
const replaceStars = (comp, options) => {
debug('replaceStars', comp, options)
// Looseness is ignored here. star is always as loose as it gets!
return comp
.trim()
.replace(re[t.STAR], '')
}
const replaceGTE0 = (comp, options) => {
debug('replaceGTE0', comp, options)
return comp
.trim()
.replace(re[options.includePrerelease ? t.GTE0PRE : t.GTE0], '')
}
// This function is passed to string.replace(re[t.HYPHENRANGE])
// M, m, patch, prerelease, build
// 1.2 - 3.4.5 => >=1.2.0 <=3.4.5
// 1.2.3 - 3.4 => >=1.2.0 <3.5.0-0 Any 3.4.x will do
// 1.2 - 3.4 => >=1.2.0 <3.5.0-0
// TODO build?
const hyphenReplace = incPr => ($0,
from, fM, fm, fp, fpr, fb,
to, tM, tm, tp, tpr) => {
if (isX(fM)) {
from = ''
} else if (isX(fm)) {
from = `>=${fM}.0.0${incPr ? '-0' : ''}`
} else if (isX(fp)) {
from = `>=${fM}.${fm}.0${incPr ? '-0' : ''}`
} else if (fpr) {
from = `>=${from}`
} else {
from = `>=${from}${incPr ? '-0' : ''}`
}
if (isX(tM)) {
to = ''
} else if (isX(tm)) {
to = `<${+tM + 1}.0.0-0`
} else if (isX(tp)) {
to = `<${tM}.${+tm + 1}.0-0`
} else if (tpr) {
to = `<=${tM}.${tm}.${tp}-${tpr}`
} else if (incPr) {
to = `<${tM}.${tm}.${+tp + 1}-0`
} else {
to = `<=${to}`
}
return `${from} ${to}`.trim()
}
const testSet = (set, version, options) => {
for (let i = 0; i < set.length; i++) {
if (!set[i].test(version)) {
return false
}
}
if (version.prerelease.length && !options.includePrerelease) {
// Find the set of versions that are allowed to have prereleases
// For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0
// That should allow `1.2.3-pr.2` to pass.
// However, `1.2.4-alpha.notready` should NOT be allowed,
// even though it's within the range set by the comparators.
for (let i = 0; i < set.length; i++) {
debug(set[i].semver)
if (set[i].semver === Comparator.ANY) {
continue
}
if (set[i].semver.prerelease.length > 0) {
const allowed = set[i].semver
if (allowed.major === version.major &&
allowed.minor === version.minor &&
allowed.patch === version.patch) {
return true
}
}
}
// Version has a -pre, but it's not one of the ones we like.
return false
}
return true
}
+333
View File
@@ -0,0 +1,333 @@
'use strict'
const debug = require('../internal/debug')
const { MAX_LENGTH, MAX_SAFE_INTEGER } = require('../internal/constants')
const { safeRe: re, t } = require('../internal/re')
const parseOptions = require('../internal/parse-options')
const { compareIdentifiers } = require('../internal/identifiers')
class SemVer {
constructor (version, options) {
options = parseOptions(options)
if (version instanceof SemVer) {
if (version.loose === !!options.loose &&
version.includePrerelease === !!options.includePrerelease) {
return version
} else {
version = version.version
}
} else if (typeof version !== 'string') {
throw new TypeError(`Invalid version. Must be a string. Got type "${typeof version}".`)
}
if (version.length > MAX_LENGTH) {
throw new TypeError(
`version is longer than ${MAX_LENGTH} characters`
)
}
debug('SemVer', version, options)
this.options = options
this.loose = !!options.loose
// this isn't actually relevant for versions, but keep it so that we
// don't run into trouble passing this.options around.
this.includePrerelease = !!options.includePrerelease
const m = version.trim().match(options.loose ? re[t.LOOSE] : re[t.FULL])
if (!m) {
throw new TypeError(`Invalid Version: ${version}`)
}
this.raw = version
// these are actually numbers
this.major = +m[1]
this.minor = +m[2]
this.patch = +m[3]
if (this.major > MAX_SAFE_INTEGER || this.major < 0) {
throw new TypeError('Invalid major version')
}
if (this.minor > MAX_SAFE_INTEGER || this.minor < 0) {
throw new TypeError('Invalid minor version')
}
if (this.patch > MAX_SAFE_INTEGER || this.patch < 0) {
throw new TypeError('Invalid patch version')
}
// numberify any prerelease numeric ids
if (!m[4]) {
this.prerelease = []
} else {
this.prerelease = m[4].split('.').map((id) => {
if (/^[0-9]+$/.test(id)) {
const num = +id
if (num >= 0 && num < MAX_SAFE_INTEGER) {
return num
}
}
return id
})
}
this.build = m[5] ? m[5].split('.') : []
this.format()
}
format () {
this.version = `${this.major}.${this.minor}.${this.patch}`
if (this.prerelease.length) {
this.version += `-${this.prerelease.join('.')}`
}
return this.version
}
toString () {
return this.version
}
compare (other) {
debug('SemVer.compare', this.version, this.options, other)
if (!(other instanceof SemVer)) {
if (typeof other === 'string' && other === this.version) {
return 0
}
other = new SemVer(other, this.options)
}
if (other.version === this.version) {
return 0
}
return this.compareMain(other) || this.comparePre(other)
}
compareMain (other) {
if (!(other instanceof SemVer)) {
other = new SemVer(other, this.options)
}
if (this.major < other.major) {
return -1
}
if (this.major > other.major) {
return 1
}
if (this.minor < other.minor) {
return -1
}
if (this.minor > other.minor) {
return 1
}
if (this.patch < other.patch) {
return -1
}
if (this.patch > other.patch) {
return 1
}
return 0
}
comparePre (other) {
if (!(other instanceof SemVer)) {
other = new SemVer(other, this.options)
}
// NOT having a prerelease is > having one
if (this.prerelease.length && !other.prerelease.length) {
return -1
} else if (!this.prerelease.length && other.prerelease.length) {
return 1
} else if (!this.prerelease.length && !other.prerelease.length) {
return 0
}
let i = 0
do {
const a = this.prerelease[i]
const b = other.prerelease[i]
debug('prerelease compare', i, a, b)
if (a === undefined && b === undefined) {
return 0
} else if (b === undefined) {
return 1
} else if (a === undefined) {
return -1
} else if (a === b) {
continue
} else {
return compareIdentifiers(a, b)
}
} while (++i)
}
compareBuild (other) {
if (!(other instanceof SemVer)) {
other = new SemVer(other, this.options)
}
let i = 0
do {
const a = this.build[i]
const b = other.build[i]
debug('build compare', i, a, b)
if (a === undefined && b === undefined) {
return 0
} else if (b === undefined) {
return 1
} else if (a === undefined) {
return -1
} else if (a === b) {
continue
} else {
return compareIdentifiers(a, b)
}
} while (++i)
}
// preminor will bump the version up to the next minor release, and immediately
// down to pre-release. premajor and prepatch work the same way.
inc (release, identifier, identifierBase) {
if (release.startsWith('pre')) {
if (!identifier && identifierBase === false) {
throw new Error('invalid increment argument: identifier is empty')
}
// Avoid an invalid semver results
if (identifier) {
const match = `-${identifier}`.match(this.options.loose ? re[t.PRERELEASELOOSE] : re[t.PRERELEASE])
if (!match || match[1] !== identifier) {
throw new Error(`invalid identifier: ${identifier}`)
}
}
}
switch (release) {
case 'premajor':
this.prerelease.length = 0
this.patch = 0
this.minor = 0
this.major++
this.inc('pre', identifier, identifierBase)
break
case 'preminor':
this.prerelease.length = 0
this.patch = 0
this.minor++
this.inc('pre', identifier, identifierBase)
break
case 'prepatch':
// If this is already a prerelease, it will bump to the next version
// drop any prereleases that might already exist, since they are not
// relevant at this point.
this.prerelease.length = 0
this.inc('patch', identifier, identifierBase)
this.inc('pre', identifier, identifierBase)
break
// If the input is a non-prerelease version, this acts the same as
// prepatch.
case 'prerelease':
if (this.prerelease.length === 0) {
this.inc('patch', identifier, identifierBase)
}
this.inc('pre', identifier, identifierBase)
break
case 'release':
if (this.prerelease.length === 0) {
throw new Error(`version ${this.raw} is not a prerelease`)
}
this.prerelease.length = 0
break
case 'major':
// If this is a pre-major version, bump up to the same major version.
// Otherwise increment major.
// 1.0.0-5 bumps to 1.0.0
// 1.1.0 bumps to 2.0.0
if (
this.minor !== 0 ||
this.patch !== 0 ||
this.prerelease.length === 0
) {
this.major++
}
this.minor = 0
this.patch = 0
this.prerelease = []
break
case 'minor':
// If this is a pre-minor version, bump up to the same minor version.
// Otherwise increment minor.
// 1.2.0-5 bumps to 1.2.0
// 1.2.1 bumps to 1.3.0
if (this.patch !== 0 || this.prerelease.length === 0) {
this.minor++
}
this.patch = 0
this.prerelease = []
break
case 'patch':
// If this is not a pre-release version, it will increment the patch.
// If it is a pre-release it will bump up to the same patch version.
// 1.2.0-5 patches to 1.2.0
// 1.2.0 patches to 1.2.1
if (this.prerelease.length === 0) {
this.patch++
}
this.prerelease = []
break
// This probably shouldn't be used publicly.
// 1.0.0 'pre' would become 1.0.0-0 which is the wrong direction.
case 'pre': {
const base = Number(identifierBase) ? 1 : 0
if (this.prerelease.length === 0) {
this.prerelease = [base]
} else {
let i = this.prerelease.length
while (--i >= 0) {
if (typeof this.prerelease[i] === 'number') {
this.prerelease[i]++
i = -2
}
}
if (i === -1) {
// didn't increment anything
if (identifier === this.prerelease.join('.') && identifierBase === false) {
throw new Error('invalid increment argument: identifier already exists')
}
this.prerelease.push(base)
}
}
if (identifier) {
// 1.2.0-beta.1 bumps to 1.2.0-beta.2,
// 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0
let prerelease = [identifier, base]
if (identifierBase === false) {
prerelease = [identifier]
}
if (compareIdentifiers(this.prerelease[0], identifier) === 0) {
if (isNaN(this.prerelease[1])) {
this.prerelease = prerelease
}
} else {
this.prerelease = prerelease
}
}
break
}
default:
throw new Error(`invalid increment argument: ${release}`)
}
this.raw = this.format()
if (this.build.length) {
this.raw += `+${this.build.join('.')}`
}
return this
}
}
module.exports = SemVer
@@ -0,0 +1,8 @@
'use strict'
const parse = require('./parse')
const clean = (version, options) => {
const s = parse(version.trim().replace(/^[=v]+/, ''), options)
return s ? s.version : null
}
module.exports = clean
+54
View File
@@ -0,0 +1,54 @@
'use strict'
const eq = require('./eq')
const neq = require('./neq')
const gt = require('./gt')
const gte = require('./gte')
const lt = require('./lt')
const lte = require('./lte')
const cmp = (a, op, b, loose) => {
switch (op) {
case '===':
if (typeof a === 'object') {
a = a.version
}
if (typeof b === 'object') {
b = b.version
}
return a === b
case '!==':
if (typeof a === 'object') {
a = a.version
}
if (typeof b === 'object') {
b = b.version
}
return a !== b
case '':
case '=':
case '==':
return eq(a, b, loose)
case '!=':
return neq(a, b, loose)
case '>':
return gt(a, b, loose)
case '>=':
return gte(a, b, loose)
case '<':
return lt(a, b, loose)
case '<=':
return lte(a, b, loose)
default:
throw new TypeError(`Invalid operator: ${op}`)
}
}
module.exports = cmp
+62
View File
@@ -0,0 +1,62 @@
'use strict'
const SemVer = require('../classes/semver')
const parse = require('./parse')
const { safeRe: re, t } = require('../internal/re')
const coerce = (version, options) => {
if (version instanceof SemVer) {
return version
}
if (typeof version === 'number') {
version = String(version)
}
if (typeof version !== 'string') {
return null
}
options = options || {}
let match = null
if (!options.rtl) {
match = version.match(options.includePrerelease ? re[t.COERCEFULL] : re[t.COERCE])
} else {
// Find the right-most coercible string that does not share
// a terminus with a more left-ward coercible string.
// Eg, '1.2.3.4' wants to coerce '2.3.4', not '3.4' or '4'
// With includePrerelease option set, '1.2.3.4-rc' wants to coerce '2.3.4-rc', not '2.3.4'
//
// Walk through the string checking with a /g regexp
// Manually set the index so as to pick up overlapping matches.
// Stop when we get a match that ends at the string end, since no
// coercible string can be more right-ward without the same terminus.
const coerceRtlRegex = options.includePrerelease ? re[t.COERCERTLFULL] : re[t.COERCERTL]
let next
while ((next = coerceRtlRegex.exec(version)) &&
(!match || match.index + match[0].length !== version.length)
) {
if (!match ||
next.index + next[0].length !== match.index + match[0].length) {
match = next
}
coerceRtlRegex.lastIndex = next.index + next[1].length + next[2].length
}
// leave it in a clean state
coerceRtlRegex.lastIndex = -1
}
if (match === null) {
return null
}
const major = match[2]
const minor = match[3] || '0'
const patch = match[4] || '0'
const prerelease = options.includePrerelease && match[5] ? `-${match[5]}` : ''
const build = options.includePrerelease && match[6] ? `+${match[6]}` : ''
return parse(`${major}.${minor}.${patch}${prerelease}${build}`, options)
}
module.exports = coerce
@@ -0,0 +1,9 @@
'use strict'
const SemVer = require('../classes/semver')
const compareBuild = (a, b, loose) => {
const versionA = new SemVer(a, loose)
const versionB = new SemVer(b, loose)
return versionA.compare(versionB) || versionA.compareBuild(versionB)
}
module.exports = compareBuild
@@ -0,0 +1,5 @@
'use strict'
const compare = require('./compare')
const compareLoose = (a, b) => compare(a, b, true)
module.exports = compareLoose
@@ -0,0 +1,7 @@
'use strict'
const SemVer = require('../classes/semver')
const compare = (a, b, loose) =>
new SemVer(a, loose).compare(new SemVer(b, loose))
module.exports = compare
+60
View File
@@ -0,0 +1,60 @@
'use strict'
const parse = require('./parse.js')
const diff = (version1, version2) => {
const v1 = parse(version1, null, true)
const v2 = parse(version2, null, true)
const comparison = v1.compare(v2)
if (comparison === 0) {
return null
}
const v1Higher = comparison > 0
const highVersion = v1Higher ? v1 : v2
const lowVersion = v1Higher ? v2 : v1
const highHasPre = !!highVersion.prerelease.length
const lowHasPre = !!lowVersion.prerelease.length
if (lowHasPre && !highHasPre) {
// Going from prerelease -> no prerelease requires some special casing
// If the low version has only a major, then it will always be a major
// Some examples:
// 1.0.0-1 -> 1.0.0
// 1.0.0-1 -> 1.1.1
// 1.0.0-1 -> 2.0.0
if (!lowVersion.patch && !lowVersion.minor) {
return 'major'
}
// If the main part has no difference
if (lowVersion.compareMain(highVersion) === 0) {
if (lowVersion.minor && !lowVersion.patch) {
return 'minor'
}
return 'patch'
}
}
// add the `pre` prefix if we are going to a prerelease version
const prefix = highHasPre ? 'pre' : ''
if (v1.major !== v2.major) {
return prefix + 'major'
}
if (v1.minor !== v2.minor) {
return prefix + 'minor'
}
if (v1.patch !== v2.patch) {
return prefix + 'patch'
}
// high and low are prereleases
return 'prerelease'
}
module.exports = diff
+5
View File
@@ -0,0 +1,5 @@
'use strict'
const compare = require('./compare')
const eq = (a, b, loose) => compare(a, b, loose) === 0
module.exports = eq
+5
View File
@@ -0,0 +1,5 @@
'use strict'
const compare = require('./compare')
const gt = (a, b, loose) => compare(a, b, loose) > 0
module.exports = gt
+5
View File
@@ -0,0 +1,5 @@
'use strict'
const compare = require('./compare')
const gte = (a, b, loose) => compare(a, b, loose) >= 0
module.exports = gte
+21
View File
@@ -0,0 +1,21 @@
'use strict'
const SemVer = require('../classes/semver')
const inc = (version, release, options, identifier, identifierBase) => {
if (typeof (options) === 'string') {
identifierBase = identifier
identifier = options
options = undefined
}
try {
return new SemVer(
version instanceof SemVer ? version.version : version,
options
).inc(release, identifier, identifierBase).version
} catch (er) {
return null
}
}
module.exports = inc
+5
View File
@@ -0,0 +1,5 @@
'use strict'
const compare = require('./compare')
const lt = (a, b, loose) => compare(a, b, loose) < 0
module.exports = lt
+5
View File
@@ -0,0 +1,5 @@
'use strict'
const compare = require('./compare')
const lte = (a, b, loose) => compare(a, b, loose) <= 0
module.exports = lte
@@ -0,0 +1,5 @@
'use strict'
const SemVer = require('../classes/semver')
const major = (a, loose) => new SemVer(a, loose).major
module.exports = major
@@ -0,0 +1,5 @@
'use strict'
const SemVer = require('../classes/semver')
const minor = (a, loose) => new SemVer(a, loose).minor
module.exports = minor
+5
View File
@@ -0,0 +1,5 @@
'use strict'
const compare = require('./compare')
const neq = (a, b, loose) => compare(a, b, loose) !== 0
module.exports = neq

Some files were not shown because too many files have changed in this diff Show More