@teambit/watcher 1.0.108 → 1.0.109

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ !function(e,o){"object"==typeof exports&&"object"==typeof module?module.exports=o():"function"==typeof define&&define.amd?define([],o):"object"==typeof exports?exports["teambit.workspace/watcher-preview"]=o():e["teambit.workspace/watcher-preview"]=o()}(self,(()=>(()=>{"use strict";var e={d:(o,t)=>{for(var r in t)e.o(t,r)&&!e.o(o,r)&&Object.defineProperty(o,r,{enumerable:!0,get:t[r]})},o:(e,o)=>Object.prototype.hasOwnProperty.call(e,o),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},o={};e.r(o),e.d(o,{compositions:()=>t,compositions_metadata:()=>i,overview:()=>r});const t=[],r=[],i={compositions:[]};return o})()));
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@teambit/watcher",
3
- "version": "1.0.108",
3
+ "version": "1.0.109",
4
4
  "homepage": "https://bit.cloud/teambit/workspace/watcher",
5
5
  "main": "dist/index.js",
6
6
  "componentId": {
7
7
  "scope": "teambit.workspace",
8
8
  "name": "watcher",
9
- "version": "1.0.108"
9
+ "version": "1.0.109"
10
10
  },
11
11
  "dependencies": {
12
12
  "chalk": "2.4.2",
@@ -18,14 +18,14 @@
18
18
  "lodash": "4.17.21",
19
19
  "@teambit/harmony": "0.4.6",
20
20
  "@teambit/component-id": "1.2.0",
21
- "@teambit/workspace": "1.0.108",
22
- "@teambit/cli": "0.0.840",
23
- "@teambit/compiler": "1.0.108",
24
- "@teambit/logger": "0.0.933",
25
- "@teambit/pubsub": "1.0.108",
26
- "@teambit/global-config": "0.0.842",
27
- "@teambit/ipc-events": "1.0.108",
28
- "@teambit/scope": "1.0.108"
21
+ "@teambit/workspace": "1.0.109",
22
+ "@teambit/cli": "0.0.841",
23
+ "@teambit/compiler": "1.0.109",
24
+ "@teambit/logger": "0.0.934",
25
+ "@teambit/pubsub": "1.0.109",
26
+ "@teambit/global-config": "0.0.843",
27
+ "@teambit/ipc-events": "1.0.109",
28
+ "@teambit/scope": "1.0.109"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/fs-extra": "9.0.7",
@@ -33,7 +33,7 @@
33
33
  "@types/mocha": "9.1.0",
34
34
  "@types/jest": "^29.2.2",
35
35
  "@types/testing-library__jest-dom": "^5.9.5",
36
- "@teambit/harmony.envs.core-aspect-env": "0.0.13"
36
+ "@teambit/harmony.envs.core-aspect-env": "0.0.14"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "@teambit/legacy": "1.0.624"
package/check-types.ts DELETED
@@ -1,5 +0,0 @@
1
- export enum CheckTypes {
2
- None, // keep this. it equals zero. this way we can do "if checkTypes() ... "
3
- EntireProject,
4
- ChangedFile,
5
- }
package/index.ts DELETED
@@ -1,7 +0,0 @@
1
- import { WatcherAspect } from './watcher.aspect';
2
-
3
- export type { WatchOptions } from './watcher';
4
- export { CheckTypes } from './check-types';
5
- export type { WatcherMain } from './watcher.main.runtime';
6
- export default WatcherAspect;
7
- export { WatcherAspect };
@@ -1,50 +0,0 @@
1
- import { OnComponentEventResult } from '@teambit/workspace';
2
- import chalk from 'chalk';
3
-
4
- const verboseComponentFilesArrayToString = (componentFiles = []) => {
5
- return componentFiles.reduce((outputString, filePath) => `${outputString} \t - ${filePath}\n`, ``);
6
- };
7
-
8
- const resultsForExtensionArrayToString = (resultsForExtension, verbose) => {
9
- return resultsForExtension.reduce(
10
- (outputString, resultForExtension) =>
11
- `${outputString}${chalk.green('√')}SUCCESS\t${resultForExtension.component}\n
12
- ${verbose ? resultForExtension.componentFilesAsString : ''}\n`,
13
- ''
14
- );
15
- };
16
-
17
- export const formatWatchPathsSortByComponent = (trackDirs) => {
18
- return Object.keys(trackDirs).reduce(
19
- (outputString, watchPath) =>
20
- `${outputString}
21
- ${chalk.green('√')} SUCCESS\t${trackDirs[watchPath]}\n
22
- \t - ${watchPath}\n\n`,
23
- ` ${chalk.underline('STATUS\t\tCOMPONENT ID')}\n`
24
- );
25
- };
26
-
27
- /**
28
- * todo: this was implemented incorrectly.
29
- * the original idea of `SerializableResults` was to have each one of the aspects registered to the slot, the
30
- * ability to have their own formatting to their results, and then `toString()` method to print them.
31
- * Here, the printing is specifically to the Compiler aspect. It should move to where it belongs.
32
- */
33
- export function formatCompileResults(compileResults: OnComponentEventResult[], verbose: boolean) {
34
- if (!compileResults.length || !Array.isArray(compileResults)) return '';
35
- return compileResults
36
- .filter((compileResult) => compileResult.results?.results && Array.isArray(compileResult.results?.results))
37
- .map((compileResult) => ({
38
- extensionId: compileResult.extensionId,
39
- resultsForExtension: compileResult.results?.results?.map((resultForExtension) => ({
40
- component: resultForExtension.component,
41
- componentFilesAsString: verboseComponentFilesArrayToString(resultForExtension.buildResults),
42
- })),
43
- }))
44
- .reduce(
45
- (outputString, compileResult) =>
46
- `${outputString}
47
- ${resultsForExtensionArrayToString(compileResult.resultsForExtension, verbose)}`,
48
- ` ${chalk.underline('STATUS\tCOMPONENT ID')}`
49
- );
50
- }
package/watch-queue.ts DELETED
@@ -1,17 +0,0 @@
1
- import PQueue from 'p-queue';
2
-
3
- export class WatchQueue {
4
- private queue: PQueue;
5
- constructor(concurrency = 1) {
6
- this.queue = new PQueue({ concurrency, autoStart: true });
7
- }
8
- getQueue() {
9
- return this.queue;
10
- }
11
- add<T>(fn: () => T, priority?: number): Promise<T> {
12
- return this.queue.add(fn, { priority });
13
- }
14
- onIdle(): Promise<void> {
15
- return this.queue.onIdle();
16
- }
17
- }
package/watch.cmd.ts DELETED
@@ -1,160 +0,0 @@
1
- import chalk from 'chalk';
2
- import moment from 'moment';
3
- import { Command, CommandOptions } from '@teambit/cli';
4
- import type { Logger } from '@teambit/logger';
5
- import type { BitBaseEvent, PubsubMain } from '@teambit/pubsub';
6
- import { OnComponentEventResult } from '@teambit/workspace';
7
-
8
- // import IDs and events
9
- import { CompilerAspect, CompilerErrorEvent } from '@teambit/compiler';
10
-
11
- import { EventMessages, WatchOptions } from './watcher';
12
- import { formatCompileResults, formatWatchPathsSortByComponent } from './output-formatter';
13
- import { CheckTypes } from './check-types';
14
- import { WatcherMain } from './watcher.main.runtime';
15
-
16
- export type WatchCmdOpts = {
17
- verbose?: boolean;
18
- skipPreCompilation?: boolean;
19
- checkTypes?: string | boolean;
20
- };
21
-
22
- export class WatchCommand implements Command {
23
- name = 'watch';
24
- description = 'automatically recompile modified components (on save)';
25
- extendedDescription = `by default, the watcher doesn't use polling, to keep the CPU idle.
26
- if this doesn't work well for you, run "bit config set watch_use_polling true" to use polling.`;
27
- helpUrl = 'reference/compiling/compiler-overview';
28
- alias = '';
29
- group = 'development';
30
- options = [
31
- ['v', 'verbose', 'show all watch events and compiler verbose output'],
32
- ['', 'skip-pre-compilation', 'skip compilation step before starting to watch'],
33
- [
34
- 't',
35
- 'check-types [string]',
36
- 'EXPERIMENTAL. show errors/warnings for types. options are [file, project] to investigate only changed file or entire project. defaults to project',
37
- ],
38
- ] as CommandOptions;
39
-
40
- constructor(
41
- /**
42
- * logger extension.
43
- */
44
- private pubsub: PubsubMain,
45
-
46
- /**
47
- * logger extension.
48
- */
49
- private logger: Logger,
50
-
51
- /**
52
- * watcher extension.
53
- */
54
- private watcher: WatcherMain
55
- ) {
56
- this.registerToEvents();
57
- }
58
-
59
- private registerToEvents() {
60
- this.pubsub.sub(CompilerAspect.id, this.eventsListener);
61
- }
62
-
63
- private eventsListener = (event: BitBaseEvent<any>) => {
64
- switch (event.type) {
65
- case CompilerErrorEvent.TYPE:
66
- this.logger.console(`Watcher error ${event.data.error}, 'error'`);
67
- break;
68
- default:
69
- }
70
- };
71
-
72
- async report(cliArgs: [], watchCmdOpts: WatchCmdOpts) {
73
- const { verbose, checkTypes } = watchCmdOpts;
74
- const getCheckTypesEnum = () => {
75
- switch (checkTypes) {
76
- case undefined:
77
- case false:
78
- return CheckTypes.None;
79
- case 'project':
80
- case true: // project is the default
81
- return CheckTypes.EntireProject;
82
- case 'file':
83
- return CheckTypes.ChangedFile;
84
- default:
85
- throw new Error(`check-types can be either "file" or "project". got "${checkTypes}"`);
86
- }
87
- };
88
- const watchOpts: WatchOptions = {
89
- msgs: getMessages(this.logger),
90
- verbose,
91
- compile: true,
92
- preCompile: !watchCmdOpts.skipPreCompilation,
93
- spawnTSServer: Boolean(checkTypes), // if check-types is enabled, it must spawn the tsserver.
94
- checkTypes: getCheckTypesEnum(),
95
- };
96
- await this.watcher.watch(watchOpts);
97
- return 'watcher terminated';
98
- }
99
- }
100
-
101
- function getMessages(logger: Logger): EventMessages {
102
- return {
103
- onAll: (event: string, path: string) => logger.console(`Event: "${event}". Path: ${path}`),
104
- onStart: () => {},
105
- onReady: (workspace, watchPathsSortByComponent, verbose?: boolean) => {
106
- clearOutdatedData();
107
- if (verbose) {
108
- logger.console(formatWatchPathsSortByComponent(watchPathsSortByComponent));
109
- }
110
- logger.console(
111
- chalk.yellow(
112
- `Watching for component changes in workspace ${workspace.config.name} (${moment().format('HH:mm:ss')})...\n`
113
- )
114
- );
115
- },
116
- onChange: (...args) => {
117
- printOnFileEvent(logger, 'changed', ...args);
118
- },
119
- onAdd: (...args) => {
120
- printOnFileEvent(logger, 'added', ...args);
121
- },
122
- onUnlink: (...args) => {
123
- printOnFileEvent(logger, 'removed', ...args);
124
- },
125
- onError: (err) => {
126
- logger.console(`Watcher error ${err}`);
127
- },
128
- };
129
- }
130
-
131
- function printOnFileEvent(
132
- logger: Logger,
133
- eventMsgPlaceholder: 'changed' | 'added' | 'removed',
134
- filePaths: string[],
135
- buildResults: OnComponentEventResult[],
136
- verbose: boolean,
137
- duration: number,
138
- failureMsg?: string
139
- ) {
140
- const files = filePaths.join(', ');
141
- if (!buildResults.length) {
142
- if (!failureMsg) {
143
- if (verbose) logger.console(`The files ${files} have been ${eventMsgPlaceholder}, but nothing to compile\n\n`);
144
- return;
145
- }
146
- logger.console(`${failureMsg}\n\n`);
147
- return;
148
- }
149
- logger.console(`The file(s) ${files} have been ${eventMsgPlaceholder}.\n\n`);
150
- logger.console(formatCompileResults(buildResults, verbose));
151
- logger.console(`Finished. (${duration}ms)`);
152
- logger.console(chalk.yellow(`Watching for component changes (${moment().format('HH:mm:ss')})...`));
153
- }
154
-
155
- /**
156
- * with console.clear() all history is deleted from the console. this function preserver the history.
157
- */
158
- function clearOutdatedData() {
159
- process.stdout.write('\x1Bc');
160
- }
package/watcher.aspect.ts DELETED
@@ -1,5 +0,0 @@
1
- import { Aspect } from '@teambit/harmony';
2
-
3
- export const WatcherAspect = Aspect.create({
4
- id: 'teambit.workspace/watcher',
5
- });
@@ -1,85 +0,0 @@
1
- import { CLIAspect, CLIMain, MainRuntime } from '@teambit/cli';
2
- import { SlotRegistry, Slot } from '@teambit/harmony';
3
- import GlobalConfigAspect, { GlobalConfigMain } from '@teambit/global-config';
4
- import ScopeAspect, { ScopeMain } from '@teambit/scope';
5
- import { ComponentID } from '@teambit/component-id';
6
- import IpcEventsAspect, { IpcEventsMain } from '@teambit/ipc-events';
7
- import { Logger, LoggerAspect, LoggerMain } from '@teambit/logger';
8
- import { PubsubAspect, PubsubMain } from '@teambit/pubsub';
9
- import WorkspaceAspect, { Workspace } from '@teambit/workspace';
10
- import pMapSeries from 'p-map-series';
11
- import { WatchCommand } from './watch.cmd';
12
- import { Watcher, WatchOptions } from './watcher';
13
- import { WatcherAspect } from './watcher.aspect';
14
-
15
- export type OnPreWatch = (componentIds: ComponentID[], watchOpts: WatchOptions) => Promise<void>;
16
- export type OnPreWatchSlot = SlotRegistry<OnPreWatch>;
17
-
18
- export class WatcherMain {
19
- constructor(
20
- private workspace: Workspace,
21
- private scope: ScopeMain,
22
- private pubsub: PubsubMain,
23
- private onPreWatchSlot: OnPreWatchSlot,
24
- readonly ipcEvents: IpcEventsMain,
25
- readonly logger: Logger,
26
- readonly globalConfig: GlobalConfigMain
27
- ) {}
28
-
29
- async watch(opts: WatchOptions) {
30
- const watcher = new Watcher(this.workspace, this.pubsub, this, opts);
31
- await watcher.watch();
32
- }
33
-
34
- async watchScopeInternalFiles() {
35
- await this.scope.watchScopeInternalFiles();
36
- }
37
-
38
- async triggerOnPreWatch(componentIds: ComponentID[], watchOpts: WatchOptions) {
39
- const preWatchFunctions = this.onPreWatchSlot.values();
40
- await pMapSeries(preWatchFunctions, async (func) => {
41
- await func(componentIds, watchOpts);
42
- });
43
- }
44
-
45
- registerOnPreWatch(onPreWatchFunc: OnPreWatch) {
46
- this.onPreWatchSlot.register(onPreWatchFunc);
47
- return this;
48
- }
49
-
50
- static slots = [Slot.withType<OnPreWatch>()];
51
- static dependencies = [
52
- CLIAspect,
53
- WorkspaceAspect,
54
- ScopeAspect,
55
- PubsubAspect,
56
- LoggerAspect,
57
- IpcEventsAspect,
58
- GlobalConfigAspect,
59
- ];
60
- static runtime = MainRuntime;
61
-
62
- static async provider(
63
- [cli, workspace, scope, pubsub, loggerMain, ipcEvents, globalConfig]: [
64
- CLIMain,
65
- Workspace,
66
- ScopeMain,
67
- PubsubMain,
68
- LoggerMain,
69
- IpcEventsMain,
70
- GlobalConfigMain
71
- ],
72
- _,
73
- [onPreWatchSlot]: [OnPreWatchSlot]
74
- ) {
75
- const logger = loggerMain.createLogger(WatcherAspect.id);
76
- const watcherMain = new WatcherMain(workspace, scope, pubsub, onPreWatchSlot, ipcEvents, logger, globalConfig);
77
- const watchCmd = new WatchCommand(pubsub, logger, watcherMain);
78
- cli.register(watchCmd);
79
- return watcherMain;
80
- }
81
- }
82
-
83
- WatcherAspect.addRuntime(WatcherMain);
84
-
85
- export default WatcherMain;
package/watcher.ts DELETED
@@ -1,426 +0,0 @@
1
- import { PubsubMain } from '@teambit/pubsub';
2
- import fs from 'fs-extra';
3
- import { dirname, basename } from 'path';
4
- import { compact, difference, partition } from 'lodash';
5
- import { ComponentID } from '@teambit/component-id';
6
- import loader from '@teambit/legacy/dist/cli/loader';
7
- import { BIT_MAP, CFG_WATCH_USE_POLLING, WORKSPACE_JSONC } from '@teambit/legacy/dist/constants';
8
- import { Consumer } from '@teambit/legacy/dist/consumer';
9
- import logger from '@teambit/legacy/dist/logger/logger';
10
- import { pathNormalizeToLinux } from '@teambit/legacy/dist/utils';
11
- import mapSeries from 'p-map-series';
12
- import chalk from 'chalk';
13
- import { ChildProcess } from 'child_process';
14
- import chokidar, { FSWatcher } from '@teambit/chokidar';
15
- import ComponentMap from '@teambit/legacy/dist/consumer/bit-map/component-map';
16
- import { PathOsBasedAbsolute } from '@teambit/legacy/dist/utils/path';
17
- import { CompilationInitiator } from '@teambit/compiler';
18
- import {
19
- WorkspaceAspect,
20
- Workspace,
21
- OnComponentEventResult,
22
- OnComponentChangeEvent,
23
- OnComponentAddEvent,
24
- OnComponentRemovedEvent,
25
- } from '@teambit/workspace';
26
- import { CheckTypes } from './check-types';
27
- import { WatcherMain } from './watcher.main.runtime';
28
- import { WatchQueue } from './watch-queue';
29
-
30
- export type WatcherProcessData = { watchProcess: ChildProcess; compilerId: ComponentID; componentIds: ComponentID[] };
31
-
32
- export type EventMessages = {
33
- onAll: Function;
34
- onStart: Function;
35
- onReady: Function;
36
- onChange: OnFileEventFunc;
37
- onAdd: OnFileEventFunc;
38
- onUnlink: OnFileEventFunc;
39
- onError: Function;
40
- };
41
-
42
- export type OnFileEventFunc = (
43
- filePaths: string[],
44
- buildResults: OnComponentEventResult[],
45
- verbose: boolean,
46
- duration: number,
47
- failureMsg?: string
48
- ) => void;
49
-
50
- export type WatchOptions = {
51
- msgs?: EventMessages;
52
- initiator?: CompilationInitiator;
53
- verbose?: boolean; // print watch events to the console. (also ts-server events if spawnTSServer is true)
54
- spawnTSServer?: boolean; // needed for check types and extract API/docs.
55
- checkTypes?: CheckTypes; // if enabled, the spawnTSServer becomes true.
56
- preCompile?: boolean; // whether compile all components before start watching
57
- compile?: boolean; // whether compile modified/added components during watch process
58
- };
59
-
60
- const DEBOUNCE_WAIT_MS = 100;
61
- type PathLinux = string; // ts fails when importing it from @teambit/legacy/dist/utils/path.
62
-
63
- export class Watcher {
64
- private fsWatcher: FSWatcher;
65
- private changedFilesPerComponent: { [componentId: string]: string[] } = {};
66
- private watchQueue = new WatchQueue();
67
- private bitMapChangesInProgress = false;
68
- private ipcEventsDir: string;
69
- private trackDirs: { [dir: PathLinux]: ComponentID } = {};
70
- private verbose = false;
71
- private multipleWatchers: WatcherProcessData[] = [];
72
- constructor(
73
- private workspace: Workspace,
74
- private pubsub: PubsubMain,
75
- private watcherMain: WatcherMain,
76
- private options: WatchOptions
77
- ) {
78
- this.ipcEventsDir = this.watcherMain.ipcEvents.eventsDir;
79
- this.verbose = this.options.verbose || false;
80
- }
81
-
82
- get consumer(): Consumer {
83
- return this.workspace.consumer;
84
- }
85
-
86
- async watch() {
87
- const { msgs, ...watchOpts } = this.options;
88
- await this.setTrackDirs();
89
- const componentIds = Object.values(this.trackDirs);
90
- await this.watcherMain.triggerOnPreWatch(componentIds, watchOpts);
91
- await this.createWatcher();
92
- const watcher = this.fsWatcher;
93
- msgs?.onStart(this.workspace);
94
-
95
- await this.workspace.scope.watchScopeInternalFiles();
96
-
97
- return new Promise((resolve, reject) => {
98
- if (this.verbose) {
99
- // @ts-ignore
100
- if (msgs?.onAll) watcher.on('all', msgs?.onAll);
101
- }
102
- watcher.on('ready', () => {
103
- msgs?.onReady(this.workspace, this.trackDirs, this.verbose);
104
- // console.log(this.fsWatcher.getWatched());
105
- loader.stop();
106
- });
107
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
108
- watcher.on('all', async (event, filePath) => {
109
- if (event !== 'change' && event !== 'add' && event !== 'unlink') return;
110
- const startTime = new Date().getTime();
111
- const { files, results, debounced, irrelevant, failureMsg } = await this.handleChange(filePath);
112
- if (debounced || irrelevant) {
113
- return;
114
- }
115
- const duration = new Date().getTime() - startTime;
116
- msgs?.onChange(files, results, this.verbose, duration, failureMsg);
117
- });
118
- watcher.on('error', (err) => {
119
- msgs?.onError(err);
120
- reject(err);
121
- });
122
- });
123
- }
124
-
125
- /**
126
- * *** DEBOUNCING ***
127
- * some actions trigger multiple files changes at (almost) the same time. e.g. "git pull".
128
- * this causes some performance and instability issues. a debouncing mechanism was implemented to help with this.
129
- * the way how it works is that the first file of the same component starts the execution with a delay (e.g. 200ms).
130
- * if, in the meanwhile, another file of the same component was changed, it won't start a new execution, instead,
131
- * it'll only add the file to `this.changedFilesPerComponent` prop.
132
- * once the execution starts, it'll delete this component-id from the `this.changedFilesPerComponent` array,
133
- * indicating the next file-change to start a new execution.
134
- *
135
- * implementation wise, `lodash.debounce` doesn't help here, because:
136
- * A) it doesn't return the results, unless "leading" option is true. here, it must be false, otherwise, it'll start
137
- * the execution immediately.
138
- * B) it debounces the method regardless the param passes to it. so it'll disregard the component-id and will delay
139
- * other components undesirably.
140
- *
141
- * *** QUEUE ***
142
- * the debouncing helps to not execute the same component multiple times concurrently. however, multiple components
143
- * and .bitmap changes execution can still be processed concurrently.
144
- * the following example explains why this is an issue.
145
- * compA is changed in the .bitmap file from version 0.0.1 to 0.0.2. its files were changed as well.
146
- * all these changes get pulled at the same time by "git pull", as a result, the execution of compA and the .bitmap
147
- * happen at the same time.
148
- * during the execution of compA, the component id is parsed as compA@0.0.1, later, it asks for the Workspace for this
149
- * id. while the workspace is looking for this id, the .bitmap execution reloaded the consumer and changed all versions.
150
- * after this change, the workspace doesn't have this id anymore, which will trigger an error.
151
- * to ensure this won't happen, we keep a flag to indicate whether the .bitmap execution is running, and if so, all
152
- * other executions are paused until the queue is empty (this is done by awaiting for queue.onIdle).
153
- * once the queue is empty, we know the .bitmap process was done and the workspace has all new ids.
154
- * in the example above, at this stage, the id will be resolved to compA@0.0.2.
155
- * one more thing, the queue is configured to have concurrency of 1. to make sure two components are not processed at
156
- * the same time. (the same way is done when loading all components from the filesystem/scope).
157
- * this way we can also ensure that if compA was started before the .bitmap execution, it will complete before the
158
- * .bitmap execution starts.
159
- */
160
- private async handleChange(filePath: string): Promise<{
161
- results: OnComponentEventResult[];
162
- files: string[];
163
- failureMsg?: string;
164
- debounced?: boolean;
165
- irrelevant?: boolean; // file/dir is not part of any component
166
- }> {
167
- try {
168
- if (filePath.endsWith(BIT_MAP)) {
169
- this.bitMapChangesInProgress = true;
170
- const buildResults = await this.watchQueue.add(() => this.handleBitmapChanges());
171
- this.bitMapChangesInProgress = false;
172
- loader.stop();
173
- return { results: buildResults, files: [filePath] };
174
- }
175
- if (this.bitMapChangesInProgress) {
176
- await this.watchQueue.onIdle();
177
- }
178
- if (dirname(filePath) === this.ipcEventsDir) {
179
- const eventName = basename(filePath);
180
- if (eventName !== 'onPostInstall') {
181
- this.watcherMain.logger.warn(`eventName ${eventName} is not recognized, please handle it`);
182
- }
183
- await this.watcherMain.ipcEvents.triggerGotEvent(eventName as 'onPostInstall');
184
- return { results: [], files: [filePath] };
185
- }
186
- if (filePath.endsWith(WORKSPACE_JSONC)) {
187
- await this.workspace.triggerOnWorkspaceConfigChange();
188
- return { results: [], files: [filePath] };
189
- }
190
- const componentId = this.getComponentIdByPath(filePath);
191
- if (!componentId) {
192
- loader.stop();
193
- return { results: [], files: [], irrelevant: true };
194
- }
195
- const compIdStr = componentId.toString();
196
- if (this.changedFilesPerComponent[compIdStr]) {
197
- this.changedFilesPerComponent[compIdStr].push(filePath);
198
- loader.stop();
199
- return { results: [], files: [], debounced: true };
200
- }
201
- this.changedFilesPerComponent[compIdStr] = [filePath];
202
- await this.sleep(DEBOUNCE_WAIT_MS);
203
- const files = this.changedFilesPerComponent[compIdStr];
204
- delete this.changedFilesPerComponent[compIdStr];
205
-
206
- const buildResults = await this.watchQueue.add(() => this.triggerCompChanges(componentId, files));
207
- const failureMsg = buildResults.length
208
- ? undefined
209
- : `files ${files.join(', ')} are inside the component ${compIdStr} but configured to be ignored`;
210
- loader.stop();
211
- return { results: buildResults, files, failureMsg };
212
- } catch (err: any) {
213
- const msg = `watcher found an error while handling ${filePath}`;
214
- logger.error(msg, err);
215
- logger.console(`${msg}, ${err.message}`);
216
- loader.stop();
217
- return { results: [], files: [filePath], failureMsg: err.message };
218
- }
219
- }
220
-
221
- private async sleep(ms: number) {
222
- return new Promise((resolve) => setTimeout(resolve, ms));
223
- }
224
-
225
- private async triggerCompChanges(
226
- componentId: ComponentID,
227
- files: PathOsBasedAbsolute[]
228
- ): Promise<OnComponentEventResult[]> {
229
- let updatedComponentId: ComponentID | undefined = componentId;
230
- if (!(await this.workspace.hasId(componentId))) {
231
- // bitmap has changed meanwhile, which triggered `handleBitmapChanges`, which re-loaded consumer and updated versions
232
- // so the original componentId might not be in the workspace now, and we need to find the updated one
233
- const ids = await this.workspace.listIds();
234
- updatedComponentId = ids.find((id) => id.isEqual(componentId, { ignoreVersion: true }));
235
- if (!updatedComponentId) {
236
- logger.debug(`triggerCompChanges, the component ${componentId.toString()} was probably removed from .bitmap`);
237
- return [];
238
- }
239
- }
240
- this.workspace.clearComponentCache(updatedComponentId);
241
- const component = await this.workspace.get(updatedComponentId);
242
- const componentMap: ComponentMap = component.state._consumer.componentMap;
243
- if (!componentMap) {
244
- throw new Error(
245
- `unable to find componentMap for ${updatedComponentId.toString()}, make sure this component is in .bitmap`
246
- );
247
- }
248
- const compFilesRelativeToWorkspace = componentMap.getFilesRelativeToConsumer();
249
- const [compFiles, nonCompFiles] = partition(files, (filePath) => {
250
- const relativeFile = this.getRelativePathLinux(filePath);
251
- return Boolean(compFilesRelativeToWorkspace.find((p) => p === relativeFile));
252
- });
253
- // nonCompFiles are either, files that were removed from the filesystem or existing files that are ignored.
254
- // the compiler takes care of removedFiles differently, e.g. removes dists dir and old symlinks.
255
- const removedFiles = compact(
256
- await Promise.all(nonCompFiles.map(async (filePath) => ((await fs.pathExists(filePath)) ? null : filePath)))
257
- );
258
-
259
- if (!compFiles.length && !removedFiles.length) {
260
- logger.debug(
261
- `the following files are part of the component ${componentId.toStringWithoutVersion()} but configured to be ignored:\n${files.join(
262
- '\n'
263
- )}'`
264
- );
265
- return [];
266
- }
267
- this.consumer.bitMap.updateComponentPaths(
268
- componentId,
269
- compFiles.map((f) => this.consumer.getPathRelativeToConsumer(f)),
270
- removedFiles.map((f) => this.consumer.getPathRelativeToConsumer(f))
271
- );
272
- const buildResults = await this.executeWatchOperationsOnComponent(
273
- updatedComponentId,
274
- compFiles,
275
- removedFiles,
276
- true
277
- );
278
- return buildResults;
279
- }
280
-
281
- /**
282
- * if .bitmap changed, it's possible that a new component has been added. trigger onComponentAdd.
283
- */
284
- private async handleBitmapChanges(): Promise<OnComponentEventResult[]> {
285
- const previewsTrackDirs = { ...this.trackDirs };
286
- await this.workspace._reloadConsumer();
287
- await this.setTrackDirs();
288
- await this.workspace.triggerOnBitmapChange();
289
- const newDirs: string[] = difference(Object.keys(this.trackDirs), Object.keys(previewsTrackDirs));
290
- const removedDirs: string[] = difference(Object.keys(previewsTrackDirs), Object.keys(this.trackDirs));
291
- const results: OnComponentEventResult[] = [];
292
- if (newDirs.length) {
293
- const addResults = await mapSeries(newDirs, async (dir) =>
294
- this.executeWatchOperationsOnComponent(this.trackDirs[dir], [], [], false)
295
- );
296
- results.push(...addResults.flat());
297
- }
298
- if (removedDirs.length) {
299
- await mapSeries(removedDirs, (dir) => this.executeWatchOperationsOnRemove(previewsTrackDirs[dir]));
300
- }
301
-
302
- return results;
303
- }
304
-
305
- private async executeWatchOperationsOnRemove(componentId: ComponentID) {
306
- logger.debug(`running OnComponentRemove hook for ${chalk.bold(componentId.toString())}`);
307
- this.pubsub.pub(WorkspaceAspect.id, this.createOnComponentRemovedEvent(componentId.toString()));
308
- await this.workspace.triggerOnComponentRemove(componentId);
309
- }
310
-
311
- private async executeWatchOperationsOnComponent(
312
- componentId: ComponentID,
313
- files: PathOsBasedAbsolute[],
314
- removedFiles: PathOsBasedAbsolute[] = [],
315
- isChange = true
316
- ): Promise<OnComponentEventResult[]> {
317
- if (this.isComponentWatchedExternally(componentId)) {
318
- // update capsule, once done, it automatically triggers the external watcher
319
- await this.workspace.get(componentId);
320
- return [];
321
- }
322
- const idStr = componentId.toString();
323
-
324
- if (isChange) {
325
- logger.debug(`running OnComponentChange hook for ${chalk.bold(idStr)}`);
326
- this.pubsub.pub(WorkspaceAspect.id, this.createOnComponentChangeEvent(idStr, 'OnComponentChange'));
327
- } else {
328
- logger.debug(`running OnComponentAdd hook for ${chalk.bold(idStr)}`);
329
- this.pubsub.pub(WorkspaceAspect.id, this.createOnComponentAddEvent(idStr, 'OnComponentAdd'));
330
- }
331
-
332
- const buildResults = isChange
333
- ? await this.workspace.triggerOnComponentChange(componentId, files, removedFiles, this.options)
334
- : await this.workspace.triggerOnComponentAdd(componentId, this.options);
335
-
336
- return buildResults;
337
- }
338
-
339
- private createOnComponentRemovedEvent(idStr) {
340
- return new OnComponentRemovedEvent(Date.now(), idStr);
341
- }
342
-
343
- private createOnComponentChangeEvent(idStr, hook) {
344
- return new OnComponentChangeEvent(Date.now(), idStr, hook);
345
- }
346
-
347
- private createOnComponentAddEvent(idStr, hook) {
348
- return new OnComponentAddEvent(Date.now(), idStr, hook);
349
- }
350
-
351
- private isComponentWatchedExternally(componentId: ComponentID) {
352
- const watcherData = this.multipleWatchers.find((m) => m.componentIds.find((id) => id.isEqual(componentId)));
353
- if (watcherData) {
354
- logger.debug(`${componentId.toString()} is watched by ${watcherData.compilerId.toString()}`);
355
- return true;
356
- }
357
- return false;
358
- }
359
-
360
- private getComponentIdByPath(filePath: string): ComponentID | null {
361
- const relativeFile = this.getRelativePathLinux(filePath);
362
- const trackDir = this.findTrackDirByFilePathRecursively(relativeFile);
363
- if (!trackDir) {
364
- // the file is not part of any component. If it was a new component, or a new file of
365
- // existing component, then, handleBitmapChanges() should deal with it.
366
- return null;
367
- }
368
- return this.trackDirs[trackDir];
369
- }
370
-
371
- private getRelativePathLinux(filePath: string) {
372
- return pathNormalizeToLinux(this.consumer.getPathRelativeToConsumer(filePath));
373
- }
374
-
375
- private findTrackDirByFilePathRecursively(filePath: string): string | null {
376
- if (this.trackDirs[filePath]) return filePath;
377
- const parentDir = dirname(filePath);
378
- if (parentDir === filePath) return null;
379
- return this.findTrackDirByFilePathRecursively(parentDir);
380
- }
381
-
382
- private async createWatcher() {
383
- const usePollingConf = await this.watcherMain.globalConfig.get(CFG_WATCH_USE_POLLING);
384
- const usePolling = usePollingConf === 'true';
385
- // const useFsEventsConf = await this.watcherMain.globalConfig.get(CFG_WATCH_USE_FS_EVENTS);
386
- // const useFsEvents = useFsEventsConf === 'true';
387
- const ignoreLocalScope = (pathToCheck: string) => {
388
- if (pathToCheck.startsWith(this.ipcEventsDir)) return false;
389
- return (
390
- pathToCheck.startsWith(`${this.workspace.path}/.git/`) || pathToCheck.startsWith(`${this.workspace.path}/.bit/`)
391
- );
392
- };
393
- this.fsWatcher = chokidar.watch(this.workspace.path, {
394
- ignoreInitial: true,
395
- // `chokidar` matchers have Bash-parity, so Windows-style backslashes are not supported as separators.
396
- // (windows-style backslashes are converted to forward slashes)
397
- ignored: ['**/node_modules/**', '**/package.json', ignoreLocalScope],
398
- /**
399
- * default to false, although it causes high CPU usage.
400
- * see: https://github.com/paulmillr/chokidar/issues/1196#issuecomment-1711033539
401
- * there is a fix for this in master. once a new version of Chokidar is released, we can upgrade it and then
402
- * default to true.
403
- */
404
- usePolling,
405
- // useFsEvents,
406
- persistent: true,
407
- });
408
- if (this.verbose) {
409
- logger.console(`chokidar.options ${JSON.stringify(this.fsWatcher.options, undefined, 2)}`);
410
- }
411
- }
412
-
413
- private async setTrackDirs() {
414
- this.trackDirs = {};
415
- const componentsFromBitMap = this.consumer.bitMap.getAllComponents();
416
- await Promise.all(
417
- componentsFromBitMap.map(async (componentMap) => {
418
- const bitId = componentMap.id;
419
- const rootDir = componentMap.getRootDir();
420
- if (!rootDir) throw new Error(`${bitId.toString()} has no rootDir, which is invalid in Harmony`);
421
- const componentId = await this.workspace.resolveComponentId(bitId);
422
- this.trackDirs[rootDir] = componentId;
423
- })
424
- );
425
- }
426
- }