Skip to content

Commit

Permalink
build: use ng-packagr to package lib
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Sep 20, 2018
1 parent fdf599a commit 0623992
Show file tree
Hide file tree
Showing 23 changed files with 2,024 additions and 1,554 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
/dist
/tmp
/out-tsc
/__gen_lib
/publish-es5
/publish-es2015
/publish

# dependencies
Expand Down Expand Up @@ -37,6 +34,7 @@ npm-debug.log
yarn-error.log
testem.log
/typings
yarn.lock

# System Files
.DS_Store
Expand Down
31 changes: 18 additions & 13 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
sudo: required
language: node_js
node_js:
- "8.11.0"

env:
- TASK=test
- TASK=lint
- TASK=build
- TASK=site:build
- '8.11'

addons:
apt:
sources:
- google-chrome
packages:
- google-chrome-stable
- google-chrome-beta
apt:
sources:
- google-chrome
packages:
- google-chrome-stable
- google-chrome-beta

git:
depth: 1

cache:
yarn: true
directories:
- ./node_modules

before_install:
- export CHROME_BIN=chromium-browser
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

env:
- TASK=build
- TASK=test
- TASK=lint
- TASK=site:build

script:
- npm run $TASK
- |
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Simple file save with FileSaver.js
[![NPM version](https://img.shields.io/npm/v/ngx-filesaver.svg)](https://www.npmjs.com/package/ngx-filesaver)
[![Build Status](https://travis-ci.org/cipchk/ngx-filesaver.svg?branch=master)](https://travis-ci.org/cipchk/ngx-filesaver)

[中文版](README.zh-CN.md)

## Examples

- [demo](https://cipchk.github.io/ngx-filesaver/)
Expand Down
103 changes: 103 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# ngx-filesaver

Simple file save with FileSaver.js

[![NPM version](https://img.shields.io/npm/v/ngx-filesaver.svg)](https://www.npmjs.com/package/ngx-filesaver)
[![Build Status](https://travis-ci.org/cipchk/ngx-filesaver.svg?branch=master)](https://travis-ci.org/cipchk/ngx-filesaver)

## 示例

- [demo](https://cipchk.github.io/ngx-filesaver/)
- [Stackblitz](https://stackblitz.com/edit/ngx-filesaver)

## 安装

```
npm install file-saver ngx-filesaver --save
```

添加 `FileSaverModule` 模块到项目中:

```
import { FileSaverModule } from 'ngx-filesaver';
@NgModule({
imports: [ FileSaverModule ]
})
```

## 使用方法

支持服务 `FileSaverService.save()` 或属性指令 `fileSaver` 两种保存方式。

### 1、FileSaverService

```typescript
constructor(private _http: Http, private _FileSaverService: FileSaverService) {
}

onSave() {
let options = new RequestOptions({
responseType: ResponseContentType.Blob // 这里必须是Blob类型
});

this._http.get('demo.pdf', options).subscribe(res => {
this._FileSaverService.save((<any>res)._body, fileName);
});
}
```

### 2、fileSaver 属性指令

#### 配置型

```html
<button type="button"
fileSaver
[method]="'GET'"
[fileName]="'中文pdf.pdf'"
[url]="'assets/files/demo.pdf'"
[header]="{ token: 'demo' }"
[query]="{ pi: 1, name: 'demo' }"
(success)="onSuc($event)"
(error)="onErr($event)">Download PDF</button>
```

**fileSaver**:属性指令名称。
**参数说明**

参数 | 说明 | 类型 | 默认值
----|------|-----|------
method | 请求方法类型 | `string` | `GET`
url | 下路路径 | `string` | -
fileName | 文件名 | `string` | -
query | 额外的查询参数,等同 `params` 值 | `string` | -
header | 请求的 `headers` 属性值,一般用来指定 _token_ 之类 | `any` | -
success | 下载成功回调 | `EventEmitter<any>` | -
error | 下载错误回调 | `EventEmitter<any>` | -

#### 自定义Http型

```html
<button type="button"
fileSaver
[http]="onRemote('pdf', true)">Download PDF</button>
```

```typescript
onRemote(type: string, fromRemote: boolean): Observable<Response> {
let options = new RequestOptions({
responseType: ResponseContentType.Blob
});
return this._http.get(`assets/files/demo.${type}`, options).map(response => {
response.headers.set('filename', `demo.${type}`)
return response;
});
}
```


#### 关于文件名

文件名的获取按以下优先级:fileName =》 response.headers.get('filename') =》 response.headers.get('x-filename')。

如果你请求的是一个CORS跨域地址,需要注意设置 `Access-Control-Allow-Headers: filename`,以免无法获取。
2 changes: 1 addition & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"options": {
"main": "lib/test.ts",
"tsConfig": "lib/tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"karmaConfig": "lib/karma.conf.js",
"polyfills": "src/polyfills.ts",
"scripts": [],
"styles": [],
Expand Down
50 changes: 0 additions & 50 deletions build.sh

This file was deleted.

4 changes: 3 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './public_api';
export * from './src/filesaver.provider';
export * from './src/filesaver.directive';
export * from './src/filesaver.module';
1 change: 0 additions & 1 deletion karma.conf.js → lib/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = function (config) {
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, '../coverage'),
dir: require('path').join(__dirname, 'coverage'), reports: ['html', 'lcovonly'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml'],
Expand Down
3 changes: 0 additions & 3 deletions lib/public_api.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/spec/filesaver.directive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { HttpClient, HttpHeaders } from '@angular/common/http';
import * as fs from 'file-saver';

import { FileSaverModule } from '../ngx-filesaver.module';
import { FileSaverModule } from '../src/filesaver.module';

function genFile(ext: string, isRealFile = true): Blob {
const blob = new Blob([
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions lib/ngx-filesaver.module.ts → lib/src/filesaver.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

import { FileSaverDirective } from './components/filesaver.directive';
import { FileSaverService } from './components/filesaver.provider';
import { FileSaverDirective } from './filesaver.directive';
import { FileSaverService } from './filesaver.provider';

@NgModule({
imports: [HttpClientModule],
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion lib/tsconfig.json → lib/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"outDir": "../release",
"lib": [
"es2015",
"dom"
Expand Down
2 changes: 1 addition & 1 deletion lib/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "./tsconfig.json",
"extends": "./tsconfig.lib.json",
"compilerOptions": {
"outDir": "../out-tsc/spec",
"baseUrl": "./",
Expand Down
Loading

0 comments on commit 0623992

Please sign in to comment.