@@ -5,6 +5,8 @@ import path from 'path';
5
5
import globby from 'globby' ;
6
6
import fs from 'fs' ;
7
7
import commandLineArgs from 'command-line-args' ;
8
+ import chokidar from 'chokidar' ;
9
+ import debounce from 'debounce' ;
8
10
9
11
import { create } from './src/create.js' ;
10
12
import {
@@ -13,6 +15,7 @@ import {
13
15
addFrameworkPlugins ,
14
16
addCustomElementsPropertyToPackageJson ,
15
17
mergeGlobsAndExcludes ,
18
+ timestamp ,
16
19
MENU
17
20
} from './src/utils/cli.js' ;
18
21
@@ -35,52 +38,68 @@ import {
35
38
const merged = mergeGlobsAndExcludes ( userConfig , cliConfig ) ;
36
39
const globs = await globby ( merged ) ;
37
40
38
- /**
39
- * Create modules for `create()`
40
- *
41
- * By default, the analyzer doesn't actually compile a users source code with the TS compiler
42
- * API. This means that by default, the typeChecker is not available in plugins.
43
- *
44
- * If users want to use the typeChecker, they can do so by adding a `overrideModuleCreation` property
45
- * in their custom-elements-manifest.config.js. `overrideModuleCreation` is a function that should return
46
- * an array of sourceFiles.
47
- */
48
- const modules = userConfig ?. overrideModuleCreation
49
- ? userConfig . overrideModuleCreation ( { ts, globs} )
50
- : globs . map ( glob => {
51
- const relativeModulePath = `./${ path . relative ( process . cwd ( ) , glob ) } ` ;
52
- const source = fs . readFileSync ( relativeModulePath ) . toString ( ) ;
41
+ async function run ( ) {
42
+ /**
43
+ * Create modules for `create()`
44
+ *
45
+ * By default, the analyzer doesn't actually compile a users source code with the TS compiler
46
+ * API. This means that by default, the typeChecker is not available in plugins.
47
+ *
48
+ * If users want to use the typeChecker, they can do so by adding a `overrideModuleCreation` property
49
+ * in their custom-elements-manifest.config.js. `overrideModuleCreation` is a function that should return
50
+ * an array of sourceFiles.
51
+ */
52
+ const modules = userConfig ?. overrideModuleCreation
53
+ ? userConfig . overrideModuleCreation ( { ts, globs} )
54
+ : globs . map ( glob => {
55
+ const relativeModulePath = path . relative ( process . cwd ( ) , glob ) ;
56
+ const source = fs . readFileSync ( relativeModulePath ) . toString ( ) ;
57
+
58
+ return ts . createSourceFile (
59
+ relativeModulePath ,
60
+ source ,
61
+ ts . ScriptTarget . ES2015 ,
62
+ true ,
63
+ ) ;
64
+ } ) ;
65
+
66
+ let plugins = await addFrameworkPlugins ( mergedOptions ) ;
67
+ plugins = [ ...plugins , ...( userConfig ?. plugins || [ ] ) ] ;
68
+
69
+ /**
70
+ * Create the manifest
71
+ */
72
+ const customElementsManifest = create ( {
73
+ modules,
74
+ plugins,
75
+ dev : mergedOptions . dev
76
+ } ) ;
53
77
54
- return ts . createSourceFile (
55
- relativeModulePath ,
56
- source ,
57
- ts . ScriptTarget . ES2015 ,
58
- true ,
59
- ) ;
60
- } ) ;
78
+ fs . writeFileSync ( `${ process . cwd ( ) } ${ path . sep } custom-elements.json` , `${ JSON . stringify ( customElementsManifest , null , 2 ) } \n` ) ;
79
+ if ( mergedOptions . dev ) {
80
+ console . log ( JSON . stringify ( customElementsManifest , null , 2 ) ) ;
81
+ }
61
82
62
- let plugins = await addFrameworkPlugins ( mergedOptions ) ;
63
- plugins = [ ...plugins , ...( userConfig ?. plugins || [ ] ) ] ;
83
+ console . log ( `[${ timestamp ( ) } ] @custom-elements-manifest/analyzer: Created new manifest.` ) ;
84
+ }
85
+ await run ( ) ;
64
86
65
87
/**
66
- * Create the manifest
88
+ * Watch mode
67
89
*/
68
- const customElementsManifest = create ( {
69
- modules,
70
- plugins,
71
- dev : mergedOptions . dev
72
- } ) ;
73
-
74
- fs . writeFileSync ( `${ process . cwd ( ) } /custom-elements.json` , `${ JSON . stringify ( customElementsManifest , null , 2 ) } \n` ) ;
75
-
76
- if ( mergedOptions . dev ) {
77
- console . log ( JSON . stringify ( customElementsManifest , null , 2 ) ) ;
90
+ if ( mergedOptions . watch ) {
91
+ const fileWatcher = chokidar . watch ( globs ) ;
92
+
93
+ const onChange = debounce ( run , 100 ) ;
94
+
95
+ fileWatcher . addListener ( 'change' , onChange ) ;
96
+ fileWatcher . addListener ( 'unlink' , onChange ) ;
78
97
}
79
98
80
99
try {
81
100
addCustomElementsPropertyToPackageJson ( ) ;
82
101
} catch {
83
- console . log ( `Could not add 'customElements' property to ${ process . cwd ( ) } / package.json. \nAdding this property helps tooling locate your Custom Elements Manifest. Please consider adding it yourself, or file an issue if you think this is a bug.\nhttps://www.github.com/open-wc/custom-elements-manifest` ) ;
102
+ console . log ( `Could not add 'customElements' property to ${ process . cwd ( ) } ${ path . sep } package.json. \nAdding this property helps tooling locate your Custom Elements Manifest. Please consider adding it yourself, or file an issue if you think this is a bug.\nhttps://www.github.com/open-wc/custom-elements-manifest` ) ;
84
103
}
85
104
} else {
86
105
console . log ( MENU ) ;
0 commit comments