2017-08-01 18:50:57 +03:00
|
|
|
const temp = require('temp')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const childProcess = require('child_process')
|
|
|
|
const GitHubApi = require('github')
|
2018-12-04 00:28:10 +03:00
|
|
|
const { getCurrentBranch } = require('./lib/utils.js')
|
2017-08-01 18:50:57 +03:00
|
|
|
const request = require('request')
|
2018-11-01 04:27:35 +03:00
|
|
|
const semver = require('semver')
|
2017-08-01 18:50:57 +03:00
|
|
|
const rootPackageJson = require('../package.json')
|
2017-07-14 00:09:21 +03:00
|
|
|
|
2018-08-17 03:08:52 +03:00
|
|
|
if (!process.env.ELECTRON_NPM_OTP) {
|
|
|
|
console.error('Please set ELECTRON_NPM_OTP')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2017-07-14 00:09:21 +03:00
|
|
|
const github = new GitHubApi({
|
|
|
|
// debug: true,
|
|
|
|
headers: { 'User-Agent': 'electron-npm-publisher' },
|
2017-08-01 18:50:57 +03:00
|
|
|
followRedirects: false
|
2017-07-14 00:09:21 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
let tempDir
|
2018-09-13 19:10:51 +03:00
|
|
|
temp.track() // track and cleanup files at exit
|
2017-07-14 00:09:21 +03:00
|
|
|
|
|
|
|
const files = [
|
|
|
|
'cli.js',
|
|
|
|
'index.js',
|
|
|
|
'install.js',
|
|
|
|
'package.json',
|
2018-01-24 02:09:08 +03:00
|
|
|
'README.md',
|
|
|
|
'LICENSE'
|
2017-07-14 00:09:21 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
const jsonFields = [
|
|
|
|
'name',
|
|
|
|
'version',
|
|
|
|
'repository',
|
|
|
|
'description',
|
|
|
|
'license',
|
|
|
|
'author',
|
|
|
|
'keywords'
|
|
|
|
]
|
|
|
|
|
2017-08-01 23:08:30 +03:00
|
|
|
let npmTag = ''
|
|
|
|
|
2017-07-14 00:09:21 +03:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
temp.mkdir('electron-npm', (err, dirPath) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
} else {
|
|
|
|
resolve(dirPath)
|
|
|
|
}
|
|
|
|
})
|
2017-08-01 07:50:40 +03:00
|
|
|
})
|
2018-09-13 19:10:51 +03:00
|
|
|
.then((dirPath) => {
|
|
|
|
tempDir = dirPath
|
|
|
|
// copy files from `/npm` to temp directory
|
|
|
|
files.forEach((name) => {
|
|
|
|
const noThirdSegment = name === 'README.md' || name === 'LICENSE'
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(tempDir, name),
|
|
|
|
fs.readFileSync(path.join(__dirname, '..', noThirdSegment ? '' : 'npm', name))
|
|
|
|
)
|
|
|
|
})
|
|
|
|
// copy from root package.json to temp/package.json
|
|
|
|
const packageJson = require(path.join(tempDir, 'package.json'))
|
|
|
|
jsonFields.forEach((fieldName) => {
|
|
|
|
packageJson[fieldName] = rootPackageJson[fieldName]
|
|
|
|
})
|
2017-07-14 00:09:21 +03:00
|
|
|
fs.writeFileSync(
|
2018-09-13 19:10:51 +03:00
|
|
|
path.join(tempDir, 'package.json'),
|
|
|
|
JSON.stringify(packageJson, null, 2)
|
2017-07-14 00:09:21 +03:00
|
|
|
)
|
|
|
|
|
2018-09-13 19:10:51 +03:00
|
|
|
return github.repos.getReleases({
|
|
|
|
owner: 'electron',
|
|
|
|
repo: rootPackageJson.version.indexOf('nightly') > 0 ? 'nightlies' : 'electron'
|
|
|
|
})
|
2017-07-14 00:09:21 +03:00
|
|
|
})
|
2018-09-13 19:10:51 +03:00
|
|
|
.then((releases) => {
|
2017-08-01 23:08:30 +03:00
|
|
|
// download electron.d.ts from release
|
2018-09-13 19:10:51 +03:00
|
|
|
const release = releases.data.find(
|
|
|
|
(release) => release.tag_name === `v${rootPackageJson.version}`
|
|
|
|
)
|
|
|
|
if (!release) {
|
|
|
|
throw new Error(`cannot find release with tag v${rootPackageJson.version}`)
|
|
|
|
}
|
|
|
|
return release
|
|
|
|
})
|
|
|
|
.then((release) => {
|
|
|
|
const tsdAsset = release.assets.find((asset) => asset.name === 'electron.d.ts')
|
|
|
|
if (!tsdAsset) {
|
|
|
|
throw new Error(`cannot find electron.d.ts from v${rootPackageJson.version} release assets`)
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request.get({
|
|
|
|
url: tsdAsset.url,
|
|
|
|
headers: {
|
|
|
|
'accept': 'application/octet-stream',
|
|
|
|
'user-agent': 'electron-npm-publisher'
|
|
|
|
}
|
|
|
|
}, (err, response, body) => {
|
|
|
|
if (err || response.statusCode !== 200) {
|
|
|
|
reject(err || new Error('Cannot download electron.d.ts'))
|
|
|
|
} else {
|
|
|
|
fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), body)
|
|
|
|
resolve(release)
|
|
|
|
}
|
|
|
|
})
|
2017-07-14 00:09:21 +03:00
|
|
|
})
|
|
|
|
})
|
2018-09-13 19:10:51 +03:00
|
|
|
.then(async (release) => {
|
2018-11-01 04:27:35 +03:00
|
|
|
const currentBranch = await getCurrentBranch()
|
|
|
|
|
2018-09-13 19:10:51 +03:00
|
|
|
if (release.tag_name.indexOf('nightly') > 0) {
|
|
|
|
if (currentBranch === 'master') {
|
2019-01-02 22:04:56 +03:00
|
|
|
// Nightlies get published to their own module, so master nightlies should be tagged as latest
|
|
|
|
npmTag = 'latest'
|
2018-09-13 19:10:51 +03:00
|
|
|
} else {
|
|
|
|
npmTag = `nightly-${currentBranch}`
|
|
|
|
}
|
2019-01-02 22:04:56 +03:00
|
|
|
|
2019-01-08 07:37:44 +03:00
|
|
|
const currentJson = JSON.parse(fs.readFileSync(path.join(tempDir, 'package.json'), 'utf8'))
|
2019-01-05 00:48:02 +03:00
|
|
|
currentJson.name = 'electron-nightly'
|
|
|
|
rootPackageJson.name = 'electron-nightly'
|
2019-01-02 22:04:56 +03:00
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(tempDir, 'package.json'),
|
|
|
|
JSON.stringify(currentJson, null, 2)
|
|
|
|
)
|
2018-08-18 04:30:21 +03:00
|
|
|
} else {
|
2018-11-01 04:27:35 +03:00
|
|
|
if (currentBranch === 'master') {
|
|
|
|
// This should never happen, master releases should be nightly releases
|
|
|
|
// this is here just-in-case
|
|
|
|
npmTag = 'master'
|
|
|
|
} else if (!release.prerelease) {
|
|
|
|
// Tag the release with a `2-0-x` style tag
|
|
|
|
npmTag = currentBranch
|
|
|
|
} else {
|
|
|
|
// Tag the release with a `beta-3-0-x` style tag
|
|
|
|
npmTag = `beta-${currentBranch}`
|
|
|
|
}
|
2018-08-18 04:30:21 +03:00
|
|
|
}
|
2018-09-13 19:10:51 +03:00
|
|
|
})
|
|
|
|
.then(() => childProcess.execSync('npm pack', { cwd: tempDir }))
|
|
|
|
.then(() => {
|
2017-08-01 23:08:30 +03:00
|
|
|
// test that the package can install electron prebuilt from github release
|
2018-09-13 19:10:51 +03:00
|
|
|
const tarballPath = path.join(tempDir, `${rootPackageJson.name}-${rootPackageJson.version}.tgz`)
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
childProcess.execSync(`npm install ${tarballPath} --force --silent`, {
|
|
|
|
env: Object.assign({}, process.env, { electron_config_cache: tempDir }),
|
|
|
|
cwd: tempDir
|
|
|
|
})
|
|
|
|
resolve(tarballPath)
|
2017-08-01 07:50:40 +03:00
|
|
|
})
|
|
|
|
})
|
2018-09-13 19:10:51 +03:00
|
|
|
.then((tarballPath) => childProcess.execSync(`npm publish ${tarballPath} --tag ${npmTag} --otp=${process.env.ELECTRON_NPM_OTP}`))
|
2018-11-01 04:27:35 +03:00
|
|
|
.then(() => {
|
|
|
|
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 22:04:56 +03: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)) {
|
|
|
|
childProcess.execSync(`npm dist-tag add electron@${localVersion} latest --otp=${process.env.ELECTRON_NPM_OTP}`)
|
|
|
|
}
|
|
|
|
if (parsedLocalVersion.prerelease[0] === 'beta' &&
|
|
|
|
semver.gt(localVersion, currentTags.beta)) {
|
|
|
|
childProcess.execSync(`npm dist-tag add electron@${localVersion} beta --otp=${process.env.ELECTRON_NPM_OTP}`)
|
|
|
|
}
|
2018-11-01 04:27:35 +03:00
|
|
|
}
|
|
|
|
})
|
2018-09-13 19:10:51 +03:00
|
|
|
.catch((err) => {
|
|
|
|
console.error(`Error: ${err}`)
|
|
|
|
process.exit(1)
|
|
|
|
})
|