在命令行下使用 js 流部分替代 shell 命令,太难记了!不知大家怎么看

2016-05-15 23:07:32 +08:00
 fritx

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"

jayin 基于through2.

如果你发现已经早已有类似的东西存在,不妨告诉我一下 ;)

2897 次点击
所在节点    分享创造
17 条回复
djyde
2016-05-15 23:22:36 +08:00
我想写一个 js to shell script 的 compiler...
audi
2016-05-15 23:27:47 +08:00
为什么要复制这些文件到另一个文件夹?
congeec
2016-05-15 23:28:10 +08:00
我只能说他 shell 用的太烂
sed /^\*$/d .gitignore | sed s/\!// | xargs -I{} cp {} ~/.dotfiles
KiriGiri
2016-05-15 23:53:21 +08:00
有一个基于 python 的 shell
http://xon.sh
muchrooms
2016-05-16 12:17:16 +08:00
@congeec 有情怀
fritx
2016-05-16 15:06:24 +08:00
@congeec 有情怀 多谢指教 学习中。。。
fritx
2016-05-16 15:07:41 +08:00
@djyde 这这。。会不会还是要求覆盖 bash 的知识面呀?
zinking
2016-05-16 23:56:52 +08:00
@KiriGiri good to know, thanks.
fritx
2016-05-17 00:24:24 +08:00
@KiriGiri thanks. 有空就试用
fritx
2016-05-17 00:26:06 +08:00
@audi 当时就是想迁移一下目录
raecoo
2016-05-17 22:36:10 +08:00
https://github.com/shelljs/shelljs 用了这东西你应该不会再说什么了吧
fritx
2016-05-17 23:23:49 +08:00
@raecoo shelljs 只覆盖了一部分命令呀 而且还得进 node 里执行?
fritx
2016-05-17 23:25:15 +08:00
@raecoo 噢!我看到这个了

```shell
$ shx mkdir -p foo
$ shx touch foo/bar.txt
$ shx rm -rf foo
```
fritx
2016-05-17 23:28:42 +08:00
不过。。 shelljs 只覆盖了部分命令。。利用 JS 表达式的方式比较容易上手
ericFork
2016-05-18 01:59:36 +08:00
你从造出这个轮子的过程中收获了开心就好
wizardforcel
2016-05-18 12:19:47 +08:00
我一贯用 py/node 长命令在控制台里写就是灾难。。

shellscipt 的表现力还是差一些。
Rabbit2B
2016-05-18 14:24:33 +08:00
sed /^\!/s/\!//g .gitignore | xargs cp -t ./.dotfiles

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/278831

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX