将目录中的文件拉入 NPM 的根文件夹中 [英] Pulling files from a directory into the root folder for NPM
问题描述
我正在向 NPM 发布一个库.
I am publishing a library to NPM.
当我构建库时,生成的工件作为 index.js
放置在位于我项目根目录的 dist
文件夹中.
When I build the library, the resulting artifact is placed in the dist
folder located in the root of my project as index.js
.
当用户从 NPM 安装时,我希望 index.js
出现在他们的 node_modules
文件夹中创建的文件夹的 root 中.目前,它保留在名为 dist
的目录中.
When users install from NPM I would like index.js
to be present in the root of the folder created in their node_modules
folder. Presently, it remains in a directory named dist
.
我该怎么做?
我的packages.json:
My packages.json:
{
"name": "my-package",
"version": "0.0.9",
"files": ["dist/*"],
"main": "index.min.js",
"private": false,
"dependencies": {},
"devDependencies": {},
"repository": "git@github.com:username/my-package.git"
}
推荐答案
我遇到了完全相同的问题.
I had exactly the same problem.
我不是通过向上复制文件来解决它,而是通过将我需要的文件向下复制到 ./dist/
文件夹中,然后从那里执行 npm publish
;然后 NPM 将该文件夹视为一个完整的包,一切都非常好.我需要从根文件夹复制的唯一文件是:
I solved it not by copying the files up, but by copying the files I needed down into the ./dist/
folder and then doing an npm publish
from there; NPM then treats that folder as a complete package and everything works very nicely. The only files I needed to copy from the root folder were:
package.json
README.md
因为我们要在发布之前将这些文件复制到 ./dist/
文件夹中,所以我们不希望 package.json
文件参考 ./dist/
.因此,完全删除 package.json
的 files
条目,因为我们不需要告诉它我们将采用哪些文件 - 我们将采用所有内容./dist/
文件夹.我正在使用 TypeScript,所以我也有一个 typings
条目,并且再次没有引用 ./dist/
.
Because we're going to copy these files down into the ./dist/
folder before we do the publish, we do NOT want the package.json
file to reference ./dist/
. So remove the package.json
's files
entry completely, because we don't need to tell it which files we'll take - we're going to take everything in the ./dist/
folder. I'm using TypeScript so I also have a typings
entry, and again no reference to ./dist/
.
{
"name": "my-package",
"version": "0.0.9",
"main": "index.min.js",
"typings": "index.d.ts",
"private": false,
"dependencies": {},
"devDependencies": {},
"repository": "git@github.com:username/my-package.git"
}
现在是发布步骤.我构建了一个 gulp 任务,它将为我执行发布,使其良好且自动化(除了增加包版本 #).
Now for the publish step. I built a gulp task that will perform the publish for me, making it nice and automated (except for incrementing the package version #).
从 gulp 开始,我将使用 Node 的 spawn() 来启动 npm 进程.但是,因为我实际上是在 Windows 上工作,所以我使用了cross-spawn"而不是与普通的内置 Node.js 生成相比(当我的路径中有空格时,我艰难地了解到它不起作用!).
From gulp I'll use Node's spawn() to kick-off the npm process. However, because I'm actually working on Windows I used "cross-spawn" rather than the normal built-in Node.js spawn (which I learned the hard way didn't work when I had spaces in my path!).
这是我的 gulp 文件,删除了 TypeScript 位:
Here's my gulp file, with the TypeScript bits removed:
var gulp = require('gulp');
var del = require('del');
var spawn = require('cross-spawn'); // WAS: require('child_process').spawn;
var config = {
src: { tsFiles: './src/**/*.ts' },
out: { path: './dist/' }
}
gulp.task('clean', () => {
return del('dist/*');
});
gulp.task('build', ['clean'], () => {
....
});
gulp.task('publish', ['build'], (done) => {
// Copy the files we'll need to publish
// We just use built-in gulp commands to do the copy
gulp.src(['package.json', 'README.md']).pipe(gulp.dest(config.out.path));
// We'll start the npm process in the dist directory
var outPath = config.out.path.replace(/(\.)|(\/)/gm,'');
var distDir = __dirname + '\\' + outPath + '\\';
console.log("dist directory = " + distDir);
// Start the npm process
spawn('npm', ['publish'], { stdio:'inherit', cwd:distDir } )
.on('close', done)
.on('error', function(error) {
console.error(' Underlying spawn error: ' + error);
throw error;
});
});
请注意,当我们调用 spawn() 时,我们传入了第三个参数,即选项.这里的主要条目是 cwd:distDir
,它告诉 spawn 从 ./dist/目录运行 npm 进程.因为使用 spawn 可能会导致问题,所以我已经开始使用 spawn 错误处理.当我对 spawn()
的使用进行故障排除时,我发现了以下 StackOverflow 文章非常有帮助.
Notice when we call spawn() we pass in a 3rd argument which is the options. The main entry here is the cwd:distDir
, which tells spawn to run the npm process from the ./dist/ directory. Because using spawn can cause problems I've hooked into the spawn error handling. As I was troubleshooting my use of spawn()
I found the following StackOverflow article very helpful.
这很有魅力;我发布的包包含根目录中的所有文件,./dist/
文件夹未发布.
This worked like a charm; my published package has all the files in the root directory and the ./dist/
folder is not published.
这篇关于将目录中的文件拉入 NPM 的根文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!