mirror of
https://github.com/goreleaser/goreleaser-action
synced 2026-07-08 23:37:32 +00:00
Initial commit
This commit is contained in:
+119
@@ -0,0 +1,119 @@
|
||||
'use strict';
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const url = require('url');
|
||||
const caw = require('caw');
|
||||
const contentDisposition = require('content-disposition');
|
||||
const archiveType = require('archive-type');
|
||||
const decompress = require('decompress');
|
||||
const filenamify = require('filenamify');
|
||||
const getStream = require('get-stream');
|
||||
const got = require('got');
|
||||
const makeDir = require('make-dir');
|
||||
const pify = require('pify');
|
||||
const pEvent = require('p-event');
|
||||
const fileType = require('file-type');
|
||||
const extName = require('ext-name');
|
||||
|
||||
const fsP = pify(fs);
|
||||
const filenameFromPath = res => path.basename(url.parse(res.requestUrl).pathname);
|
||||
|
||||
const getExtFromMime = res => {
|
||||
const header = res.headers['content-type'];
|
||||
|
||||
if (!header) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const exts = extName.mime(header);
|
||||
|
||||
if (exts.length !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return exts[0].ext;
|
||||
};
|
||||
|
||||
const getFilename = (res, data) => {
|
||||
const header = res.headers['content-disposition'];
|
||||
|
||||
if (header) {
|
||||
const parsed = contentDisposition.parse(header);
|
||||
|
||||
if (parsed.parameters && parsed.parameters.filename) {
|
||||
return parsed.parameters.filename;
|
||||
}
|
||||
}
|
||||
|
||||
let filename = filenameFromPath(res);
|
||||
|
||||
if (!path.extname(filename)) {
|
||||
const ext = (fileType(data) || {}).ext || getExtFromMime(res);
|
||||
|
||||
if (ext) {
|
||||
filename = `${filename}.${ext}`;
|
||||
}
|
||||
}
|
||||
|
||||
return filename;
|
||||
};
|
||||
|
||||
const getProtocolFromUri = uri => {
|
||||
let {protocol} = url.parse(uri);
|
||||
|
||||
if (protocol) {
|
||||
protocol = protocol.slice(0, -1);
|
||||
}
|
||||
|
||||
return protocol;
|
||||
};
|
||||
|
||||
module.exports = (uri, output, opts) => {
|
||||
if (typeof output === 'object') {
|
||||
opts = output;
|
||||
output = null;
|
||||
}
|
||||
|
||||
const protocol = getProtocolFromUri(uri);
|
||||
|
||||
opts = Object.assign({
|
||||
encoding: null,
|
||||
rejectUnauthorized: process.env.npm_config_strict_ssl !== 'false'
|
||||
}, opts);
|
||||
|
||||
const agent = caw(opts.proxy, {protocol});
|
||||
const stream = got.stream(uri, Object.assign({agent}, opts))
|
||||
.on('redirect', (response, nextOptions) => {
|
||||
const redirectProtocol = getProtocolFromUri(nextOptions.href);
|
||||
if (redirectProtocol && redirectProtocol !== protocol) {
|
||||
nextOptions.agent = caw(opts.proxy, {protocol: redirectProtocol});
|
||||
}
|
||||
});
|
||||
|
||||
const promise = pEvent(stream, 'response').then(res => {
|
||||
const encoding = opts.encoding === null ? 'buffer' : opts.encoding;
|
||||
return Promise.all([getStream(stream, {encoding}), res]);
|
||||
}).then(result => {
|
||||
const [data, res] = result;
|
||||
|
||||
if (!output) {
|
||||
return opts.extract && archiveType(data) ? decompress(data, opts) : data;
|
||||
}
|
||||
|
||||
const filename = opts.filename || filenamify(getFilename(res, data));
|
||||
const outputFilepath = path.join(output, filename);
|
||||
|
||||
if (opts.extract && archiveType(data)) {
|
||||
return decompress(data, path.dirname(outputFilepath), opts);
|
||||
}
|
||||
|
||||
return makeDir(path.dirname(outputFilepath))
|
||||
.then(() => fsP.writeFile(outputFilepath, data))
|
||||
.then(() => data);
|
||||
});
|
||||
|
||||
stream.then = promise.then.bind(promise);
|
||||
stream.catch = promise.catch.bind(promise);
|
||||
|
||||
return stream;
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Kevin Mårtensson <kevinmartensson@gmail.com> (github.com/kevva)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"download@7.1.0",
|
||||
"X:\\dev\\neard\\www\\github\\ghaction\\ghaction-goreleaser"
|
||||
]
|
||||
],
|
||||
"_from": "download@7.1.0",
|
||||
"_id": "download@7.1.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-xqnBTVd/E+GxJVrX5/eUJiLYjCGPwMpdL+jGhGU57BvtcA7wwhtHVbXBeUk51kOpW3S7Jn3BQbN9Q1R1Km2qDQ==",
|
||||
"_location": "/download",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "download@7.1.0",
|
||||
"name": "download",
|
||||
"escapedName": "download",
|
||||
"rawSpec": "7.1.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "7.1.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/download/-/download-7.1.0.tgz",
|
||||
"_spec": "7.1.0",
|
||||
"_where": "X:\\dev\\neard\\www\\github\\ghaction\\ghaction-goreleaser",
|
||||
"author": {
|
||||
"name": "Kevin Mårtensson",
|
||||
"email": "kevinmartensson@gmail.com",
|
||||
"url": "github.com/kevva"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/kevva/download/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"archive-type": "^4.0.0",
|
||||
"caw": "^2.0.1",
|
||||
"content-disposition": "^0.5.2",
|
||||
"decompress": "^4.2.0",
|
||||
"ext-name": "^5.0.0",
|
||||
"file-type": "^8.1.0",
|
||||
"filenamify": "^2.0.0",
|
||||
"get-stream": "^3.0.0",
|
||||
"got": "^8.3.1",
|
||||
"make-dir": "^1.2.0",
|
||||
"p-event": "^2.1.0",
|
||||
"pify": "^3.0.0"
|
||||
},
|
||||
"description": "Download and extract files",
|
||||
"devDependencies": {
|
||||
"ava": "*",
|
||||
"is-zip": "^1.0.0",
|
||||
"nock": "^9.2.5",
|
||||
"path-exists": "^3.0.0",
|
||||
"random-buffer": "^0.1.0",
|
||||
"rimraf": "^2.6.2",
|
||||
"xo": "*"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"homepage": "https://github.com/kevva/download#readme",
|
||||
"keywords": [
|
||||
"download",
|
||||
"extract",
|
||||
"http",
|
||||
"request",
|
||||
"url"
|
||||
],
|
||||
"license": "MIT",
|
||||
"name": "download",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/kevva/download.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava"
|
||||
},
|
||||
"version": "7.1.0"
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
# download [](https://travis-ci.org/kevva/download)
|
||||
|
||||
> Download and extract files
|
||||
|
||||
*See [download-cli](https://github.com/kevva/download-cli) for the command-line version.*
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install download
|
||||
```
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const fs = require('fs');
|
||||
const download = require('download');
|
||||
|
||||
download('http://unicorn.com/foo.jpg', 'dist').then(() => {
|
||||
console.log('done!');
|
||||
});
|
||||
|
||||
download('http://unicorn.com/foo.jpg').then(data => {
|
||||
fs.writeFileSync('dist/foo.jpg', data);
|
||||
});
|
||||
|
||||
download('unicorn.com/foo.jpg').pipe(fs.createWriteStream('dist/foo.jpg'));
|
||||
|
||||
Promise.all([
|
||||
'unicorn.com/foo.jpg',
|
||||
'cats.com/dancing.gif'
|
||||
].map(x => download(x, 'dist'))).then(() => {
|
||||
console.log('files downloaded!');
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
## API
|
||||
|
||||
### download(url, [destination], [options])
|
||||
|
||||
Returns both a `Promise<Buffer>` and a [Duplex stream](https://nodejs.org/api/stream.html#stream_class_stream_duplex) with [additional events](https://github.com/sindresorhus/got#streams-1).
|
||||
|
||||
#### url
|
||||
|
||||
Type: `string`
|
||||
|
||||
URL to download.
|
||||
|
||||
#### destination
|
||||
|
||||
Type: `string`
|
||||
|
||||
Path to where your file will be written.
|
||||
|
||||
#### options
|
||||
|
||||
Type: `Object`
|
||||
|
||||
Same options as [`got`](https://github.com/sindresorhus/got#options) and [`decompress`](https://github.com/kevva/decompress#options) in addition to the ones below.
|
||||
|
||||
##### extract
|
||||
|
||||
Type: `boolean`<br>
|
||||
Default: `false`
|
||||
|
||||
If set to `true`, try extracting the file using [`decompress`](https://github.com/kevva/decompress).
|
||||
|
||||
##### filename
|
||||
|
||||
Type: `string`
|
||||
|
||||
Name of the saved file.
|
||||
|
||||
##### proxy
|
||||
|
||||
Type: `string`
|
||||
|
||||
Proxy endpoint.
|
||||
|
||||
|
||||
## License
|
||||
|
||||
MIT © [Kevin Mårtensson](https://github.com/kevva)
|
||||
Reference in New Issue
Block a user