script to publish electron to npm directly

This commit is contained in:
Vanessa Yuen 2017-07-13 17:09:21 -04:00
parent 5dfee2a23c
commit 663710e8eb
5 changed files with 128 additions and 6 deletions

2
.env.example Normal file
View file

@ -0,0 +1,2 @@
# .env.example committed to repo
GITHUB_TOKEN=

1
.gitignore vendored
View file

@ -38,3 +38,4 @@ node_modules/
/brightray/brightray.suo
/brightray/brightray.v12.suo
/brightray/brightray.xcodeproj/
.env

View file

@ -21,10 +21,7 @@
"standard": "^5.4.1",
"tape": "^3.0.1"
},
"author": "Mathias Buus",
"license": "MIT",
"directories": {
"test": "test"
},
"keywords": ["electron"]
}
}

View file

@ -1,15 +1,20 @@
{
"name": "electron",
"version": "1.7.4",
"repository": "https://github.com/electron/electron",
"description": "Build cross platform desktop apps with JavaScript, HTML, and CSS",
"devDependencies": {
"asar": "^0.11.0",
"browserify": "^13.1.0",
"dotenv-safe": "^4.0.4",
"electabul": "~0.0.4",
"electron-docs-linter": "^2.3.3",
"electron-typescript-definitions": "^1.2.7",
"github": "^9.2.0",
"request": "^2.68.0",
"standard": "^8.4.0",
"standard-markdown": "^4.0.0"
"standard-markdown": "^4.0.0",
"temp": "^0.8.3"
},
"standard": {
"ignore": [
@ -41,9 +46,15 @@
"create-api-json": "electron-docs-linter docs --outfile=out/electron-api.json --version=$npm_package_version",
"create-typescript-definitions": "npm run create-api-json && electron-typescript-definitions --in=out/electron-api.json --out=out/electron.d.ts",
"preinstall": "node -e 'process.exit(0)'",
"publish-to-npm": "node ./script/publish-to-npm.js",
"release": "./script/upload.py -p",
"repl": "python ./script/start.py --interactive",
"start": "python ./script/start.py",
"test": "python ./script/test.py"
}
},
"license": "MIT",
"author": "Electron Community",
"keywords": [
"electron"
]
}

111
script/publish-to-npm.js Normal file
View file

@ -0,0 +1,111 @@
require('dotenv-safe').load()
const temp = require('temp'),
fs = require('fs'),
util = require('util'),
path = require('path'),
childProcess = require('child_process'),
GitHubApi = require('github'),
request = require('request'),
rootPackageJson = require('../package.json')
const github = new GitHubApi({
// debug: true,
headers: { 'User-Agent': 'electron-npm-publisher' },
followRedirects: false,
})
github.authenticate({
type: 'token',
token: process.env.GITHUB_TOKEN
})
let tempDir
temp.track() // track and cleanup files at exit
const files = [
'cli.js',
'index.js',
'install.js',
'package.json',
'README.md'
]
const jsonFields = [
'name',
'version',
'repository',
'description',
'license',
'author',
'keywords'
]
new Promise((resolve, reject) => {
temp.mkdir('electron-npm', (err, dirPath) => {
if (err) {
reject(err)
} else {
resolve(dirPath)
}
})
}).then((dirPath) => {
tempDir = dirPath
// copy files from `/npm` to temp directory
files.forEach((name) => {
fs.writeFileSync(
path.join(tempDir, name),
fs.readFileSync(path.join(__dirname, '..', '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]
})
fs.writeFileSync(
path.join(tempDir, 'package.json'),
JSON.stringify(packageJson, null, 2)
)
return github.repos.getReleases({
owner: 'electron',
repo: 'electron',
})
}).then((releases) => {
// download electron.d.ts from draft release
const draftRelease = releases.data.find(
// (release) => release.draft && release.tag_name === `v${rootPackageJson.version}`
(release) => release.draft && release.tag_name === `test`
)
if (!draftRelease) {
throw `cannot find release with tag v${rootPackageJson.version}`
}
return draftRelease.assets.find((asset) => asset.name === 'electron.d.ts')
}).then((tsdAsset) => {
if (!tsdAsset) {
throw 'cannot find electron.d.ts from draft release assets'
}
return new Promise((resolve, reject) => {
request.get({
url: tsdAsset.url,
headers: {
'accept': 'application/octet-stream',
'user-agent': 'electron-npm-publisher',
Authorization: `token ${process.env.GITHUB_TOKEN}`
}
}, (err, response, body) => {
if (err) {
reject(err)
} else {
fs.writeFileSync(path.join(tempDir, 'electron.d.ts'), body)
resolve()
}
})
})
})
.then(() => childProcess.execSync('npm publish', { cwd: tempDir }))
.catch((err) => console.error(`Error: ${err}`))