2017-11-19 12 views
1

Phoenix 앱에서 assets/ 디렉토리에있는 React 프론트 엔드를 실행 중입니다 ...이 파일은 자신의 package.json 파일이므로 실행할 수 있기를 원합니다. Phoenix 앱의 mix.exs 파일의 별칭 NPM 스크립트. 나는 Elixir의 System.cmd()을 실험 해 왔지만 트릭을 수행하지 않는 것 같습니다. assets/package.json에, 내가 기대 작품으로Phoenix framework - 서브 디렉토리에 NPM 스크립트를 실행하십시오.

"lint": "eslint ./*.js ./js/*.js || exit 0" 

assets/ 디렉토리에서 $ npm run lint을 실행있어 ...하지만 스매싱 것 -

기본적으로, 나는 나의 프론트 엔드의 lint 명령을 설정 한 앱의 최상위 수준에서 명령을 실행할 수 있습니다.

defp aliases do 
    [ 
    "lint": [System.cmd("npm", ["run lint"], cd: "assets") 
    ] 
end 

하지만 mix lint을 실행하는 것은 꽤 말의 오류 발생 : 내 mix.exs 파일에서

, 정말 같은 별칭을 실험했다

** (FunctionClauseError) no function clause matching in Mix.Task.run_alias/3  

The following arguments were given to Mix.Task.run_alias/3: 

    # 1 
    [{"\nUsage: npm <command>\n\nwhere <command> is one of:\n access, adduser, bin, bugs, c, cache, completion, config,\n ddp, dedupe, deprecate, dist-tag, docs, edit, explore, get,\n help, help-search, i, init, install, install-test, it, link,\n list, ln, login, logout, ls, outdated, owner, pack, ping,\n prefix, prune, publish, rb, rebuild, repo, restart, root,\n run, run-script, s, se, search, set, shrinkwrap, star,\n stars, start, stop, t, tag, team, test, tst, un, uninstall,\n unpublish, unstar, up, update, v, version, view, whoami\n\nnpm <cmd> -h  quick help on <cmd>\nnpm -l   display full usage info\nnpm help <term> search for help on <term>\nnpm help npm  involved overview\n\nSpecify configs in the ini-formatted file:\n /Users/user/.npmrc\nor on the command line via: npm <command> --key value\nConfig info can be viewed via: npm help config\n\[email protected] /Users/user/.nvm/versions/node/v6.11.1/lib/node_modules/npm\n", 1}] 

    # 2 
    [] 

    # 3 
    :ok 

Attempted function clauses (showing 3 out of 3): 

    defp run_alias([h | t], alias_args, _res) when is_binary(h) 
    defp run_alias([h | t], alias_args, _res) when is_function(h, 1) 
    defp run_alias([], _alias_task, res) 

(mix) lib/mix/task.ex:331: Mix.Task.run_alias/3 
(mix) lib/mix/task.ex:266: Mix.Task.run/2 
(mix) lib/mix/cli.ex:75: Mix.CLI.run_task/2 
(elixir) lib/code.ex:376: Code.require_file/2 

좋아, 그래서 분명 뭔가 잘못하고 있어요을 . Phoenix 앱의 하위 디렉토리에서 NPM 명령을 실행할 수 있습니까?

답변

1

먼저 runlint은 인수 목록에서 별도의 문자열이어야합니다.

두 번째로 별칭을 호출 할 때 대신 명령을 즉시 실행합니다.

이 같은 별칭에서 임의 코드를 실행할 수있는 "run -e ..." 별칭을 사용할 수 있습니다

"lint": [~s|run -e 'System.cmd("npm", ["run", "lint"], cd: "assets")'|) 

(난 그냥 쉽게 단일 및 이중 따옴표 모두 문자열을 입력 할 수 있도록 ~s|| 구문을 사용하고 있습니다.)

+0

굉장! 이것은 올바르게 작동하는 것처럼 보이지만 두 가지 빠른 것들이 있습니다. 1. 코드에 작은 오타가 있습니다. 닫는 괄호는 대괄호 여야합니다. 내가 그것을 고쳤을 때, 응용 프로그램이 컴파일되고 작업이 오류없이 실행되었습니다. 그러나 2. 작업 출력이 터미널에서 보이지 않았습니다. 출력을 기존 쉘로 파이프 할 수있는 방법이 있습니까? 필자는'System' 문서의 옵션을 보았지만 아무 것도 보지 못했습니다. (필자는'System ... '전에 IO.inspect를 추가하려고 시도했지만 결과는 깔끔한 ESLint 포맷이 아닌 ...). – skwidbreth

+1

아, 알겠습니다! IO.stream (: stdio, : line)]'옵션에'': IO.stream (: stdio, : line)'을 추가해야했습니다. 감사합니다 Dogbert !!! – skwidbreth