11#!/usr/bin/env node
22
33/**
4- * Script to cut a new version from the repository's /docs directory
5- * This is used for creating new versions (4.7+) after the GitBook migration
4+ * Script to cut a new version from the latest versioned docs
5+ * Since we no longer have a /docs directory, we copy from the latest version
66 *
7- * Usage: npm run version <version>
8- * Example: npm run version 4.7
7+ * Usage: npm run version [version]
8+ * Example: npm run version 4.8
9+ *
10+ * If no version is provided, it will auto-increment the latest version
911 */
1012
1113const fs = require ( 'node:fs' ) ;
1214const path = require ( 'node:path' ) ;
13- const { execSync } = require ( 'node:child_process' ) ;
1415
15- const SCRIPT_DIR = __dirname ;
16- const SITE_DIR = path . dirname ( SCRIPT_DIR ) ;
17- const REPO_ROOT = path . dirname ( SITE_DIR ) ;
18- const REPO_DOCS = path . join ( REPO_ROOT , 'docs' ) ;
19- const SITE_DOCS = path . join ( SITE_DIR , 'docs' ) ;
16+ const REPO_ROOT = path . dirname ( __dirname ) ;
17+ const VERSIONED_DOCS = path . join ( REPO_ROOT , 'versioned_docs' ) ;
18+ const VERSIONED_SIDEBARS = path . join ( REPO_ROOT , 'versioned_sidebars' ) ;
19+ const VERSIONS_FILE = path . join ( REPO_ROOT , 'versions.json' ) ;
2020
2121function copyDirectory ( src , dest ) {
2222 // Create destination directory
@@ -39,78 +39,82 @@ function copyDirectory(src, dest) {
3939 }
4040}
4141
42- function removeDirectory ( dir ) {
43- if ( fs . existsSync ( dir ) ) {
44- fs . rmSync ( dir , { recursive : true , force : true } ) ;
45- }
42+ function getLatestVersion ( ) {
43+ const versions = JSON . parse ( fs . readFileSync ( VERSIONS_FILE , 'utf8' ) ) ;
44+ return versions [ 0 ] ; // First version is the latest
45+ }
46+
47+ function incrementVersion ( version ) {
48+ const [ major , minor ] = version . split ( '.' ) . map ( Number ) ;
49+ return `${ major } .${ minor + 1 } ` ;
4650}
4751
4852function main ( ) {
49- const version = process . argv [ 2 ] ;
53+ let newVersion = process . argv [ 2 ] ;
54+ const latestVersion = getLatestVersion ( ) ;
5055
51- if ( ! version ) {
52- console . error ( 'Usage: npm run version <version>' ) ;
53- console . error ( 'Example: npm run version 4.7' ) ;
54- process . exit ( 1 ) ;
56+ // If no version provided, auto-increment
57+ if ( ! newVersion ) {
58+ newVersion = incrementVersion ( latestVersion ) ;
59+ console . log ( `No version specified, auto-incrementing from ${ latestVersion } to ${ newVersion } ` ) ;
5560 }
5661
5762 // Validate version format
58- if ( ! / ^ \d + \. \d + $ / . test ( version ) ) {
59- console . error ( `Error: Invalid version format "${ version } ". Expected format: X.Y (e.g., 4.7 )` ) ;
63+ if ( ! / ^ \d + \. \d + $ / . test ( newVersion ) ) {
64+ console . error ( `Error: Invalid version format "${ newVersion } ". Expected format: X.Y (e.g., 4.8 )` ) ;
6065 process . exit ( 1 ) ;
6166 }
6267
63- console . log ( `\nCutting version ${ version } from repository docs...` ) ;
68+ console . log ( `\nCutting version ${ newVersion } from version ${ latestVersion } ...` ) ;
69+
70+ const sourceDocsDir = path . join ( VERSIONED_DOCS , `version-${ latestVersion } ` ) ;
71+ const targetDocsDir = path . join ( VERSIONED_DOCS , `version-${ newVersion } ` ) ;
72+ const sourceSidebarFile = path . join ( VERSIONED_SIDEBARS , `version-${ latestVersion } -sidebars.json` ) ;
73+ const targetSidebarFile = path . join ( VERSIONED_SIDEBARS , `version-${ newVersion } -sidebars.json` ) ;
6474
65- // Check if repo docs exist
66- if ( ! fs . existsSync ( REPO_DOCS ) ) {
67- console . error ( `Error: Repository docs not found at ${ REPO_DOCS } ` ) ;
68- console . error ( 'After migration, the repository /docs directory should contain vNext documentation.' ) ;
75+ // Check if source exists
76+ if ( ! fs . existsSync ( sourceDocsDir ) ) {
77+ console . error ( `Error: Source docs not found at ${ sourceDocsDir } ` ) ;
6978 process . exit ( 1 ) ;
7079 }
7180
72- // Remove existing site/docs if it exists (it's just a build-time copy)
73- if ( fs . existsSync ( SITE_DOCS ) ) {
74- console . log ( 'Removing existing site/docs (build-time copy)...' ) ;
75- removeDirectory ( SITE_DOCS ) ;
81+ // Check if target already exists
82+ if ( fs . existsSync ( targetDocsDir ) ) {
83+ console . error ( `Error: Version ${ newVersion } already exists at ${ targetDocsDir } ` ) ;
84+ process . exit ( 1 ) ;
7685 }
7786
7887 try {
79- // Copy repo docs to site docs
80- console . log ( 'Copying repository docs to site/docs...' ) ;
81- copyDirectory ( REPO_DOCS , SITE_DOCS ) ;
82-
83- // Run Docusaurus version command
84- console . log ( `\nRunning Docusaurus version command for ${ version } ...` ) ;
85- execSync ( `npm run docusaurus docs:version ${ version } ` , {
86- cwd : SITE_DIR ,
87- stdio : 'inherit' ,
88- } ) ;
89-
90- console . log ( `\n✅ Successfully created version ${ version } ` ) ;
91- console . log ( ` - Versioned docs created at: versioned_docs/version-${ version } /` ) ;
88+ // Copy docs
89+ console . log ( `Copying docs from version-${ latestVersion } to version-${ newVersion } ...` ) ;
90+ copyDirectory ( sourceDocsDir , targetDocsDir ) ;
91+
92+ // Copy sidebar
93+ console . log ( `Copying sidebar configuration...` ) ;
94+ fs . copyFileSync ( sourceSidebarFile , targetSidebarFile ) ;
95+
96+ // Update versions.json
97+ console . log ( 'Updating versions.json...' ) ;
98+ const versions = JSON . parse ( fs . readFileSync ( VERSIONS_FILE , 'utf8' ) ) ;
99+ versions . unshift ( newVersion ) ; // Add new version at the beginning
100+ fs . writeFileSync ( VERSIONS_FILE , JSON . stringify ( versions , null , 0 ) + '\n' ) ;
101+
102+ console . log ( `\n✅ Successfully created version ${ newVersion } ` ) ;
103+ console . log ( ` - Versioned docs created at: versioned_docs/version-${ newVersion } /` ) ;
104+ console . log ( ` - Sidebar created at: versioned_sidebars/version-${ newVersion } -sidebars.json` ) ;
92105 console . log ( ` - Version added to versions.json` ) ;
93106
94- // Clean up - remove the temporary site/docs (it's in .gitignore anyway)
95- console . log ( '\nCleaning up temporary site/docs...' ) ;
96- removeDirectory ( SITE_DOCS ) ;
97-
98107 console . log ( '\n🎉 Version creation complete!' ) ;
99108 console . log ( '\nNext steps:' ) ;
100- console . log ( '1. Create a PR with the new versioned docs and updated versions.json' ) ;
101- console . log ( '2. Site will deploy automatically when PR is merged' ) ;
102- console . log ( `\nNote: Version ${ version } is now the latest and will be synced to site/docs during build` ) ;
109+ console . log ( `1. Update docusaurus.config.ts to set lastVersion to '${ newVersion } '` ) ;
110+ console . log ( `2. Update the version config in docusaurus.config.ts for the new version` ) ;
111+ console . log ( `3. If this is a release, update onlyIncludeVersions in production to include '${ latestVersion } '` ) ;
112+ console . log ( '4. Create a PR with the changes' ) ;
113+ console . log ( `\nNote: Version ${ newVersion } is now the latest development version` ) ;
103114 } catch ( error ) {
104115 console . error ( '\n❌ Error creating version:' , error . message || error ) ;
105-
106- // Clean up on error
107- if ( fs . existsSync ( SITE_DOCS ) ) {
108- console . log ( 'Cleaning up temporary site/docs...' ) ;
109- removeDirectory ( SITE_DOCS ) ;
110- }
111-
112116 process . exit ( 1 ) ;
113117 }
114118}
115119
116- main ( ) ;
120+ main ( ) ;
0 commit comments