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

Regenerate compiled files on yarn/npm reinstall #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-toys-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'xstate-codegen': patch
---

Regenerate compiled files on yarn/npm reinstall.
2 changes: 2 additions & 0 deletions packages/xstate-compiled/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"glob": "^7.1.6",
"handlebars": "^4.7.6",
"handlebars-helpers": "^0.10.0",
"mkdirp": "^1.0.4",
"pkg-up": "^3.1.0",
"rimraf": "^3.0.2",
"rollup": "^2.26.3",
Expand All @@ -33,6 +34,7 @@
"@types/glob": "^7.1.3",
"@types/handlebars-helpers": "^0.5.2",
"@types/minimist": "^1.2.0",
"@types/mkdirp": "^1.0.1",
"@types/node": "^14.0.14",
"@types/rimraf": "^3.0.0",
"@xstate/react": "^0.8.1",
Expand Down
20 changes: 19 additions & 1 deletion packages/xstate-compiled/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import path from 'path';
import minimist from 'minimist';
import { introspectMachine } from './introspectMachine';
import { extractMachines } from './extractMachines';
import { printToFile, printJsFiles } from './printToFile';
import { printToFile, printJsFiles, getTargetDir } from './printToFile';

const { _: patterns, ...objectArgs } = minimist(process.argv.slice(2));
const onlyOnce = objectArgs.once;
Expand Down Expand Up @@ -105,3 +105,21 @@ async function addToCache(filePath: string) {
function removeFromCache(filePath: string) {
delete fileCache[filePath];
}

if (!onlyOnce) {
const start = () => {
const targetDirWatcher = chokidar.watch(`${getTargetDir()}/package.json`, {
persistent: true,
disableGlobbing: true,
ignoreInitial: true,
});
targetDirWatcher.on('unlink', () => {
printToFile(fileCache, objectArgs.outDir);
console.log('Generated files vanished, written again'.red.bold);
// need to restart, it won't work otherwise
targetDirWatcher.close();
start();
});
};
start();
}
29 changes: 7 additions & 22 deletions packages/xstate-compiled/src/printToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,7 @@ import path from 'path';
import { introspectMachine, SubState } from './introspectMachine';
import pkgUp from 'pkg-up';
import rimraf from 'rimraf';

const ensureFolderExists = (absoluteDir: string) => {
if (fs.existsSync(absoluteDir)) {
return;
}
fs.mkdirSync(absoluteDir);
};

const ensureMultipleFoldersExist = (absoluteRoot: string, paths: string[]) => {
let concatenatedPath = absoluteRoot;

paths.forEach((dir) => {
concatenatedPath = path.join(concatenatedPath, dir);
ensureFolderExists(concatenatedPath);
});
};
import mkdirp from 'mkdirp';

const renderSubstate = (subState: SubState): string => {
return `{
Expand Down Expand Up @@ -83,19 +68,20 @@ export const getNodeModulesDir = () => {
return targetDir;
};

export const getTargetDir = () =>
path.resolve(getNodeModulesDir(), '@xstate/compiled');

export const printToFile = (
cache: Record<string, ReturnType<typeof introspectMachine> & { id: string }>,
outDir?: string,
) => {
const files = getDeclarationFileTexts(cache);
const nodeModulesDir = getNodeModulesDir();
const targetDir = path.resolve(nodeModulesDir, '@xstate/compiled');
const targetDir = getTargetDir();

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

printJsFiles();
ensureMultipleFoldersExist(nodeModulesDir, ['@xstate', 'compiled']);

fs.writeFileSync(
outDir
Expand Down Expand Up @@ -124,10 +110,9 @@ export const printToFile = (
* to statically analyse
*/
export const printJsFiles = () => {
const nodeModulesDir = getNodeModulesDir();
const targetDir = path.resolve(nodeModulesDir, '@xstate/compiled');
const targetDir = getTargetDir();

ensureMultipleFoldersExist(nodeModulesDir, ['@xstate', 'compiled']);
mkdirp.sync(targetDir);

const indexJsTemplate = fs
.readFileSync(path.resolve(__dirname, './templates/index.js.hbs'))
Expand Down
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,13 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=

"@types/mkdirp@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/mkdirp/-/mkdirp-1.0.1.tgz#0930b948914a78587de35458b86c907b6e98bbf6"
integrity sha512-HkGSK7CGAXncr8Qn/0VqNtExEE+PHMWb+qlR1faHMao7ng6P3tAaoWWBMdva0gL5h4zprjIO89GJOLXsMcDm1Q==
dependencies:
"@types/node" "*"

"@types/node@*":
version "14.0.24"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.24.tgz#b0f86f58564fa02a28b68f8b55d4cdec42e3b9d6"
Expand Down Expand Up @@ -2732,6 +2739,11 @@ mixme@^0.3.1:
resolved "https://registry.yarnpkg.com/mixme/-/mixme-0.3.5.tgz#304652cdaf24a3df0487205e61ac6162c6906ddd"
integrity sha512-SyV9uPETRig5ZmYev0ANfiGeB+g6N2EnqqEfBbCGmmJ6MgZ3E4qv5aPbnHVdZ60KAHHXV+T3sXopdrnIXQdmjQ==

mkdirp@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

moment@^2.18.1:
version "2.27.0"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.27.0.tgz#8bff4e3e26a236220dfe3e36de756b6ebaa0105d"
Expand Down