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')
|
|
|
|
const request = require('request')
|
|
|
|
const assert = require('assert')
|
|
|
|
const rootPackageJson = require('../package.json')
|
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
|
|
|
|
temp.track() // track and cleanup files at exit
|
|
|
|
|
|
|
|
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
|
|
|
})
|
|
|
|
.then((dirPath) => {
|
2017-07-14 00:09:21 +03:00
|
|
|
tempDir = dirPath
|
|
|
|
// copy files from `/npm` to temp directory
|
|
|
|
files.forEach((name) => {
|
2018-02-12 19:01:50 +03:00
|
|
|
const noThirdSegment = name === 'README.md' || name === 'LICENSE'
|
2017-07-14 00:09:21 +03:00
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(tempDir, name),
|
2018-01-24 02:09:08 +03:00
|
|
|
fs.readFileSync(path.join(__dirname, '..', noThirdSegment ? '' : 'npm', name))
|
2017-07-14 00:09:21 +03:00
|
|
|
)
|
|
|
|
})
|
|
|
|
// copy from root package.json to temp/package.json
|
|
|
|
const packageJson = require(path.join(tempDir, 'package.json'))
|
|
|
|
jsonFields.forEach((fieldName) => {
|
|
|
|
packageJson[fieldName] = rootPackageJson[fieldName]
|
|
|
|
})
|
|
|
|
fs.writeFileSync(
|
|
|
|
path.join(tempDir, 'package.json'),
|
|
|
|
JSON.stringify(packageJson, null, 2)
|
|
|
|
)
|
|
|
|
|
|
|
|
return github.repos.getReleases({
|
|
|
|
owner: 'electron',
|
2017-08-01 18:50:57 +03:00
|
|
|
repo: 'electron'
|
2017-07-14 00:09:21 +03:00
|
|
|
})
|
2017-08-01 07:50:40 +03:00
|
|
|
})
|
|
|
|
.then((releases) => {
|
2017-08-01 23:08:30 +03:00
|
|
|
// download electron.d.ts from release
|
|
|
|
const release = releases.data.find(
|
|
|
|
(release) => release.tag_name === `v${rootPackageJson.version}`
|
2017-07-14 00:09:21 +03:00
|
|
|
)
|
2017-08-01 23:08:30 +03:00
|
|
|
if (!release) {
|
2017-08-01 18:50:57 +03:00
|
|
|
throw new Error(`cannot find release with tag v${rootPackageJson.version}`)
|
2017-07-14 00:09:21 +03:00
|
|
|
}
|
2017-08-01 23:08:30 +03:00
|
|
|
return release
|
2017-08-01 07:50:40 +03:00
|
|
|
})
|
2017-08-01 23:08:30 +03:00
|
|
|
.then((release) => {
|
|
|
|
const tsdAsset = release.assets.find((asset) => asset.name === 'electron.d.ts')
|
2017-07-14 00:09:21 +03:00
|
|
|
if (!tsdAsset) {
|
2017-08-01 23:08:30 +03:00
|
|
|
throw new Error(`cannot find electron.d.ts from v${rootPackageJson.version} release assets`)
|
2017-07-14 00:09:21 +03:00
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
request.get({
|
|
|
|
url: tsdAsset.url,
|
|
|
|
headers: {
|
|
|
|
'accept': 'application/octet-stream',
|
2017-08-01 23:10:29 +03:00
|
|
|
'user-agent': 'electron-npm-publisher'
|
2017-07-14 00:09:21 +03:00
|
|
|
}
|
|
|
|
}, (err, response, body) => {
|
2017-08-02 04:06:50 +03:00
|
|
|
if (err || response.statusCode !== 200) {
|
|
|
|
reject(err || new Error('Cannot download electron.d.ts'))
|
2017-07-14 00:09:21 +03:00
|
|
|
} else {
|
|
|
|
fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), body)
|
2017-08-01 23:08:30 +03:00
|
|
|
resolve(release)
|
2017-07-14 00:09:21 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2017-08-01 23:08:30 +03:00
|
|
|
.then((release) => {
|
|
|
|
npmTag = release.prerelease ? 'beta' : 'latest'
|
|
|
|
})
|
2017-08-01 07:50:40 +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
|
2017-08-02 04:06:50 +03:00
|
|
|
const tarballPath = path.join(tempDir, `${rootPackageJson.name}-${rootPackageJson.version}.tgz`)
|
2017-08-01 07:50:40 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
childProcess.execSync(`npm install ${tarballPath} --force --silent`, {
|
2017-08-01 23:08:30 +03:00
|
|
|
env: Object.assign({}, process.env, { electron_config_cache: tempDir }),
|
2017-08-01 18:50:57 +03:00
|
|
|
cwd: tempDir
|
2017-08-01 07:50:40 +03:00
|
|
|
})
|
2017-08-01 23:08:30 +03:00
|
|
|
const checkVersion = childProcess.execSync(`${path.join(tempDir, 'node_modules', '.bin', 'electron')} -v`)
|
2017-10-23 18:02:50 +03:00
|
|
|
assert.ok((`v${rootPackageJson.version}`.indexOf(checkVersion.toString().trim()) === 0), `Version is correct`)
|
2017-08-01 23:08:30 +03:00
|
|
|
resolve(tarballPath)
|
2017-08-01 07:50:40 +03:00
|
|
|
})
|
|
|
|
})
|
2017-08-01 23:08:30 +03:00
|
|
|
.then((tarballPath) => childProcess.execSync(`npm publish ${tarballPath} --tag ${npmTag}`))
|
2017-08-02 04:06:50 +03:00
|
|
|
.catch((err) => {
|
|
|
|
console.error(`Error: ${err}`)
|
|
|
|
process.exit(1)
|
|
|
|
})
|