A
A
aheadweb2020-01-03 12:35:00
JavaScript
aheadweb, 2020-01-03 12:35:00

File generation in gulp. Automating build processes?

To automate processes, I want to introduce automatic creation of the necessary files into the assembly [gulp + webpack (only js)].
What you need: in the pages folder are *.pug || *.html files of layout pages. When adding any file to this folder, it is necessary to make the automatic creation of the file in another controllers folder approx. pagename.controller.js In which the template code will already lie.
Structure example:
pages
- about.pug
controllers
- about.controller.js
After adding the store.pug file to the pages folder.
pages
- about.pug
- store.pug
controllers
- about.controller.js
- store.controller.js - automatically generated file with pre-existing template code.
Question.
1. In which direction to dig, in terms of ready-made solutions, materials.
2. If you use a regular plugin to create files using gulp, how is the logic of automatic substitution of imports in the necessary files carried out? Or is it necessary to hang a gulp listener on a new file?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2020-01-03
@aheadweb

Google this way

const gulp = require('gulp');
const fs = require('fs');
const path = require('path');

const watcher = gulp.watch('./pages', { events: ['add'] });

watcher.on('add', function(p, stats) {
  const name = path.basename(p, path.extname(p));
  fs.writeFileSync(`./controllers/${name}.controller.js`, 'то, что будет в файле');
});

+ a lot of checks
PS: although normal people do this through console commands, for example laravel
php artisan make:controller PhotoController

V
Vyach Gor, 2020-01-03
@sharnirio

Maybe not quite the same, but nevertheless similar - link

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question