| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | import fs from 'fs-extra'; |
| | import { parse, ParserOptions } from '@babel/parser'; |
| | import traverse from '@babel/traverse'; |
| | import { invertObject } from './invertObject'; |
| | import { evalToString } from './evalToString'; |
| | import { paths } from '../constants'; |
| | import { safeVariableName } from '../utils'; |
| | import { pascalCase } from 'pascal-case'; |
| |
|
| | const babelParserOptions: ParserOptions = { |
| | sourceType: 'module', |
| | |
| | |
| | |
| | |
| | plugins: [ |
| | 'classProperties', |
| | 'flow', |
| | 'jsx', |
| | 'trailingFunctionCommas', |
| | 'objectRestSpread', |
| | ], |
| | } as ParserOptions; |
| |
|
| | export async function extractErrors(opts: any) { |
| | if (!opts || !opts.errorMapFilePath) { |
| | throw new Error( |
| | 'Missing options. Ensure you pass an object with `errorMapFilePath`.' |
| | ); |
| | } |
| |
|
| | if (!opts.name || !opts.name) { |
| | throw new Error('Missing options. Ensure you pass --name flag to tsdx'); |
| | } |
| |
|
| | const errorMapFilePath = opts.errorMapFilePath; |
| | let existingErrorMap: any; |
| | try { |
| | |
| | |
| | |
| | |
| | |
| | const fileContents = await fs.readFile(errorMapFilePath, 'utf-8'); |
| | existingErrorMap = JSON.parse(fileContents); |
| | } catch (e) { |
| | existingErrorMap = {}; |
| | } |
| |
|
| | const allErrorIDs = Object.keys(existingErrorMap); |
| | let currentID: any; |
| |
|
| | if (allErrorIDs.length === 0) { |
| | |
| | currentID = 0; |
| | } else { |
| | currentID = Math.max.apply(null, allErrorIDs as any) + 1; |
| | } |
| |
|
| | |
| | existingErrorMap = invertObject(existingErrorMap); |
| |
|
| | function transform(source: string) { |
| | const ast = parse(source, babelParserOptions); |
| |
|
| | traverse(ast, { |
| | CallExpression: { |
| | exit(astPath: any) { |
| | if (astPath.get('callee').isIdentifier({ name: 'invariant' })) { |
| | const node = astPath.node; |
| |
|
| | |
| | |
| | const errorMsgLiteral = evalToString(node.arguments[1]); |
| | addToErrorMap(errorMsgLiteral); |
| | } |
| | }, |
| | }, |
| | }); |
| | } |
| |
|
| | function addToErrorMap(errorMsgLiteral: any) { |
| | if (existingErrorMap.hasOwnProperty(errorMsgLiteral)) { |
| | return; |
| | } |
| | existingErrorMap[errorMsgLiteral] = '' + currentID++; |
| | } |
| |
|
| | async function flush() { |
| | const prettyName = pascalCase(safeVariableName(opts.name)); |
| | |
| | await fs.ensureDir(paths.appErrors); |
| |
|
| | |
| | await fs.writeFile( |
| | errorMapFilePath, |
| | JSON.stringify(invertObject(existingErrorMap), null, 2) + '\n', |
| | 'utf-8' |
| | ); |
| |
|
| | |
| | await fs.writeFile( |
| | paths.appErrors + '/ErrorDev.js', |
| | ` |
| | function ErrorDev(message) { |
| | const error = new Error(message); |
| | error.name = 'Invariant Violation'; |
| | return error; |
| | } |
| | |
| | export default ErrorDev; |
| | `, |
| | 'utf-8' |
| | ); |
| |
|
| | await fs.writeFile( |
| | paths.appErrors + '/ErrorProd.js', |
| | ` |
| | function ErrorProd(code) { |
| | // TODO: replace this URL with yours |
| | let url = 'https://reactjs.org/docs/error-decoder.html?invariant=' + code; |
| | for (let i = 1; i < arguments.length; i++) { |
| | url += '&args[]=' + encodeURIComponent(arguments[i]); |
| | } |
| | return new Error( |
| | \`Minified ${prettyName} error #$\{code}; visit $\{url} for the full message or \` + |
| | 'use the non-minified dev environment for full errors and additional ' + |
| | 'helpful warnings. ' |
| | ); |
| | } |
| | |
| | export default ErrorProd; |
| | `, |
| | 'utf-8' |
| | ); |
| | } |
| |
|
| | return async function extractErrors(source: any) { |
| | transform(source); |
| | await flush(); |
| | }; |
| | } |
| |
|