This commit is contained in:
Kevin Sawicki 2016-12-22 10:16:59 -08:00
Коммит 1d4f9586ef
11 изменённых файлов: 267 добавлений и 0 удалений

3
.gitignore поставляемый Normal file
Просмотреть файл

@ -0,0 +1,3 @@
node_modules
bin
npm-debug.log

5
.npmignore Normal file
Просмотреть файл

@ -0,0 +1,5 @@
.npmignore
.travis.yml
appveyor.yml
test
bin

17
.travis.yml Normal file
Просмотреть файл

@ -0,0 +1,17 @@
language: node_js
node_js:
- "6"
branches:
only:
- master
cache:
directories:
- node_modules
notifications:
email:
on_success: never
on_failure: change

36
README.md Normal file
Просмотреть файл

@ -0,0 +1,36 @@
# Electron Mksnapshot
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
[![devDependencies:?](https://img.shields.io/david/electron/mksnapshot.svg)](https://david-dm.org/electron/mksnapshot)
<br>
[![license:mit](https://img.shields.io/badge/license-mit-blue.svg)](https://opensource.org/licenses/MIT)
[![npm:](https://img.shields.io/npm/v/electron-mksnapshot.svg)](https://www.npmjs.com/packages/electron-mksnapshot)
[![dependencies:?](https://img.shields.io/npm/dm/electron-mksnapshot.svg)](https://www.npmjs.com/packages/electron-mksnapshot)
Simple node module to download the `mksnapshot` binaries compatible with
Electron for creating v8 snapshots.
This minor version of this library tracks the minor version of the Electron
versions released. So if you are using Electron `1.0.x` you would want to use
an `electron-mksnapshot` dependency of `~1.0.0` in your `package.json` file.
## Using
```sh
npm install --save-dev electron-mksnapshot
mksnapshot --help
```
## Custom Mirror
You can set the `ELECTRON_MIRROR` or [`NPM_CONFIG_ELECTRON_MIRROR`](https://docs.npmjs.com/misc/config#environment-variables)
environment variables to use a custom base URL for downloading ChromeDriver zips.
```sh
# Electron mirror for China
ELECTRON_MIRROR="https://npm.taobao.org/mirrors/electron/"
# Local mirror
# Example of requested URL: http://localhost:8080/1.2.0/mksnapshot-v1.2.0-darwin-x64.zip
ELECTRON_MIRROR="http://localhost:8080/"
```

22
appveyor.yml Normal file
Просмотреть файл

@ -0,0 +1,22 @@
build: off
skip_tags: true
branches:
only:
- master
environment:
nodejs_version: "6"
cache:
- node_modules -> package.json
install:
- ps: Install-Product node $env:nodejs_version
- npm install
test_script:
- node --version
- npm --version
- npm test

61
download-mksnapshot.js Normal file
Просмотреть файл

@ -0,0 +1,61 @@
var Decompress = require('decompress')
var fs = require('fs')
var mkdirp = require('mkdirp')
var path = require('path')
var request = require('request')
var versionSegments = require('./package').version.split('.')
var baseUrl = process.env.NPM_CONFIG_ELECTRON_MIRROR ||
process.env.npm_config_electron_mirror ||
process.env.ELECTRON_MIRROR ||
process.env.electron_mirror ||
'https://github.com/electron/electron/releases/download/v'
var proxy = process.env.NPM_CONFIG_HTTPS_PROXY ||
process.env.npm_config_https_proxy ||
process.env.NPM_CONFIG_PROXY ||
process.env.npm_config_proxy
var config = {
baseUrl: baseUrl,
// Sync minor version of package to minor version of Electron release
electron: versionSegments[0] + '.' + versionSegments[1] + '.0',
outputPath: path.join(__dirname, 'bin'),
proxy: proxy
}
function handleError (url, error) {
if (!error) return
var message = error.message || error
console.error('Downloading ' + url + ' failed: ' + message)
process.exit(1)
}
function unzip (zipped, callback) {
var decompress = new Decompress()
decompress.src(zipped)
decompress.dest(config.outputPath)
decompress.use(Decompress.zip())
decompress.run(callback)
}
mkdirp(config.outputPath, function (error) {
var fileName = 'mksnapshot-v' + config.electron + '-' + process.platform + '-' + process.arch + '.zip'
var fullUrl = config.baseUrl + config.electron + '/' + fileName
if (error) return handleError(fullUrl, error)
request.get({uri: fullUrl, encoding: null, proxy: config.proxy}, function (error, response, body) {
if (error) return handleError(fullUrl, error)
if (response.statusCode !== 200) return handleError(fullUrl, Error('Non-200 response (' + response.statusCode + ')'))
unzip(body, function (error) {
if (error) return handleError(fullUrl, error)
if (process.platform !== 'win32') {
fs.chmod(path.join(__dirname, 'bin', 'mksnapshot'), '755', function (error) {
if (error) return handleError(fullUrl, error)
})
}
})
})
})

30
mksnapshot.js Executable file
Просмотреть файл

@ -0,0 +1,30 @@
#!/usr/bin/env node
var ChildProcess = require('child_process')
var path = require('path')
var command = path.join(__dirname, 'bin', 'mksnapshot')
var args = process.argv.slice(2)
var options = {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit'
}
var mksnapshotProcess = ChildProcess.spawn(command, args, options)
mksnapshotProcess.on('exit', function (code, signal) {
if (code == null && signal === 'SIGILL') {
code = 1
}
process.exit(code)
})
var killMksnapshot = function () {
try {
mksnapshotProcess.kill()
} catch (ignored) {
}
}
process.on('exit', killMksnapshot)
process.on('SIGTERM', killMksnapshot)

29
package.json Normal file
Просмотреть файл

@ -0,0 +1,29 @@
{
"name": "electron-mksnapshot",
"version": "1.4.0",
"description": "Electron version of the mksnapshot binary",
"repository": "https://github.com/electron/mksnapshot",
"bin": {
"mksnapshot": "./mksnapshot.js"
},
"scripts": {
"install": "node ./download-mksnapshot.js",
"test": "mocha && standard"
},
"license": "MIT",
"dependencies": {
"decompress": "^3.0.0",
"mkdirp": "^0.5.1",
"request": "^2.65.0"
},
"devDependencies": {
"mocha": "^2.3.3",
"standard": "^5.3.1",
"temp": "^0.8.3"
},
"standard": {
"ignore": [
"test/fixtures"
]
}
}

1
test/fixtures/invalid.js поставляемый Normal file
Просмотреть файл

@ -0,0 +1 @@
1}2{3

3
test/fixtures/snapshot.js поставляемый Normal file
Просмотреть файл

@ -0,0 +1,3 @@
var foo = function () {
return 'bar'
}

60
test/mksnapshot-test.js Normal file
Просмотреть файл

@ -0,0 +1,60 @@
var assert = require('assert')
var ChildProcess = require('child_process')
var fs = require('fs')
var path = require('path')
var temp = require('temp').track()
var describe = global.describe
var it = global.it
describe('mksnapshot binary', function () {
this.timeout(10000)
it('creates a snapshot for a valid file', function (done) {
var outputFile = path.join(temp.mkdirSync('mksnapshot-'), 'snapshot_blob.bin')
var args = [
path.join(__dirname, '..', 'mksnapshot.js'),
path.join(__dirname, 'fixtures', 'snapshot.js'),
'--startup_blob',
outputFile
]
var mksnapshot = ChildProcess.spawn(process.execPath, args)
var output = ''
mksnapshot.stdout.on('data', function (data) { output += data })
mksnapshot.stderr.on('data', function (data) { output += data })
mksnapshot.on('close', function (code) {
assert.equal(code, 0)
assert.equal(output.indexOf('Loading script for embedding'), 0, output)
assert.equal(fs.existsSync(outputFile), true)
done()
})
mksnapshot.on('error', done)
})
it('fails for invalid JavaScript files', function (done) {
var outputFile = path.join(temp.mkdirSync('mksnapshot-'), 'snapshot_blob.bin')
var args = [
path.join(__dirname, '..', 'mksnapshot.js'),
path.join(__dirname, 'fixtures', 'invalid.js'),
'--startup_blob',
outputFile
]
var mksnapshot = ChildProcess.spawn(process.execPath, args)
var output = ''
mksnapshot.stdout.on('data', function (data) { output += data })
mksnapshot.stderr.on('data', function (data) { output += data })
mksnapshot.on('close', function (code) {
assert.equal(code, 1)
assert.notEqual(output.indexOf('Fatal error'), -1, output)
assert.equal(fs.existsSync(outputFile), true)
done()
})
mksnapshot.on('error', done)
})
})