programing

커스텀 스크립트를 패키지에 추가하려면 어떻게 해야 하나요?jascript 파일을 실행하는 json 파일?

sourcejob 2022. 9. 22. 00:18
반응형

커스텀 스크립트를 패키지에 추가하려면 어떻게 해야 하나요?jascript 파일을 실행하는 json 파일?

명령어를 실행할 수 있도록 하고 싶다.script1실행할 프로젝트 디렉토리에서node script1.js.

script1.js는, 같은 디렉토리에 있는 파일입니다.이 명령어는 프로젝트 디렉토리에 고유해야 합니다.즉, 다른 사용자에게 프로젝트 폴더를 보내도 동일한 명령을 실행할 수 있습니다.

지금까지 다음을 추가하려고 했습니다.

"scripts": {
    "script1": "node script1.js"
}

내 소포로 보내줘.json 파일을 실행하려고 하면script1다음과 같은 출력이 표시됩니다.

zsh: command not found: script1

상기 스크립트를 프로젝트 폴더에 추가하기 위해 필요한 절차를 알고 있는 사람이 있습니까?

*주의: 명령어는 bash 프로파일에 추가할 수 없습니다(머신 고유의 명령어는 사용할 수 없습니다).

뭔가 설명이 필요하시면 말씀해주세요.

커스텀 스크립트

npm run-script <custom_script_name>

또는

npm run <custom_script_name>

이 예에서는 실행하려고 합니다.npm run-script script1또는npm run script1.

https://docs.npmjs.com/cli/run-script 를 참조해 주세요.

라이프 사이클 스크립트

노드에서는 특정 라이프 사이클 이벤트에 대해 커스텀 스크립트를 실행할 수도 있습니다(예:npm install운영된다.이 이곳에서 발견될 수 있다.

예를 들어 다음과 같습니다.

"scripts": {
    "postinstall": "electron-rebuild",
},

이 동작은electron-rebuild잠시 후에npm install명령어를 입력합니다.

다음을 작성했습니다.시스템에서 동작합니다.다음을 시도해 보십시오.

패키지.json:

{
  "name": "test app",
  "version": "1.0.0",
  "scripts": {
    "start": "node script1.js"   
  }
}

script1.discript:

console.log('testing')

명령줄에서 다음 명령을 실행합니다.

npm start

추가 사용 사례

제 소포요.json 파일에는 일반적으로 다음과 같은 스크립트가 있습니다.이를 통해 파일 유형, sass 컴파일 및 서버 실행도 감시할 수 있습니다.

 "scripts": {
    "start": "concurrently \"sass --watch ./style/sass:./style/css\" \"npm run tsc:w\" \"npm run lite\" ",    
    "tsc": "tsc",
    "tsc:w": "tsc -w", 
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install" 
  }

순서는 다음과 같습니다.

  1. 포장되어 있습니다.json 추가:

    "bin":{
        "script1": "bin/script1.js" 
    }
    
  2. 작성하다bin프로젝트 디렉토리의 폴더 및 파일 추가runScript1.js코드 포함:

    #! /usr/bin/env node
    var shell = require("shelljs");
    shell.exec("node step1script.js");
    
  3. 달려.npm install shelljs말미에

  4. 달려.npm link말미에

  5. 터미널에서 실행할 수 있습니다.script1그것은 실행된다node script1.js

참고 자료: http://blog.npmjs.org/post/118810260230/building-a-simple-command-line-tool-with-npm

스크립트에서는 1개의 명령어로2개의 명령어를 실행한다고 합니다.

"scripts":{
  "start":"any command",
  "singleCommandToRunTwoCommand":"some command here && npm start"
}

이제 터미널로 가서 실행하세요.npm run singleCommandToRunTwoCommand.

이 스크립트 행이 package.json에 있다고 가정합니다.

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "export_advertisements": "node export.js advertisements",
    "export_homedata": "node export.js homedata",
    "export_customdata": "node export.js customdata",
    "export_rooms": "node export.js rooms"
  },

이제 "export_advertisements" 스크립트를 실행하려면 터미널에 가서

npm run export_advertisements

예:

  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "build_c": "ng build --prod && del \"../../server/front-end/*.*\" /s /q & xcopy /s dist \"../../server/front-end\"",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },

보시다시피 스크립트 build_c는 각도 어플리케이션을 빌드하고 디렉토리에서 오래된 파일을 모두 삭제한 후 최종적으로 결과 빌드 파일을 복사합니다.

언급URL : https://stackoverflow.com/questions/36433461/how-do-i-add-a-custom-script-to-my-package-json-file-that-runs-a-javascript-file

반응형