|
| 1 | +const jsonfile = require('jsonfile'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const packageFile = './package.json'; |
| 6 | +const packageData = jsonfile.readFileSync(packageFile); |
| 7 | +const currentVersion = packageData.version; |
| 8 | +const authorName = packageData.author; |
| 9 | +const versionParts = currentVersion.split('.'); |
| 10 | +let [major, minor, patch] = versionParts.map(Number); |
| 11 | +const incrementType = process.argv[2]; |
| 12 | +switch (incrementType) { |
| 13 | + case '-minor': |
| 14 | + minor += 1; |
| 15 | + patch = 0; |
| 16 | + break; |
| 17 | + case '-patch': |
| 18 | + patch += 1; |
| 19 | + break; |
| 20 | + case '-major': |
| 21 | + major += 1; |
| 22 | + minor = 0; |
| 23 | + patch = 0; |
| 24 | + break; |
| 25 | + default: |
| 26 | + console.log('Invalid increment type. Please use -minor, -patch, or -major.'); |
| 27 | + process.exit(1); |
| 28 | +} |
| 29 | +const newVersion = `${major}.${minor}.${patch}`; |
| 30 | +packageData.version = newVersion; |
| 31 | +jsonfile.writeFileSync(packageFile, packageData, { spaces: 2 }); |
| 32 | + |
| 33 | +const filesToUpdate = [ |
| 34 | + "./docs/dist/docsify-sidebar.js", |
| 35 | + "./docs/dist/docsify-sidebar.min.js", |
| 36 | + "./dist/docsify-sidebar.min.js" |
| 37 | +]; |
| 38 | + |
| 39 | +filesToUpdate.forEach(filePath => { |
| 40 | + const fileName = getFileName(filePath); |
| 41 | + const header = `/*! ${fileName} ${newVersion} | (c) ${authorName} */\n`; |
| 42 | + const fileContent = fs.readFileSync(filePath, 'utf8'); |
| 43 | + const headerRegex = /^\/\*![\s\S]*?\*\//; // Regular expression to match the header comment |
| 44 | + const contentWithoutHeader = fileContent.replace(headerRegex, ''); |
| 45 | + const updatedContent = header + contentWithoutHeader.trimStart(); |
| 46 | + fs.writeFileSync(filePath, updatedContent, 'utf8'); |
| 47 | + console.log(`Header added to ${filePath}.`); |
| 48 | +}); |
| 49 | + |
| 50 | +console.log('Header added successfully to all files.'); |
| 51 | + |
| 52 | +const changelogPath = './CHANGELOG.md'; |
| 53 | +const changelogContent = generateChangelog(newVersion, incrementType); |
| 54 | +fs.writeFileSync(changelogPath, changelogContent, 'utf8'); |
| 55 | +console.log('Changelog generated successfully.'); |
| 56 | + |
| 57 | +function generateChangelog(version, incrementType) { |
| 58 | + const currentDate = new Date().toDateString(); |
| 59 | + const changeDescription = getChangeDescription(incrementType); |
| 60 | + |
| 61 | + // Read the existing changelog content if it exists |
| 62 | + let existingChangelog = ''; |
| 63 | + if (fs.existsSync(changelogPath)) { |
| 64 | + existingChangelog = fs.readFileSync(changelogPath, 'utf8'); |
| 65 | + } |
| 66 | + const newChangelogEntry = `\n## ${version} - ${currentDate}\n\n${changeDescription}\n`; |
| 67 | + return newChangelogEntry + existingChangelog; |
| 68 | +} |
| 69 | + |
| 70 | +function getChangeDescription(incrementType) { |
| 71 | + switch (incrementType) { |
| 72 | + case '-minor': |
| 73 | + return '### Added\n\n- Add your change description here.'; |
| 74 | + case '-patch': |
| 75 | + return '### Fixed\n\n- Fix your change description here.'; |
| 76 | + case '-major': |
| 77 | + return '### Breaking Changes\n\n- Describe any breaking changes here.'; |
| 78 | + default: |
| 79 | + return ''; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +function getFileName(filePath) { |
| 84 | + const fileNameWithExtension = path.basename(filePath); |
| 85 | + const fileName = fileNameWithExtension.split('.')[0]; |
| 86 | + return fileName; |
| 87 | +} |
0 commit comments