Skip to content

Commit

Permalink
Make the xstate sky command compatible with more React and TypeScri…
Browse files Browse the repository at this point in the history
…pt sources (#385)

* wip

* Use babelTs parser for recast

* Add changeset

* improved logging
  • Loading branch information
mellson authored Oct 11, 2023
1 parent 7f3053d commit 26421cf
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 390 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-radios-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xstate/cli': patch
---

Make the `xstate sky` command compatible with more React and TypeScript sources.
2 changes: 1 addition & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build": "esbuild src/bin.ts --outdir=bin --platform=node --format=cjs --bundle --loader:.node=file"
},
"devDependencies": {
"@statelyai/sky": "0.0.3",
"@statelyai/sky": "0.0.4",
"@types/isomorphic-fetch": "^0.0.36",
"@types/jest": "^27.4.0",
"@types/node": "^16.0.1",
Expand Down
3 changes: 1 addition & 2 deletions apps/cli/src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@ program
'-k, --api-key <key>',
'API key to use for interacting with the Stately Studio',
)
.option('-h, --host <host>', 'URL pointing to the Stately Studio host')
.action(
async (
filesPattern: string,
opts: { watch?: boolean; apiKey?: string; host?: string },
) => {
const envApiKey = process.env.STATELY_API_KEY;
const envApiKey = process.env.SKY_API_KEY;
const apiKey = opts.apiKey ?? envApiKey;

if (opts.watch) {
Expand Down
3 changes: 2 additions & 1 deletion apps/cli/src/sky/writeConfigToFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const writeConfigToFiles = async (opts: {
}) => {
try {
if (doesSkyConfigExist(opts.uri)) {
console.log('SkyConfig for machine already exists, skipping');
console.log(`${opts.uri} - skipping, sky config already exists`);
return;
}
const fileContents = await fs.readFile(opts.uri, 'utf8');
Expand Down Expand Up @@ -58,6 +58,7 @@ export const writeConfigToFiles = async (opts: {
});

await modifySkyConfigSource({ filePath: opts.uri });
console.log(`${opts.uri} - updated with sky config`);
} catch (error) {
console.error(error);
}
Expand Down
6 changes: 4 additions & 2 deletions packages/machine-extractor/src/sky/skyConfigModifySource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import * as fs from 'fs/promises';
import * as path from 'path';
import * as prettier from 'prettier';
import * as recast from 'recast';
import * as parser from 'recast/parsers/typescript';
import * as babelTs from 'recast/parsers/babel-ts';
import { ALLOWED_SKY_CONFIG_CALL_EXPRESSION_NAMES } from './skyConfigUtils';

export const modifySkyConfigSource = async (opts: { filePath: string }) => {
const fileContents = await fs.readFile(opts.filePath, 'utf8');
const ast = recast.parse(fileContents, { parser });
const ast = recast.parse(fileContents, {
parser: babelTs,
});
const b = recast.types.builders;
const importIdentifier = 'skyConfig';
const name = path
Expand Down
3 changes: 2 additions & 1 deletion packages/machine-extractor/src/sky/skyConfigUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const ALLOWED_SKY_CONFIG_CALL_EXPRESSION_NAMES = [
'actorFromStately',
'machineFromStately',
'actorFromStately',
'useStatelyActor',
];
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "dist/xstate-tools-shared.cjs.js",
"license": "MIT",
"dependencies": {
"@statelyai/sky": "0.0.3",
"@statelyai/sky": "0.0.4",
"@xstate/machine-extractor": "^0.12.0"
},
"scripts": {
Expand Down
414 changes: 32 additions & 382 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit 26421cf

Please sign in to comment.