|
| 1 | +const fs = require('fs'); |
| 2 | +const { join } = require('path'); |
| 3 | +const { last, set } = require('lodash'); |
| 4 | +const { google } = require('googleapis'); |
| 5 | +const { exec } = require('child_process'); |
| 6 | +const csvParser = require('csv-parser'); |
| 7 | +const { stringify } = require('csv-stringify'); |
| 8 | + |
| 9 | +const licenseFolderName = 'licenses'; |
| 10 | +const spreadsheetId = process.env.SPREADSHEET_ID; |
| 11 | +const outputFilePath = `./${licenseFolderName}/licenses.csv`; |
| 12 | +const summaryFilePath = `./${licenseFolderName}/summary.csv`; |
| 13 | +const allData = []; |
| 14 | +let csvFiles = []; |
| 15 | + |
| 16 | + |
| 17 | +// Main function |
| 18 | +async function main() { |
| 19 | + const folderPath = './'; |
| 20 | + const packageJsons = findPackageJsonFiles(folderPath); // Find all package.json files in the given folder |
| 21 | + |
| 22 | + console.log('All package.jsons was found:', packageJsons); |
| 23 | + |
| 24 | + // Create the folder if it doesn't exist |
| 25 | + if (!fs.existsSync(licenseFolderName)) { |
| 26 | + fs.mkdirSync(licenseFolderName); |
| 27 | + } |
| 28 | + |
| 29 | + try { |
| 30 | + await Promise.all(packageJsons.map(runLicenseCheck)); |
| 31 | + console.log('All csv files was generated'); |
| 32 | + await generateSummary() |
| 33 | + await sendLicensesToGoogleSheet() |
| 34 | + } catch (error) { |
| 35 | + console.error('An error occurred:', error); |
| 36 | + process.exit(1); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +main(); |
| 41 | + |
| 42 | +// Function to find all package.json files in a given folder |
| 43 | +function findPackageJsonFiles(folderPath) { |
| 44 | + const packageJsonPaths = []; |
| 45 | + const packageJsonName = 'package.json'; |
| 46 | + const excludeFolders = ['dist', 'node_modules', 'static', 'electron', 'redisgraph']; |
| 47 | + |
| 48 | + // Recursive function to search for package.json files |
| 49 | + function searchForPackageJson(currentPath) { |
| 50 | + const files = fs.readdirSync(currentPath); |
| 51 | + |
| 52 | + for (const file of files) { |
| 53 | + const filePath = join(currentPath, file); |
| 54 | + const stats = fs.statSync(filePath); |
| 55 | + |
| 56 | + if (stats.isDirectory() && !excludeFolders.includes(file)) { |
| 57 | + searchForPackageJson(filePath); |
| 58 | + } else if (file === packageJsonName) { |
| 59 | + packageJsonPaths.push(`./${filePath.slice(0, -packageJsonName.length - 1)}`); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + searchForPackageJson(folderPath); |
| 65 | + return packageJsonPaths; |
| 66 | +} |
| 67 | + |
| 68 | +// Function to run license check for a given package.json file |
| 69 | +async function runLicenseCheck(path) { |
| 70 | + const name = last(path.split('/')) || 'electron'; |
| 71 | + |
| 72 | + const COMMANDS = [ |
| 73 | + `license-checker --start ${path} --csv --out ./${licenseFolderName}/${name}_prod.csv --production`, |
| 74 | + `license-checker --start ${path} --csv --out ./${licenseFolderName}/${name}_dev.csv --development`, |
| 75 | + ] |
| 76 | + |
| 77 | + return await Promise.all(COMMANDS.map((command) => |
| 78 | + new Promise((resolve, reject) => { |
| 79 | + exec(command, (error, stdout, stderr) => { |
| 80 | + if (error) { |
| 81 | + console.error(`Failed command: ${commandProd}, error:`, stderr); |
| 82 | + reject(error); |
| 83 | + } |
| 84 | + resolve(); |
| 85 | + }); |
| 86 | + }) |
| 87 | + )); |
| 88 | +} |
| 89 | + |
| 90 | +async function sendLicensesToGoogleSheet() { |
| 91 | + try { |
| 92 | + const serviceAccountKey = JSON.parse(fs.readFileSync('./gasKey.json', 'utf-8')); |
| 93 | + |
| 94 | + // Set up JWT client |
| 95 | + const jwtClient = new google.auth.JWT( |
| 96 | + serviceAccountKey.client_email, |
| 97 | + null, |
| 98 | + serviceAccountKey.private_key, |
| 99 | + ['https://www.googleapis.com/auth/spreadsheets'] |
| 100 | + ); |
| 101 | + |
| 102 | + const sheets = google.sheets('v4'); |
| 103 | + |
| 104 | + // Read all .csv files in the 'licenses' folder |
| 105 | + csvFiles.forEach((csvFile) => { |
| 106 | + // Extract sheet name from file name |
| 107 | + const sheetName = csvFile.replace('.csv', '').replaceAll('_', ' '); |
| 108 | + |
| 109 | + const data = []; |
| 110 | + fs.createReadStream(`./${licenseFolderName}/${csvFile}`) |
| 111 | + .pipe(csvParser({ headers: false })) |
| 112 | + .on('data', (row) => { |
| 113 | + data.push(Object.values(row)); |
| 114 | + }) |
| 115 | + .on('end', async () => { |
| 116 | + const resource = { values: data }; |
| 117 | + |
| 118 | + try { |
| 119 | + const response = await sheets.spreadsheets.get({ |
| 120 | + auth: jwtClient, |
| 121 | + spreadsheetId, |
| 122 | + }); |
| 123 | + |
| 124 | + const sheet = response.data.sheets.find((sheet) => sheet.properties.title === sheetName); |
| 125 | + if (sheet) { |
| 126 | + // Clear contents of the sheet starting from cell A2 |
| 127 | + await sheets.spreadsheets.values.clear({ |
| 128 | + auth: jwtClient, |
| 129 | + spreadsheetId, |
| 130 | + range: `${sheetName}!A1:Z`, // Assuming Z is the last column |
| 131 | + }); |
| 132 | + } else { |
| 133 | + // Create the sheet if it doesn't exist |
| 134 | + await sheets.spreadsheets.batchUpdate({ |
| 135 | + auth: jwtClient, |
| 136 | + spreadsheetId, |
| 137 | + resource: set({}, 'requests[0].addSheet.properties.title', sheetName), |
| 138 | + }); |
| 139 | + } |
| 140 | + } catch (error) { |
| 141 | + console.error(`Error checking/creating sheet for ${sheetName}:`, error); |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + await sheets.spreadsheets.values.batchUpdate({ |
| 146 | + auth: jwtClient, |
| 147 | + spreadsheetId, |
| 148 | + resource: { |
| 149 | + valueInputOption: 'RAW', |
| 150 | + data: [ |
| 151 | + { |
| 152 | + range: `${sheetName}!A1`, // Use the sheet name as the range and start from A2 |
| 153 | + majorDimension: 'ROWS', |
| 154 | + values: data, |
| 155 | + }, |
| 156 | + ], |
| 157 | + }, |
| 158 | + }); |
| 159 | + |
| 160 | + console.log(`CSV data has been inserted into ${sheetName} sheet.`); |
| 161 | + } catch (err) { |
| 162 | + console.error(`Error inserting data for ${sheetName}:`, err); |
| 163 | + } |
| 164 | + }); |
| 165 | + }); |
| 166 | + } catch (error) { |
| 167 | + console.error('Error loading service account key:', error); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// Function to read and process each CSV file |
| 172 | +const processCSVFile = (file) => { |
| 173 | + return new Promise((resolve, reject) => { |
| 174 | + const parser = csvParser({ columns: true, trim: true }); |
| 175 | + const input = fs.createReadStream(`./${licenseFolderName}/${file}`); |
| 176 | + |
| 177 | + parser.on('data', (record) => { |
| 178 | + allData.push(record); |
| 179 | + }); |
| 180 | + |
| 181 | + parser.on('end', () => { |
| 182 | + resolve(); |
| 183 | + }); |
| 184 | + |
| 185 | + parser.on('error', (err) => { |
| 186 | + reject(err); |
| 187 | + }); |
| 188 | + |
| 189 | + input.pipe(parser); |
| 190 | + }); |
| 191 | +}; |
| 192 | + |
| 193 | +// Process and aggregate license data |
| 194 | +const processLicenseData = () => { |
| 195 | + const licenseCountMap = {}; |
| 196 | + for (const record of allData) { |
| 197 | + const license = record.license; |
| 198 | + licenseCountMap[license] = (licenseCountMap[license] || 0) + 1; |
| 199 | + } |
| 200 | + return licenseCountMap; |
| 201 | +}; |
| 202 | + |
| 203 | +// Create summary CSV data |
| 204 | +const createSummaryData = (licenseCountMap) => { |
| 205 | + const summaryData = [['License', 'Count']]; |
| 206 | + for (const license in licenseCountMap) { |
| 207 | + summaryData.push([license, licenseCountMap[license]]); |
| 208 | + } |
| 209 | + return summaryData; |
| 210 | +}; |
| 211 | + |
| 212 | +// Write summary CSV file |
| 213 | +const writeSummaryCSV = async (summaryData) => { |
| 214 | + try { |
| 215 | + const summaryCsvString = await stringifyPromise(summaryData); |
| 216 | + fs.writeFileSync(summaryFilePath, summaryCsvString); |
| 217 | + csvFiles.push(last(summaryFilePath.split('/'))); |
| 218 | + console.log(`Summary CSV saved as ${summaryFilePath}`); |
| 219 | + } catch (err) { |
| 220 | + console.error(`Error: ${err}`); |
| 221 | + } |
| 222 | +}; |
| 223 | + |
| 224 | +// Stringify as a promise |
| 225 | +const stringifyPromise = (data) => { |
| 226 | + return new Promise((resolve, reject) => { |
| 227 | + stringify(data, (err, csvString) => { |
| 228 | + if (err) { |
| 229 | + reject(err); |
| 230 | + } else { |
| 231 | + resolve(csvString); |
| 232 | + } |
| 233 | + }); |
| 234 | + }); |
| 235 | +}; |
| 236 | + |
| 237 | +async function generateSummary() { |
| 238 | + csvFiles = fs.readdirSync(licenseFolderName).filter(file => file.endsWith('.csv')).sort(); |
| 239 | + |
| 240 | + for (const file of csvFiles) { |
| 241 | + try { |
| 242 | + await processCSVFile(file); |
| 243 | + } catch (err) { |
| 244 | + console.error(`Error processing ${file}: ${err}`); |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + const licenseCountMap = processLicenseData(); |
| 249 | + const summaryData = createSummaryData(licenseCountMap); |
| 250 | + |
| 251 | + await writeSummaryCSV(summaryData); |
| 252 | +} |
0 commit comments