diff --git a/.env.example b/.env.example new file mode 100644 index 000000000000..92d3ac2a8717 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +# .env.example committed to repo +GITHUB_TOKEN= diff --git a/.gitignore b/.gitignore index f2dfe5633ba8..93753bed5768 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ node_modules/ /brightray/brightray.suo /brightray/brightray.v12.suo /brightray/brightray.xcodeproj/ +.env diff --git a/npm/package.json b/npm/package.json index 2f9375f022a2..b34c1751700e 100644 --- a/npm/package.json +++ b/npm/package.json @@ -21,10 +21,7 @@ "standard": "^5.4.1", "tape": "^3.0.1" }, - "author": "Mathias Buus", - "license": "MIT", "directories": { "test": "test" - }, - "keywords": ["electron"] + } } diff --git a/package.json b/package.json index 2850b84d27a6..c3a4547881e0 100644 --- a/package.json +++ b/package.json @@ -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" + ] } diff --git a/script/publish-to-npm.js b/script/publish-to-npm.js new file mode 100644 index 000000000000..5066a9fff1bb --- /dev/null +++ b/script/publish-to-npm.js @@ -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}`))