如何从package json中的依赖项克隆位存储库,构建并打包

时间:2019-05-12 03:26:47

标签: angular npm

我有一个具有许多依赖项的主项目,其中一些依赖项是直接从npm安装的,其中的几个要作为库从位桶中克隆并构建。目前,我分别对其进行克隆/拉动,然后运行build and package命令,然后在本地(.tgz)文件中的本地更新已构建软件包package.json的路径。我不确定,因为我可以简单地实现,只要我在主package.json上运行npm install,它就会自动下载所需的子存储库,进行构建和打包。

以下是我的主要package.json-

{
  "name": "main",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "build-n-watch": "ng build --watch"
  },
  "private": true,
  "dependencies": {
    .
    .
    .
    "@angular/router": "~7.2.0",
    "admin": "file:C:/Users/some-project-path/dist/prj1/prj1-0.0.1.tgz",
    "teacher": "file:C:/Users/some-project-path/dist/prj2/prj2-0.0.1.tgz",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },

以下是我的子存储库package.json,我需要对其进行克隆,构建和打包-

{
  "name": "admission",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "build-n-watch": "ng build --watch",
    "build-n-pack": "npm run build & cd dist/prj1 & npm pack"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/cdk": "^7.3.3",
    "@angular/common": "~7.2.0",
    .
    .
    .

以下是我的子存储库所在的位桶网址-

https://bitbucket.org/team-a/prj1/src/master/

我基本上需要从该网址clone/pullpm install中自动npm run build-n-pack

1 个答案:

答案 0 :(得分:0)

如果我穿上你的鞋子,我会去找一些自定义的script.js文件,例如以下文件

const { spawn } = require('child_process')

function execPull () {
  return new Promise((resolve, reject) => {
    const commandExecuter = spawn('git', ['pull'])
    let stdOutData = ''
    let stderrData = ''

    commandExecuter.stdout.on('data', data => (stdOutData += data))
    commandExecuter.stderr.on('data', data => (stderrData += data))
    commandExecuter.on('close', code => {
      console.log('Pull complete')
      return code != 0
        ? reject(stderrData.toString())
        : resolve(stdOutData.toString())
    })
  })
}

function execNpmInstall () {
  return new Promise((resolve, reject) => {
    const commandExecuter = spawn('npm.cmd', ['install'])

    let stdOutData = ''
    let stderrData = ''

    commandExecuter.stdout.on('data', data => (stdOutData += data))
    commandExecuter.stderr.on('data', data => (stderrData += data))
    commandExecuter.on('close', code => {
      console.log('Node modules installed')
      return code != 0
        ? reject(stderrData.toString())
        : resolve(stdOutData.toString())
    })
  })
}

execPull()
  .then(() => execNpmInstall())
  .catch(e => {
    throw new Error(e)
  })

以相同的方式,您可以在链中链接多个npm命令。

之后,如果需要,您可以创建可以与npm run {your script name}一起运行的自定义脚本

{
  ...,
  "scripts": {
     ...,
    "{your script command}": "node {your script name}.js"
  },
  ...
}

您可以签出此npm软件包run-git-command来运行git命令。