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

Commit 9253fd1

Browse files
author
Daniel K
committed
Regenerate compiled files on yarn/npm reinstall
1 parent c458dd6 commit 9253fd1

File tree

5 files changed

+45
-23
lines changed

5 files changed

+45
-23
lines changed

.changeset/ninety-toys-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'xstate-codegen': patch
3+
---
4+
5+
Regenerate compiled files on yarn/npm reinstall.

packages/xstate-compiled/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"glob": "^7.1.6",
2121
"handlebars": "^4.7.6",
2222
"handlebars-helpers": "^0.10.0",
23+
"mkdirp": "^1.0.4",
2324
"pkg-up": "^3.1.0",
2425
"rimraf": "^3.0.2",
2526
"rollup": "^2.26.3",
@@ -33,6 +34,7 @@
3334
"@types/glob": "^7.1.3",
3435
"@types/handlebars-helpers": "^0.5.2",
3536
"@types/minimist": "^1.2.0",
37+
"@types/mkdirp": "^1.0.1",
3638
"@types/node": "^14.0.14",
3739
"@types/rimraf": "^3.0.0",
3840
"@xstate/react": "^0.8.1",

packages/xstate-compiled/src/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import path from 'path';
66
import minimist from 'minimist';
77
import { introspectMachine } from './introspectMachine';
88
import { extractMachines } from './extractMachines';
9-
import { printToFile, printJsFiles } from './printToFile';
9+
import { printToFile, printJsFiles, getTargetDir } from './printToFile';
1010

1111
const { _: patterns, ...objectArgs } = minimist(process.argv.slice(2));
1212
const onlyOnce = objectArgs.once;
@@ -105,3 +105,21 @@ async function addToCache(filePath: string) {
105105
function removeFromCache(filePath: string) {
106106
delete fileCache[filePath];
107107
}
108+
109+
if (!onlyOnce) {
110+
const start = () => {
111+
const targetDirWatcher = chokidar.watch(`${getTargetDir()}/package.json`, {
112+
persistent: true,
113+
disableGlobbing: true,
114+
ignoreInitial: true,
115+
});
116+
targetDirWatcher.on('unlink', () => {
117+
printToFile(fileCache, objectArgs.outDir);
118+
console.log('Generated files vanished, written again'.red.bold);
119+
// need to restart, it won't work otherwise
120+
targetDirWatcher.close();
121+
start();
122+
});
123+
};
124+
start();
125+
}

packages/xstate-compiled/src/printToFile.ts

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,7 @@ import path from 'path';
66
import { introspectMachine, SubState } from './introspectMachine';
77
import pkgUp from 'pkg-up';
88
import rimraf from 'rimraf';
9-
10-
const ensureFolderExists = (absoluteDir: string) => {
11-
if (fs.existsSync(absoluteDir)) {
12-
return;
13-
}
14-
fs.mkdirSync(absoluteDir);
15-
};
16-
17-
const ensureMultipleFoldersExist = (absoluteRoot: string, paths: string[]) => {
18-
let concatenatedPath = absoluteRoot;
19-
20-
paths.forEach((dir) => {
21-
concatenatedPath = path.join(concatenatedPath, dir);
22-
ensureFolderExists(concatenatedPath);
23-
});
24-
};
9+
import mkdirp from 'mkdirp';
2510

2611
const renderSubstate = (subState: SubState): string => {
2712
return `{
@@ -83,19 +68,20 @@ export const getNodeModulesDir = () => {
8368
return targetDir;
8469
};
8570

71+
export const getTargetDir = () =>
72+
path.resolve(getNodeModulesDir(), '@xstate/compiled');
73+
8674
export const printToFile = (
8775
cache: Record<string, ReturnType<typeof introspectMachine> & { id: string }>,
8876
outDir?: string,
8977
) => {
9078
const files = getDeclarationFileTexts(cache);
91-
const nodeModulesDir = getNodeModulesDir();
92-
const targetDir = path.resolve(nodeModulesDir, '@xstate/compiled');
79+
const targetDir = getTargetDir();
9380

9481
/** Delete @xstate/compiled directory so that it triggers VSCode to re-check it */
9582
rimraf.sync(path.join(targetDir, '*.d.ts'));
9683

9784
printJsFiles();
98-
ensureMultipleFoldersExist(nodeModulesDir, ['@xstate', 'compiled']);
9985

10086
fs.writeFileSync(
10187
outDir
@@ -124,10 +110,9 @@ export const printToFile = (
124110
* to statically analyse
125111
*/
126112
export const printJsFiles = () => {
127-
const nodeModulesDir = getNodeModulesDir();
128-
const targetDir = path.resolve(nodeModulesDir, '@xstate/compiled');
113+
const targetDir = getTargetDir();
129114

130-
ensureMultipleFoldersExist(nodeModulesDir, ['@xstate', 'compiled']);
115+
mkdirp.sync(targetDir);
131116

132117
const indexJsTemplate = fs
133118
.readFileSync(path.resolve(__dirname, './templates/index.js.hbs'))

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,13 @@
614614
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
615615
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=
616616

617+
"@types/mkdirp@^1.0.1":
618+
version "1.0.1"
619+
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-1.0.1.tgz#0930b948914a78587de35458b86c907b6e98bbf6"
620+
integrity sha512-HkGSK7CGAXncr8Qn/0VqNtExEE+PHMWb+qlR1faHMao7ng6P3tAaoWWBMdva0gL5h4zprjIO89GJOLXsMcDm1Q==
621+
dependencies:
622+
"@types/node" "*"
623+
617624
"@types/node@*":
618625
version "14.0.24"
619626
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.24.tgz#b0f86f58564fa02a28b68f8b55d4cdec42e3b9d6"
@@ -2732,6 +2739,11 @@ mixme@^0.3.1:
27322739
resolved "https://registry.yarnpkg.com/mixme/-/mixme-0.3.5.tgz#304652cdaf24a3df0487205e61ac6162c6906ddd"
27332740
integrity sha512-SyV9uPETRig5ZmYev0ANfiGeB+g6N2EnqqEfBbCGmmJ6MgZ3E4qv5aPbnHVdZ60KAHHXV+T3sXopdrnIXQdmjQ==
27342741

2742+
mkdirp@^1.0.4:
2743+
version "1.0.4"
2744+
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
2745+
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==
2746+
27352747
moment@^2.18.1:
27362748
version "2.27.0"
27372749
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"

0 commit comments

Comments
 (0)