2017-08-01 15:50:57 +00:00
|
|
|
const temp = require('temp');
|
2023-06-22 14:21:42 +00:00
|
|
|
const fs = require('node:fs');
|
|
|
|
const path = require('node:path');
|
|
|
|
const childProcess = require('node:child_process');
|
2018-11-01 01:27:35 +00:00
|
|
|
const semver = require('semver');
|
2021-08-12 17:34:49 +00:00
|
|
|
|
|
|
|
const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils');
|
2022-10-25 06:44:43 +00:00
|
|
|
const { getElectronVersion } = require('../lib/get-version');
|
2019-06-24 17:18:04 +00:00
|
|
|
const rootPackageJson = require('../../package.json');
|
2020-08-05 15:59:52 +00:00
|
|
|
|
|
|
|
const { Octokit } = require('@octokit/rest');
|
2021-08-12 17:34:49 +00:00
|
|
|
const { getAssetContents } = require('./get-asset');
|
2020-08-05 15:59:52 +00:00
|
|
|
const octokit = new Octokit({
|
2021-07-27 13:52:18 +00:00
|
|
|
userAgent: 'electron-npm-publisher',
|
|
|
|
auth: process.env.ELECTRON_GITHUB_TOKEN
|
2019-01-08 20:05:58 +00:00
|
|
|
});
|
2017-07-13 21:09:21 +00:00
|
|
|
|
2018-08-17 00:08:52 +00:00
|
|
|
if (!process.env.ELECTRON_NPM_OTP) {
|
|
|
|
console.error('Please set ELECTRON_NPM_OTP');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:09:21 +00:00
|
|
|
let tempDir;
|
2018-09-13 16:10:51 +00:00
|
|
|
temp.track(); // track and cleanup files at exit
|
2017-07-13 21:09:21 +00:00
|
|
|
|
|
|
|
const files = [
|
|
|
|
'cli.js',
|
|
|
|
'index.js',
|
|
|
|
'install.js',
|
|
|
|
'package.json',
|
2018-01-23 23:09:08 +00:00
|
|
|
'README.md',
|
|
|
|
'LICENSE'
|
2017-07-13 21:09:21 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
const jsonFields = [
|
|
|
|
'name',
|
|
|
|
'repository',
|
|
|
|
'description',
|
|
|
|
'license',
|
|
|
|
'author',
|
|
|
|
'keywords'
|
|
|
|
];
|
|
|
|
|
2017-08-01 20:08:30 +00:00
|
|
|
let npmTag = '';
|
|
|
|
|
2022-10-25 06:44:43 +00:00
|
|
|
const currentElectronVersion = getElectronVersion();
|
|
|
|
const isNightlyElectronVersion = currentElectronVersion.includes('nightly');
|
2024-05-31 17:58:39 +00:00
|
|
|
const targetRepo = getRepo();
|
|
|
|
|
|
|
|
function getRepo () {
|
|
|
|
return isNightlyElectronVersion ? 'nightlies' : 'electron';
|
|
|
|
}
|
2022-10-25 06:44:43 +00:00
|
|
|
|
2017-07-13 21:09:21 +00:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
temp.mkdir('electron-npm', (err, dirPath) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve(dirPath);
|
|
|
|
}
|
|
|
|
});
|
2017-08-01 04:50:40 +00:00
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
.then((dirPath) => {
|
|
|
|
tempDir = dirPath;
|
|
|
|
// copy files from `/npm` to temp directory
|
2023-08-31 14:36:43 +00:00
|
|
|
for (const name of files) {
|
2018-09-13 16:10:51 +00:00
|
|
|
const noThirdSegment = name === 'README.md' || name === 'LICENSE';
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(tempDir, name),
|
2019-06-24 17:18:04 +00:00
|
|
|
fs.readFileSync(path.join(ELECTRON_DIR, noThirdSegment ? '' : 'npm', name))
|
2018-09-13 16:10:51 +00:00
|
|
|
);
|
2023-08-31 14:36:43 +00:00
|
|
|
}
|
2018-09-13 16:10:51 +00:00
|
|
|
// copy from root package.json to temp/package.json
|
|
|
|
const packageJson = require(path.join(tempDir, 'package.json'));
|
2023-08-31 14:36:43 +00:00
|
|
|
for (const fieldName of jsonFields) {
|
2018-09-13 16:10:51 +00:00
|
|
|
packageJson[fieldName] = rootPackageJson[fieldName];
|
2023-08-31 14:36:43 +00:00
|
|
|
}
|
2022-10-25 06:44:43 +00:00
|
|
|
packageJson.version = currentElectronVersion;
|
2017-07-13 21:09:21 +00:00
|
|
|
fs.writeFileSync(
|
2018-09-13 16:10:51 +00:00
|
|
|
path.join(tempDir, 'package.json'),
|
|
|
|
JSON.stringify(packageJson, null, 2)
|
2017-07-13 21:09:21 +00:00
|
|
|
);
|
|
|
|
|
2019-01-08 20:05:58 +00:00
|
|
|
return octokit.repos.listReleases({
|
2018-09-13 16:10:51 +00:00
|
|
|
owner: 'electron',
|
2024-05-31 17:58:39 +00:00
|
|
|
repo: targetRepo
|
2018-09-13 16:10:51 +00:00
|
|
|
});
|
2017-07-13 21:09:21 +00:00
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
.then((releases) => {
|
2017-08-01 20:08:30 +00:00
|
|
|
// download electron.d.ts from release
|
2018-09-13 16:10:51 +00:00
|
|
|
const release = releases.data.find(
|
2022-10-25 06:44:43 +00:00
|
|
|
(release) => release.tag_name === `v${currentElectronVersion}`
|
2018-09-13 16:10:51 +00:00
|
|
|
);
|
|
|
|
if (!release) {
|
2022-10-25 06:44:43 +00:00
|
|
|
throw new Error(`cannot find release with tag v${currentElectronVersion}`);
|
2018-09-13 16:10:51 +00:00
|
|
|
}
|
|
|
|
return release;
|
|
|
|
})
|
2021-08-13 20:07:17 +00:00
|
|
|
.then(async (release) => {
|
2018-09-13 16:10:51 +00:00
|
|
|
const tsdAsset = release.assets.find((asset) => asset.name === 'electron.d.ts');
|
|
|
|
if (!tsdAsset) {
|
2022-10-25 06:44:43 +00:00
|
|
|
throw new Error(`cannot find electron.d.ts from v${currentElectronVersion} release assets`);
|
2018-09-13 16:10:51 +00:00
|
|
|
}
|
2021-08-12 17:34:49 +00:00
|
|
|
|
|
|
|
const typingsContent = await getAssetContents(
|
2024-05-31 17:58:39 +00:00
|
|
|
targetRepo,
|
2021-08-12 17:34:49 +00:00
|
|
|
tsdAsset.id
|
|
|
|
);
|
|
|
|
|
|
|
|
fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), typingsContent);
|
2021-08-13 20:26:27 +00:00
|
|
|
|
|
|
|
return release;
|
2017-07-13 21:09:21 +00:00
|
|
|
})
|
2021-08-19 18:26:17 +00:00
|
|
|
.then(async (release) => {
|
|
|
|
const checksumsAsset = release.assets.find((asset) => asset.name === 'SHASUMS256.txt');
|
|
|
|
if (!checksumsAsset) {
|
2022-10-25 06:44:43 +00:00
|
|
|
throw new Error(`cannot find SHASUMS256.txt from v${currentElectronVersion} release assets`);
|
2021-08-19 18:26:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const checksumsContent = await getAssetContents(
|
2024-05-31 17:58:39 +00:00
|
|
|
targetRepo,
|
2021-08-19 18:26:17 +00:00
|
|
|
checksumsAsset.id
|
|
|
|
);
|
|
|
|
|
|
|
|
const checksumsObject = {};
|
|
|
|
for (const line of checksumsContent.trim().split('\n')) {
|
|
|
|
const [checksum, file] = line.split(' *');
|
|
|
|
checksumsObject[file] = checksum;
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(path.join(tempDir, 'checksums.json'), JSON.stringify(checksumsObject, null, 2));
|
|
|
|
|
|
|
|
return release;
|
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
.then(async (release) => {
|
2018-11-01 01:27:35 +00:00
|
|
|
const currentBranch = await getCurrentBranch();
|
|
|
|
|
2022-10-25 06:44:43 +00:00
|
|
|
if (isNightlyElectronVersion) {
|
2023-01-12 17:05:26 +00:00
|
|
|
// Nightlies get published to their own module, so they should be tagged as latest
|
|
|
|
npmTag = currentBranch === 'main' ? 'latest' : `nightly-${currentBranch}`;
|
2019-01-02 19:04:56 +00:00
|
|
|
|
2019-01-08 04:37:44 +00:00
|
|
|
const currentJson = JSON.parse(fs.readFileSync(path.join(tempDir, 'package.json'), 'utf8'));
|
2019-01-04 21:48:02 +00:00
|
|
|
currentJson.name = 'electron-nightly';
|
|
|
|
rootPackageJson.name = 'electron-nightly';
|
2019-01-02 19:04:56 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(tempDir, 'package.json'),
|
|
|
|
JSON.stringify(currentJson, null, 2)
|
|
|
|
);
|
2018-08-18 01:30:21 +00:00
|
|
|
} else {
|
2023-01-12 17:05:26 +00:00
|
|
|
if (currentBranch === 'main') {
|
2021-05-28 19:13:12 +00:00
|
|
|
// This should never happen, main releases should be nightly releases
|
2018-11-01 01:27:35 +00:00
|
|
|
// this is here just-in-case
|
2021-05-28 19:13:12 +00:00
|
|
|
throw new Error('Unreachable release phase, can\'t tag a non-nightly release on the main branch');
|
2018-11-01 01:27:35 +00:00
|
|
|
} else if (!release.prerelease) {
|
|
|
|
// Tag the release with a `2-0-x` style tag
|
|
|
|
npmTag = currentBranch;
|
2021-07-20 00:58:15 +00:00
|
|
|
} else if (release.tag_name.indexOf('alpha') > 0) {
|
|
|
|
// Tag the release with an `alpha-3-0-x` style tag
|
|
|
|
npmTag = `alpha-${currentBranch}`;
|
2018-11-01 01:27:35 +00:00
|
|
|
} else {
|
|
|
|
// Tag the release with a `beta-3-0-x` style tag
|
|
|
|
npmTag = `beta-${currentBranch}`;
|
|
|
|
}
|
2018-08-18 01:30:21 +00:00
|
|
|
}
|
2018-09-13 16:10:51 +00:00
|
|
|
})
|
|
|
|
.then(() => childProcess.execSync('npm pack', { cwd: tempDir }))
|
|
|
|
.then(() => {
|
2017-08-01 20:08:30 +00:00
|
|
|
// test that the package can install electron prebuilt from github release
|
2022-10-25 06:44:43 +00:00
|
|
|
const tarballPath = path.join(tempDir, `${rootPackageJson.name}-${currentElectronVersion}.tgz`);
|
2018-09-13 16:10:51 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-08-19 18:26:17 +00:00
|
|
|
const result = childProcess.spawnSync('npm', ['install', tarballPath, '--force', '--silent'], {
|
2022-06-27 08:29:18 +00:00
|
|
|
env: { ...process.env, electron_config_cache: tempDir },
|
2021-08-19 18:26:17 +00:00
|
|
|
cwd: tempDir,
|
|
|
|
stdio: 'inherit'
|
2018-09-13 16:10:51 +00:00
|
|
|
});
|
2021-08-19 18:26:17 +00:00
|
|
|
if (result.status !== 0) {
|
|
|
|
return reject(new Error(`npm install failed with status ${result.status}`));
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const electronPath = require(path.resolve(tempDir, 'node_modules', rootPackageJson.name));
|
|
|
|
if (typeof electronPath !== 'string') {
|
|
|
|
return reject(new Error(`path to electron binary (${electronPath}) returned by the ${rootPackageJson.name} module is not a string`));
|
|
|
|
}
|
|
|
|
if (!fs.existsSync(electronPath)) {
|
|
|
|
return reject(new Error(`path to electron binary (${electronPath}) returned by the ${rootPackageJson.name} module does not exist on disk`));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return reject(new Error(`loading the generated ${rootPackageJson.name} module failed with an error`));
|
|
|
|
}
|
2018-09-13 16:10:51 +00:00
|
|
|
resolve(tarballPath);
|
2017-08-01 04:50:40 +00:00
|
|
|
});
|
|
|
|
})
|
2021-04-26 20:02:14 +00:00
|
|
|
.then((tarballPath) => {
|
2022-10-27 01:31:20 +00:00
|
|
|
const existingVersionJSON = childProcess.execSync(`npx npm@7 view ${rootPackageJson.name}@${currentElectronVersion} --json`).toString('utf-8');
|
2021-04-26 20:02:14 +00:00
|
|
|
// It's possible this is a re-run and we already have published the package, if not we just publish like normal
|
|
|
|
if (!existingVersionJSON) {
|
|
|
|
childProcess.execSync(`npm publish ${tarballPath} --tag ${npmTag} --otp=${process.env.ELECTRON_NPM_OTP}`);
|
|
|
|
}
|
|
|
|
})
|
2018-11-01 01:27:35 +00:00
|
|
|
.then(() => {
|
|
|
|
const currentTags = JSON.parse(childProcess.execSync('npm show electron dist-tags --json').toString());
|
2022-10-25 06:44:43 +00:00
|
|
|
const parsedLocalVersion = semver.parse(currentElectronVersion);
|
2019-01-02 19:04:56 +00:00
|
|
|
if (rootPackageJson.name === 'electron') {
|
|
|
|
// We should only customly add dist tags for non-nightly releases where the package name is still
|
|
|
|
// "electron"
|
|
|
|
if (parsedLocalVersion.prerelease.length === 0 &&
|
2022-10-25 06:44:43 +00:00
|
|
|
semver.gt(currentElectronVersion, currentTags.latest)) {
|
|
|
|
childProcess.execSync(`npm dist-tag add electron@${currentElectronVersion} latest --otp=${process.env.ELECTRON_NPM_OTP}`);
|
2019-01-02 19:04:56 +00:00
|
|
|
}
|
|
|
|
if (parsedLocalVersion.prerelease[0] === 'beta' &&
|
2022-10-25 06:44:43 +00:00
|
|
|
semver.gt(currentElectronVersion, currentTags.beta)) {
|
|
|
|
childProcess.execSync(`npm dist-tag add electron@${currentElectronVersion} beta --otp=${process.env.ELECTRON_NPM_OTP}`);
|
2019-01-02 19:04:56 +00:00
|
|
|
}
|
2021-07-20 00:58:15 +00:00
|
|
|
if (parsedLocalVersion.prerelease[0] === 'alpha' &&
|
2022-10-25 06:44:43 +00:00
|
|
|
semver.gt(currentElectronVersion, currentTags.alpha)) {
|
|
|
|
childProcess.execSync(`npm dist-tag add electron@${currentElectronVersion} alpha --otp=${process.env.ELECTRON_NPM_OTP}`);
|
2021-07-20 00:58:15 +00:00
|
|
|
}
|
2018-11-01 01:27:35 +00:00
|
|
|
}
|
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
.catch((err) => {
|
2021-08-19 18:26:17 +00:00
|
|
|
console.error('Error:', err);
|
2018-09-13 16:10:51 +00:00
|
|
|
process.exit(1);
|
|
|
|
});
|