UNPKG

2.93 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const { Command } = require('commander')
4
5const program = new Command()
6program.version('0.0.1')
7
8program
9 .command('config <arg> [value]')
10 .description('set or show config for host', {
11 arg: 'the params to set, maybe host、port or token',
12 value: 'the value to set'
13 })
14 .action((arg, value) => {
15 const avalivableArgs = ['host', 'port', 'token']
16 if(avalivableArgs.includes(arg)) {
17 const parseConf = require('../src/parse-conf')
18 const configContent = parseConf()
19 if(value) {
20 const { updateConf } = require('../src/update-conf')
21 configContent[arg] = value
22 updateConf(configContent)
23 }else{
24 console.log(configContent[arg])
25 }
26 }
27 else
28 {
29 console.log(`unknown params: ${arg}`)
30 }
31
32 })
33
34program
35 .command('add', { isDefault: true })
36 .description('upload file')
37 .option('-d, --debug', 'output extra debugging')
38 .option('-p, --path <path>', 'wrapper dir', '/')
39 .option('--host <host>', 'ipfs cluster api host', '')
40 .option('--port <port>', 'ipfs cluster api port', '')
41 .option('--showAll', 'show all file cid')
42 .option('-t, --token <token>', 'ipfs cluster api base auth token', '')
43 .option('-r, --recursive', 'recursive all sub dir')
44 .option('-a, --all', 'include hidden file')
45 .arguments('<file>')
46 .action((file)=>{
47 run(file)
48 })
49 program.parse(process.argv)
50
51
52
53function run(file) {
54 const IpfsClusterAPI = require('ipfs-cluster-api')
55
56 const options = program.opts()
57 if (options.debug) console.log(options)
58
59 let headers = {}
60 const parseConf = require('../src/parse-conf')
61 const conf = parseConf()
62 options.host = options.host || conf.host
63 options.port = options.port || conf.port
64 options.token = options.token || conf.token
65 if(options.token) {
66 //dXNlcjp1dnh6Z2NCMThMT1Zv
67 headers = {
68 authorization: 'Basic ' + options.token
69 }
70 }
71
72 const cluster = IpfsClusterAPI({
73 host: options.host,
74 port: options.port,
75 protocol: 'http',
76 headers,
77 })
78 const loadPath = require('../src/load-path')
79 const files = loadPath({path: file,hidden: options.all, ignore: false}, {
80 recursive: options.recursive,
81 }, options.path)
82 cluster.add(files).then(res=>{
83 if(!options.showAll && options.recursive) {
84 const path = require('path')
85 let filePath = path.resolve(file)
86 filePath = filePath.split(path.sep).join('/')
87 const fullDir = filePath + (filePath.endsWith('/') ? '' : '/')
88 let dirName = fullDir.split('/')
89 dirName = dirName[dirName.length - 2]
90 res = res.filter((item)=>{return item.path==dirName})
91 }
92 if(options.showAll) {
93 console.log(res)
94 }
95 else{
96 console.log(res[0])
97 }
98 })
99}