1- /**
2- * allow only patch changes from release branches.
3- * Major and minor changes allowed only from main branch.
4- * pre-release only from branch containing dev or alpha in the branchname
5- */
6-
7- /** Let the following error be thrown by npm. There are situations where publish could have failed for different reasons. */
8- // throws an exception if process.env.oldv === process.env.v The library version is not up to date, error(" Not able to release to the same version.
9-
10- const { execSync } = require ( "child_process" ) ;
11- const fs = require ( "fs" ) ;
12- const path = require ( "path" ) ;
13-
14- const BRANCH = process . env . BRANCH ;
15- const DEFAULT_BRANCH = process . env . DEFAULT_BRANCH ;
16-
17- const isLatestRelease = BRANCH === DEFAULT_BRANCH || BRANCH . includes ( "release-" ) ;
18- let tag = "" ;
19-
20- const { version : OLD_VERSION , name } = require ( "../lib/package.json" ) ;
21- if ( ! isLatestRelease ) {
22- /** pre-release branch name should be the tag name (e.g., beta, canery, etc.) or tag name followed by a '-' and version or other specifiers. e.g. beta-2.0 */
23- tag = BRANCH . split ( "-" ) [ 0 ] ;
24- try {
25- execSync ( `pnpm changeset pre enter ${ tag } ` ) ;
26- } catch ( e ) {
27- console . log ( { e } ) ;
28- }
29- }
30- /** Apply changeset */
31- execSync ( "pnpm changeset version" ) ;
32-
33- // exit pre mode -- to avoid collision with full releases
34- try {
35- execSync ( "pnpm changeset pre exit" ) ;
36- } catch {
37- // empty
38- }
39-
40- /** not requiring as require is cached by npm/node */
41- const NEW_VERSION = JSON . parse (
42- fs . readFileSync ( path . resolve ( __dirname , ".." , "lib" , "package.json" ) ) ,
43- ) . version ;
44-
45- const [ newMajor , newMinor ] = NEW_VERSION . split ( "." ) ;
46- const [ oldMajor , oldMinor ] = OLD_VERSION . split ( "." ) ;
47-
48- const isNotPatch = newMajor !== oldMajor || newMinor !== oldMinor ;
49-
50- const pushCmd = `git add . && git commit -m "Apply changesets and update CHANGELOG" && git push origin ${ BRANCH } ` ;
51-
52- if ( isNotPatch && BRANCH === DEFAULT_BRANCH ) {
53- try {
54- execSync ( pushCmd ) ;
55- } catch ( e ) {
56- console . log ( { e } ) ;
57- }
58- require ( "./update-security-md" ) ( `${ newMajor } .${ newMinor } ` , `${ oldMajor } .${ oldMinor } ` ) ;
59- /** Create new release branch for every Major or Minor release */
60- const releaseBranch = `release-${ newMajor } .${ newMinor } ` ;
61- execSync ( `git checkout -b ${ releaseBranch } && git push origin ${ releaseBranch } ` ) ;
62- } else if ( isLatestRelease ) {
63- /** New version must be valid SEMVER version. No pre-release (beta/alpha etc.) */
64- if ( ! / ^ \d + \. \d + .\d + $ / . test ( NEW_VERSION ) ) throw new Error ( "Invalid version" ) ;
65-
66- if ( isNotPatch )
67- throw new Error ( "Major or Minor changes can be published only from the default branch." ) ;
68-
69- // Push changes back to the repo
70- try {
71- execSync ( pushCmd ) ;
72- } catch ( e ) {
73- console . log ( { e } ) ;
74- }
75- } else {
76- try {
77- execSync ( pushCmd ) ;
78- } catch ( e ) {
79- console . log ( { e } ) ;
80- }
81- }
82-
83- const { visibility } = JSON . parse ( execSync ( "gh repo view --json visibility" ) . toString ( ) ) ;
84- const provenance = visibility . toLowerCase ( ) === "public" ? "--provenance" : "" ;
85-
86- let LATEST_VERSION = "0.0.-1" ;
87-
88- try {
89- LATEST_VERSION = execSync ( `npm view ${ name } version` ) . toString ( ) . trim ( ) ?? "0.0.-1" ;
90- } catch {
91- // empty
92- }
93-
94- const latest = LATEST_VERSION . split ( "." ) . map ( parseInt ) ;
95- const current = NEW_VERSION . split ( "." ) . map ( parseInt ) ;
96-
97- let isLatest = false ;
98-
99- if ( latest [ 0 ] < current [ 0 ] ) {
100- isLatest = true ;
101- } else if ( latest [ 0 ] === current [ 0 ] && latest [ 1 ] < current [ 1 ] ) {
102- isLatest = true ;
103- } else if ( latest [ 0 ] === current [ 0 ] && latest [ 1 ] === current [ 1 ] && latest [ 2 ] < current [ 2 ] ) {
104- isLatest = true ;
105- }
106-
107- const reTag = isLatest ? "" : ` && npm dist-tag add ${ name } @${ LATEST_VERSION } latest` ;
108- /** Create release */
109- const publishCmd = `cd lib && pnpm build && npm publish ${ provenance } --access public${ tag && ` --tag ${ tag } ` } ` ;
110- execSync ( publishCmd + reTag ) ;
111-
112- /** Create GitHub release */
113- execSync (
114- `gh release create ${ NEW_VERSION } --generate-notes${ isLatestRelease ? " --latest" : "" } -n "$(sed '1,/^## /d;/^## /,$d' lib/CHANGELOG.md)" --title "Release v${ NEW_VERSION } "` ,
115- ) ;
116-
117- try {
118- // Publish canonical packages
119- execSync ( "node scripts/publish-canonical.js" ) ;
120- } catch {
121- console . error ( "Failed to publish canonical packages" ) ;
122- }
123-
124- execSync ( "node ./scripts/lite.js" ) ;
125- execSync ( publishCmd + reTag . replace ( "@" , "-lite@" ) ) ;
1+ /**
2+ * allow only patch changes from release branches.
3+ * Major and minor changes allowed only from main branch.
4+ * pre-release only from branch containing dev or alpha in the branchname
5+ */
6+
7+ /** Let the following error be thrown by npm. There are situations where publish could have failed for different reasons. */
8+ // throws an exception if process.env.oldv === process.env.v The library version is not up to date, error(" Not able to release to the same version.
9+
10+ const { execSync } = require ( "child_process" ) ;
11+ const fs = require ( "fs" ) ;
12+ const path = require ( "path" ) ;
13+
14+ const BRANCH = process . env . BRANCH ;
15+ const DEFAULT_BRANCH = process . env . DEFAULT_BRANCH ;
16+
17+ const isLatestRelease = BRANCH === DEFAULT_BRANCH || BRANCH . includes ( "release-" ) ;
18+ let tag = "" ;
19+
20+ const { version : OLD_VERSION , name } = require ( "../lib/package.json" ) ;
21+ if ( ! isLatestRelease ) {
22+ /** pre-release branch name should be the tag name (e.g., beta, canery, etc.) or tag name followed by a '-' and version or other specifiers. e.g. beta-2.0 */
23+ tag = BRANCH . split ( "-" ) [ 0 ] ;
24+ try {
25+ execSync ( `pnpm changeset pre enter ${ tag } ` ) ;
26+ } catch ( e ) {
27+ console . log ( { e } ) ;
28+ }
29+ }
30+ /** Apply changeset */
31+ execSync ( "pnpm changeset version" ) ;
32+
33+ // exit pre mode -- to avoid collision with full releases
34+ try {
35+ execSync ( "pnpm changeset pre exit" ) ;
36+ } catch {
37+ // empty
38+ }
39+
40+ /** not requiring as require is cached by npm/node */
41+ const NEW_VERSION = JSON . parse (
42+ fs . readFileSync ( path . resolve ( __dirname , ".." , "lib" , "package.json" ) ) ,
43+ ) . version ;
44+
45+ const [ newMajor , newMinor ] = NEW_VERSION . split ( "." ) ;
46+ const [ oldMajor , oldMinor ] = OLD_VERSION . split ( "." ) ;
47+
48+ const isNotPatch = newMajor !== oldMajor || newMinor !== oldMinor ;
49+
50+ const pushCmd = `git add . && git commit -m "Apply changesets and update CHANGELOG" && git push origin ${ BRANCH } ` ;
51+
52+ if ( isNotPatch && BRANCH === DEFAULT_BRANCH ) {
53+ try {
54+ execSync ( pushCmd ) ;
55+ } catch ( e ) {
56+ console . log ( { e } ) ;
57+ }
58+ require ( "./update-security-md" ) ( `${ newMajor } .${ newMinor } ` , `${ oldMajor } .${ oldMinor } ` ) ;
59+ /** Create new release branch for every Major or Minor release */
60+ const releaseBranch = `release-${ newMajor } .${ newMinor } ` ;
61+ execSync ( `git checkout -b ${ releaseBranch } && git push origin ${ releaseBranch } ` ) ;
62+ } else if ( isLatestRelease ) {
63+ /** New version must be valid SEMVER version. No pre-release (beta/alpha etc.) */
64+ if ( ! / ^ \d + \. \d + .\d + $ / . test ( NEW_VERSION ) ) throw new Error ( "Invalid version" ) ;
65+
66+ if ( isNotPatch )
67+ throw new Error ( "Major or Minor changes can be published only from the default branch." ) ;
68+
69+ // Push changes back to the repo
70+ try {
71+ execSync ( pushCmd ) ;
72+ } catch ( e ) {
73+ console . log ( { e } ) ;
74+ }
75+ } else {
76+ try {
77+ execSync ( pushCmd ) ;
78+ } catch ( e ) {
79+ console . log ( { e } ) ;
80+ }
81+ }
82+
83+ const { visibility } = JSON . parse ( execSync ( "gh repo view --json visibility" ) . toString ( ) ) ;
84+ const provenance = visibility . toLowerCase ( ) === "public" ? "--provenance" : "" ;
85+
86+ let LATEST_VERSION = "0.0.-1" ;
87+
88+ try {
89+ LATEST_VERSION = execSync ( `npm view ${ name } version` ) . toString ( ) . trim ( ) ?? "0.0.-1" ;
90+ } catch {
91+ // empty
92+ }
93+
94+ const latest = LATEST_VERSION . split ( "." ) . map ( parseInt ) ;
95+ const current = NEW_VERSION . split ( "." ) . map ( parseInt ) ;
96+
97+ let isLatest = false ;
98+
99+ if ( latest [ 0 ] < current [ 0 ] ) {
100+ isLatest = true ;
101+ } else if ( latest [ 0 ] === current [ 0 ] && latest [ 1 ] < current [ 1 ] ) {
102+ isLatest = true ;
103+ } else if ( latest [ 0 ] === current [ 0 ] && latest [ 1 ] === current [ 1 ] && latest [ 2 ] < current [ 2 ] ) {
104+ isLatest = true ;
105+ }
106+
107+ const reTag = isLatest ? "" : ` && npm dist-tag add ${ name } @${ LATEST_VERSION } latest` ;
108+ /** Create release */
109+ const publishCmd = `cd lib && pnpm build && npm publish ${ provenance } --access public${ tag && ` --tag ${ tag } ` } ` ;
110+ execSync ( publishCmd + reTag ) ;
111+
112+ /** Create GitHub release */
113+ execSync (
114+ `gh release create ${ NEW_VERSION } --generate-notes${ isLatestRelease ? " --latest" : "" } -n "$(sed '1,/^## /d;/^## /,$d' lib/CHANGELOG.md)" --title "Release v${ NEW_VERSION } "` ,
115+ ) ;
116+
117+ try {
118+ // Publish canonical packages
119+ execSync ( "node scripts/publish-canonical.js" ) ;
120+ } catch {
121+ console . error ( "Failed to publish canonical packages" ) ;
122+ }
123+
0 commit comments