[英]Generate a file From npm global module To the user's current path
I am creating a NPM module that will be installed globally on my computer, let's say it reside inside <my-global-npm-module>
and inside of this folder I have my index.html
file. 我正在创建一个NPM模块,该模块将全局安装在我的计算机上,假设它位于
<my-global-npm-module>
并且在此文件夹中,我有index.html
文件。
Goal: My goal is to copy the index.html
file and paste into whatever/location folder my user is using, example: my-app
folder. 目标:我的目标是复制
index.html
文件并粘贴到用户正在使用的任意位置文件夹中,例如: my-app
文件夹。
I have the following .js
code: 我有以下
.js
代码:
const fs = require('fs'); //File Server
const path = require('path');
let filename = 'index.html';
let src = path.join(__dirname, filename);
let destDir = path.join(__dirname, 'BlueFolder');
fs.access(destDir, (err) => {
if(err)
fs.mkdirSync(destDir);
copyFile(src, path.join(destDir, filename));
});
function copyFile(src, dest) {
let readStream = fs.createReadStream(src);
readStream.once('error', (err) => {
console.log(err);
});
readStream.once('end', () => {
console.log('File has been copied.');
});
readStream.pipe(fs.createWriteStream(dest));
}
If run this .js
file it will copy and paste a new copy of the index.html
file inside of BlueFolder
which is also inside of my <my-global-npm-module>
folder. 如果运行此
.js
文件,它将在BlueFolder
复制并粘贴index.html
文件的新副本,该文件也在我的<my-global-npm-module>
文件夹中。 The correct should be inside of my user's folder, example: my-app
folder. 正确的应该位于用户文件夹内,例如:
my-app
文件夹。
The function you are looking for is process.cwd()
. 您正在寻找的功能是
process.cwd()
。 This function returns the current working directory as a string. 此函数以字符串形式返回当前工作目录 。
From the documentation : 从文档中 :
The process.cwd() method returns the current working directory of the Node.js process.
process.cwd()方法返回Node.js进程的当前工作目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.