Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added code Readability in this file #13694

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@
"*.{js,json,yml,yaml,css,scss,ts,tsx,md}": [
"prettier --write"
]
},
"dependencies": {
"react-scripts": "^5.0.1"
}
}
23 changes: 15 additions & 8 deletions tasks/screencast-start.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,53 @@ const execa = require('execa');
const meow = require('meow');
const multimatch = require('multimatch');

//The main entry point for the program, which starts execution based on CLI arguments
main(meow());

//Main function that controls the execution of the command specified by the user
function main(cli) {
let count = 0;
let count = 0;// Keeps track of the times the pattern has matched

const start = Date.now();
const duration = parseInt(cli.flags.timeout, 10) * 1000;
const start = Date.now();//start time for the process
const duration = parseInt(cli.flags.timeout, 10) * 1000;//time limit in milliseconds
const cp = execa(cli.flags.command, { shell: true });

const target = parseInt(cli.flags.patternCount || '1', 10);

//listening to standard output (stdout) of the running command.
cp.stdout.on('data', data => {
process.stdout.write(data);
const matches = multimatch([String(data)], cli.flags.pattern);
const errMatches = multimatch([String(data)], cli.flags.errorPattern);

if (matches.length > 0) {
count++;
count++;//increment the match count
}

if (errMatches.length > 0) {
process.exit(1);
process.exit(1);//exits the err if err is found
}

//if the target number of matches is reached, exit after a delay.
if (count >= target) {
setTimeout(() => {
process.exit(0);
process.exit(0);//exits successfully (code 0)
}, duration);
}
});

//Handles when command exits
cp.on('exit', e => {
const elapsed = Date.now() - start;
const elapsed = Date.now() - start;//Calculate the elapsed time since the command started

//if the process took longer than the timeout, exit immediately
if (elapsed >= duration) {
return;
}

//Waiting for the remaining duration before exiting with the same exit code
setTimeout(() => {
process.exit(e.exitCode);
process.exit(e.exitCode);//exit with the code returned by the command
}, duration - elapsed);
});
}
45 changes: 28 additions & 17 deletions tasks/screencast.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,77 @@

'use strict';

// Import necessary modules
const fs = require('fs');
const path = require('path');
const execa = require('execa');
const tempy = require('tempy');

main();
main();// Execute the main function

function main() {
const previous = process.cwd();
const cwd = tempy.directory();
const previous = process.cwd();// Store the current working directory
const cwd = tempy.directory();// Create a temporary directory for the screencast

const cast = path.join(cwd, 'screencast.json');
const script = path.join(__dirname, 'screencast.sh');
const out = path.join(previous, 'screencast.svg');
const cast = path.join(cwd, 'screencast.json');// Path for the screencast JSON file
const script = path.join(__dirname, 'screencast.sh');// Path for the shell script to run
const out = path.join(previous, 'screencast.svg'); // Path for the output SVG file

// Define functions to identify specific lines in the output
const resolveLine = l => l.indexOf('🔍 Resolving packages...') > -1;
const fetchLine = l => l.indexOf('🚚 Fetching packages...') > -1;
const countLine = l => l.match(/Saved [0-9]+ new dependencies/);
const doneLine = l => l.indexOf('✨ Done in') > -1;

try {
process.chdir(cwd);
console.log(`Recording screencast ...`);
process.chdir(cwd);// Change to the temporary directory
console.log(`Recording screencast ...`);// Log the recording start message
// Execute the 'asciinema' command to record the output of the shell script
execa.sync('asciinema', ['rec', '--command', `sh ${script}`, cast], {
cwd,
stdio: 'inherit',
stdio: 'inherit',// Inherit stdio to see the output
});

console.log('Cleaning data ...');
const data = require(cast);
console.log('Cleaning data ...');// Log the data cleaning start message
const data = require(cast); // Load the recorded screencast data

// Cut the recorded frames to keep only relevant parts
cut(data.stdout, { start: resolveLine, end: fetchLine });
cut(data.stdout, { start: countLine, end: doneLine });
// Replace the current working directory in the frames with '~' for privacy
replace(data.stdout, [{ in: cwd, out: '~' }]);

// Write the cleaned data back to the screencast JSON file
fs.writeFileSync(cast, JSON.stringify(data, null, ' '));

console.log('Rendering SVG ...');
console.log('Rendering SVG ...');// Log the SVG rendering start message
// Execute the 'svg-term' command to render the screencast into an SVG file
execa.sync('svg-term', ['--window', '--in', cast, '--out', out]);

// Log success messages for both the recorded screencast and rendered SVG
console.log(`Recorded screencast to ${cast}`);
console.log(`Rendered SVG to ${out}`);
} finally {
process.chdir(previous);
process.chdir(previous);// Ensure the working directory is restored
}
}

// Function to cut the frames based on specified start and end conditions
function cut(frames, { start, end }) {
const si = frames.findIndex(([, l]) => start(l));
const ei = frames.findIndex(([, l]) => end(l));
const si = frames.findIndex(([, l]) => start(l)); // Find the start index
const ei = frames.findIndex(([, l]) => end(l));// Find the end index

// If either index is not found, exit the function
if (si === -1 || ei === -1) {
return;
}

// Remove the frames between the start and end indices
frames.splice(si + 1, ei - si - 1);
}

// Function to replace specified strings in the frames
function replace(frames, replacements) {
frames.forEach(frame => {
replacements.forEach(r => (frame[1] = frame[1].split(r.in).join(r.out)));
replacements.forEach(r => (frame[1] = frame[1].split(r.in).join(r.out)));// Replace occurrences of 'in' with 'out'
});
}