Skip to content

Commit

Permalink
expose fp-ts-rxjs modules without lib/es6 prefix, closes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jan 16, 2021
1 parent e3c3549 commit 6ad1fb1
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 22 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
*.log
node_modules
lib
es6
dist
dev
.idea
coverage
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ high state of flux, you're at risk of it changing without notice.

- **New Feature**
- add `ObservableThese`, #44 (@siuvdlec)
- expose `fp-ts-rxjs` modules without lib/es6 prefix, closes #40 (@gcanti)
- **Polish**
- export `MonadObservable` from `index.ts` (@canti)
- **Internal**
Expand Down
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 8 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
"name": "fp-ts-rxjs",
"version": "0.6.12",
"description": "fp-ts bindings for RxJS",
"files": [
"lib",
"es6"
],
"main": "lib/index.js",
"module": "es6/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -16,12 +12,15 @@
"fix-prettier": "prettier --no-semi --single-quote --print-width 120 --parser typescript --write \"{src,test}/**/*.ts\"",
"jest": "jest --ci",
"test": "npm run prettier && npm run lint && npm run jest && npm run docs",
"clean": "rm -rf lib/* es6/*",
"clean": "rm -rf ./dist",
"prebuild": "npm run clean",
"build": "tsc -p ./tsconfig.build.json && tsc -p ./tsconfig.build-es6.json",
"prepublish": "npm run build",
"build": "tsc -p ./tsconfig.build.json && tsc -p ./tsconfig.build-es6.json && npm run import-path-rewrite && ts-node scripts/build",
"postbuild": "prettier --loglevel=silent --write \"./dist/**/*.ts\"",
"prepublishOnly": "ts-node scripts/pre-publish",
"docs": "docs-ts",
"postbuild": "import-path-rewrite"
"prerelease": "npm run build",
"release": "ts-node scripts/release",
"import-path-rewrite": "import-path-rewrite"
},
"repository": {
"type": "git",
Expand All @@ -39,7 +38,7 @@
},
"devDependencies": {
"@types/mocha": "2.2.38",
"@types/node": "7.0.4",
"@types/node": "^13.11.0",
"docs-ts": "^0.3.4",
"fp-ts": "^2.4.4",
"import-path-rewrite": "github:gcanti/import-path-rewrite",
Expand Down
29 changes: 29 additions & 0 deletions scripts/FileSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as TE from 'fp-ts/lib/TaskEither'
import { flow } from 'fp-ts/lib/function'
import * as fs from 'fs'
import G from 'glob'

export interface FileSystem {
readonly readFile: (path: string) => TE.TaskEither<Error, string>
readonly writeFile: (path: string, content: string) => TE.TaskEither<Error, void>
readonly copyFile: (from: string, to: string) => TE.TaskEither<Error, void>
readonly glob: (pattern: string) => TE.TaskEither<Error, ReadonlyArray<string>>
readonly mkdir: (path: string) => TE.TaskEither<Error, void>
}

const readFile = TE.taskify<fs.PathLike, string, NodeJS.ErrnoException, string>(fs.readFile)
const writeFile = TE.taskify<fs.PathLike, string, NodeJS.ErrnoException, void>(fs.writeFile)
const copyFile = TE.taskify<fs.PathLike, fs.PathLike, NodeJS.ErrnoException, void>(fs.copyFile)
const glob = TE.taskify<string, Error, ReadonlyArray<string>>(G)
const mkdirTE = TE.taskify(fs.mkdir)

export const fileSystem: FileSystem = {
readFile: path => readFile(path, 'utf8'),
writeFile,
copyFile,
glob,
mkdir: flow(
mkdirTE,
TE.map(() => undefined)
)
}
85 changes: 85 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as path from 'path'
import * as E from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/pipeable'
import * as RTE from 'fp-ts/lib/ReaderTaskEither'
import * as A from 'fp-ts/lib/Array'
import * as TE from 'fp-ts/lib/TaskEither'
import { FileSystem, fileSystem } from './FileSystem'
import { run } from './run'

interface Build<A> extends RTE.ReaderTaskEither<FileSystem, Error, A> {}

const OUTPUT_FOLDER = 'dist'
const PKG = 'package.json'

export const copyPackageJson: Build<void> = C =>
pipe(
C.readFile(PKG),
TE.chain(s => TE.fromEither(E.parseJSON(s, E.toError))),
TE.map(v => {
const clone = Object.assign({}, v as any)

delete clone.scripts
delete clone.files
delete clone.devDependencies

return clone
}),
TE.chain(json => C.writeFile(path.join(OUTPUT_FOLDER, PKG), JSON.stringify(json, null, 2)))
)

export const FILES: Array<string> = ['CHANGELOG.md', 'LICENSE', 'README.md']

export const copyFiles: Build<ReadonlyArray<void>> = C =>
A.array.traverse(TE.taskEither)(FILES, from => C.copyFile(from, path.resolve(OUTPUT_FOLDER, from)))

const traverse = A.array.traverse(TE.taskEither)

export const makeModules: Build<void> = C =>
pipe(
C.glob(`${OUTPUT_FOLDER}/lib/*.js`),
TE.map(getModules),
TE.chain(x => traverse(x, makeSingleModule(C))),
TE.map(() => undefined)
)

function getModules(paths: ReadonlyArray<string>): Array<string> {
return paths.map(filePath => path.basename(filePath, '.js')).filter(x => x !== 'index')
}

function makeSingleModule(C: FileSystem): (module: string) => TE.TaskEither<Error, void> {
return m =>
pipe(
C.mkdir(path.join(OUTPUT_FOLDER, m)),
TE.chain(() => makePkgJson(m)),
TE.chain(data => C.writeFile(path.join(OUTPUT_FOLDER, m, 'package.json'), data))
)
}

function makePkgJson(module: string): TE.TaskEither<Error, string> {
return pipe(
JSON.stringify(
{
main: `../lib/${module}.js`,
module: `../es6/${module}.js`,
typings: `../lib/${module}.d.ts`,
sideEffects: false
},
null,
2
),
TE.right
)
}

const main: Build<void> = pipe(
copyPackageJson,
RTE.chain(() => copyFiles),
RTE.chain(() => makeModules)
)

run(
main({
...fileSystem
})
)
6 changes: 6 additions & 0 deletions scripts/pre-publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { left } from 'fp-ts/lib/TaskEither'
import { run } from './run'

const main = left(new Error('"npm publish" can not be run from root, run "npm run release" instead'))

run(main)
23 changes: 23 additions & 0 deletions scripts/release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { run } from './run'
import * as child_process from 'child_process'
import { left, right } from 'fp-ts/lib/Either'
import * as TE from 'fp-ts/lib/TaskEither'

const DIST = 'dist'

const exec = (cmd: string, args?: child_process.ExecOptions): TE.TaskEither<Error, void> => () =>
new Promise(resolve => {
child_process.exec(cmd, args, err => {
if (err !== null) {
return resolve(left(err))
}

return resolve(right(undefined))
})
})

export const main = exec('npm publish', {
cwd: DIST
})

run(main)
21 changes: 21 additions & 0 deletions scripts/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { fold } from 'fp-ts/lib/Either'
import { TaskEither } from 'fp-ts/lib/TaskEither'

export function run<A>(eff: TaskEither<Error, A>): void {
eff()
.then(
fold(
e => {
throw e
},
_ => {
process.exitCode = 0
}
)
)
.catch(e => {
console.error(e) // tslint:disable-line no-console

process.exitCode = 1
})
}
5 changes: 3 additions & 2 deletions tsconfig.build-es6.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"outDir": "./es6",
"module": "es6"
"outDir": "./dist/es6",
"module": "es6",
"target": "es6"
}
}
8 changes: 5 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"compilerOptions": {
"noEmit": true,
"outDir": "./lib",
"outDir": "./dist/lib",
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"lib": ["es6", "dom"],
"sourceMap": false,
"declaration": true,
Expand All @@ -13,7 +14,8 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"stripInternal": true
},
"include": ["./src", "./test"]
"include": ["./src", "./test", "./scripts"]
}

0 comments on commit 6ad1fb1

Please sign in to comment.