2020-03-20 20:28:31 +00:00
|
|
|
const temp = require('temp');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const childProcess = require('child_process');
|
|
|
|
const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils');
|
|
|
|
const request = require('request');
|
|
|
|
const semver = require('semver');
|
|
|
|
const rootPackageJson = require('../../package.json');
|
2020-08-05 15:59:52 +00:00
|
|
|
|
|
|
|
const { Octokit } = require('@octokit/rest');
|
|
|
|
const octokit = new Octokit({
|
|
|
|
userAgent: 'electron-npm-publisher'
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2017-07-13 21:09:21 +00:00
|
|
|
|
2018-08-17 00:08:52 +00:00
|
|
|
if (!process.env.ELECTRON_NPM_OTP) {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error('Please set ELECTRON_NPM_OTP');
|
|
|
|
process.exit(1);
|
2018-08-17 00:08:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let tempDir;
|
|
|
|
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'
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2017-07-13 21:09:21 +00:00
|
|
|
|
|
|
|
const jsonFields = [
|
|
|
|
'name',
|
|
|
|
'version',
|
|
|
|
'repository',
|
|
|
|
'description',
|
|
|
|
'license',
|
|
|
|
'author',
|
|
|
|
'keywords'
|
2020-03-20 20:28:31 +00:00
|
|
|
];
|
2017-07-13 21:09:21 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
let npmTag = '';
|
2017-08-01 20:08:30 +00:00
|
|
|
|
2017-07-13 21:09:21 +00:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
temp.mkdir('electron-npm', (err, dirPath) => {
|
|
|
|
if (err) {
|
2020-03-20 20:28:31 +00:00
|
|
|
reject(err);
|
2017-07-13 21:09:21 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
resolve(dirPath);
|
2017-07-13 21:09:21 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
2017-08-01 04:50:40 +00:00
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
.then((dirPath) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
tempDir = dirPath;
|
2018-09-13 16:10:51 +00:00
|
|
|
// copy files from `/npm` to temp directory
|
|
|
|
files.forEach((name) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const noThirdSegment = name === 'README.md' || name === 'LICENSE';
|
2018-09-13 16:10:51 +00:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(tempDir, name),
|
2019-06-24 17:18:04 +00:00
|
|
|
fs.readFileSync(path.join(ELECTRON_DIR, noThirdSegment ? '' : 'npm', name))
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
|
|
|
});
|
2018-09-13 16:10:51 +00:00
|
|
|
// copy from root package.json to temp/package.json
|
2020-03-20 20:28:31 +00:00
|
|
|
const packageJson = require(path.join(tempDir, 'package.json'));
|
2018-09-13 16:10:51 +00:00
|
|
|
jsonFields.forEach((fieldName) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
packageJson[fieldName] = rootPackageJson[fieldName];
|
|
|
|
});
|
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)
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
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',
|
|
|
|
repo: rootPackageJson.version.indexOf('nightly') > 0 ? 'nightlies' : 'electron'
|
2020-03-20 20:28:31 +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(
|
|
|
|
(release) => release.tag_name === `v${rootPackageJson.version}`
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!release) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`cannot find release with tag v${rootPackageJson.version}`);
|
2018-09-13 16:10:51 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
return release;
|
2018-09-13 16:10:51 +00:00
|
|
|
})
|
|
|
|
.then((release) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const tsdAsset = release.assets.find((asset) => asset.name === 'electron.d.ts');
|
2018-09-13 16:10:51 +00:00
|
|
|
if (!tsdAsset) {
|
2020-03-20 20:28:31 +00:00
|
|
|
throw new Error(`cannot find electron.d.ts from v${rootPackageJson.version} release assets`);
|
2018-09-13 16:10:51 +00:00
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request.get({
|
|
|
|
url: tsdAsset.url,
|
|
|
|
headers: {
|
2020-03-20 15:12:18 +00:00
|
|
|
accept: 'application/octet-stream',
|
2018-09-13 16:10:51 +00:00
|
|
|
'user-agent': 'electron-npm-publisher'
|
|
|
|
}
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err || response.statusCode !== 200) {
|
2020-03-20 20:28:31 +00:00
|
|
|
reject(err || new Error('Cannot download electron.d.ts'));
|
2018-09-13 16:10:51 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), body);
|
|
|
|
resolve(release);
|
2018-09-13 16:10:51 +00:00
|
|
|
}
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
});
|
2017-07-13 21:09:21 +00:00
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
.then(async (release) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const currentBranch = await getCurrentBranch();
|
2018-11-01 01:27:35 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
if (release.tag_name.indexOf('nightly') > 0) {
|
|
|
|
if (currentBranch === 'master') {
|
2019-01-02 19:04:56 +00:00
|
|
|
// Nightlies get published to their own module, so master nightlies should be tagged as latest
|
2020-03-20 20:28:31 +00:00
|
|
|
npmTag = 'latest';
|
2018-09-13 16:10:51 +00:00
|
|
|
} else {
|
2020-03-20 20:28:31 +00:00
|
|
|
npmTag = `nightly-${currentBranch}`;
|
2018-09-13 16:10:51 +00:00
|
|
|
}
|
2019-01-02 19:04:56 +00:00
|
|
|
|
2020-03-20 20:28:31 +00:00
|
|
|
const currentJson = JSON.parse(fs.readFileSync(path.join(tempDir, 'package.json'), 'utf8'));
|
|
|
|
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)
|
2020-03-20 20:28:31 +00:00
|
|
|
);
|
2018-08-18 01:30:21 +00:00
|
|
|
} else {
|
2018-11-01 01:27:35 +00:00
|
|
|
if (currentBranch === 'master') {
|
|
|
|
// This should never happen, master releases should be nightly releases
|
|
|
|
// this is here just-in-case
|
2020-03-20 20:28:31 +00:00
|
|
|
npmTag = 'master';
|
2018-11-01 01:27:35 +00:00
|
|
|
} else if (!release.prerelease) {
|
|
|
|
// Tag the release with a `2-0-x` style tag
|
2020-03-20 20:28:31 +00:00
|
|
|
npmTag = currentBranch;
|
2018-11-01 01:27:35 +00:00
|
|
|
} else {
|
|
|
|
// Tag the release with a `beta-3-0-x` style tag
|
2020-03-20 20:28:31 +00:00
|
|
|
npmTag = `beta-${currentBranch}`;
|
2018-11-01 01:27:35 +00:00
|
|
|
}
|
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
|
2020-03-20 20:28:31 +00:00
|
|
|
const tarballPath = path.join(tempDir, `${rootPackageJson.name}-${rootPackageJson.version}.tgz`);
|
2018-09-13 16:10:51 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
childProcess.execSync(`npm install ${tarballPath} --force --silent`, {
|
|
|
|
env: Object.assign({}, process.env, { electron_config_cache: tempDir }),
|
|
|
|
cwd: tempDir
|
2020-03-20 20:28:31 +00:00
|
|
|
});
|
|
|
|
resolve(tarballPath);
|
|
|
|
});
|
2017-08-01 04:50:40 +00:00
|
|
|
})
|
2021-04-26 20:02:14 +00:00
|
|
|
.then((tarballPath) => {
|
|
|
|
const existingVersionJSON = childProcess.execSync(`npm view electron@${rootPackageJson.version} --json`).toString('utf-8');
|
|
|
|
// 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(() => {
|
2020-03-20 20:28:31 +00:00
|
|
|
const currentTags = JSON.parse(childProcess.execSync('npm show electron dist-tags --json').toString());
|
|
|
|
const localVersion = rootPackageJson.version;
|
|
|
|
const parsedLocalVersion = semver.parse(localVersion);
|
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 &&
|
|
|
|
semver.gt(localVersion, currentTags.latest)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
childProcess.execSync(`npm dist-tag add electron@${localVersion} latest --otp=${process.env.ELECTRON_NPM_OTP}`);
|
2019-01-02 19:04:56 +00:00
|
|
|
}
|
|
|
|
if (parsedLocalVersion.prerelease[0] === 'beta' &&
|
|
|
|
semver.gt(localVersion, currentTags.beta)) {
|
2020-03-20 20:28:31 +00:00
|
|
|
childProcess.execSync(`npm dist-tag add electron@${localVersion} beta --otp=${process.env.ELECTRON_NPM_OTP}`);
|
2019-01-02 19:04:56 +00:00
|
|
|
}
|
2018-11-01 01:27:35 +00:00
|
|
|
}
|
|
|
|
})
|
2018-09-13 16:10:51 +00:00
|
|
|
.catch((err) => {
|
2020-03-20 20:28:31 +00:00
|
|
|
console.error(`Error: ${err}`);
|
|
|
|
process.exit(1);
|
|
|
|
});
|