如何通过 postinstall npm 脚本自动将文件从包复制到本地目录?

Posted

技术标签:

【中文标题】如何通过 postinstall npm 脚本自动将文件从包复制到本地目录?【英文标题】:How to automatically copy files from package to local directory via postinstall npm script? 【发布时间】:2016-04-19 07:25:21 【问题描述】:

我想在运行后自动将某些文件从npm 包复制到用户的本地目录

npm install my-package

我可以通过在package.json 中声明"files" 来安装它们。问题是---文件没有放在本地目录中。所以我需要运行postinstall 脚本。

但是现在我不知道包安装在哪里(可能是目录树的更高位置),那么我怎样才能可靠地访问文件并通过脚本将它们复制到本地目录?

(通过本地目录我的意思是 --- 从我运行 npm install my-package 作为用户消费包。)

更新。似乎postinstall 脚本作为npm 拥有的进程运行,主目录为node_modules/my-package,所以除了天真的../../ 之外,我仍然不知道如何访问用户的主目录。

【问题讨论】:

一个老问题,我知道,但我面临着类似的问题。你有没有找到一个好的解决方案? @danielv 是的,请参阅github.com/dmitriz/min-karma 谢谢。我希望您找到了一个更优雅的解决方案来查找除../../ 之外的基本安装目录。 @danielv 如果您遇到更好的解决方案,请告诉我。 @danilev 最终,当npmyarn 无法就安装位置达成一致时,我发现这个幼稚的解决方案是最方便和可靠的解决方案。 【参考方案1】:

经过大量搜索,我发现这适用于跨平台

"scripts":
  "postinstall": "node ./post-install.js",

// post-install.js

/**
 * Script to run after npm install
 *
 * Copy selected files to user's directory
 */

'use strict'

var gentlyCopy = require('gently-copy')

var filesToCopy = ['.my-env-file', 'demo']

// User's local directory
var userPath = process.env.INIT_CWD

// Moving files to user's local directory
gentlyCopy(filesToCopy, userPath)

【讨论】:

【参考方案2】:

如果你是用 yarn 或 npm 构建的。你只能做“cp”。


    "scripts": 
        "start": "yarn run tailwind && react-scripts start",
        "build": "yarn run tailwind && yarn run purge-tailwind && cross-env GENERATE_SOURCEMAP=false react-scripts build &&  cp .htaccess build/ ",
    ,

如果在构建之后需要这个 .htaccess,只需在构建 npm 指令之后添加它。 && cp .htaccess build/

【讨论】:

【参考方案3】:

我会使用 shellscript/bash

-package.json

"scripts":
  "postinstall": "./postinstall.sh",

-postinstall.sh

#!/bin/bash

# go to YOUR_NEEDED_DIRECTORY .e.g "dist" or $INIT_CWD/dist
cd YOUR_NEEDED_DIRECTORY

# copy each file/dir to user dir(~/)
for node in `ls`
do
  cp -R $node ~/$node
done

别忘了!

chmod +x postinstall.sh

【讨论】:

不清楚这有什么帮助,我仍然需要知道如何在这段代码中引用本地目录,所以同样的问题仍然存在。 因为您的脚本作为生命周期事件的一部分运行,所以 $INIT_CWD 在此脚本中可用。【参考方案4】:

从 npm 3.4 开始,您可以使用 $INIT_CWD 环境变量: https://blog.npmjs.org/post/164504728630/v540-2017-08-22

运行生命周期脚本时,INIT_CWD 现在将包含执行 npm 的原始工作目录。

要解决您的问题,请在 package.json 中添加以下安装后脚本:

  "scripts": 
    "postinstall": "cp fileYouWantToCopy $INIT_CWD",
  ,

【讨论】:

【参考方案5】:

var cwd = require('path').resolve();

注意:如果要解析的参数具有零长度字符串,则将使用当前工作目录而不是它们。

来自https://nodejs.org/api/path.html

【讨论】:

我仍然很困惑 cwdpostinstall 脚本中 --- 本地目录还是安装文件所在的位置? cwd 始终是执行程序的位置。如果您需要从文件本身指向当前 js 文件的路径,则每个文件中都有 __filename__dirname 变量可用。 我需要本地目录的路径,请参阅我的问题的最后一行来定义我的意思。 很遗憾,这种方法并不能解决问题。由于node 脚本是从node_modules 下的包目录执行的,因此它们的cwd 永远不会指向用户运行npm install 的本地目录。

以上是关于如何通过 postinstall npm 脚本自动将文件从包复制到本地目录?的主要内容,如果未能解决你的问题,请参考以下文章

NPM:如何在安装后运行?

有条件地运行 npm 脚本

输入npm install 报错npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! node-sass@4.13.1 postinstall: `no

错误 npm install - node-sass@4.13.1 postinstall: `node scripts/build.js

npm ERR! Failed at the node-sass@4.13.0 postinstall script.

Failed at the node-sass@4.14.1 postinstall script. npm ERR! This is probably not a problem with npm(