我正在使用 Gulp 在文件保存时从 LESS-CSS 生成 CSS。我希望将 css 文件立即上传到服务器,因此我正在尝试使用 Vinyl-FTP。我是 NPM/NodeJS/Gulp/JavaScript 的新手,所以我需要一些帮助。
在我的 gulpfile.js 中,我包含了这段代码(当然隐藏了主机、用户和密码):
// Vinyl FTP
gulp.task( 'deploy', function () {
var conn = ftp.create( {
host: 'ftp-server',
user: 'user',
password: 'password',
parallel: 10,
log: gutil.log
} );
var globs = [
'../site/templates/templatename/css/bootstrap.min.css'
];
return gulp.src( globs, { base: '.', buffer: false } )
.pipe( conn.newer( '/public_html/dev2/templates/templatename/css' ) )
.pipe( conn.dest( '/public_html/dev2/templates/templatename/css' ) );
} );
我希望每次点击“保存”时都会上传 bootstrap.min.css 文件。该文件位于相对于我的 gulp 目录的 ../site/templates/templatename/css/bootstrap.min.css
。我希望将其上传到我的开发站点,该站点位于服务器上的 /public_html/dev2/templates/templatename/css
(是的,这是 Joomla)。
显然,我使用了错误的路径,因为这就是它产生的结果:
[14:44:21] Using gulpfile /mnt/e/Sites/successfulspeakernow.com/gulp/gulpfile.js
[14:44:21] Starting 'less'...
[14:44:21] Finished 'less' after 20 ms
[14:44:21] Starting 'watch'...
[14:44:21] Finished 'watch' after 267 ms
[14:44:21] Starting 'deploy'...
[14:44:21] CONN
[14:44:23] READY
[14:44:23] MLSD /public_html/dev2/templates/templatename/site/templates/templatename/css
[14:44:23] MLSD /public_html/dev2/templates/templatename/site/templates/templatename
[14:44:23] MLSD /public_html/dev2/templates/templatename/site/templates
[14:44:23] MLSD /public_html/dev2/templates/templatename/site
[14:44:23] MLSD /public_html/dev2/templates/templatename
[14:44:23] MLSD /public_html/dev2/templates
[14:44:23] MKDIR /public_html/dev2/templates/templatename/site
[14:44:23] MKDIR /public_html/dev2/templates/templatename/site/templates
[14:44:23] MKDIR /public_html/dev2/templates/templatename/site/templates/templatename
[14:44:23] MKDIR /public_html/dev2/templates/templatename/site/templates/templatename/css
[14:44:23] PUT /public_html/dev2/templates/templatename/site/templates/templatename/css/bootstrap.min.css
[14:44:23] UP 37% /public_html/dev2/templates/templatename/site/templates/templatename/css/bootstrap.min.css
[14:44:23] UP 74% /public_html/dev2/templates/templatename/site/templates/templatename/css/bootstrap.min.css
[14:44:23] UP 100% /public_html/dev2/templates/templatename/site/templates/templatename/css/bootstrap.min.css
[14:44:23] Finished 'deploy' after 1.86 s
[14:44:23] Starting 'default'...
[14:44:23] Finished 'default' after 8.9 μs
[14:44:23] DISC
当我使用 FTP 程序去那里时,我发现了这个:
/public_html/dev2/templates/templatename/site/templates/templatename/css/bootstrap.min.css
您能否解释一下要调整哪些内容才能将 bootstrap.min.css 文件上传到服务器上的正确目录?
谢谢,
汤姆
最佳答案
我也遇到了同样的问题。 Vinylftp 在目标文件夹中创建源文件夹结构。为了避免这个问题,只需将目标更改为网络服务器的根目录即可。 我的代码有点不同,但它显示了问题出在哪里。
var gulp = require('gulp');
var gutil = require( 'gulp-util' );
var ftp = require( 'vinyl-ftp' );
/** Configuration **/
var user = 'username';
var password = 'password';
var host = 'hostname';
var port = 21;
var localFilesGlob = ['public_html/templates/protostar/css/template.css'];
var remoteFolder = ''; // <- HERE MUST BE AN EMPTY STRING TO POINT THE VINYLFTP TO ROOT
function getFtpConnection() {
return ftp.create({
host: host,
port: port,
user: user,
password: password,
parallel: 5,
log: gutil.log
});
}
gulp.task('ftp-deploy', function() {
var conn = getFtpConnection();
return gulp.src(localFilesGlob, { base: '.', buffer: false })
.pipe( conn.newer( remoteFolder ) ) // only upload newer files
.pipe( conn.dest( remoteFolder ) )
;
});
关于node.js - 用于 Gulp 配置的 Vinyl FTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44308257/