Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import 'colors'; import fs from 'fs'; import Handlebars from 'handlebars'; import helpers from 'handlebars-helpers'; import path from 'path'; import { introspectMachine, SubState } from './introspectMachine'; import pkgUp from 'pkg-up'; 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); }); }; const renderSubstate = (subState: SubState): string => { return `{ targets: ${subState.targets}; sources: ${subState.sources}; states: { ${Object.entries(subState.states) .map(([key, state]) => { return `${key}: ${renderSubstate(state)}`; }) .join('\n')} }; }`; }; export const getFileTexts = ( cache: Record<string, ReturnType<typeof introspectMachine> & { id: string }>, ) => { const indexTemplateString = fs .readFileSync(path.resolve(__dirname, './templates/index.d.ts.hbs')) .toString(); const reactTemplateString = fs .readFileSync(path.resolve(__dirname, './templates/react.d.ts.hbs')) .toString(); const indexJsTemplate = fs .readFileSync(path.resolve(__dirname, './templates/index.js.hbs')) .toString(); const reactJsTemplate = fs .readFileSync(path.resolve(__dirname, './templates/react.js.hbs')) .toString(); const packageJsonTemplate = fs .readFileSync(path.resolve(__dirname, './templates/package.json.hbs')) .toString(); helpers({ handlebars: Handlebars, }); const indexTemplate = Handlebars.compile(indexTemplateString); const reactTemplate = Handlebars.compile(reactTemplateString); const machines = Object.values(cache).map((machine) => ({ id: machine.id, machine: { ...machine, subState: renderSubstate(machine.subState), }, })); return { 'index.d.ts': indexTemplate({ machines }), 'react.d.ts': reactTemplate({ machines }), 'index.js': indexJsTemplate, 'react.js': reactJsTemplate, 'package.json': packageJsonTemplate, }; }; export const printToFile = ( cache: Record<string, ReturnType<typeof introspectMachine> & { id: string }>, outDir?: string, ) => { const packageJson = pkgUp.sync(); if (!packageJson) { throw new Error( 'Could not find a package.json in any directory in or above this one.', ); } ensureMultipleFoldersExist(path.dirname(packageJson), [ 'node_modules', '@xstate', 'compiled', ]); const targetDir = path.resolve( path.dirname(packageJson), 'node_modules/@xstate/compiled', ); const files = getFileTexts(cache); fs.writeFileSync( outDir ? path.resolve(process.cwd(), outDir, 'index.d.ts') : path.join(targetDir, 'index.d.ts'), files['index.d.ts'], ); fs.writeFileSync( outDir ? path.resolve(process.cwd(), outDir, 'react.d.ts') : path.join(targetDir, 'react.d.ts'), files['react.d.ts'], ); if (outDir) { // If the user specifies an outDir, we need to add some dummy types // so that we can override something fs.writeFileSync(path.join(targetDir, 'react.d.ts'), `export default any;`); fs.writeFileSync(path.join(targetDir, 'index.d.ts'), `export default any;`); } fs.writeFileSync(path.join(targetDir, 'index.js'), files['index.js']); fs.writeFileSync(path.join(targetDir, 'react.js'), files['react.js']); fs.writeFileSync(path.join(targetDir, 'package.json'), files['package.json']); }; |