-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add makeStylesImport, withStylesImport options
fix #2
- Loading branch information
1 parent
ded795f
commit 1cfe281
Showing
8 changed files
with
155 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Options, ASTPath, Node } from 'jscodeshift' | ||
import pathsInRange from 'jscodeshift-paths-in-range' | ||
|
||
type Filter = ( | ||
path: ASTPath<Node>, | ||
index: number, | ||
paths: Array<ASTPath<Node>> | ||
) => boolean | ||
|
||
export function getFilter(options: Options): Filter { | ||
if (options.selectionStart) { | ||
const selectionStart = parseInt(options.selectionStart) | ||
const selectionEnd = options.selectionEnd | ||
? parseInt(options.selectionEnd) | ||
: selectionStart | ||
return pathsInRange(selectionStart, selectionEnd) | ||
} else { | ||
return (): boolean => true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import * as path from 'path' | ||
import { Options, ImportDeclaration, FileInfo, API } from 'jscodeshift' | ||
import getStylesPackage from './getStylesPackage' | ||
import pkgConf from 'pkg-conf' | ||
import defaults from 'lodash/defaults' | ||
import mapValues from 'lodash/fp/mapValues' | ||
import pick from 'lodash/fp/pick' | ||
import pickBy from 'lodash/fp/pickBy' | ||
import flow from 'lodash/fp/flow' | ||
|
||
type Imports = { | ||
themeImport: ImportDeclaration | ||
withStylesImport: ImportDeclaration | ||
makeStylesImport: ImportDeclaration | ||
} | ||
|
||
export default function getImports( | ||
{ path: file }: FileInfo, | ||
{ j }: API, | ||
options: Options | ||
): Imports { | ||
const { statement } = j.template | ||
const stylesPackage = getStylesPackage(file) | ||
const packageConf = pkgConf.sync('material-ui-codemorphs', { | ||
cwd: path.dirname(file), | ||
skipOnFalse: true, | ||
}) | ||
const filepath = pkgConf.filepath(packageConf) | ||
const resolveImport = (cwd: string) => (imp: string): ImportDeclaration => { | ||
const parsed = statement([imp]) | ||
if (parsed.source.value.startsWith('.') && file) { | ||
parsed.source.value = path.relative( | ||
path.dirname(file), | ||
path.resolve(cwd, parsed.source.value) | ||
) | ||
} | ||
return parsed | ||
} | ||
const resolveImports = ( | ||
cwd: string | ||
): ((raw: Record<string, unknown>) => Partial<Imports>) => | ||
flow([ | ||
pick(['themeImport', 'withStylesImport', 'makeStylesImport']), | ||
pickBy(Boolean), | ||
mapValues(resolveImport(cwd)), | ||
]) | ||
return defaults( | ||
resolveImports(process.cwd())(options), | ||
resolveImports(path.dirname(filepath || process.cwd()))(packageConf), | ||
resolveImports(process.cwd())({ | ||
themeImport: `import { Theme } from '${stylesPackage}'`, | ||
withStylesImport: `import { withStyles } from '${stylesPackage}'`, | ||
makeStylesImport: `import { makeStyles } from '${stylesPackage}'`, | ||
}) | ||
) as Imports | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.