You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So I've found this nice were one can post his problems to receive community help? ChatGpt.txt
My problem boils down to that the sequence gulp.src('…').replace('…','…').php2html().htmlmin().gulp.dest('…') is not executing the php2html. No error, but on action either. A control message (console.log) at the beginning of my routine will issue, indicating that the watch fired and called correctly. I attached a question I posted to ChatGpt that describes exactly what I want to do with gulp (but it's answers were of no help).
Here comes my gulpfile, help welcome, please!
// gulpfile.js is in a subdirectory. go to directiory, that is ./ throughout the script:
process.chdir('..');
const gulp = require('gulp'),
htmlmin = require('gulp-htmlmin'),
php2html = require('gulp-php2html'),
replace = require('gulp-replace'),
langs = ['en', 'es', 'ga', 'de']
htmlmin_options = {
// nothing special. scipping these for brevity
},
php2html_options = {
verbose: true,
haltOnError: true,
bin: 'C:/Program Files/php/php.exe'
}
;
function changeLang(lang, cb) {
const isDefaultLang = lang === 'en',
destPath = isDefaultLang ? '../' : `../${lang}/`,
replace_options = { replace: '$curLang="en"',
with: `$curLang="${lang}"` }
;
console.log(`in changeLang2 for ${lang}`);
gulp
.src('./index.php')
.pipe(replace(replace_options.replace,replace_options.with))
.pipe(gulp.dest(destPath));
cb();
}
function php2htmlMinDel(lang, cb) { // not working
const isDefaultLang = lang === 'en',
watchFile = isDefaultLang ? '../index.php' : `../${lang}/index.php`
;
console.log(`in php2htmlMinDel2 for ${lang}`);
gulp
.src(watchFile)
//.pipe(gulp.dest('test/.')); // TEST: Ok, yes, the file stream is ready and complete.
.pipe(php2html(php2html_options))
.pipe(htmlmin(htmlmin_options))
.pipe(gulp.dest('.'));
cb();
}
// TEST: both tasks in one routine: (not working. ChatGpt says reason is that replace is syncronous while php2html is async)
// This is what I would really like to run.
function singleLangChangeLang(lang, cb) {
const isDefaultLang = lang === 'en',
destPath = isDefaultLang ? '../' : `../${lang}/`,
replace_options = { replace: '$curLang="en"',
with: `$curLang="${lang}"` }
;
console.log(`in singleLangChangeLang for ${lang}`);
gulp
.src('./index.php')
.pipe(replace(replace_options.replace,replace_options.with))
.pipe(php2html(php2html_options))
.pipe(htmlmin(htmlmin_options))
.pipe(gulp.dest(destPath));
cb();
}
// TEST: interesting: only htmlmin without php2html: works.
// this is the same construct as with 'php2htmlMinDel' and it works.
// So the problem is not program flow or parameters, but it's with php2html.
function onlyHtmlMinimize(lang, cb) {
// testfile test/test.html:
// <body>
// <!-- comment and cr/lf will be removed. String $curLang="es" to be altered. -->
// <?=$curLang="en";?>
// </body>
const watchFile = 'test/test.html'
replace_options = { replace: '$curLang="en"',
with: `$curLang="${lang}"` }
;
console.log(`in onlyHtmlMinimize for ${lang}`);
gulp
.src(`${watchFile}`)
.pipe(replace(replace_options.replace,replace_options.with))
//.pipe(php2html(php2html_options))
.pipe(htmlmin(htmlmin_options))
.pipe(gulp.dest('test/changed/.'));
cb();
}
// HERE COME ALL THE TASKS AND TESTS:
gulp.task('watch', function (done) {
// 1. lang/lang_strs_….json SINGLE language file change:
// This is split in two parts:
// 1.) changeLang: take ./index.php and replace the language string. Write new index.php to ${lang} directory.
// 2.) Watch ${lang} directories for new/changed index.php file, php2html convert it, minimize it, write back to same directory as index.html
// Reason: replace() followed by php2html() in same task doesn't work, because former is a synchrounos task while latter is asysc (thats what ChatGpt says)
langs.forEach(lang => {
gulp.watch( [`./lang/lang_strs_${lang}.json`] ,
gulp.series(
changeLang.bind(null, lang), // ok
php2htmlMinDel.bind(null, lang) // doing nothing
));
});
// 2.) On ./*.PHP change: do the langTask for ALL languages:
gulp.watch( ['*.php'] , gulp.series(
langs.map(lang => changeLang.bind(null, lang)), // ok. produces .php with changed string
langs.map(lang => php2htmlMinDel.bind(null, lang)) // Not working: doesn't produce .html
));
// TEST: do the above in one routine: (change globs to try)
gulp.watch( ['*.phpXXX'] , gulp.series(
langs.map(lang => singleLangChangeLang.bind(null, lang))
));
// TEST: does php2html work at all? => Yes, working ok.
gulp.watch('test/test.php', function php2HtmlTest () {
return gulp
.src('test/test.php')
.pipe(php2html(php2html_options))
.pipe(gulp.dest('test/.'));
});
// TEST: with same construct like php2htmlMinDel, onlyHtmlMinimize works:
// (to try, manually create test/test.html)
gulp.watch('test/test.html', gulp.series(
onlyHtmlMinimize.bind(null, 'xx')
));
done();
});
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
So I've found this nice were one can post his problems to receive community help?
ChatGpt.txt
My problem boils down to that the sequence gulp.src('…').replace('…','…').php2html().htmlmin().gulp.dest('…') is not executing the php2html. No error, but on action either. A control message (console.log) at the beginning of my routine will issue, indicating that the watch fired and called correctly. I attached a question I posted to ChatGpt that describes exactly what I want to do with gulp (but it's answers were of no help).
Here comes my gulpfile, help welcome, please!
Beta Was this translation helpful? Give feedback.
All reactions