@charlietango/react-docs-net
Advanced tools
Comparing version
#!/usr/bin/env node | ||
const docsNet = require('../lib/index'); | ||
const chalk = require('chalk'); | ||
const chalk = require('ansi-colors'); | ||
const meow = require('meow'); | ||
@@ -5,0 +5,0 @@ |
// | ||
const upperFirst = require('lodash/fp/upperFirst'); | ||
const camelCase = require('lodash/fp/camelCase'); | ||
@@ -58,2 +59,44 @@ | ||
/** | ||
* Define a map for types to what they should import. | ||
* If the type (key) matches a prop type, it will add the "using" statement to the top of the file. | ||
*/ | ||
const usingMap = new Map(); | ||
usingMap.set('DateTime', 'using System;'); | ||
usingMap.set( | ||
'EnumType', | ||
'using Newtonsoft.Json;\nusing Newtonsoft.Json.Converters;' | ||
); | ||
/** | ||
* Figure out if the models are using types that need to import C# classes (also known as using), | ||
* and return a list of these so they can be added to the files that need them. | ||
*/ | ||
function getUsingTypes( | ||
models , | ||
props | ||
) { | ||
const using = new Set(); | ||
// All files should include the System.Collections.Generic type | ||
using.add('using System.Collections.Generic;'); | ||
if (props) { | ||
props.forEach(type => { | ||
// Check all the prop types, to see if they have a match with a using import | ||
if (usingMap.has(type)) { | ||
using.add(usingMap.get(type)); | ||
} | ||
}); | ||
} | ||
models.forEach((type ) => { | ||
if (!!type.types && !isEnumNumbers(type.types)) { | ||
// If the type has enum like types, mark it as such | ||
using.add(usingMap.get('EnumType')); | ||
} | ||
}); | ||
return [...using].join('\n'); | ||
} | ||
module.exports = { | ||
@@ -68,2 +111,3 @@ comment, | ||
buildPropFlags, | ||
getUsingTypes, | ||
}; |
@@ -7,3 +7,3 @@ // | ||
const globby = require('globby'); | ||
const chalk = require('chalk'); | ||
const chalk = require('ansi-colors'); | ||
const mkdirp = require('mkdirp'); | ||
@@ -10,0 +10,0 @@ const createViewModel = require('./models/view-model'); |
@@ -19,2 +19,3 @@ // | ||
const onlyNumbers = isEnumNumbers(types); | ||
return types.elements | ||
@@ -21,0 +22,0 @@ .map((el, i) => { |
@@ -8,2 +8,3 @@ // | ||
import { getUsingTypes } from '../helpers'; | ||
@@ -24,2 +25,3 @@ const upperFirst = require('lodash/fp/upperFirst'); | ||
const models = []; | ||
const propMap = new Map(); | ||
const warnings = []; | ||
@@ -33,2 +35,3 @@ const referencedModels = []; | ||
if (!type || !type.type) return ''; | ||
propMap.set(name, type.type); | ||
@@ -51,3 +54,3 @@ let header = ''; | ||
return { output, models, warnings, referencedModels }; | ||
return { output, props: propMap, models, warnings, referencedModels }; | ||
} | ||
@@ -64,4 +67,5 @@ | ||
const propsResult = injectProps(name, model.props, 8); | ||
const fileSource = `using System.Collections.Generic; | ||
const fileSource = `${getUsingTypes(propsResult.models, propsResult.props)} | ||
/* | ||
@@ -68,0 +72,0 @@ GENERATED FILE - DO NOT MODIFY |
@@ -8,3 +8,5 @@ // | ||
import { getUsingTypes } from '../helpers'; | ||
const createSubViewModel = require('./sub-view-model'); | ||
@@ -31,2 +33,3 @@ const isEnumNumbers = require('../helpers').isEnumNumbers; | ||
const referencedModels = []; | ||
const propMap = new Map(); | ||
@@ -42,2 +45,3 @@ keys.forEach(name => { | ||
if (!type || !type.type) return ''; | ||
propMap.set(name, type.type); | ||
@@ -84,3 +88,3 @@ let header = ''; | ||
return { output, models, warnings, referencedModels }; | ||
return { output, props: propMap, models, warnings, referencedModels }; | ||
} | ||
@@ -94,12 +98,5 @@ | ||
const propsResult = injectProps(model.displayName || name, model.props, 8); | ||
const hasEnumTypes = propsResult.models.some( | ||
model => !!model.types && !isEnumNumbers(model.types) | ||
); | ||
const fileSource = `using System.Collections.Generic;${ | ||
hasEnumTypes | ||
? `\nusing Newtonsoft.Json; | ||
using Newtonsoft.Json.Converters;` | ||
: '' | ||
} | ||
const fileSource = `${getUsingTypes(propsResult.models, propsResult.props)} | ||
/* | ||
@@ -106,0 +103,0 @@ GENERATED FILE - DO NOT MODIFY |
{ | ||
"name": "@charlietango/react-docs-net", | ||
"description": "Package and publish a NuGet package to a custom feed.", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"main": "lib/index.js", | ||
@@ -30,3 +30,3 @@ "bin": { | ||
"dependencies": { | ||
"chalk": "^2.4.1", | ||
"ansi-colors": "^3.2.2", | ||
"del": "^3.0.0", | ||
@@ -42,3 +42,3 @@ "globby": "^8.0.1", | ||
"chokidar": "^2.0.4", | ||
"flow-bin": "^0.86.0", | ||
"flow-bin": "^0.87.0", | ||
"jest": "^23.6.0", | ||
@@ -50,3 +50,3 @@ "jest-plugin-fs": "^2.7.0", | ||
}, | ||
"gitHead": "5e0a576e12a710a757e4c82ea78f5991e282191e" | ||
"gitHead": "1e8df2d63714f2bca3e03b89997a02ca3ba0ef4d" | ||
} |
# React Docs Net | ||
Take the JSON output from react-docgen, and convert it C# ViewModels for consumption in .NET projects. | ||
Take the JSON output from [react-docgen](https://github.com/reactjs/react-docgen), and convert it to **C#** ViewModels for consumption in .NET projects. | ||
Why would you do this? It allows us to define the ViewModels in the Frontend where we actually use them. | ||
@@ -8,4 +8,4 @@ | ||
> | ||
> This is a basic rewrite of the React props to C#/.NET. No validation is done on the actual files. | ||
> Not all Flow features are supported, since there's not a simple way to convert them to C#. | ||
> This is a basic rewrite of the React props to **C#**/**.NET**. No validation is done on the actual files. | ||
> Not all Flow features are supported, since there's not a simple way to convert them to **C#**. | ||
@@ -12,0 +12,0 @@ - All models are converted to upper camelcase, with `ViewModel` appendend. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
47114
6.29%787
4.79%+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed