|
| 1 | +import { readdir, stat, writeFile, readFile } from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { dirname } from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = dirname(__filename); |
| 8 | + |
| 9 | +// Project root directory, back one directory from repo_management_files. |
| 10 | +// normalize the 'directoryPath' string to go from the form `/absolute_path/template-nodes-project/repo_management_files/../` |
| 11 | +// to `/absolute_path/template-nodes-project/` to subtract from the beginning of the filePath |
| 12 | +const directoryPath = path.normalize(__dirname + '/../'); |
| 13 | + |
| 14 | +// Exclude auto generated files and directories. |
| 15 | +const exclude = ['.dccache', '.git', 'node_modules', 'dist', 'coverage', 'CHANGELOG.md', 'bun.lockb', 'docs']; |
| 16 | + |
| 17 | +/** |
| 18 | + * Subtracts all occurrences of a sub-string from a main string. |
| 19 | + * |
| 20 | + * This function takes two strings as input: a main string and a sub-string. |
| 21 | + * It removes all occurrences of the sub-string from the main string. |
| 22 | + * |
| 23 | + * @param {string} mainString - The main string from which sub-string will be removed. |
| 24 | + * @return {string} The resulting string after removing all occurrences of the sub-string. |
| 25 | + */ |
| 26 | +function subtractStrings(mainString) { |
| 27 | + // Create a regular expression to match all occurrences of the subString |
| 28 | + const regex = new RegExp(directoryPath, 'g'); |
| 29 | + |
| 30 | + // Replace all occurrences of subString with an empty string |
| 31 | + |
| 32 | + return mainString.replace(regex, '').toLowerCase(); |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +/** |
| 37 | + * Lists all files and directories in a directory excluding specified items. |
| 38 | + * @param {string} dirPath - The directory path to list items from. |
| 39 | + * @param {Array<string>} excludeList - The list of file and directory names to exclude. |
| 40 | + * @returns {Promise<Array<string>>} - A promise that resolves to the list of file paths. |
| 41 | + */ |
| 42 | +function listFilesAndDirectories(dirPath, excludeList) { |
| 43 | + const fileScopes = []; |
| 44 | + |
| 45 | + return new Promise((resolve, reject) => { |
| 46 | + readdir(dirPath, (err, files) => { |
| 47 | + if (err) { |
| 48 | + return reject(`Unable to read directory: ${err.message}`); |
| 49 | + } |
| 50 | + |
| 51 | + const fileStatPromises = files.map(file => { |
| 52 | + const filePath = path.join(dirPath, file); |
| 53 | + |
| 54 | + return new Promise((resolve, reject) => { |
| 55 | + if (excludeList.includes(file)) { |
| 56 | + return resolve(); |
| 57 | + } |
| 58 | + |
| 59 | + stat(filePath, (err, stats) => { |
| 60 | + if (err) { |
| 61 | + return reject(`Unable to stat file: ${err.message}`); |
| 62 | + } |
| 63 | + |
| 64 | + if (stats.isDirectory()) { |
| 65 | + // Recursively list the contents of the directory |
| 66 | + listFilesAndDirectories(filePath, excludeList).then(innerFileScopes => { |
| 67 | + fileScopes.push(...innerFileScopes); |
| 68 | + resolve(); |
| 69 | + }).catch(reject); |
| 70 | + } else { |
| 71 | + // File |
| 72 | + fileScopes.push(filePath); |
| 73 | + resolve(); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + Promise.all(fileStatPromises).then(() => { |
| 80 | + resolve(fileScopes); |
| 81 | + }).catch(reject); |
| 82 | + }); |
| 83 | + }); |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +/** |
| 88 | + * Updates the scopes array in the package.json file. |
| 89 | + * |
| 90 | + * @param {string} packageJsonPath - The path to the package.json file. |
| 91 | + * @param {string[]} newScopes - The new scopes array to set. |
| 92 | + */ |
| 93 | +function updateScopesInPackageJson(packageJsonPath, newScopes) { |
| 94 | + // Read the package.json file |
| 95 | + readFile(packageJsonPath, 'utf8', (err, data) => { |
| 96 | + if (err) { |
| 97 | + console.error('Error reading package.json:', err); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + // Parse the JSON data |
| 103 | + const packageJson = JSON.parse(data); |
| 104 | + |
| 105 | + // Update the scopes array |
| 106 | + if (packageJson.config && packageJson.config['cz-gitmoji-adapter']) { |
| 107 | + packageJson.config['cz-gitmoji-adapter'].scopes = newScopes; |
| 108 | + //console.log(newScopes); |
| 109 | + } else { |
| 110 | + console.error('cz-gitmoji-adapter config not found in package.json'); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + // Convert the modified JSON object back to a string |
| 115 | + const updatedPackageJson = JSON.stringify(packageJson, null, 2); |
| 116 | + |
| 117 | + // Write the updated package.json back to the file |
| 118 | + writeFile(packageJsonPath, updatedPackageJson, 'utf8', (err) => { |
| 119 | + if (err) { |
| 120 | + console.error('Error writing package.json:', err); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + console.log('Successfully updated the scopes array in package.json'); |
| 125 | + }); |
| 126 | + } catch (err) { |
| 127 | + console.error('Error parsing package.json:', err); |
| 128 | + } |
| 129 | + }); |
| 130 | +} |
| 131 | + |
| 132 | +listFilesAndDirectories(directoryPath, exclude) |
| 133 | + .then(fileScopes => { |
| 134 | + updateScopesInPackageJson(directoryPath + 'package.json', fileScopes.map(subtractStrings)); |
| 135 | + }) |
| 136 | + .catch(err => { |
| 137 | + console.error('Error:', err); |
| 138 | + }) |
0 commit comments