1
1
/* eslint-disable unicorn/no-process-exit */
2
2
/* eslint-disable unicorn/no-await-expression-member */
3
3
import chalk from "chalk" ;
4
- import globby from "globby" ;
5
- import { join } from "pathe" ;
6
- import { getMonoRepoPackages } from "src/shared/file/getMonoRepoPackages" ;
7
- import { emoji , highlightFilepath } from "src/shared/ui" ;
4
+ import { dirname } from "pathe" ;
5
+ import { emoji } from "src/shared/ui" ;
8
6
import { DoDevopsHandler } from "src/@types/command" ;
9
7
import { IAutoindexOptions } from "./options" ;
10
8
import { askForAutoindexConfig } from "src/shared/interactive" ;
11
- import { getProjectConfig } from "src/shared/config" ;
12
- import { getPackageJson } from "src/shared/npm" ;
13
9
import { logger } from "src/shared/core" ;
14
- import { isAutoindexFile , processFiles } from "../private" ;
15
- import { IAutoindexWatchlist , watch } from "./watch" ;
16
-
17
- export const REQUIRED_BLACKLIST = [ "**node_modules/**" ] ;
18
-
19
- export const INDEX_LIST_DEFAULTS = [ "**/index.ts" , "**/index.js" , "**/index.mjs" , "**/index.cjs" ] ;
20
- export const WHITE_LIST_DEFAULTS = [ "src/**/*.ts" , "src/**/*.vue" , "!node_modules" ] ;
21
- export const BLACK_LIST_DEFAULTS = [
22
- "src/**/*.d.ts" ,
23
- "packages/**/*" ,
24
- "dist/**" ,
25
- "lib/**" ,
26
- ".webpack/**" ,
27
- "node_modules/**" ,
28
- ] ;
10
+ import { processFiles } from "../private" ;
11
+ import { watch } from "./watch" ;
12
+ import { getPackageJson } from "src/shared/npm/package-json" ;
13
+ import { exit } from "node:process" ;
14
+ import { AutoindexGroupDefinition , getContent , getIndex } from "./getGlobs" ;
15
+ import { getMonoRepoPackages , getSubdirectories } from "src/shared/file" ;
29
16
30
17
/**
31
18
* Finds all `index.ts` and `index.js` files and looks for the `#autoindex`
32
19
* signature. If found then it _rebuilds_ these file based on files in
33
20
* the file's current directory
34
21
*/
35
22
export const handler : DoDevopsHandler < IAutoindexOptions > = async ( { opts, observations, argv } ) => {
36
- let projectConfig = getProjectConfig ( ) ;
37
23
// the sfc flag on CLI is inverted logically
38
24
opts = {
39
25
...opts ,
@@ -47,127 +33,126 @@ export const handler: DoDevopsHandler<IAutoindexOptions> = async ({ opts, observ
47
33
process . exit ( ) ;
48
34
}
49
35
50
- const monoRepoPackages = await getMonoRepoPackages ( process . cwd ( ) ) ;
51
- if ( monoRepoPackages . length > 0 && ! opts . quiet && argv . length === 0 ) {
52
- console . log (
53
- chalk `- ${ emoji . eyeballs } monorepo detected with {yellow ${ String (
54
- monoRepoPackages . length
55
- ) } } packages`
56
- ) ;
57
- for ( const pkg of monoRepoPackages ) {
58
- console . log ( chalk `{gray - {bold ${ pkg . name } } {italic at} {dim ${ pkg . path } }}` ) ;
59
- }
60
- console . log ( chalk `- ⚡️ will run each package separately based on it's own configuration` ) ;
61
- }
62
-
63
- monoRepoPackages . push ( { path : "." , name : getPackageJson ( process . cwd ( ) ) . name } ) ;
64
- const isMonorepo = monoRepoPackages . length > 1 ;
65
- const watchlist : IAutoindexWatchlist [ ] = [ ] ;
36
+ const kind = opts . explicitFiles
37
+ ? "explicit-files"
38
+ : observations . has ( "monorepo" )
39
+ ? "monorepo"
40
+ : "repo" ;
66
41
67
- // ITERATE OVER EACH REPO
68
- for ( const repo of monoRepoPackages ) {
69
- const isRoot = isMonorepo && repo . path === "." ;
70
- if ( isMonorepo ) {
71
- projectConfig = getProjectConfig ( repo . path ) ;
72
- opts = opts . sfc === undefined ? { ...opts , sfc : projectConfig . autoindex ?. sfc } : opts ;
73
- }
74
- const name = isRoot ? `${ repo . name } (ROOT)` : repo . name ;
75
- if ( isMonorepo ) {
76
- log . info ( chalk `\n- ${ emoji . run } running {blue autoindex} on repo {bold {yellow ${ name } }}` ) ;
77
- }
42
+ /**
43
+ * The "packages" we will use to iterate over if no explicit
44
+ * index files are provided.
45
+ */
46
+ const groups : AutoindexGroupDefinition [ ] = [ ] ;
78
47
79
- const prepList = async ( globs : string [ ] ) => {
80
- return (
81
- await globby ( globs , {
82
- cwd : join ( process . cwd ( ) , repo . path ) ,
83
- onlyFiles : true ,
84
- } )
85
- ) . map ( ( i ) => join ( repo . path , i ) ) ;
86
- } ;
87
- const indexGlobs = projectConfig . autoindex ?. indexGlobs || INDEX_LIST_DEFAULTS ;
88
- const hasExplicitFiles = argv ?. length > 0 && ( await prepList ( indexGlobs ) ) . includes ( argv [ 0 ] ) ;
89
- if ( ! hasExplicitFiles && argv ?. length > 0 ) {
90
- log . shout (
91
- chalk `- ${ emoji . confused } you have added ${
92
- argv ?. length > 1 ? "files" : "a file"
93
- } to your autoindex file which doesn't appear to be a valid autoindex file ({italic {dim aka, it doesn't fit your index glob patterns}})`
48
+ switch ( kind ) {
49
+ case "explicit-files" :
50
+ log . info (
51
+ chalk `- you have passed in specific {italic index} files to evaluate [{dim ${ argv . length } }]; we will group by each file ...`
94
52
) ;
95
- log . shout ( chalk `- files: ${ argv ?. map ( ( i ) => highlightFilepath ( i ) ) . join ( ", " ) } ` ) ;
96
- process . exit ( ) ;
97
- }
98
- if ( hasExplicitFiles ) {
99
- log . info ( chalk `{dim - Using explicit autoindex files passed into CLI}` ) ;
100
- }
53
+ // note: it seems argv params -- if in a "glob" format are automatically converted
54
+ // to literal filenames
55
+ for ( const file of argv ) {
56
+ const baseDir = dirname ( file ) ;
57
+ const indexFiles = [ file ] ;
58
+ const { contentGlobs, contentFiles } = await getContent ( baseDir , opts ) ;
59
+
60
+ const pkg : AutoindexGroupDefinition = {
61
+ kind : "explicit-files" ,
62
+ path : "." ,
63
+ name : `index file: ${ file } ` ,
64
+ indexGlobs : [ file ] ,
65
+ indexFiles,
66
+ contentGlobs,
67
+ contentFiles,
68
+ nonAutoindexFiles : [ ] ,
69
+ } ;
70
+
71
+ groups . push ( pkg ) ;
72
+ }
73
+ break ;
74
+ case "repo" :
75
+ const { indexGlobs, indexFiles, nonAutoindexFiles } = await getIndex ( "." , opts ) ;
76
+ const { contentGlobs, contentFiles } = await getContent ( "." , opts ) ;
77
+
78
+ log . info ( chalk `{dim - no monorepo was detected so will run just once using glob patterns}` ) ;
79
+
80
+ groups . push ( {
81
+ kind : "repo" ,
82
+ path : "." ,
83
+ name : getPackageJson ( process . cwd ( ) ) . name || "unnamed" ,
84
+ contentGlobs,
85
+ indexGlobs,
86
+ contentFiles,
87
+ indexFiles,
88
+ nonAutoindexFiles,
89
+ } ) ;
90
+ break ;
91
+
92
+ case "monorepo" :
93
+ const subDirs = getSubdirectories ( "." ) ;
94
+ const hasSrcOrLibAtRoot = subDirs . includes ( "src" ) || subDirs . includes ( "lib" ) ;
95
+
96
+ const repos = hasSrcOrLibAtRoot
97
+ ? [
98
+ { path : "." , name : getPackageJson ( process . cwd ( ) ) . name || "unnamed" } ,
99
+ ...( await getMonoRepoPackages ( "." , opts . exclude ) ) ,
100
+ ]
101
+ : await getMonoRepoPackages ( "." , opts . exclude ) ;
101
102
102
- const blackGlobs = [
103
- ...( projectConfig . autoindex ?. blacklistGlobs || BLACK_LIST_DEFAULTS ) ,
104
- ...REQUIRED_BLACKLIST ,
105
- ] ;
106
- const blacklist = await prepList ( blackGlobs ) ;
103
+ for ( const r of repos ) {
104
+ const { indexGlobs, indexFiles, nonAutoindexFiles } = await getIndex ( r . path , opts ) ;
105
+ const { contentGlobs, contentFiles } = await getContent ( r . path , opts ) ;
107
106
108
- const indexList = hasExplicitFiles
109
- ? argv
110
- : ( await prepList ( indexGlobs ) ) . filter ( ( i ) => ! blacklist . includes ( i ) ) ;
107
+ const group : AutoindexGroupDefinition = {
108
+ kind : "monorepo" ,
109
+ ...r ,
110
+ indexFiles,
111
+ indexGlobs,
112
+ contentFiles,
113
+ contentGlobs,
114
+ nonAutoindexFiles,
115
+ } ;
111
116
112
- const whiteGlobs = [ ... ( projectConfig . autoindex ?. whitelistGlobs || WHITE_LIST_DEFAULTS ) ] ;
113
- const whitelist = ( await prepList ( whiteGlobs ) ) . filter ( ( w ) => ! indexList . includes ( w ) ) ;
117
+ groups . push ( group ) ;
118
+ }
114
119
115
- /** those files known to be autoindex files */
116
- const autoIndexFiles = indexList . filter ( ( fc ) => isAutoindexFile ( fc ) ) ;
117
- if ( indexList . length === autoIndexFiles . length ) {
118
- if ( indexList . length > 0 ) {
120
+ if ( groups . length > 0 ) {
119
121
log . info (
120
- chalk `- found {yellow ${ String (
121
- indexList . length
122
- ) } } index files, {bold {yellow {italic all}}} of which are setup to be auto-indexed.\n `
122
+ chalk `- ${ emoji . eyeballs } monorepo detected with {yellow ${ String (
123
+ groups . length
124
+ ) } } packages `
123
125
) ;
124
- }
125
- } else {
126
- log . info (
127
- chalk `- found {yellow ${ String (
128
- indexList . length
129
- ) } } {italic candidate} files, of which {yellow ${ String (
130
- autoIndexFiles . length
131
- ) } } have been setup to be auto-indexed.`
132
- ) ;
133
- const diff = indexList . length - autoIndexFiles . length ;
134
- if ( diff > 0 ) {
135
- log . info ( chalk `- files {italic not} setup were:` ) ;
136
- const missing = indexList . filter ( ( i ) => ! autoIndexFiles . includes ( i ) ) ;
137
- for ( const file of missing ) {
138
- log . info ( chalk `{dim - ${ file } }` ) ;
126
+ for ( const pkg of groups ) {
127
+ log . info ( chalk `{gray - {bold ${ pkg . name } } {italic at} {dim ${ pkg . path } }}` ) ;
139
128
}
129
+ log . info ( chalk `- ⚡️ will run each package separately based on it's own configuration` ) ;
130
+ } else {
131
+ log . info (
132
+ chalk `- after excluding packages -- {gray ${ opts . exclude ?. join (
133
+ ", "
134
+ ) } } -- no packages were left to run {bold autoindex} on\n`
135
+ ) ;
136
+ exit ( 0 ) ;
140
137
}
141
- console . log ( ) ;
138
+ }
139
+
140
+ const watchlist : AutoindexGroupDefinition [ ] = [ ] ;
141
+
142
+ for ( const group of groups ) {
143
+ if ( groups . length > 1 ) {
144
+ log . info ( chalk `\n- ${ emoji . run } starting analysis of {blue ${ group . name } }` ) ;
142
145
}
143
146
144
- if ( autoIndexFiles . length > 0 ) {
145
- await processFiles ( autoIndexFiles , opts , observations , {
146
- whitelist,
147
- blacklist,
148
- } ) ;
147
+ if ( group . indexFiles . length > 0 ) {
148
+ await processFiles ( group , opts , observations ) ;
149
149
} else {
150
- log . info ( chalk `- ${ emoji . confused } no {italic index} files found in this package ` ) ;
150
+ log . info ( chalk `- ${ emoji . confused } no {italic index} files found in {blue ${ group . name } } ` ) ;
151
151
}
152
152
153
153
if ( opts . watch ) {
154
- watchlist . push ( {
155
- name : repo . name ,
156
- dir : repo . path ,
157
- indexGlobs,
158
- whiteGlobs,
159
- blackGlobs,
160
- } ) ;
161
- }
162
- }
163
-
164
- if ( opts . watch ) {
165
- watch ( watchlist , opts ) ;
166
- } else {
167
- if ( isMonorepo ) {
168
- log . shout ( chalk `\n- ${ emoji . party } all repos have been updated with {blue autoindex}` ) ;
169
- } else {
170
- log . shout ( chalk `- ${ emoji . party } {blue autoindex} job updated successfully` ) ;
154
+ watchlist . push ( group ) ;
155
+ watch ( group , opts ) ;
171
156
}
172
157
}
173
158
} ;
0 commit comments