diff --git a/package.json b/package.json index fa502ad..921a758 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/setupSystem.ts b/src/setupSystem.ts index 477dc24..84acae4 100644 --- a/src/setupSystem.ts +++ b/src/setupSystem.ts @@ -3,68 +3,33 @@ 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 => { + /* 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 = {} + + 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 => { - 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 } @@ -72,7 +37,7 @@ module.exports = function setupMaterialUISystem( 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