V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
fritx
V2EX  ›  分享创造

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

  •  
  •   fritx ·
    fritx · 2016-05-15 23:07:32 +08:00 · 2887 次点击
    这是一个创建于 2904 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 each
    • x: 当前的 input 内容
    • i: current index value (with -e)
    • exec(cmd): child_process.execSync(cmd)

    jayin 基于through2.

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

    第 1 条附言  ·  2016-05-17 00:32:41 +08:00

    新增了 -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/'
    
    第 2 条附言  ·  2016-05-17 00:33:39 +08:00

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

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

    shellscipt 的表现力还是差一些。
    Rabbit2B
        17
    Rabbit2B  
       2016-05-18 14:24:33 +08:00
    sed /^\!/s/\!//g .gitignore | xargs cp -t ./.dotfiles
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3036 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 00:08 · PVG 08:08 · LAX 17:08 · JFK 20:08
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.