Skip to content

Commit

Permalink
import without /lib or /es6 paths, closes #147
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Mar 2, 2021
1 parent 118153a commit 5c61045
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
**Note**: Gaps between patch versions are faulty/broken releases. **Note**: A feature tagged as Experimental is in a
high state of flux, you're at risk of it changing without notice.

# 2.3.6

- **Polish**
- import without `/lib` or `/es6` paths, closes #147 (@gcanti)

# 2.3.5

- **Experimental**
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/At/ReadonlySet.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Added in v2.2.0
**Signature**

```ts
export declare const atReadonlySet: <A = never>(E: Eq<A>) => At<any, A, boolean>
export declare const atReadonlySet: <A = never>(E: Eq<A>) => At<ReadonlySet<A>, A, boolean>
```
Added in v2.2.0
16 changes: 13 additions & 3 deletions package-lock.json

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

23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"name": "monocle-ts",
"version": "2.3.5",
"version": "2.3.6",
"description": "A porting of scala monocle library to TypeScript",
"files": [
"lib",
"es6"
],
"main": "lib/index.js",
"module": "es6/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -17,14 +13,18 @@
"prettier": "prettier --no-semi --single-quote --print-width 120 --parser typescript --list-different \"{src,test,examples}/**/*.ts\"",
"fix-prettier": "prettier --no-semi --single-quote --print-width 120 --parser typescript --write \"{src,test,examples}/**/*.ts\"",
"test": "npm run prettier && npm run lint && npm run dtslint && npm run jest && npm run docs",
"clean": "rimraf lib/* es6/*",
"build": "npm run clean && tsc -p ./tsconfig.build.json && tsc -p ./tsconfig.build-es6.json",
"postbuild": "import-path-rewrite && prettier --write \"./{lib,es6}/**/*.ts\"",
"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",
"docs-fix-prettier": "prettier --no-semi --single-quote --print-width 120 --parser markdown --write \"README.md\"",
"dtslint": "dtslint dtslint",
"mocha": "TS_NODE_CACHE=false mocha -r ts-node/register test/*.ts",
"docs": "docs-ts"
"docs": "docs-ts",
"prerelease": "npm run build",
"release": "ts-node scripts/release",
"import-path-rewrite": "import-path-rewrite"
},
"repository": {
"type": "git",
Expand All @@ -40,8 +40,9 @@
"fp-ts": "^2.0.0"
},
"devDependencies": {
"@types/glob": "^7.1.3",
"@types/jest": "22.2.2",
"@types/node": "7.0.4",
"@types/node": "^13.11.0",
"docs-ts": "^0.6.2",
"dtslint": "github:gcanti/dtslint",
"fp-ts": "^2.9.3",
Expand Down
30 changes: 30 additions & 0 deletions scripts/FileSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as TE from 'fp-ts/TaskEither'
import { flow } from 'fp-ts/function'
import * as fs from 'fs'
import * as 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),
TE.orElse((e) => (e.code === 'EEXIST' ? TE.of(undefined) : TE.left(e)))
)
}
123 changes: 123 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
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'
import * as O from 'fp-ts/Option'

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<readonly [string, O.Option<string>]> {
return paths
.map((filePath) => {
const parent = pipe(
path.basename(path.dirname(filePath)),
O.fromPredicate((s) => s !== 'lib')
)
return [path.basename(filePath, '.js'), parent] as const
})
.filter((x) => x[0] !== 'index')
}

function makeSingleModule(C: FileSystem): (module: readonly [string, O.Option<string>]) => TE.TaskEither<Error, void> {
return (module) => {
return pipe(
module[1],
O.fold(
() =>
pipe(
C.mkdir(path.join(OUTPUT_FOLDER, module[0])),
TE.chain(() => C.writeFile(path.join(OUTPUT_FOLDER, module[0], 'package.json'), makePkgJson(module)))
),
(parent) =>
pipe(
C.mkdir(path.join(OUTPUT_FOLDER, parent)),
TE.chain(() => C.mkdir(path.join(OUTPUT_FOLDER, parent, module[0]))),
TE.chain(() =>
C.writeFile(path.join(OUTPUT_FOLDER, parent, module[0], 'package.json'), makePkgJson(module))
)
)
)
)
}
}

function makePkgJson(module: readonly [string, O.Option<string>]): string {
const name = module[0]
const prefix = pipe(
module[1],
O.fold(
() => '',
() => '../'
)
)
const parent = pipe(
module[1],
O.fold(
() => '',
(parent) => parent + '/'
)
)
return JSON.stringify(
{
main: `${prefix}../lib/${parent}${name}.js`,
module: `${prefix}../es6/${parent}${name}.js`,
typings: `${prefix}../lib/${parent}${name}.d.ts`,
sideEffects: false
},
null,
2
)
}

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/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 --tag=next', {
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
})
}
2 changes: 1 addition & 1 deletion tsconfig.build-es6.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./tsconfig.build.json",
"compilerOptions": {
"outDir": "./es6",
"outDir": "./dist/es6",
"module": "es6"
}
}
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"noEmit": true,
"outDir": "./lib",
"outDir": "./dist/lib",
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
Expand All @@ -16,5 +16,5 @@
"forceConsistentCasingInFileNames": true,
"stripInternal": true
},
"include": ["./src", "./test"]
"include": ["./src", "./test", "./scripts"]
}

0 comments on commit 5c61045

Please sign in to comment.