Skip to content

Commit

Permalink
Setup yarn workspace for AE design patterns mock form app (#33506)
Browse files Browse the repository at this point in the history
* add package.json for using yarn workspaces

* fix: get workspace working by adding package path to root package.json

* fix: remove comma

* add nodemon package to workspace

* test node script to nuke all the node_modules with a fancy spinner lol

* jsdoc comments
  • Loading branch information
adamwhitlock1 authored Dec 13, 2024
1 parent 9548fe8 commit 20acea2
Show file tree
Hide file tree
Showing 5 changed files with 1,060 additions and 264 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@
"src/applications/vaos",
"src/applications/claims-status",
"src/applications/accredited-representative-portal/accreditation/21a",
"src/applications/accredited-representative-portal"
"src/applications/accredited-representative-portal",
"src/applications/_mock-form-ae-design-patterns"
],
"nohoist": [
"**/applications-accredited-representative-portal/accreditation/21a/react-router",
Expand Down
29 changes: 29 additions & 0 deletions src/applications/_mock-form-ae-design-patterns/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@department-of-veterans-affairs/applications-mock-form-ae-design-patterns",
"version": "1.0.0",
"scripts": {
"demo": "echo DEMOING $npm_package_name v $npm_package_version with WORKSPACES"
},
"licence": "MIT",
"dependencies": {},
"devDependencies": {
"@testing-library/dom": "^7.26.6",
"@testing-library/react": "^11.1.2",
"chai": "^4.3.4",
"mockdate": "^3.0.5",
"mocker-api": "^2.9.0",
"nodemon": "^3.1.7",
"sinon": "^3.2.1"
},
"peerDependencies": {
"lodash": "*",
"prop-types": "*",
"react": "*",
"react-redux": "*",
"react-router": "*",
"react-router-dom": "*",
"redux": "*",
"redux-mock-store": "*"
},
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// disabling rule since this is run in node and not in browser
/* eslint-disable no-console */
const fs = require('fs-extra');
const path = require('path');
const readline = require('readline');
const createSpinner = require('./spinner');

/**
* find all node_modules directories in a directory
* @param {string} dir - directory to search for node_modules
* @returns {string[]} array of paths to node_modules directories that were found
*/
const findNodeModules = dir => {
try {
const results = [];
const items = fs.readdirSync(dir);

for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);

if (stat.isDirectory()) {
if (item === 'node_modules') {
results.push(fullPath);
} else if (!fullPath.includes('node_modules')) {
// Don't search inside other node_modules directories
results.push(...findNodeModules(fullPath));
}
}
}

return results;
} catch (err) {
console.error(`Error searching directory ${dir}:`, err);
return [];
}
};

/**
* confirm deletion of node_modules directories
* @param {string[]} folders - array of paths to node_modules directories
* @returns {Promise<boolean>} resolves to true if confirmed deletion, false otherwise
*/
const confirmDeletion = async folders => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

console.log('Found these node_modules folders:');
folders.forEach(folder => console.log(folder));

return new Promise(resolve => {
rl.question(
'Are you sure you want to delete these directories? (Y/N) ',
answer => {
rl.close();
resolve(answer.toLowerCase() === 'y');
},
);
});
};

/**
* delete all node_modules directories in a directory
* @param {string} startDir - directory to search for node_modules
*/
const deleteNodeModules = async (startDir = '.') => {
try {
const foundDirs = findNodeModules(startDir);

if (foundDirs.length === 0) {
console.log('No node_modules directories found.');
return;
}

const confirmed = await confirmDeletion(foundDirs);

if (confirmed) {
const spinner = createSpinner('Deleting node_modules directories...');
await Promise.all(
foundDirs.map(dir => {
console.log(`Deleted ${dir}`);
return fs.remove(dir);
}),
);
spinner.stop('Deletion complete.');
} else {
console.log('Operation cancelled.');
}
} catch (err) {
console.error('Error:', err);
process.exit(1);
}
};

// Get directory from command line argument or use current directory
const targetDir = process.argv[2] || '.';
deleteNodeModules(targetDir);
22 changes: 22 additions & 0 deletions src/applications/_mock-form-ae-design-patterns/script/spinner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-console */
const createSpinner = message => {
const frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
let i = 0;

process.stdout.write('\x1B[?25l'); // Hide cursor
const spinner = setInterval(() => {
process.stdout.write(`\r${frames[i]} ${message}`);
i = (i + 1) % frames.length;
}, 80);

return {
stop: endMessage => {
clearInterval(spinner);
process.stdout.write('\r\x1B[K'); // Clear line
process.stdout.write('\x1B[?25h'); // Show cursor
if (endMessage) console.log(endMessage);
},
};
};

module.exports = createSpinner;
Loading

0 comments on commit 20acea2

Please sign in to comment.