asset-pipe-client
Advanced tools
Comparing version 1.0.0-beta.4 to 1.0.0
@@ -8,4 +8,3 @@ #!/usr/bin/env node | ||
program | ||
.version(pckage.version); | ||
program.version(pckage.version); | ||
@@ -17,3 +16,2 @@ /* | ||
program | ||
@@ -27,3 +25,2 @@ .command('write') | ||
program.on('--help', () => { | ||
@@ -33,3 +30,2 @@ console.log(' Help here:'); | ||
if (!process.argv.slice(2).length) { | ||
@@ -36,0 +32,0 @@ program.outputHelp(); |
@@ -7,4 +7,3 @@ 'use strict'; | ||
module.exports.js = (options) => { | ||
module.exports.js = options => { | ||
if (options.source && options.destination) { | ||
@@ -21,5 +20,4 @@ const writeStream = fs.createWriteStream(options.destination); | ||
module.exports.help = () => { | ||
console.log(' Examples here:'); | ||
}; |
186
lib/main.js
@@ -6,9 +6,55 @@ 'use strict'; | ||
const request = require('request'); | ||
const Writer = require('asset-pipe-js-writer'); | ||
const JsWriter = require('asset-pipe-js-writer'); | ||
const CssWriter = require('asset-pipe-css-writer'); | ||
const assert = require('assert'); | ||
const { extname } = require('path'); | ||
const isStream = require('is-stream'); | ||
const extensions = Object.freeze({ | ||
JS: '.js', | ||
CSS: '.css', | ||
}); | ||
const endpoints = Object.freeze({ | ||
UPLOAD: 'feed', | ||
BUNDLE: 'bundle', | ||
}); | ||
function post(options) { | ||
const opts = { | ||
url: options.url, | ||
timeout: 5000, | ||
headers: { | ||
'content-type': 'application/json', | ||
accept: 'application/json', | ||
}, | ||
}; | ||
if (!isStream(options.body)) { | ||
opts.body = options.body; | ||
} | ||
return new Promise((resolve, reject) => { | ||
const req = request.post(opts, (error, response, body) => { | ||
if (error) return reject(error); | ||
try { | ||
body = JSON.parse(body); | ||
} catch (err) {} | ||
resolve({ response, body }); | ||
}); | ||
if (isStream(options.body)) { | ||
options.body.pipe(req); | ||
} | ||
}); | ||
} | ||
module.exports = class Client { | ||
constructor (options = {}) { | ||
this.options = Object.assign({ | ||
buildServerUri: 'http://127.0.0.1:7100', | ||
}, options); | ||
constructor(options = {}) { | ||
this.options = Object.assign( | ||
{ | ||
buildServerUri: 'http://127.0.0.1:7100', | ||
}, | ||
options | ||
); | ||
@@ -19,3 +65,3 @@ this.transforms = []; | ||
transform (transform, options) { | ||
transform(transform, options) { | ||
this.transforms.push({ | ||
@@ -27,3 +73,3 @@ transform, | ||
plugin (plugin, options) { | ||
plugin(plugin, options) { | ||
this.plugins.push({ | ||
@@ -35,3 +81,27 @@ plugin, | ||
uploadJsFeed(files, options) { | ||
const writer = new JsWriter(files, options, false, true); | ||
this.transforms.forEach(entry => { | ||
writer.transform(entry.transform, entry.options); | ||
}); | ||
this.plugins.forEach(entry => { | ||
writer.plugin(entry.plugin, entry.options); | ||
}); | ||
return post({ | ||
url: url.resolve(this.options.buildServerUri, endpoints.UPLOAD), | ||
body: writer.bundle().pipe(JSONStream.stringify()), | ||
}); | ||
} | ||
uploadCssFeed(files) { | ||
const writer = new CssWriter(files); | ||
return post({ | ||
url: url.resolve(this.options.buildServerUri, endpoints.UPLOAD), | ||
body: writer.pipe(JSONStream.stringify()), | ||
}); | ||
} | ||
/** | ||
@@ -41,36 +111,45 @@ * Upload asset feed to asset server | ||
uploadFeed (files = [], options = {}) { | ||
return new Promise((resolve, reject) => { | ||
const writer = new Writer(files, options, false, true); | ||
async uploadFeed(files, options = {}) { | ||
assert( | ||
Array.isArray(files), | ||
`Expected 'files' to be an array, instead got ${files}` | ||
); | ||
assert( | ||
files.length, | ||
`Expected 'files' array to contain at least 1 item` | ||
); | ||
assert( | ||
files.every(file => typeof file == 'string'), | ||
`Expected each item in array 'files' to be a string, got ${files}` | ||
); | ||
assert( | ||
files.every(file => file.includes('.js')) || | ||
files.every(file => file.includes('.css')), | ||
`Expected ALL items in array 'files' to end with .js | ||
or ALL items in array 'files' to end with .css, got ${files}` | ||
); | ||
this.transforms.forEach((entry) => { | ||
writer.transform(entry.transform, entry.options); | ||
}); | ||
let upload; | ||
this.plugins.forEach((entry) => { | ||
writer.plugin(entry.plugin, entry.options); | ||
}); | ||
switch (extname(files[0])) { | ||
case extensions.CSS: | ||
upload = await this.uploadCssFeed(files); | ||
break; | ||
case extensions.JS: | ||
upload = await this.uploadJsFeed(files, options); | ||
break; | ||
} | ||
writer | ||
.bundle() | ||
.pipe(JSONStream.stringify()) | ||
.pipe(request.post({ | ||
url: url.resolve(this.options.buildServerUri, 'feed'), | ||
json: true, | ||
}, (error, response, body) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
if (response.statusCode === 200) { | ||
return resolve(body); | ||
} | ||
if (response.statusCode === 400) { | ||
return reject(new Error(body.message)); | ||
} | ||
reject(new Error(`Asset build server responded with unknown error. Http status ${response.statusCode}`)); | ||
})); | ||
}); | ||
const { response, body } = upload; | ||
if (response.statusCode === 200) { | ||
return Promise.resolve(body); | ||
} | ||
if (response.statusCode === 400) { | ||
throw new Error(body.message); | ||
} | ||
throw new Error( | ||
`Asset build server responded with unknown error. Http status ${response.statusCode}` | ||
); | ||
} | ||
/** | ||
@@ -80,25 +159,18 @@ * Make a bundle out of asset feeds on the asset server | ||
createRemoteBundle (sources) { | ||
return new Promise((resolve, reject) => { | ||
request.post({ | ||
url: url.resolve(this.options.buildServerUri, 'bundle'), | ||
body: sources, | ||
json: true, | ||
}, (error, response, body) => { | ||
if (error) { | ||
return reject(error); | ||
} | ||
if (response.statusCode === 200) { | ||
return resolve(body); | ||
} | ||
if (response.statusCode === 202) { | ||
return resolve(body); | ||
} | ||
if (response.statusCode === 400) { | ||
return reject(new Error(body.message)); | ||
} | ||
reject(new Error(`Asset build server responded with unknown error. Http status ${response.statusCode}`)); | ||
}); | ||
async createRemoteBundle(sources) { | ||
const { response, body } = await post({ | ||
url: url.resolve(this.options.buildServerUri, endpoints.BUNDLE), | ||
body: JSON.stringify(sources), | ||
}); | ||
if ([200, 202].includes(response.statusCode)) { | ||
return Promise.resolve(body); | ||
} | ||
if (response.statusCode === 400) { | ||
throw new Error(body.message); | ||
} | ||
throw new Error( | ||
`Asset build server responded with unknown error. Http status ${response.statusCode}` | ||
); | ||
} | ||
}; |
@@ -1,49 +0,1 @@ | ||
{ | ||
"name": "asset-pipe-client", | ||
"version": "1.0.0-beta.4", | ||
"author": { | ||
"name": "Trygve Lie", | ||
"email": "post@trygve-lie.com" | ||
}, | ||
"files": [ | ||
"bin", | ||
"lib" | ||
], | ||
"description": "Asset pipe client", | ||
"main": "./lib/main.js", | ||
"bin": "./bin/cli.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:asset-pipe/asset-pipe-cli.git" | ||
}, | ||
"keywords": [ | ||
"assets" | ||
], | ||
"contributors": [ | ||
{ | ||
"name": "Trygve Lie", | ||
"email": "post@trygve-lie.com" | ||
} | ||
], | ||
"bugs": { | ||
"url": "https://github.com/asset-pipe/asset-pipe-cli/issues" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"JSONStream": "^1.3.0", | ||
"asset-pipe-js-writer": "1.0.0-beta.5", | ||
"commander": "2.10.0", | ||
"request": "^2.79.0" | ||
}, | ||
"devDependencies": { | ||
"asset-pipe-test-es5a": "^1.0.0", | ||
"asset-pipe-test-es5b": "^1.0.0", | ||
"tap": "10.7.0", | ||
"eslint": "^4.1.0", | ||
"eslint-config-finn": "^1.0.1" | ||
}, | ||
"scripts": { | ||
"lint": "eslint lib test", | ||
"test": "tap test/*.js" | ||
} | ||
} | ||
{"name":"asset-pipe-client","version":"1.0.0","author":"Trygve Lie <post@trygve-lie.com>","files":["bin","lib"],"description":"Asset pipe client","main":"./lib/main.js","bin":"./bin/cli.js","repository":{"type":"git","url":"https://github.com/asset-pipe/asset-pipe-client.git"},"keywords":["assets"],"contributors":["Trygve Lie <post@trygve-lie.com>","Richard Walker <digitalsadhu@gmail.com>","Sveinung Røsaker <sveinung.rosaker@gmail.com>","Trygve Lie (http://www.trygve-lie.com/)","Sveinung Røsaker (https://github.com/sveisvei)","Greenkeeper (http://greenkeeper.io/)"],"bugs":{"url":"https://github.com/asset-pipe/asset-pipe-client/issues"},"license":"MIT","dependencies":{"JSONStream":"^1.3.0","asset-pipe-css-writer":"^1.0.0-alpha.1","asset-pipe-js-writer":"1.0.0-beta.5","commander":"2.10.0","is-stream":"^1.1.0","request":"^2.79.0"},"devDependencies":{"asset-pipe-test-es5a":"^1.0.0","asset-pipe-test-es5b":"^1.0.0","body-parser":"^1.18.2","commitizen":"^2.9.6","cz-conventional-changelog":"^2.0.0","eslint":"^4.9.0","eslint-config-finn":"^2.0.0","eslint-config-finn-prettier":"^3.0.1","express":"^4.16.2","husky":"^0.14.3","jest":"^21.2.1","lint-staged":"^4.2.3","prettier":"^1.7.4","projectz":"^1.4.0","semantic-release":"^8.2.0"},"scripts":{"lint:format":"eslint --fix .","lint":"eslint .","test":"jest --coverage","precommit":"lint-staged","cm":"git-cz","readme":"projectz compile","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"jest":{"clearMocks":true,"coverageThreshold":{"global":{"branches":100,"functions":100,"lines":100,"statements":100}}},"lint-staged":{"*.js":["eslint --fix --config ./.eslintrc","git add"],"{package.json,README.md,LICENSE.md}":["projectz compile","git add"]},"config":{"commitizen":{"path":"cz-conventional-changelog"}},"badges":{"list":["travisci","npmversion","daviddm","daviddmdev"]},"maintainers":[]} |
@@ -1,3 +0,18 @@ | ||
# asset-pipe-client | ||
<!-- TITLE/ --> | ||
<h1>asset-pipe-client</h1> | ||
<!-- /TITLE --> | ||
<!-- BADGES/ --> | ||
<span class="badge-travisci"><a href="http://travis-ci.org/asset-pipe/asset-pipe-client" title="Check this project's build status on TravisCI"><img src="https://img.shields.io/travis/asset-pipe/asset-pipe-client/master.svg" alt="Travis CI Build Status" /></a></span> | ||
<span class="badge-npmversion"><a href="https://npmjs.org/package/asset-pipe-client" title="View this project on NPM"><img src="https://img.shields.io/npm/v/asset-pipe-client.svg" alt="NPM version" /></a></span> | ||
<span class="badge-daviddm"><a href="https://david-dm.org/asset-pipe/asset-pipe-client" title="View the status of this project's dependencies on DavidDM"><img src="https://img.shields.io/david/asset-pipe/asset-pipe-client.svg" alt="Dependency Status" /></a></span> | ||
<span class="badge-daviddmdev"><a href="https://david-dm.org/asset-pipe/asset-pipe-client#info=devDependencies" title="View the status of this project's development dependencies on DavidDM"><img src="https://img.shields.io/david/dev/asset-pipe/asset-pipe-client.svg" alt="Dev Dependency Status" /></a></span> | ||
<!-- /BADGES --> | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/asset-pipe/asset-pipe-client.svg)](https://greenkeeper.io/) | ||
@@ -143,36 +158,11 @@ | ||
## Contributing | ||
The contribution process is as follows: | ||
## License | ||
The MIT License (MIT) | ||
Copyright (c) 2017 - Trygve Lie - post@trygve-lie.com | ||
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. | ||
[commonjs]: https://nodejs.org/docs/latest/api/modules.html | ||
[asset-pipe]: https://github.com/asset-pipe | ||
[asset-pipe-build-server]: https://github.com/asset-pipe/asset-pipe-build-server | ||
[browserify]: https://github.com/substack/node-browserify | ||
[browserify-opts]: https://github.com/substack/node-browserify#browserifyfiles--opts | ||
[browserify-plugin]: https://github.com/substack/node-browserify#bpluginplugin-opts | ||
[browserify-transform]: https://github.com/substack/node-browserify#btransformtr-opts | ||
- Fork this repository. | ||
- Make your changes as desired. | ||
- Run the tests using `npm test`. This will also check to ensure that 100% code coverage is maintained. If not you may need to add additional tests. | ||
- Stage your changes. | ||
- Run `git commit` or, if you are not familiar with [sematic commit messages](https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit), please run `npm run cm` and follow the prompts instead which will help you write a correct semantic commit message. | ||
- Push your changes and submit a PR. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14994
6
188
1
6
15
168
2
+ Addedis-stream@^1.1.0
+ Addedansi-styles@3.2.1(transitive)
+ Addedasset-pipe-css-writer@1.0.0(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedfind-up@2.1.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedhosted-git-info@2.8.9(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedjson-parse-better-errors@1.0.2(transitive)
+ Addedload-json-file@4.0.0(transitive)
+ Addedlocate-path@2.0.0(transitive)
+ Addednormalize-package-data@2.5.0(transitive)
+ Addedp-limit@1.3.0(transitive)
+ Addedp-locate@2.0.0(transitive)
+ Addedp-try@1.0.0(transitive)
+ Addedparse-json@4.0.0(transitive)
+ Addedpath-exists@3.0.0(transitive)
+ Addedpath-type@3.0.0(transitive)
+ Addedpify@2.3.03.0.0(transitive)
+ Addedpostcss@6.0.23(transitive)
+ Addedpostcss-import@11.1.0(transitive)
+ Addedpostcss-value-parser@3.3.1(transitive)
+ Addedread-cache@1.0.0(transitive)
+ Addedread-pkg@3.0.0(transitive)
+ Addedread-pkg-up@3.0.0(transitive)
+ Addedsemver@5.7.2(transitive)
+ Addedsource-map@0.6.1(transitive)
+ Addedspdx-correct@3.2.0(transitive)
+ Addedspdx-exceptions@2.5.0(transitive)
+ Addedspdx-expression-parse@3.0.1(transitive)
+ Addedspdx-license-ids@3.0.21(transitive)
+ Addedstrip-bom@3.0.0(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedvalidate-npm-package-license@3.0.4(transitive)