How do you quickly convert all Jade files into Pug files?
Asked Answered
W

2

5

Jade isn't called "Jade" any more. It is now known as "Pug". In order to convert an old jade project into the new pug format, you need to rename all the old .jade files to .pug files.

Doing it by hand is painful and tedious, surely there is a faster way to do it?

Wirehaired answered 7/7, 2016 at 9:50 Comment(0)
M
5

On Windows, you can rename extension recursively by this command:

for /R %x in (*.jade) do ren "%x" *.pug

Taken from this answer.

Note that this only works in Windows Command Prompt. You will receive errors if you try to run this command in programs like Git Bash or Windows PowerShell.

If you are a Mac user, use this command in Mac Terminal:

find . -name '*.jade' | xargs rename 's/\.jade$/.pug/'
Marrs answered 18/10, 2017 at 8:31 Comment(1)
Just a note: rename is not a standard unix command, and is not available on a mac. Not a big issue, however, a brew install rename can solve the problem :)Conscript
W
3

I made this Gulp task to convert all the files for me in one hit :)

It requires the gulp-rename and del npm plugins to work.

Use this if all the jade files are within the root gulp folder (ie. the folder that the main gulp file is in)

//Use this if all jade files are inside gulps root folder

var rename = require("gulp-rename");
var del = require('del');

gulp.task('switch-to-pug', function() {
    console.log('\nCreated:\n');
    gulp.src(['**/*.jade'])
        .pipe(rename(function(path){
            path.extname = ".pug";
            console.log(path.dirname+'/'+path.basename + path.extname+'\n');
        }))
        .pipe(gulp.dest('./'))
        .on('end', function(){
            del(['**/*.jade']).then(function(paths){
                console.log('\nDeleted:\n\n', paths.join('\n'));
            });
        });
});

Use this (and edit the paths to suit your needs) if there are files outside the root gulp folder that you also want to rename:

//Use this (and edit accordingly) if jade files are also found outside the root folder

var rename = require("gulp-rename");
var del = require('del');

gulp.task('switch-to-pug', function() {
    console.log('\nCreated:\n');
    gulp.src(['../**/*.jade'])
        .pipe(rename(function(path){
            path.extname = ".pug";
            console.log(path.dirname+'/'+path.basename + path.extname+'\n');
        }))
        .pipe(gulp.dest('../'))
        .on('end', function(){
            del(['../**/*.jade'], { force: true }).then(function(paths){
                console.log('\nDeleted:\n\n', paths.join('\n'));
            });
        });
});

Then just run this and it will change all the files for you :)

gulp switch-to-pug
Wirehaired answered 7/7, 2016 at 9:50 Comment(4)
As much as I like gulp, it's really the wrong tool for the job in this case. Simply typing find . -name '*.jade' | xargs rename 's/\.jade$/.pug/' into a terminal would have been way faster and easier than this.Trentontrepan
I couldn't find an easy way to bulk rename files by Googling for it and I knew Gulp had the capacity to do it so that's why I ended up doing it that way. I didn't know any better.Wirehaired
I've edited the question to be less specific. Mind posting your answer as a proper answer?Wirehaired
That command doesn't seem to work on windows :( I get the error: xargs: rename: No such file or directoryWirehaired

© 2022 - 2024 — McMap. All rights reserved.