publish-to-npm.js 8.1 KB

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