This Gulp script automates the replacement of a specific string within project files, likely for build or deployment purposes, and handles asynchronous execution with error handling.
npm run import -- "use gulp"
var gulp = require('gulp');
var tap = require('gulp-tap');
var replace = require('gulp-replace');
// rename the project
gulp.task('build :src :dest', function (files, project) {
return gulp
.src(files, {cwd: project})
.pipe(replace('appId: \'my-app-id\'', 'appId: \'jupytangular-module-server\''))
.pipe(tap(function (file) {
console.log(file.contents.toString());
}))
});
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var project = PROFILE_PATH + '/Documents/universal';
gulp.task('default', () => gulp.task(['build :src :dest'])(['**/app.browser.module.ts'], project));
$.async()
try {
gulp.series(() => gulp.task(['default'])(), function (done) {
done();
$.sendResult();
})();
} catch (e) {
$.sendError(e);
}
// Import required gulp plugins
const { src, dest, series, parallel } = require('gulp');
const replace = require('gulp-replace');
const tap = require('gulp-tap');
const path = require('path');
const dotenv = require('dotenv'); // Load environment variables from.env file
// Load environment variables from.env file
dotenv.config();
// Define constants
const PROJECT_DIR = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
const PROJECT_PATH = path.join(PROJECT_DIR, 'Documents/universal');
const SRC_FILES = path.join(PROJECT_PATH, '**/app.browser.module.ts');
// Gulp task: Build project
gulp.task('build', (cb) => {
// Use parallel to run multiple tasks concurrently
return parallel(
// Rename the project
() => src(SRC_FILES, { cwd: PROJECT_PATH })
.pipe(replace('appId: \'my-app-id\'', 'appId: \'jupytangular-module-server\''))
.pipe(tap((file) => {
console.log(file.contents.toString());
})),
)(cb);
});
// Gulp task: Default
gulp.task('default', async () => {
try {
// Use series to run tasks sequentially
await series('build');
$.sendResult();
} catch (e) {
$.sendError(e);
}
});
// Gulp task: Run default task
gulp.task('run', Gulp.task('default'));
This code snippet uses the Gulp build tool to modify a project's source code.
Here's a breakdown:
Dependencies:
gulp
: The core Gulp library for defining tasks and pipelines.gulp-tap
: A Gulp plugin that allows you to tap into the stream of files being processed, giving you access to their contents.gulp-replace
: A Gulp plugin for replacing specific text patterns within files.build :src :dest
Task:
gulp.src(files, {cwd: project})
: Reads files specified by files
from the project
directory.pipe(replace('appId: \'my-app-id\'', 'appId: \'jupytangular-module-server\''))
: Replaces the string "appId: 'my-app-id'" with "appId: 'jupytangular-module-server'" in each file.pipe(tap(function (file) { console.log(file.contents.toString()); }))
: Logs the contents of each modified file to the console.Project Path:
PROFILE_PATH
: Determines the user's home directory based on environment variables.project
: Sets the project directory to PROFILE_PATH + '/Documents/universal'
.default
Task:
gulp
.build :src :dest
task with specific file patterns (**/app.browser.module.ts
) and the project
directory.Async Execution:
$.async()
and $.sendResult()
to handle asynchronous operations and send results back to an external system (likely a CI/CD pipeline).gulp.series()
call in a try...catch
block to handle potential errors.In essence, this code automates the process of replacing a specific string within multiple files in a project, likely as part of a build or deployment process.