Github: https://github.com/fritx/jayin/tree/cn
假如你有一个类似于 gitignore 的文件:
# .gitignore * !.gitignore !README.md !prs-welcome.svg !.bashrc !.bash_profile !.exports !.aliases !.editorconfig
你想要 cp 这些文件到另外一个文件夹。
用 bash 来搞?
files=$(cat .gitignore | sed /^\*$/d | sed s/\!//) for file in $files; do cp $file ./dotfiles/; done # or even cat file | sed /^\*$/d | sed s/\!// \ | while read -r file; do cp $file ./dotfiles/; done
瓦特?
作为一名 node.js 工程师,为什么不能用 js 流来处理呢?
cat .gitignore | js -ti 'x.trim().split(`\n`).slice(1).map(x => x.slice(1))' \ | js -e 'exec(`cp ${x} ./dotfiles/`)' # same as cat .gitignore | js -ti 'x.trim().split(`\n`)' \ | js 'x.slice(1)' \ | js 'x.map(x => x.slice(1))' \ | js -e 'exec(`cp ${x} ./dotfiles/`)'
不要忘了搞一个 alias ,如果你需要的话。
npm install -g jayin alias js="jayin"
-ti
: 以文本的形式直接 input ,无需 JSON.parse-to
: 以文本的形式直接 output ,无需 JSON.stringify-t
: 以上两者-e
: for eachx
: 当前的 input 内容i
: current index value (with -e)exec(cmd)
: child_process.execSync(cmd)jayin 基于through2.
如果你发现已经早已有类似的东西存在,不妨告诉我一下 ;)
新增了 -c
: exec(cmd) 快捷方式
cat .gitignore | js -ti 'x.trim().split(`\n`)' \ | js 'x.slice(1)' \ | js 'x.map(x => x.slice(1))' \ | js -e -c 'cp ${x} ./dotfiles/'
lodash也已经集成进来
echo '[1,2,3,4]' | js '_.filter(x, x => x %2)' \ | js '_.reverse(x)' \ >> file # or in chain echo '[1,2,3,4]' \ | js '_(x).filter(x => x %2).reverse().value()' \ >> file
![]() | 1 djyde 2016-05-15 23:22:36 +08:00 我想写一个 js to shell script 的 compiler... |
![]() | 2 audi 2016-05-15 23:27:47 +08:00 via iPhone 为什么要复制这些文件到另一个文件夹? |
![]() | 3 congeec 2016-05-15 23:28:10 +08:00 via iPhone ![]() 我只能说他 shell 用的太烂 sed /^\*$/d .gitignore | sed s/\!// | xargs -I{} cp {} ~/.dotfiles |
![]() | 4 KiriGiri 2016-05-15 23:53:21 +08:00 有一个基于 python 的 shell http://xon.sh |
![]() | 11 raecoo 2016-05-17 22:36:10 +08:00 https://github.com/shelljs/shelljs 用了这东西你应该不会再说什么了吧 |
13 fritx OP |
14 fritx OP 不过。。 shelljs 只覆盖了部分命令。。利用 JS 表达式的方式比较容易上手 |
![]() | 15 ericFork 2016-05-18 01:59:36 +08:00 你从造出这个轮子的过程中收获了开心就好 |
![]() | 16 wizardforcel 2016-05-18 12:19:47 +08:00 via Android 我一贯用 py/node 长命令在控制台里写就是灾难。。 shellscipt 的表现力还是差一些。 |
17 Rabbit2B 2016-05-18 14:24:33 +08:00 sed /^\!/s/\!//g .gitignore | xargs cp -t ./.dotfiles |