gulp | Fix project relatives as a service for updating files when they change | Cell 6 | Search

This code defines a function searchNotebooks that searches for text within Jupyter Notebook files in a project directory and returns a promise that resolves with an array of matching cells.

Run example

npm run import -- "search notebooks gulp"

search notebooks gulp

var importer = require('../Core');
var path = require('path');
var gulp = require('gulp');
var tap = require('gulp-tap');
var Duplex = require('stream').Duplex;
var tasksToPromise = importer.import("gulp tasks to promise",
"{gulp}");

var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var project = path.join(__dirname, '..');

function bufferToStream(buffer) {
    let stream = new Duplex();
    stream.push(buffer);
    stream.push(null);
    return stream;
}

function searchNotebooks(search) {
    var cells;
    gulp.task('search notebooks', function () {
        cells = [];
        return gulp.src(
            ['**/*.ipynb', '!node_modules{,/**}', '!**/\\.*'],
            {cwd: project})
            .pipe(tap(file => {
                const result = importer.getCells(file.path);
                result.forEach(r => {
                    if(r.source.join('').match(search)) {
                        cells.push(r);
                    }
                })
            }));
    });

    return tasksToPromise(['search notebooks'], [])
        .then(() => cells);
}

module.exports = searchNotebooks;

What the code could have been:

// Import required modules
const { getCells } = require('../Core');
const path = require('path');
const gulp = require('gulp');
const tap = require('gulp-tap');
const { Duplex } = require('stream');
const tasksToPromise = require('../Core').import('gulp tasks to promise', { gulp });

// Define constants
const PROFILE_PATH = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) || '';
const PROJECT_ROOT = path.join(__dirname, '..');

/**
 * Convert a buffer to a stream
 * @param {Buffer} buffer The buffer to convert
 * @returns {Duplex} The resulting stream
 */
function bufferToStream(buffer) {
    const stream = new Duplex();
    stream.push(buffer);
    stream.push(null);
    return stream;
}

/**
 * Search notebooks for a given search term
 * @param {string} search The search term to use
 * @returns {Promise<Array>} A promise resolving to an array of matching cells
 */
async function searchNotebooks(search) {
    // Initialize an empty array to store matching cells
    let cells = [];

    // Define the gulp task to search for notebooks
    const searchTask = gulp.task('search notebooks', async () => {
        // Use gulp.src to find all notebooks in the project root
        await gulp.src(['**/*.ipynb', '!node_modules{,/**}', '!**/\\.*'], { cwd: PROJECT_ROOT })
           .pipe(tap((file) => {
                // Get the cells from the notebook
                const result = getCells(file.path);

                // Filter the cells based on the search term
                result.forEach((r) => {
                    if (r.source.join('').match(search)) {
                        cells.push(r);
                    }
                });
            }));
    });

    // Run the gulp task and wait for it to complete
    await tasksToPromise(['search notebooks'], []);

    // Return the array of matching cells
    return cells;
}

// Export the searchNotebooks function
module.exports = searchNotebooks;

This code defines a function searchNotebooks that searches for specific text within Jupyter Notebook files within a project directory.

Here's a breakdown:

  1. Initialization:

  2. bufferToStream Function:

  3. searchNotebooks Function:

  4. Export: