Skip to content
This repository was archived by the owner on Mar 5, 2023. It is now read-only.

Commit 4c851f1

Browse files
author
Daniel K
committed
Support multiple input patterns
1 parent 1a09a31 commit 4c851f1

File tree

6 files changed

+336
-128
lines changed

6 files changed

+336
-128
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'xstate-codegen': minor
3+
---
4+
5+
CLI now supports multiple input patterns. There are some minor changes in output of logs, but nothing too serious. Also the check if some files are matching the given patterns was lost in the process.

packages/xstate-compiled/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@
99
"@babel/core": "^7.10.4",
1010
"@babel/helper-split-export-declaration": "^7.11.0",
1111
"@babel/parser": "^7.10.4",
12-
"@babel/plugin-proposal-optional-chaining": "^7.10.4",
1312
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.10.4",
13+
"@babel/plugin-proposal-optional-chaining": "^7.10.4",
1414
"@babel/plugin-transform-typescript": "^7.10.4",
1515
"@rollup/plugin-babel": "^5.2.0",
1616
"@rollup/plugin-node-resolve": "^9.0.0",
1717
"babel-plugin-macros": "^2.8.0",
18+
"chokidar": "^3.4.3",
1819
"colors": "^1.4.0",
19-
"gaze": "^1.1.3",
2020
"glob": "^7.1.6",
2121
"handlebars": "^4.7.6",
2222
"handlebars-helpers": "^0.10.0",
2323
"pkg-up": "^3.1.0",
24+
"promise.allsettled": "^1.0.2",
2425
"rimraf": "^3.0.2",
2526
"rollup": "^2.26.3",
2627
"xstate": "^4.12.0"
2728
},
2829
"devDependencies": {
29-
"@types/rimraf": "^3.0.0",
3030
"@changesets/cli": "^2.9.2",
3131
"@types/babel-plugin-macros": "^2.8.2",
3232
"@types/babel__core": "^7.1.9",
@@ -35,9 +35,11 @@
3535
"@types/handlebars-helpers": "^0.5.2",
3636
"@types/minimist": "^1.2.0",
3737
"@types/node": "^14.0.14",
38+
"@types/promise.allsettled": "^1.0.3",
39+
"@types/rimraf": "^3.0.0",
3840
"@xstate/react": "^0.8.1",
39-
"typescript": "^3.9.7",
40-
"nodemon": "2.0.4"
41+
"nodemon": "2.0.4",
42+
"typescript": "^3.9.7"
4143
},
4244
"scripts": {
4345
"local-link": "yarn unlink && npm run build && npm run chmod:index && yarn link",
Lines changed: 68 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

3-
import gaze from 'gaze';
3+
import chokidar from 'chokidar';
4+
import allSettled from 'promise.allsettled';
45
import fs from 'fs';
56
import path from 'path';
67
import minimist from 'minimist';
@@ -9,125 +10,92 @@ import { extractMachines } from './extractMachines';
910
import { printToFile, printJsFiles } from './printToFile';
1011

1112
const { _: arrayArgs, ...objectArgs } = minimist(process.argv.slice(2));
13+
const onlyOnce = objectArgs.once;
1214

13-
const [, , pattern] = process.argv;
15+
const [, , ...patterns] = process.argv;
1416

15-
type RecordOfArrays = Record<string, string[]>;
16-
17-
const flattenRecords = (
18-
filesAsRecord: Record<string, RecordOfArrays | string[]> | string[],
19-
): string[] => {
20-
if (Array.isArray(filesAsRecord)) {
21-
return filesAsRecord;
22-
}
23-
return Object.values(filesAsRecord).reduce(
24-
(array, paths) => array.concat(flattenRecords(paths)),
25-
[] as any,
26-
) as string[];
27-
};
28-
29-
if (!pattern) {
30-
console.log('You must pass a glob, for instance "**/src/**.machine.ts"');
17+
if (patterns.length === 0) {
18+
console.log(
19+
'You must pass at least one glob, for instance "**/src/**.machine.ts"',
20+
);
3121
process.exit(1);
3222
}
3323

34-
const toRelative = (filePath: string) => path.relative(process.cwd(), filePath);
35-
3624
const typedSuffix = /\.typed\.(js|ts|tsx|jsx)$/;
37-
3825
const tsExtension = /\.(ts|tsx|js|jsx)$/;
26+
function isValidFile(filePath: string) {
27+
return !typedSuffix.test(filePath) && tsExtension.test(filePath);
28+
}
3929

40-
let fileCache: Record<
30+
const fileCache: Record<
4131
string,
4232
ReturnType<typeof introspectMachine> & { id: string }
4333
> = {};
4434

45-
gaze(pattern, {}, async function(err, watcher) {
46-
if (err) {
47-
console.log(err);
48-
process.exit(1);
49-
}
35+
printJsFiles();
36+
console.clear();
5037

51-
const filesAsRecord: Record<string, string[]> = watcher.watched() as any;
38+
const watcher = chokidar.watch(patterns, {
39+
persistent: !onlyOnce,
40+
});
5241

53-
const files = flattenRecords(filesAsRecord);
42+
watcher.on('error', (err) => {
43+
console.error(err);
44+
process.exit(1);
45+
});
5446

55-
const filteredFiles = files.filter((filePath) => {
56-
return !typedSuffix.test(filePath) && tsExtension.test(filePath);
57-
});
47+
const toRelative = (filePath: string) => path.relative(process.cwd(), filePath);
5848

59-
if (filteredFiles.length === 0) {
60-
console.log('No files found from that glob');
61-
process.exit(1);
49+
watcher.on('all', async (eventName, filePath) => {
50+
if (!isValidFile(filePath)) {
51+
return;
6252
}
63-
64-
printJsFiles();
65-
console.clear();
66-
67-
const addToCache = async (filePath: string) => {
68-
let code: string = '';
69-
try {
70-
code = fs.readFileSync(filePath, 'utf8');
71-
} catch (e) {}
72-
if (!code) {
73-
console.log(`Could not read from path ${filePath}`);
74-
return;
75-
}
76-
if (!code.includes('@xstate/compiled')) {
77-
return;
78-
}
79-
const machines = await extractMachines(filePath);
80-
if (machines.length === 0) {
81-
return;
82-
}
83-
const { machine, id } = machines[0];
84-
fileCache[filePath] = { ...introspectMachine(machine), id };
85-
};
86-
87-
await filteredFiles.reduce(async (promise, filePath) => {
88-
await promise;
89-
try {
90-
console.log(`Scanning File: `.cyan.bold + toRelative(filePath).gray);
91-
await addToCache(filePath);
92-
} catch (e) {
93-
console.log(e);
94-
if (objectArgs.once) {
95-
console.log('Could not complete due to errors'.red.bold);
96-
// @ts-ignore
97-
this.close();
98-
process.exit(1);
99-
}
100-
}
101-
}, Promise.resolve());
102-
103-
printToFile(fileCache, objectArgs.outDir);
104-
105-
if (objectArgs.once) {
106-
console.log('Completed!'.green.bold);
107-
// @ts-ignore
108-
this.close();
109-
process.exit(0);
53+
if (eventName === 'add') {
54+
console.log(`Scanning File: `.cyan.bold + toRelative(filePath).gray);
55+
await addToCache(filePath);
11056
}
111-
112-
// @ts-ignore
113-
this.on('changed', async (filePath) => {
57+
if (eventName === 'change') {
11458
console.log(`File Changed: `.cyan.bold + toRelative(filePath).gray);
11559
await addToCache(filePath);
116-
printToFile(fileCache, objectArgs.outDir);
117-
});
118-
// @ts-ignore
119-
this.on('added', async (filePath) => {
120-
console.log(`File Added: `.green.bold + toRelative(filePath).gray);
121-
await addToCache(filePath);
122-
printToFile(fileCache, objectArgs.outDir);
123-
});
124-
125-
// @ts-ignore
126-
this.on('deleted', async (filePath) => {
60+
}
61+
if (eventName === 'unlink') {
12762
console.log(`File Deleted: `.red.bold + toRelative(filePath).gray);
128-
delete fileCache[filePath];
129-
printToFile(fileCache, objectArgs.outDir);
130-
});
63+
removeFromCache(filePath);
64+
}
65+
printToFile(fileCache, objectArgs.outDir);
66+
});
67+
68+
process.on('exit', () => {
69+
console.log('Completed!'.green.bold);
70+
});
13171

132-
console.log(`Watching for file changes in: `.cyan.bold + pattern.gray);
72+
watcher.on('ready', async () => {
73+
if (!onlyOnce) {
74+
patterns.forEach((pattern) => {
75+
console.log(`Watching for file changes in: `.cyan.bold + pattern);
76+
});
77+
}
13378
});
79+
80+
async function addToCache(filePath: string) {
81+
let code: string = '';
82+
try {
83+
code = fs.readFileSync(filePath, 'utf8');
84+
} catch (e) {}
85+
if (!code) {
86+
throw new Error(`Could not read from path ${filePath}`);
87+
}
88+
if (!code.includes('@xstate/compiled')) {
89+
return;
90+
}
91+
const machines = await extractMachines(filePath);
92+
if (machines.length === 0) {
93+
return;
94+
}
95+
const { machine, id } = machines[0];
96+
fileCache[filePath] = { ...introspectMachine(machine), id };
97+
}
98+
99+
function removeFromCache(filePath: string) {
100+
delete fileCache[filePath];
101+
}

packages/xstate-compiled/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ execSync(
99
`cd examples && ${
1010
// If in CI, use node. If in local development, use ts-node
1111
process.env.CI ? 'node ../bin/index.js' : 'ts-node -T ../src/index.ts'
12-
} "*.machine.ts" --once`,
12+
} "*.machine.ts" "*Machine.ts" --once`,
1313
{
1414
stdio: 'inherit',
1515
},

0 commit comments

Comments
 (0)