publish-to-npm.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. const temp = require('temp');
  2. const fs = require('node:fs');
  3. const path = require('node:path');
  4. const childProcess = require('node:child_process');
  5. const semver = require('semver');
  6. const { getCurrentBranch, ELECTRON_DIR } = require('../lib/utils');
  7. const { getElectronVersion } = require('../lib/get-version');
  8. const rootPackageJson = require('../../package.json');
  9. const { Octokit } = require('@octokit/rest');
  10. const { getAssetContents } = require('./get-asset');
  11. const octokit = new Octokit({
  12. userAgent: 'electron-npm-publisher',
  13. auth: process.env.ELECTRON_GITHUB_TOKEN
  14. });
  15. if (!process.env.ELECTRON_NPM_OTP) {
  16. console.error('Please set ELECTRON_NPM_OTP');
  17. process.exit(1);
  18. }
  19. let tempDir;
  20. temp.track(); // track and cleanup files at exit
  21. const files = [
  22. 'cli.js',
  23. 'index.js',
  24. 'install.js',
  25. 'package.json',
  26. 'README.md',
  27. 'LICENSE'
  28. ];
  29. const jsonFields = [
  30. 'name',
  31. 'repository',
  32. 'description',
  33. 'license',
  34. 'author',
  35. 'keywords'
  36. ];
  37. let npmTag = '';
  38. const currentElectronVersion = getElectronVersion();
  39. const isNightlyElectronVersion = currentElectronVersion.includes('nightly');
  40. new Promise((resolve, reject) => {
  41. temp.mkdir('electron-npm', (err, dirPath) => {
  42. if (err) {
  43. reject(err);
  44. } else {
  45. resolve(dirPath);
  46. }
  47. });
  48. })
  49. .then((dirPath) => {
  50. tempDir = dirPath;
  51. // copy files from `/npm` to temp directory
  52. files.forEach((name) => {
  53. const noThirdSegment = name === 'README.md' || name === 'LICENSE';
  54. fs.writeFileSync(
  55. path.join(tempDir, name),
  56. fs.readFileSync(path.join(ELECTRON_DIR, noThirdSegment ? '' : 'npm', name))
  57. );
  58. });
  59. // copy from root package.json to temp/package.json
  60. const packageJson = require(path.join(tempDir, 'package.json'));
  61. jsonFields.forEach((fieldName) => {
  62. packageJson[fieldName] = rootPackageJson[fieldName];
  63. });
  64. packageJson.version = currentElectronVersion;
  65. fs.writeFileSync(
  66. path.join(tempDir, 'package.json'),
  67. JSON.stringify(packageJson, null, 2)
  68. );
  69. return octokit.repos.listReleases({
  70. owner: 'electron',
  71. repo: isNightlyElectronVersion ? 'nightlies' : 'electron'
  72. });
  73. })
  74. .then((releases) => {
  75. // download electron.d.ts from release
  76. const release = releases.data.find(
  77. (release) => release.tag_name === `v${currentElectronVersion}`
  78. );
  79. if (!release) {
  80. throw new Error(`cannot find release with tag v${currentElectronVersion}`);
  81. }
  82. return release;
  83. })
  84. .then(async (release) => {
  85. const tsdAsset = release.assets.find((asset) => asset.name === 'electron.d.ts');
  86. if (!tsdAsset) {
  87. throw new Error(`cannot find electron.d.ts from v${currentElectronVersion} release assets`);
  88. }
  89. const typingsContent = await getAssetContents(
  90. isNightlyElectronVersion ? 'nightlies' : 'electron',
  91. tsdAsset.id
  92. );
  93. fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), typingsContent);
  94. return release;
  95. })
  96. .then(async (release) => {
  97. const checksumsAsset = release.assets.find((asset) => asset.name === 'SHASUMS256.txt');
  98. if (!checksumsAsset) {
  99. throw new Error(`cannot find SHASUMS256.txt from v${currentElectronVersion} release assets`);
  100. }
  101. const checksumsContent = await getAssetContents(
  102. isNightlyElectronVersion ? 'nightlies' : 'electron',
  103. checksumsAsset.id
  104. );
  105. const checksumsObject = {};
  106. for (const line of checksumsContent.trim().split('\n')) {
  107. const [checksum, file] = line.split(' *');
  108. checksumsObject[file] = checksum;
  109. }
  110. fs.writeFileSync(path.join(tempDir, 'checksums.json'), JSON.stringify(checksumsObject, null, 2));
  111. return release;
  112. })
  113. .then(async (release) => {
  114. const currentBranch = await getCurrentBranch();
  115. if (isNightlyElectronVersion) {
  116. // Nightlies get published to their own module, so they should be tagged as latest
  117. npmTag = currentBranch === 'main' ? 'latest' : `nightly-${currentBranch}`;
  118. const currentJson = JSON.parse(fs.readFileSync(path.join(tempDir, 'package.json'), 'utf8'));
  119. currentJson.name = 'electron-nightly';
  120. rootPackageJson.name = 'electron-nightly';
  121. fs.writeFileSync(
  122. path.join(tempDir, 'package.json'),
  123. JSON.stringify(currentJson, null, 2)
  124. );
  125. } else {
  126. if (currentBranch === 'main') {
  127. // This should never happen, main releases should be nightly releases
  128. // this is here just-in-case
  129. throw new Error('Unreachable release phase, can\'t tag a non-nightly release on the main branch');
  130. } else if (!release.prerelease) {
  131. // Tag the release with a `2-0-x` style tag
  132. npmTag = currentBranch;
  133. } else if (release.tag_name.indexOf('alpha') > 0) {
  134. // Tag the release with an `alpha-3-0-x` style tag
  135. npmTag = `alpha-${currentBranch}`;
  136. } else {
  137. // Tag the release with a `beta-3-0-x` style tag
  138. npmTag = `beta-${currentBranch}`;
  139. }
  140. }
  141. })
  142. .then(() => childProcess.execSync('npm pack', { cwd: tempDir }))
  143. .then(() => {
  144. // test that the package can install electron prebuilt from github release
  145. const tarballPath = path.join(tempDir, `${rootPackageJson.name}-${currentElectronVersion}.tgz`);
  146. return new Promise((resolve, reject) => {
  147. const result = childProcess.spawnSync('npm', ['install', tarballPath, '--force', '--silent'], {
  148. env: { ...process.env, electron_config_cache: tempDir },
  149. cwd: tempDir,
  150. stdio: 'inherit'
  151. });
  152. if (result.status !== 0) {
  153. return reject(new Error(`npm install failed with status ${result.status}`));
  154. }
  155. try {
  156. const electronPath = require(path.resolve(tempDir, 'node_modules', rootPackageJson.name));
  157. if (typeof electronPath !== 'string') {
  158. return reject(new Error(`path to electron binary (${electronPath}) returned by the ${rootPackageJson.name} module is not a string`));
  159. }
  160. if (!fs.existsSync(electronPath)) {
  161. return reject(new Error(`path to electron binary (${electronPath}) returned by the ${rootPackageJson.name} module does not exist on disk`));
  162. }
  163. } catch (e) {
  164. console.error(e);
  165. return reject(new Error(`loading the generated ${rootPackageJson.name} module failed with an error`));
  166. }
  167. resolve(tarballPath);
  168. });
  169. })
  170. .then((tarballPath) => {
  171. const existingVersionJSON = childProcess.execSync(`npx npm@7 view ${rootPackageJson.name}@${currentElectronVersion} --json`).toString('utf-8');
  172. // It's possible this is a re-run and we already have published the package, if not we just publish like normal
  173. if (!existingVersionJSON) {
  174. childProcess.execSync(`npm publish ${tarballPath} --tag ${npmTag} --otp=${process.env.ELECTRON_NPM_OTP}`);
  175. }
  176. })
  177. .then(() => {
  178. const currentTags = JSON.parse(childProcess.execSync('npm show electron dist-tags --json').toString());
  179. const parsedLocalVersion = semver.parse(currentElectronVersion);
  180. if (rootPackageJson.name === 'electron') {
  181. // We should only customly add dist tags for non-nightly releases where the package name is still
  182. // "electron"
  183. if (parsedLocalVersion.prerelease.length === 0 &&
  184. semver.gt(currentElectronVersion, currentTags.latest)) {
  185. childProcess.execSync(`npm dist-tag add electron@${currentElectronVersion} latest --otp=${process.env.ELECTRON_NPM_OTP}`);
  186. }
  187. if (parsedLocalVersion.prerelease[0] === 'beta' &&
  188. semver.gt(currentElectronVersion, currentTags.beta)) {
  189. childProcess.execSync(`npm dist-tag add electron@${currentElectronVersion} beta --otp=${process.env.ELECTRON_NPM_OTP}`);
  190. }
  191. if (parsedLocalVersion.prerelease[0] === 'alpha' &&
  192. semver.gt(currentElectronVersion, currentTags.alpha)) {
  193. childProcess.execSync(`npm dist-tag add electron@${currentElectronVersion} alpha --otp=${process.env.ELECTRON_NPM_OTP}`);
  194. }
  195. }
  196. })
  197. .catch((err) => {
  198. console.error('Error:', err);
  199. process.exit(1);
  200. });