-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
expose modules without lib/es6 prefix, closes #67
- Loading branch information
Showing
12 changed files
with
257 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
*.log | ||
node_modules | ||
lib | ||
es6 | ||
dist | ||
dev | ||
coverage |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as TE from 'fp-ts/TaskEither' | ||
import { pipe } from 'fp-ts/function' | ||
import * as fs from 'fs' | ||
import G from 'glob' | ||
import * as path from 'path' | ||
import * as A from 'fp-ts/ReadonlyArray' | ||
|
||
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) | ||
|
||
function mkdir(p: string): TE.TaskEither<Error, void> { | ||
function go(p: ReadonlyArray<string>, i: number): TE.TaskEither<Error, void> { | ||
if (A.isOutOfBound(i, p)) { | ||
return TE.of(undefined) | ||
} else { | ||
return pipe( | ||
pipe( | ||
mkdirTE(path.join(...p.slice(0, i + 1))), | ||
TE.map(() => undefined) | ||
), | ||
TE.alt(() => TE.of(undefined)), // dir already exists | ||
TE.chain(() => go(p, i + 1)), | ||
TE.map(() => undefined) | ||
) | ||
} | ||
} | ||
return go(p.split(path.sep), 0) | ||
} | ||
|
||
export const fileSystem: FileSystem = { | ||
readFile: (path) => readFile(path, 'utf8'), | ||
writeFile, | ||
copyFile, | ||
glob, | ||
mkdir | ||
} |
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,107 @@ | ||
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) | ||
) | ||
|
||
interface Module { | ||
readonly name: string | ||
readonly directories: Array<string> | ||
} | ||
|
||
function getModules(paths: ReadonlyArray<string>): ReadonlyArray<Module> { | ||
return paths | ||
.map((filePath) => { | ||
const name = path.basename(filePath, '.js') | ||
const directories = path.dirname(filePath).split(path.sep).slice(2) | ||
return { name, directories } | ||
}) | ||
.filter((x) => x.name !== 'index' || x.directories.length > 0) | ||
} | ||
|
||
function isIndex(module: Module): boolean { | ||
return module.name === 'index' | ||
} | ||
|
||
function makeSingleModule(C: FileSystem): (module: Module) => TE.TaskEither<Error, void> { | ||
return (module) => { | ||
const dirs = module.directories | ||
const dir = path.join(OUTPUT_FOLDER, ...dirs, isIndex(module) ? '' : module.name) | ||
return pipe( | ||
C.mkdir(dir), | ||
TE.chain(() => makePkgJson(module)), | ||
TE.chain((data) => C.writeFile(path.join(dir, PKG), data)) | ||
) | ||
} | ||
} | ||
|
||
function makePkgJson(module: Module): TE.TaskEither<Error, string> { | ||
const root = A.replicate(module.directories.length + (isIndex(module) ? 0 : 1), '..') | ||
return pipe( | ||
JSON.stringify( | ||
{ | ||
main: path.join(...root, 'lib', ...module.directories, `${module.name}.js`), | ||
module: path.join(...root, 'es6', ...module.directories, `${module.name}.js`), | ||
typings: path.join(...root, 'lib', ...module.directories, `${module.name}.d.ts`), | ||
sideEffects: false | ||
}, | ||
null, | ||
2 | ||
), | ||
TE.right | ||
) | ||
} | ||
|
||
const main: Build<void> = pipe( | ||
copyPackageJson, | ||
RTE.chain(() => copyFiles), | ||
RTE.chain(() => makeModules) | ||
) | ||
|
||
run( | ||
main({ | ||
...fileSystem | ||
}) | ||
) |
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,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) | ||
|
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,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) |
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,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 | ||
}) | ||
} |
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,7 @@ | ||
{ | ||
"extends": "./tsconfig.build.json", | ||
"compilerOptions": { | ||
"outDir": "./dist/es6", | ||
"module": "es6" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "./es6", | ||
"module": "es6" | ||
} | ||
"noEmit": false | ||
}, | ||
"include": ["./src"] | ||
} |
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "./lib", | ||
"declaration": true, | ||
"outDir": "./dist/lib", | ||
"noEmit": true, | ||
"esModuleInterop": true, | ||
"target": "es5", | ||
"module": "commonjs", | ||
"noImplicitReturns": false, | ||
"moduleResolution": "node", | ||
"lib": ["es6", "dom"], | ||
"sourceMap": false, | ||
"declaration": true, | ||
"strict": true, | ||
"noImplicitReturns": true, | ||
"noUnusedLocals": true, | ||
"noUnusedParameters": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noEmitOnError": false, | ||
"strict": true, | ||
"target": "es5", | ||
"moduleResolution": "node", | ||
"forceConsistentCasingInFileNames": true, | ||
"lib": ["es6"] | ||
"forceConsistentCasingInFileNames": true | ||
}, | ||
"include": ["./src/**/*"] | ||
"include": ["./src", "./test"] | ||
} |