Sfoglia il codice sorgente

feat: change packageName and add options strip

zhangyuhan 3 anni fa
parent
commit
04ea1688fc
4 ha cambiato i file con 28 aggiunte e 20 eliminazioni
  1. 4 4
      README.md
  2. 6 6
      package.json
  3. 9 5
      src/index.ts
  4. 9 5
      test/blah.test.ts

+ 4 - 4
README.md

@@ -1,4 +1,4 @@
-# download-remote-project-template
+# @jianyu/dl-template download-remote-project-template
 
 ## Install
 
@@ -6,12 +6,12 @@
 # set register
 nrm use xxx
 # install
-yarn add @jianyu/dr-template
+yarn add @jianyu/dl-template
 ```
 
 ## Examples
 ```javascript
-import DownloadRemoteProjectTemplate from 'download-remote-project-template'
+import DownloadRemoteProjectTemplate from '@jianyu/dl-template'
 
 const dl = new DownloadRemoteProjectTemplate()
 dl.get({
@@ -24,7 +24,7 @@ dl.get({
 
 ## DiyLinks Examples
 ```javascript
-  import DownloadRemoteProjectTemplate from 'download-remote-project-template'
+  import DownloadRemoteProjectTemplate from '@jianyu/dl-template'
 
   const dl = new DownloadRemoteProjectTemplate({
     getTransformLinks: (params: FormatParams) => {

+ 6 - 6
package.json

@@ -1,5 +1,5 @@
 {
-  "version": "0.1.1",
+  "version": "0.2.0",
   "license": "MIT",
   "main": "dist/index.js",
   "typings": "dist/index.d.ts",
@@ -35,20 +35,20 @@
     "tabWidth": 2,
     "spaceBeforeFunctionParen": true
   },
-  "name": "@jianyu/dr-template",
-  "author": "zhangyuhan2016",
+  "name": "@jianyu/dl-template",
+  "author": "zhangyuhan",
   "repository": {
     "type": "git",
     "url": "http://192.168.3.207:20080/packages/download-remote-project-template"
   },
-  "module": "dist/dr-template.esm.js",
+  "module": "dist/dl-template.esm.js",
   "size-limit": [
     {
-      "path": "dist/dr-template.cjs.production.min.js",
+      "path": "dist/dl-template.cjs.production.min.js",
       "limit": "10 KB"
     },
     {
-      "path": "dist/dr-template.esm.js",
+      "path": "dist/dl-template.esm.js",
       "limit": "10 KB"
     }
   ],

+ 9 - 5
src/index.ts

@@ -5,12 +5,13 @@ const decompress = require('decompress')
 const path = require('path')
 const fs = require('fs')
 
-interface FormatParams {
+export interface FormatParams {
   type: string
   group: string
   name: string
   checkout: string
   path: string
+  strip: number
 }
 
 async function initLanguage (lang = 'cn') {
@@ -29,7 +30,7 @@ async function initLanguage (lang = 'cn') {
 
 initLanguage()
 
-export class DownloadRemoteProjectTemplate {
+export default class DownloadRemoteProjectTemplate {
   private hooks: any
   private FileName: any
 
@@ -48,7 +49,8 @@ export class DownloadRemoteProjectTemplate {
     destinationFolder: string
     extract?: boolean
   }) {
-    const url = this.getLink(this.getFormatParams(repository, remoteFolder))
+    const params = this.getFormatParams(repository, remoteFolder)
+    const url = this.getLink(params)
     const realDestinationFolder = path.resolve(__dirname, destinationFolder)
     this.checkCreateFolder(realDestinationFolder)
     if (!url) {
@@ -60,7 +62,8 @@ export class DownloadRemoteProjectTemplate {
     return this.download(url, realDestinationFolder, this.hooks.getOptions()).then(async () => {
       if (extract) {
         await decompress(path.resolve(realDestinationFolder, this.FileName), realDestinationFolder, {
-          filter: this.hooks.doFilterFiles
+          filter: this.hooks.doFilterFiles,
+          strip: params.strip
         }).then(() => {
           console.log(i18next.t('success_extract'))
         })
@@ -95,7 +98,8 @@ export class DownloadRemoteProjectTemplate {
       group: matchArgs[8] || matchArgs[4],
       name: matchArgs[9] || matchArgs[5],
       checkout: matchArgs[6] || 'master',
-      path: isRootFolder ? '' : remoteFolder
+      path: isRootFolder ? '' : remoteFolder,
+      strip: isRootFolder ? 1 : remoteFolder.split('/').length + 1
     }
     return result
   }

+ 9 - 5
test/blah.test.ts

@@ -1,4 +1,4 @@
-import { DownloadRemoteProjectTemplate } from '../src'
+import DownloadRemoteProjectTemplate from '../src'
 
 describe('getFormatParams', () => {
   const dl = new DownloadRemoteProjectTemplate()
@@ -8,7 +8,8 @@ describe('getFormatParams', () => {
       group: 'group',
       name: 'repo',
       checkout: 'master',
-      path: ''
+      path: '',
+      strip: 1
     })
   })
 
@@ -18,7 +19,8 @@ describe('getFormatParams', () => {
       group: 'group',
       name: 'repo',
       checkout: 'v1.0',
-      path: ''
+      path: '',
+      strip: 1
     })
   })
 
@@ -28,7 +30,8 @@ describe('getFormatParams', () => {
       group: 'group',
       name: 'repo',
       checkout: '5ccc555c22',
-      path: ''
+      path: '',
+      strip: 1
     })
   })
 
@@ -38,7 +41,8 @@ describe('getFormatParams', () => {
       group: 'group',
       name: 'repo',
       checkout: 'v1.2',
-      path: 'test'
+      path: 'test',
+      strip: 2
     })
   })
 })