Skip to content

Commit

Permalink
fix(setupSystem): revert all the babel-node madness
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Jan 21, 2020
1 parent 0699a5c commit b727c34
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 63 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@
"homepage": "https://github.com/jedwards1211/material-ui-codemorphs#readme",
"devDependencies": {
"@babel/cli": "^7.1.5",
"@babel/core": "^7.1.6",
"@babel/node": "^7.8.3",
"@babel/plugin-proposal-class-properties": "^7.1.0",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.4.4",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.6.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.1.0",
"@babel/preset-env": "^7.1.6",
"@babel/preset-typescript": "^7.7.2",
"@babel/register": "^7.0.0",
"@commitlint/cli": "^6.0.2",
Expand Down Expand Up @@ -126,11 +131,6 @@
"typescript": "^3.7.2"
},
"dependencies": {
"@babel/core": "^7.1.6",
"@babel/node": "^7.8.3",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
"@babel/plugin-proposal-export-namespace-from": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/runtime": "^7.1.5",
"jscodeshift": "^0.7.0",
"jscodeshift-add-imports": "^1.0.3",
Expand Down
81 changes: 23 additions & 58 deletions src/setupSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,41 @@
import { JSCodeshift } from 'jscodeshift'
import addImports from 'jscodeshift-add-imports'
import pipeline from './util/pipeline'
import { execFileSync } from 'child_process'
import { uniq, map, compact, flatMap } from 'lodash/fp'
import * as nodepath from 'path'
import pkgConf from 'pkg-conf'
import { memoize } from 'lodash'

const getSystem = (dir: string): string => `
var system = require('${dir}/node_modules/@material-ui/system')
var result = {}
for (var key in system) {
var value = system[key]
if (value && Array.isArray(value.filterProps)) {
value.filterProps.forEach((prop) => {
if (
!result[prop] ||
system[result[prop]].filterProps.length < value.filterProps.length
) {
result[prop] = key
}
})
import resolve from 'resolve'

const getSystemImports = (dir: string): Record<string, string> => {
/* eslint-disable @typescript-eslint/no-var-requires */
const system = require(resolve.sync('@material-ui/system', { basedir: dir }))
/* eslint-enable @typescript-eslint/no-var-requires */

const result: Record<string, string> = {}

for (const key in system) {
const value = system[key]
if (value && Array.isArray(value.filterProps)) {
value.filterProps.forEach((prop: string) => {
if (
!result[prop] ||
system[result[prop]].filterProps.length < value.filterProps.length
) {
result[prop] = key
}
})
}
}
}

console.log(JSON.stringify(result))
`

const findRoot = (file: string): string => {
const conf = pkgConf.sync('version', { cwd: nodepath.dirname(file) })
const filepath = pkgConf.filepath(conf)
if (!filepath) throw new Error(`failed to get root path for ${file}`)
return nodepath.dirname(filepath)
return result
}

const getSystemImports = memoize(
(dir: string): Record<string, string> => {
const cwd = findRoot(__filename)

const babelNode = nodepath.join(cwd, 'node_modules', '.bin', 'babel-node')

const out = execFileSync(
babelNode,
[
'--presets',
'@babel/preset-env',
'--plugins',
'@babel/plugin-proposal-export-default-from',
'@babel/plugin-proposal-export-namespace-from',
'-e',
getSystem(dir),
],
{
cwd,
encoding: 'utf8',
}
)

return JSON.parse(out)
}
)

module.exports = function setupMaterialUISystem(
{ path, source }: { path: string; source: string },
{ jscodeshift: j }: { jscodeshift: JSCodeshift }
): string {
const root = j(source)
const { statement } = j.template

const systemImports = getSystemImports(findRoot(path))
const systemImports = getSystemImports(path)

const breakpointKeys = new Set(['xs', 'sm', 'md', 'lg', 'xl'])
let hasBreakpoints = false
Expand Down

0 comments on commit b727c34

Please sign in to comment.