-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite fp-ts import paths in es6 dir on post build (#33)
* rewrite fp-ts import paths in es6 dir on post build * use 'Eff<A>' as parameter type * rename 'Monad' names
- Loading branch information
1 parent
46bb471
commit b87b1d8
Showing
8 changed files
with
128 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,36 @@ | ||
import { mapLeft, taskify } from 'fp-ts/lib/TaskEither' | ||
import { flow } from 'fp-ts/lib/function' | ||
import { pipe } from 'fp-ts/lib/pipeable' | ||
import * as fs from 'fs' | ||
import Glob from 'glob' | ||
import { Eff } from './program' | ||
|
||
export interface FileSystem { | ||
readonly readFile: (path: string) => Eff<string> | ||
readonly writeFile: (path: string, content: string) => Eff<void> | ||
readonly glob: (pattern: string) => Eff<string[]> | ||
} | ||
|
||
const readFileTE = taskify<fs.PathLike, string, NodeJS.ErrnoException, string>(fs.readFile) | ||
const writeFileTE = taskify<fs.PathLike, string, NodeJS.ErrnoException, void>(fs.writeFile) | ||
const globTE = taskify<string, Error, string[]>(Glob) | ||
const toError = (e: Error): string => e.message | ||
|
||
export const fileSystemNode: FileSystem = { | ||
readFile: path => | ||
pipe( | ||
readFileTE(path, 'utf8'), | ||
mapLeft(toError) | ||
), | ||
|
||
writeFile: flow( | ||
writeFileTE, | ||
mapLeft(toError) | ||
), | ||
|
||
glob: pattern => | ||
pipe( | ||
globTE(pattern), | ||
mapLeft(toError) | ||
) | ||
} |
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,13 @@ | ||
import { info, log } from 'fp-ts/lib/Console' | ||
import { rightIO } from 'fp-ts/lib/TaskEither' | ||
import { Eff } from './program' | ||
|
||
export interface Logger { | ||
readonly info: (s: string) => Eff<void> | ||
readonly log: (s: string) => Eff<void> | ||
} | ||
|
||
export const loggerConsole: Logger = { | ||
info: s => rightIO(info(`> ${s}`)), | ||
log: s => rightIO(log(`\n> ${s}`)) | ||
} |
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,44 @@ | ||
import { array } from 'fp-ts/lib/Array' | ||
import * as RTE from 'fp-ts/lib/ReaderTaskEither' | ||
import * as TE from 'fp-ts/lib/TaskEither' | ||
import { pipe } from 'fp-ts/lib/pipeable' | ||
import { FileSystem, fileSystemNode } from './helpers/fs' | ||
import { Logger, loggerConsole } from './helpers/logger' | ||
import { Program, run } from './helpers/program' | ||
|
||
const PATH_REGEXP = /(\s(?:from|module)\s['|"]fp-ts)\/lib\/([\w-\/]+['|"])/gm | ||
const ES6_GLOB_PATTERN = 'es6/**/*.@(ts|js)' | ||
|
||
const traverseRTE = array.traverse(RTE.readerTaskEither) | ||
|
||
interface Capabilities extends FileSystem, Logger {} | ||
|
||
interface AppEff<A> extends Program<Capabilities, A> {} | ||
|
||
const getES6Paths: AppEff<string[]> = C => C.glob(ES6_GLOB_PATTERN) | ||
|
||
const replacePath = (content: string): string => content.replace(PATH_REGEXP, '$1/es6/$2') | ||
|
||
const rewritePaths = (file: string): AppEff<void> => C => | ||
pipe( | ||
C.info(`Rewriting file ${file}`), | ||
TE.chain(() => C.readFile(file)), | ||
TE.map(replacePath), | ||
TE.chain(content => C.writeFile(file, content)) | ||
) | ||
|
||
const log = (s: string): AppEff<void> => C => C.log(s) | ||
|
||
const main: AppEff<void[]> = pipe( | ||
getES6Paths, | ||
RTE.chain(files => traverseRTE(files, rewritePaths)), | ||
RTE.chainFirst(() => log('ES6 import paths rewritten')) | ||
) | ||
|
||
// --- Run the program | ||
run( | ||
main({ | ||
...fileSystemNode, | ||
...loggerConsole | ||
}) | ||
) |
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