@@ -11,7 +11,9 @@ const {
1111} = require ( './knowledge-paths.cjs' ) ;
1212
1313/**
14- * Classifies changed paths without reading PR-controlled content.
14+ * Classifies changed paths. It reads no pull-request content as code: the
15+ * only content it reads is GitHub's patch text for a Lab component's three
16+ * entry files, as data (spec:AST-030 FR26).
1517 *
1618 * A spec-only PR may change only spec records. Templates, schemas, indexes,
1719 * architecture, guidance, audits, workflows, and code deliberately do not
@@ -31,6 +33,152 @@ const CHANGESET_PATTERN = /^\.changeset\/(?!README\.md$)[^/]+\.md$/;
3133// runs the Lab tests and the Storybook build (spec:AST-030 FR26).
3234const LAB_ROOT = 'packages/lab/' ;
3335
36+ // A new Lab component also adds its routes, its CLI discovery entry and its
37+ // Storybook story. These three files still count toward a Lab-only change when
38+ // their diff names only Lab components the change itself carries. The diff is
39+ // read as data (GitHub's patch text, never run); a missing patch, or any line
40+ // the checks do not recognize, keeps the change out of the Lab-only lane.
41+ const ROUTES_LOCK = 'internal/shadcn-registry/routes.lock.json' ;
42+ const OFFICIAL_PACKAGES = 'packages/cli/assets/discover/official-packages.json' ;
43+ const LAB_STORY =
44+ / ^ a p p s \/ s t o r y b o o k \/ s t o r i e s \/ ( [ A - Z ] [ A - Z a - z 0 - 9 ] * ) \. s t o r i e s \. t s x $ / ;
45+ const LAB_COMPONENT_DIR = / ^ p a c k a g e s \/ l a b \/ s r c \/ ( [ A - Z ] [ A - Z a - z 0 - 9 ] * ) \/ / ;
46+
47+ // The registry's route slug for a component name. It mirrors
48+ // slugifyRegistryName in apps/docsite/src/lib/shadcnRegistry.mjs (a test pins
49+ // the two equal): the trusted classifier loads no docsite module.
50+ function labRouteSlug ( name ) {
51+ return String ( name )
52+ . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g, '$1-$2' )
53+ . replace ( / ( [ A - Z ] + ) ( [ A - Z ] [ a - z ] ) / g, '$1-$2' )
54+ . replace ( / [ ^ a - z A - Z 0 - 9 ] + / g, '-' )
55+ . replace ( / ^ - + | - + $ / g, '' )
56+ . toLowerCase ( ) ;
57+ }
58+
59+ // The changed lines of a unified diff, without the file headers.
60+ function changedLines ( patch ) {
61+ return patch
62+ . split ( '\n' )
63+ . filter ( line => / ^ [ + - ] / . test ( line ) && ! / ^ ( \+ \+ \+ | - - - ) / . test ( line ) )
64+ . map ( line => ( {
65+ sign : line [ 0 ] ,
66+ text : line . slice ( 1 ) . trim ( ) . replace ( / , $ / , '' ) ,
67+ } ) ) ;
68+ }
69+
70+ function routesLockNamesLabOnly ( patch , slugs ) {
71+ const ownSlug = value =>
72+ [ ...slugs ] . some (
73+ slug =>
74+ value === `components/lab/${ slug } ` ||
75+ value . startsWith ( `examples/${ slug } /` ) ||
76+ value . startsWith ( `showcases/${ slug } /` ) ,
77+ ) ;
78+ const ownName = value =>
79+ [ ...slugs ] . some (
80+ slug =>
81+ value === `component-lab-${ slug } ` ||
82+ value . startsWith ( `example-${ slug } -` ) ||
83+ value . startsWith ( `showcase-${ slug } -` ) ,
84+ ) ;
85+ const lines = changedLines ( patch ) ;
86+ return (
87+ lines . length > 0 &&
88+ lines . every ( ( { text} ) => {
89+ if ( [ '{' , '}' , ']' , '"aliases": []' , '"aliases": [' ] . includes ( text ) )
90+ return true ;
91+ const name = / ^ " n a m e " : " ( [ ^ " ] + ) " $ / . exec ( text ) ;
92+ if ( name ) return ownName ( name [ 1 ] ) ;
93+ const route = / ^ " p a t h " : " ( [ ^ " ] + ) " $ / . exec ( text ) ;
94+ if ( route ) return ownSlug ( route [ 1 ] ) ;
95+ const alias = / ^ " ( [ ^ " ] + ) " $ / . exec ( text ) ;
96+ return (
97+ alias != null &&
98+ / ^ c o m p o n e n t s \/ l a b \/ / . test ( alias [ 1 ] ) &&
99+ ownSlug ( alias [ 1 ] )
100+ ) ;
101+ } )
102+ ) ;
103+ }
104+
105+ // The Lab package's line range in the base's official-packages.json.
106+ function labPackageLines ( baseText ) {
107+ const lines = baseText . split ( '\n' ) ;
108+ const start = lines . findIndex ( line =>
109+ line . includes ( '"package": "@astryxdesign/lab"' ) ,
110+ ) ;
111+ if ( start === - 1 ) return null ;
112+ const next = lines . findIndex (
113+ ( line , index ) => index > start && line . includes ( '"package": "' ) ,
114+ ) ;
115+ return { first : start + 1 , last : next === - 1 ? lines . length : next } ;
116+ }
117+
118+ function officialPackagesNamesLabOnly ( patch , names , baseText ) {
119+ if ( typeof baseText !== 'string' ) return false ;
120+ const range = labPackageLines ( baseText ) ;
121+ if ( range == null ) return false ;
122+ const hunks = [
123+ ...patch . matchAll ( / ^ @ @ - ( \d + ) (?: , ( \d + ) ) ? \+ \d + (?: , \d + ) ? @ @ / gm) ,
124+ ] ;
125+ if ( hunks . length === 0 ) return false ;
126+ // Every hunk edits inside the Lab package's entry of the base file.
127+ const inLab = hunks . every ( ( [ , start , count = '1' ] ) => {
128+ const first = Number ( start ) ;
129+ const last = first + Math . max ( Number ( count ) , 1 ) - 1 ;
130+ return first > range . first && last < range . last ;
131+ } ) ;
132+ const lines = changedLines ( patch ) ;
133+ return (
134+ inLab &&
135+ lines . length > 0 &&
136+ lines . every ( ( { text} ) => {
137+ if ( [ '{' , '}' , '"kind": "component"' ] . includes ( text ) ) return true ;
138+ const name = / ^ " n a m e " : " ( [ ^ " ] + ) " $ / . exec ( text ) ;
139+ return name != null && names . has ( name [ 1 ] ) ;
140+ } )
141+ ) ;
142+ }
143+
144+ function storyImportsLab ( patch ) {
145+ const added = patch
146+ . split ( '\n' )
147+ . filter ( line => line . startsWith ( '+' ) && ! line . startsWith ( '+++ ' ) )
148+ . map ( line => line . slice ( 1 ) )
149+ . join ( '\n' ) ;
150+ return / f r o m ' @ a s t r y x d e s i g n \/ l a b (?: \/ [ ^ ' ] * ) ? ' ; / . test ( added ) ;
151+ }
152+
153+ /**
154+ * Whether a Lab entry file's change belongs to the Lab components this change
155+ * carries (spec:AST-030 FR26). `names` are the Lab component folders the change
156+ * touches; `patches` maps a path to GitHub's patch text; `officialPackagesBase`
157+ * is the base's official-packages.json.
158+ */
159+ function isLabEntryChange ( change , { names, patches, officialPackagesBase} ) {
160+ const patch = patches ?. [ change . filename ] ;
161+ if (
162+ typeof patch !== 'string' ||
163+ patch === '' ||
164+ change . previous_filename != null
165+ )
166+ return false ;
167+ const slugs = new Set ( [ ...names ] . map ( labRouteSlug ) ) ;
168+ if ( change . filename === ROUTES_LOCK )
169+ return routesLockNamesLabOnly ( patch , slugs ) ;
170+ if ( change . filename === OFFICIAL_PACKAGES )
171+ return officialPackagesNamesLabOnly ( patch , names , officialPackagesBase ) ;
172+ const story = LAB_STORY . exec ( change . filename ) ;
173+ // Only a new story: its whole text is in the patch.
174+ return (
175+ story != null &&
176+ change . status === 'A' &&
177+ names . has ( story [ 1 ] ) &&
178+ storyImportsLab ( patch )
179+ ) ;
180+ }
181+
34182const SURFACES = Object . freeze ( {
35183 KNOWLEDGE : 'knowledge' ,
36184 DOCSITE : 'docsite' ,
@@ -175,7 +323,10 @@ function normalizeChange(change) {
175323 } ;
176324}
177325
178- function classifyChanges ( changes , { expectedCount} = { } ) {
326+ function classifyChanges (
327+ changes ,
328+ { expectedCount, patches = null , officialPackagesBase = null } = { } ,
329+ ) {
179330 const normalized = changes
180331 . map ( normalizeChange )
181332 . filter ( change => change . filename ) ;
@@ -239,16 +390,42 @@ function classifyChanges(changes, {expectedCount} = {}) {
239390 const docsiteOnly = exactSurface ( SURFACES . DOCSITE ) ;
240391 const toolingOnly = exactSurface ( SURFACES . NODE_TOOLING ) ;
241392 // Every changed path is in the Lab package, besides Changesets, which no
242- // test or build reads (spec:AST-030 FR26).
393+ // test or build reads, and the Lab entry files of the Lab components the
394+ // change carries (spec:AST-030 FR26).
395+ const labNames = new Set (
396+ allPaths . flatMap ( filePath => {
397+ const match = LAB_COMPONENT_DIR . exec ( filePath ) ;
398+ return match ? [ match [ 1 ] ] : [ ] ;
399+ } ) ,
400+ ) ;
401+ const labEntries = new Set (
402+ normalized
403+ . filter ( change =>
404+ isLabEntryChange ( change , {
405+ names : labNames ,
406+ patches,
407+ officialPackagesBase,
408+ } ) ,
409+ )
410+ . map ( change => change . filename ) ,
411+ ) ;
243412 const labOnly =
244413 complete &&
245414 allPaths . some ( filePath => filePath . startsWith ( LAB_ROOT ) ) &&
246415 allPaths . every (
247416 filePath =>
248- filePath . startsWith ( LAB_ROOT ) || CHANGESET_PATTERN . test ( filePath ) ,
417+ filePath . startsWith ( LAB_ROOT ) ||
418+ CHANGESET_PATTERN . test ( filePath ) ||
419+ labEntries . has ( filePath ) ,
420+ ) ;
421+ // An incomplete list, or any shared infrastructure, runs everything. A Lab
422+ // entry file of a Lab-only change is not shared infrastructure for it.
423+ const fullSuite =
424+ ! complete ||
425+ allPaths . some (
426+ filePath =>
427+ isSharedInfraPath ( filePath ) && ! ( labOnly && labEntries . has ( filePath ) ) ,
249428 ) ;
250- // An incomplete list, or any shared infrastructure, runs everything.
251- const fullSuite = ! complete || allPaths . some ( isSharedInfraPath ) ;
252429 return {
253430 specOnly,
254431 toolingOnly,
@@ -324,7 +501,23 @@ if (require.main === module) {
324501 . filter ( Boolean ) ;
325502 process . stdout . write ( `${ JSON . stringify ( classifyPaths ( paths ) ) } \n` ) ;
326503 } else {
327- const result = classifyChanges ( parseNameStatus ( fs . readFileSync ( 0 , 'utf8' ) ) ) ;
504+ // `--patches <file>`: GitHub's patch text per path, as JSON, for the Lab
505+ // entry files. `--official-packages-base <file>`: the base's copy.
506+ const flag = name => {
507+ const index = process . argv . indexOf ( name ) ;
508+ return index === - 1 ? null : process . argv [ index + 1 ] ;
509+ } ;
510+ const readJson = file =>
511+ file ? JSON . parse ( fs . readFileSync ( file , 'utf8' ) ) : null ;
512+ const readText = file =>
513+ file && fs . existsSync ( file ) ? fs . readFileSync ( file , 'utf8' ) : null ;
514+ const result = classifyChanges (
515+ parseNameStatus ( fs . readFileSync ( 0 , 'utf8' ) ) ,
516+ {
517+ patches : readJson ( flag ( '--patches' ) ) ,
518+ officialPackagesBase : readText ( flag ( '--official-packages-base' ) ) ,
519+ } ,
520+ ) ;
328521 if ( result . specChangesetConflict ) {
329522 console . error (
330523 'Pure spec-record changes must not add a Changeset; they do not release packages.' ,
@@ -346,6 +539,8 @@ if (require.main === module) {
346539
347540module . exports = {
348541 LAB_ROOT ,
542+ OFFICIAL_PACKAGES ,
543+ ROUTES_LOCK ,
349544 NODE_TOOLING_PATHS ,
350545 SURFACES ,
351546 classifyChanges,
@@ -354,6 +549,8 @@ module.exports = {
354549 isNodeToolingPath,
355550 isPackageReleasePath,
356551 isSharedInfraPath,
552+ isLabEntryChange,
357553 isSpecRecordPath,
554+ labRouteSlug,
358555 parseNameStatus,
359556} ;
0 commit comments