Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fp ts bare import #137

Merged
merged 13 commits into from
Aug 19, 2020
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
*.log
node_modules
lib
es6
/dist
dev
coverage
16 changes: 8 additions & 8 deletions package-lock.json

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

24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
"name": "io-ts-types",
"version": "0.5.8",
"description": "A collection of codecs and combinators for use with io-ts",
"files": [
"lib",
"es6"
],
"main": "lib/index.js",
"module": "es6/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -16,13 +12,17 @@
"fix-prettier": "prettier --no-semi --single-quote --print-width 120 --parser typescript --write \"{src,test}/**/*.ts\"",
"jest": "jest --ci",
"test": "npm run build && npm run lint && npm run dtslint && npm run prettier && npm run jest && npm run docs",
"clean": "rm -rf lib/* es6/*",
"build": "npm run clean && tsc && tsc -p ./tsconfig.es6.json",
"prepublish": "npm run build",
"clean": "rm -rf ./dist",
"prebuild": "npm run clean",
"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",
"dtslint": "dtslint dtslint",
"mocha": "mocha -r ts-node/register test/*.ts",
"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 @@ -42,11 +42,10 @@
},
"devDependencies": {
"@types/jest": "^24.0.15",
"@types/node": "7.0.4",
"@types/node": "^14.0.27",
"docs-ts": "^0.3.4",
"dtslint": "github:gcanti/dtslint",
"fp-ts": "^2.0.0",
"import-path-rewrite": "github:gcanti/import-path-rewrite",
"fp-ts": "^2.8.0",
"io-ts": "^2.0.0",
"jest": "^24.8.0",
"mocha": "^5.2.0",
Expand All @@ -57,7 +56,8 @@
"ts-node": "3.2.1",
"tslint": "^5.12.1",
"tslint-config-standard": "^8.0.1",
"typescript": "^3.9.3"
"typescript": "^3.9.3",
"import-path-rewrite": "github:gcanti/import-path-rewrite"
},
"tags": [
"io-ts",
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/TaskEither'
import { flow } from 'fp-ts/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)
)
}
88 changes: 88 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import * as path from 'path'
import * as E from 'fp-ts/Either'
import { pipe } from 'fp-ts/function'
import * as RTE from 'fp-ts/ReaderTaskEither'
import * as A from 'fp-ts/ReadonlyArray'
import * as TE from 'fp-ts/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: ReadonlyArray<string> = ['CHANGELOG.md', 'LICENSE', 'README.md']

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

const traverse = A.traverse(TE.taskEither)

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

function getModules(paths: ReadonlyArray<string>): ReadonlyArray<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
})
)
7 changes: 7 additions & 0 deletions scripts/pre-publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { left } from 'fp-ts/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/Either'
import * as TE from 'fp-ts/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/Either'
import { TaskEither } from 'fp-ts/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
})
}
7 changes: 7 additions & 0 deletions tsconfig.build-es6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"outDir": "./dist/es6",
"module": "es6"
}
}
6 changes: 3 additions & 3 deletions tsconfig.es6.json → tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./es6",
"module": "es6"
}
"noEmit": false
},
"include": ["./src"]
}
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"compilerOptions": {
"outDir": "./lib",
"outDir": "./dist/lib",
"noEmit": true,
"esModuleInterop": true,
"sourceMap": false,
"declaration": true,
"module": "commonjs",
Expand Down