| | 1514 | grunt.registerTask( 'folders', 'Ensures every directory has an index.php file.', function() { |
| | 1515 | var folders = [ |
| | 1516 | 'js', |
| | 1517 | 'wp-includes', |
| | 1518 | 'wp-admin', |
| | 1519 | 'wp-content', |
| | 1520 | ], |
| | 1521 | blocked = [ |
| | 1522 | 'node_modules', |
| | 1523 | 'src/js/_enqueues', |
| | 1524 | 'src/wp-admin/network/', |
| | 1525 | 'src/wp-admin/user/', |
| | 1526 | 'src/wp-includes/blocks/', |
| | 1527 | 'src/wp-includes/ID3', |
| | 1528 | 'src/wp-includes/js', |
| | 1529 | 'src/wp-includes/PHPMailer', |
| | 1530 | 'src/wp-includes/pomo', |
| | 1531 | 'src/wp-includes/Requests', |
| | 1532 | 'src/wp-includes/SimplePie', |
| | 1533 | 'src/wp-includes/sodium_compat', |
| | 1534 | 'src/wp-includes/Text', |
| | 1535 | ], |
| | 1536 | prefix = 'src/'; |
| | 1537 | |
| | 1538 | /** |
| | 1539 | * |
| | 1540 | * @param {string} dirPath Directory path to search against. |
| | 1541 | * @param {array} arrayOfFiles Array of files in the directory. |
| | 1542 | * @param {array} arrayOfFolders Array of directories in the directory. |
| | 1543 | * @returns {array} Array of folders |
| | 1544 | */ |
| | 1545 | var getAllFolders = function( dirPath, arrayOfFiles, arrayOfFolders ) { |
| | 1546 | // Let's get all of the folders on this path. |
| | 1547 | var files = fs.readdirSync( dirPath ); |
| | 1548 | arrayOfFiles = arrayOfFiles || []; |
| | 1549 | arrayOfFolders = arrayOfFolders || []; |
| | 1550 | |
| | 1551 | // Let's loop through the folders, looking for more directories. |
| | 1552 | files.forEach(function(file) { |
| | 1553 | // Let's look for a directory. |
| | 1554 | if (fs.statSync(dirPath + "/" + file).isDirectory()) { |
| | 1555 | // Let's see if the path contains any of the `blocked` paths. |
| | 1556 | var dir = dirPath + "/" + file; |
| | 1557 | var block = false; |
| | 1558 | blocked.map(function(path) { |
| | 1559 | if( dir.includes(path) ) { |
| | 1560 | block = true; |
| | 1561 | } |
| | 1562 | }); |
| | 1563 | // This is a legit path, let's add to our array of valid paths. |
| | 1564 | if ( ! block ) { |
| | 1565 | arrayOfFolders.push( dir ); |
| | 1566 | arrayOfFolders = getAllFolders( dirPath + "/" + file, arrayOfFiles, arrayOfFolders ); |
| | 1567 | } |
| | 1568 | } else { |
| | 1569 | // Keep digging for paths. |
| | 1570 | arrayOfFiles.push( path.join( __dirname, dirPath, "/", file ) ); |
| | 1571 | } |
| | 1572 | }); |
| | 1573 | return arrayOfFolders; |
| | 1574 | |
| | 1575 | } |
| | 1576 | |
| | 1577 | /** |
| | 1578 | * |
| | 1579 | * @param {string} dirPath Path to start a search for index.php files. |
| | 1580 | * @returns {bool} |
| | 1581 | */ |
| | 1582 | var doesDirHaveIndex = function ( dirPath ) { |
| | 1583 | var index = '/index.php'; |
| | 1584 | return fs.existsSync(dirPath + index); |
| | 1585 | } |
| | 1586 | |
| | 1587 | // Loop through folders, looking for blank index.php files. |
| | 1588 | folders.forEach(function(path) { |
| | 1589 | var folder = getAllFolders( prefix + path ); |
| | 1590 | folder.forEach(function(path) { |
| | 1591 | if( ! doesDirHaveIndex( path ) ) { |
| | 1592 | var newFile = path + '/index.php'; |
| | 1593 | // Read a default file, and stream that into a new file. |
| | 1594 | var index = fs.readFileSync('src/wp-content/index.php'); |
| | 1595 | // The `wx` flag ensures that it doesn't overwrite any existing files. |
| | 1596 | fs.writeFileSync( newFile, index, {flag: "wx"} ); |
| | 1597 | } |
| | 1598 | }) |
| | 1599 | }); |
| | 1600 | |
| | 1601 | } ); |
| | 1602 | |