|
1 | | -#!/usr/bin/env node |
2 | | - |
3 | | -/*! |
4 | | - * Script to update version number references in the project. |
5 | | - * Copyright 2017-2021 The Bootstrap Authors |
6 | | - * Copyright 2017-2021 Twitter, Inc. |
7 | | - * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE) |
8 | | - */ |
9 | | - |
10 | | -'use strict'; |
11 | | - |
12 | | -const pkg = require('../package.json'); |
13 | | - |
14 | | -const fs = require('fs').promises; |
| 1 | +const fs = require('fs'); |
15 | 2 | const path = require('path'); |
16 | 3 | const globby = require('globby'); |
17 | 4 |
|
18 | | -const VERBOSE = process.argv.includes('--verbose'); |
19 | | -const DRY_RUN = |
20 | | - process.argv.includes('--dry') || process.argv.includes('--dry-run'); |
21 | | - |
22 | | -// These are the filetypes we only care about replacing the version |
23 | | -const GLOB = [ |
24 | | - '**/*.{css,js,json,md,scss,txt,yml,ts,nuspec,properties}', |
25 | | - '**/shell.html', |
26 | | - '**/installing.html', |
27 | | - '**/templates/index.html', |
28 | | - '!**/change-log*', |
29 | | -]; |
30 | | -const GLOBBY_OPTIONS = { |
31 | | - cwd: path.join(__dirname, '..'), |
32 | | - gitignore: true, |
33 | | -}; |
34 | | - |
35 | | -// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37 |
36 | | -function regExpQuote(string) { |
37 | | - return string.replace(/[$()*+-.?[\\\]^{|}]/g, '\\$&'); |
38 | | -} |
39 | | - |
40 | | -function regExpQuoteReplacement(string) { |
41 | | - return string.replace(/\$/g, '$$'); |
42 | | -} |
43 | | - |
44 | | -async function replaceRecursively(file, oldVersion, newVersion) { |
45 | | - const originalString = await fs.readFile(file, 'utf8'); |
46 | | - const newString = originalString.replace( |
47 | | - new RegExp(regExpQuote(oldVersion), 'g'), |
48 | | - regExpQuoteReplacement(newVersion) |
49 | | - ); |
50 | | - |
51 | | - // No need to move any further if the strings are identical |
52 | | - if (originalString === newString) { |
53 | | - return; |
54 | | - } |
55 | | - |
56 | | - //if (VERBOSE) { |
57 | | - console.log(`FILE: ${file}`); |
58 | | - //} |
| 5 | +// Function to update version in a file |
| 6 | +function updateVersionInFile(filePath, oldVersion, newVersion) { |
| 7 | + try { |
| 8 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 9 | + const updated = content.replace( |
| 10 | + new RegExp(oldVersion.replace(/\./g, '\\.'), 'g'), |
| 11 | + newVersion |
| 12 | + ); |
59 | 13 |
|
60 | | - if (DRY_RUN) { |
61 | | - return; |
| 14 | + if (content !== updated) { |
| 15 | + fs.writeFileSync(filePath, updated, 'utf8'); |
| 16 | + console.log(`Updated version in ${filePath}`); |
| 17 | + } |
| 18 | + } catch (error) { |
| 19 | + console.error(`Error processing ${filePath}:`, error); |
62 | 20 | } |
63 | | - |
64 | | - await fs.writeFile(file, newString, 'utf8'); |
65 | 21 | } |
66 | 22 |
|
67 | | -async function main(args) { |
68 | | - let newVersion = args[0]; |
69 | | - let oldVersion = pkg.version; |
70 | | - |
71 | | - if (!newVersion) { |
72 | | - console.error( |
73 | | - 'USAGE: change-version new_version [--verbose] [--dry[-run]]' |
| 23 | +async function updateVersions(newVersion) { |
| 24 | + try { |
| 25 | + // Read current version from package.json |
| 26 | + const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8')); |
| 27 | + const currentVersion = packageJson.version; |
| 28 | + |
| 29 | + if (!currentVersion) { |
| 30 | + throw new Error('Could not find version in package.json'); |
| 31 | + } |
| 32 | + |
| 33 | + // Update package.json |
| 34 | + packageJson.version = newVersion; |
| 35 | + fs.writeFileSync( |
| 36 | + './package.json', |
| 37 | + JSON.stringify(packageJson, null, 2) + '\n' |
74 | 38 | ); |
75 | | - console.error('Got arguments:', args); |
76 | | - process.exit(1); |
77 | | - } |
| 39 | + console.log('Updated version in package.json'); |
| 40 | + |
| 41 | + const GLOB = [ |
| 42 | + '**/*.{json,md,nuspec,properties}', |
| 43 | + 'src/js/tempus-dominus.ts', |
| 44 | + '**/shell.html', |
| 45 | + '**/installing.html', |
| 46 | + '**/templates/index.html', |
| 47 | + '!**/change-log*', |
| 48 | + '!test/**/*', |
| 49 | + ]; |
| 50 | + |
| 51 | + const GLOBBY_OPTIONS = { |
| 52 | + cwd: path.join(__dirname, '..'), |
| 53 | + gitignore: true, |
| 54 | + }; |
78 | 55 |
|
79 | | - // Strip any leading `v` from arguments because otherwise we will end up with duplicate `v`s |
80 | | - [oldVersion, newVersion] = [oldVersion, newVersion].map((arg) => |
81 | | - arg.startsWith('v') ? arg.slice(1) : arg |
82 | | - ); |
83 | | - |
84 | | - try { |
85 | 56 | const files = await globby(GLOB, GLOBBY_OPTIONS); |
86 | 57 |
|
87 | | - await Promise.all( |
88 | | - files.map((file) => replaceRecursively(file, oldVersion, newVersion)) |
| 58 | + //make sure that the dist folder is included |
| 59 | + files.push( |
| 60 | + ...(await globby(['dist/**/*'], { |
| 61 | + cwd: path.join(__dirname, '..'), |
| 62 | + })) |
89 | 63 | ); |
| 64 | + |
| 65 | + // Update version in each file |
| 66 | + for (const file of files) { |
| 67 | + updateVersionInFile(file, currentVersion, newVersion); |
| 68 | + } |
90 | 69 | } catch (error) { |
91 | | - console.error(error); |
| 70 | + console.error('Error:', error); |
92 | 71 | process.exit(1); |
93 | 72 | } |
94 | 73 | } |
95 | 74 |
|
96 | | -main(process.argv.slice(2)).then(); |
| 75 | +// Get new version from command line argument |
| 76 | +const newVersion = process.argv[2]; |
| 77 | + |
| 78 | +if (!newVersion) { |
| 79 | + console.error('Please provide a new version number as an argument'); |
| 80 | + console.error('Usage: node update-version.js <new-version>'); |
| 81 | + process.exit(1); |
| 82 | +} |
| 83 | + |
| 84 | +// Execute the update |
| 85 | +updateVersions(newVersion); |
0 commit comments