Skip to content

Commit 02b8f08

Browse files
author
Malte Legenhausen
authored
feat: Support for import without lib or es6 (#65)
1 parent 674a2ac commit 02b8f08

File tree

10 files changed

+246
-21
lines changed

10 files changed

+246
-21
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
/.idea
22
/node_modules
33
/dist
4-
/es6

package-lock.json

Lines changed: 63 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,45 @@
66
"typings": "dist/index.d.ts",
77
"sideEffects": false,
88
"scripts": {
9-
"build": "tsc -p ./tsconfig.build.json && tsc -p ./tsconfig.es6.json",
9+
"build": "tsc -p ./tsconfig.build.json && tsc -p ./tsconfig.es6.json && npm run import-path-rewrite && ts-node scripts/build",
1010
"test": "npm run tslint && npm run prettier && npm run jest",
1111
"tslint": "tslint -c tslint.json --project tsconfig.json './src/**/*.ts'",
1212
"jest": "jest",
13-
"prettier": "prettier --list-different \"./src/**/*.ts\"",
14-
"prettier:fix": "prettier --write \"./src/**/*.ts\"",
15-
"prepublishOnly": "npm run test && npm run build",
16-
"postbuild": "import-path-rewrite",
13+
"prettier": "prettier --list-different \"./{src,scripts}/**/*.ts\"",
14+
"prettier:fix": "prettier --write \"./{src,scripts}/**/*.ts\"",
15+
"prepublishOnly": "ts-node scripts/pre-publish",
16+
"prerelease": "npm run build",
17+
"import-path-rewrite": "import-path-rewrite",
18+
"release": "ts-node scripts/release",
1719
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
1820
"version": "npm run changelog && git add CHANGELOG.md"
1921
},
2022
"author": "devexperts",
2123
"license": "MPL-2.0",
2224
"devDependencies": {
2325
"@devexperts/lint": "^0.29.1",
26+
"@types/glob": "^7.1.3",
2427
"@types/jest": "^22.2.3",
2528
"conventional-changelog-cli": "^2.0.21",
2629
"import-path-rewrite": "github:gcanti/import-path-rewrite",
2730
"jest": "^24.8.0",
2831
"jest-cli": "^24.8.0",
32+
"fp-ts": "^2.5.0",
33+
"glob": "^7.1.7",
34+
"io-ts": "^2.0.0",
35+
"io-ts-types": "^0.5.7",
2936
"prettier": "^1.17.1",
3037
"ts-jest": "^23.10.5",
3138
"tslint": "^5.16.0",
3239
"tslint-config-prettier": "^1.18.0",
3340
"tslint-plugin-prettier": "^1.3.0",
41+
"ts-node": "8.8.2",
3442
"typescript": "^3.5.2"
3543
},
36-
"dependencies": {
44+
"peerDependencies": {
3745
"fp-ts": "^2.0.0",
3846
"io-ts": "^2.0.0",
39-
"io-ts-types": "^0.5.7",
40-
"tslib": "^1.9.3"
47+
"io-ts-types": "^0.5.7"
4148
},
4249
"repository": {
4350
"type": "git",

scripts/FileSystem.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as TE from 'fp-ts/lib/TaskEither';
2+
import { flow } from 'fp-ts/lib/function';
3+
import * as fs from 'fs';
4+
import G from 'glob';
5+
6+
export interface FileSystem {
7+
readonly readFile: (path: string) => TE.TaskEither<Error, string>;
8+
readonly writeFile: (path: string, content: string) => TE.TaskEither<Error, void>;
9+
readonly copyFile: (from: string, to: string) => TE.TaskEither<Error, void>;
10+
readonly glob: (pattern: string) => TE.TaskEither<Error, ReadonlyArray<string>>;
11+
readonly mkdir: (path: string) => TE.TaskEither<Error, void>;
12+
}
13+
14+
const readFile = TE.taskify<fs.PathLike, string, NodeJS.ErrnoException, string>(fs.readFile);
15+
const writeFile = TE.taskify<fs.PathLike, string, NodeJS.ErrnoException, void>(fs.writeFile);
16+
const copyFile = TE.taskify<fs.PathLike, fs.PathLike, NodeJS.ErrnoException, void>(fs.copyFile);
17+
const glob = TE.taskify<string, Error, ReadonlyArray<string>>(G);
18+
const mkdirTE = TE.taskify(fs.mkdir);
19+
20+
export const fileSystem: FileSystem = {
21+
readFile: path => readFile(path, 'utf8'),
22+
writeFile,
23+
copyFile,
24+
glob,
25+
mkdir: flow(
26+
mkdirTE,
27+
TE.map(() => undefined),
28+
),
29+
};

scripts/build.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import * as path from 'path';
2+
import * as E from 'fp-ts/lib/Either';
3+
import { pipe } from 'fp-ts/lib/pipeable';
4+
import * as RTE from 'fp-ts/lib/ReaderTaskEither';
5+
import * as A from 'fp-ts/lib/ReadonlyArray';
6+
import * as TE from 'fp-ts/lib/TaskEither';
7+
import { FileSystem, fileSystem } from './FileSystem';
8+
import { run } from './run';
9+
10+
interface Build<A> extends RTE.ReaderTaskEither<FileSystem, Error, A> {}
11+
12+
const OUTPUT_FOLDER = 'dist';
13+
const PKG = 'package.json';
14+
15+
export const copyPackageJson: Build<void> = C =>
16+
pipe(
17+
C.readFile(PKG),
18+
TE.chain(s => TE.fromEither(E.parseJSON(s, E.toError))),
19+
TE.map(v => {
20+
const clone = Object.assign({}, v as any);
21+
22+
delete clone.scripts;
23+
delete clone.files;
24+
delete clone.devDependencies;
25+
26+
return clone;
27+
}),
28+
TE.chain(json => C.writeFile(path.join(OUTPUT_FOLDER, PKG), JSON.stringify(json, null, 2))),
29+
);
30+
31+
export const FILES: ReadonlyArray<string> = ['CHANGELOG.md', 'LICENSE', 'README.md'];
32+
33+
export const copyFiles: Build<ReadonlyArray<void>> = C =>
34+
A.readonlyArray.traverse(TE.taskEither)(FILES, from => C.copyFile(from, path.resolve(OUTPUT_FOLDER, from)));
35+
36+
const traverse = A.readonlyArray.traverse(TE.taskEither);
37+
38+
export const makeModules: Build<void> = C =>
39+
pipe(
40+
C.glob(`${OUTPUT_FOLDER}/lib/*.js`),
41+
TE.map(getModules),
42+
TE.chain(modules => traverse(modules, makeSingleModule(C))),
43+
TE.map(() => undefined),
44+
);
45+
46+
function getModules(paths: ReadonlyArray<string>): ReadonlyArray<string> {
47+
return paths.map(filePath => path.basename(filePath, '.js')).filter(x => x !== 'index');
48+
}
49+
50+
function makeSingleModule(C: FileSystem): (module: string) => TE.TaskEither<Error, void> {
51+
return m =>
52+
pipe(
53+
C.mkdir(path.join(OUTPUT_FOLDER, m)),
54+
TE.chain(() => makePkgJson(m)),
55+
TE.chain(data => C.writeFile(path.join(OUTPUT_FOLDER, m, 'package.json'), data)),
56+
);
57+
}
58+
59+
function makePkgJson(module: string): TE.TaskEither<Error, string> {
60+
return pipe(
61+
JSON.stringify(
62+
{
63+
main: `../lib/${module}.js`,
64+
module: `../es6/${module}.js`,
65+
typings: `../lib/${module}.d.ts`,
66+
sideEffects: false,
67+
},
68+
null,
69+
2,
70+
),
71+
TE.right,
72+
);
73+
}
74+
75+
const main: Build<void> = pipe(
76+
copyPackageJson,
77+
RTE.chain(() => copyFiles),
78+
RTE.chain(() => makeModules),
79+
);
80+
81+
run(
82+
main({
83+
...fileSystem,
84+
}),
85+
);

scripts/pre-publish.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { left } from 'fp-ts/lib/TaskEither';
2+
import { run } from './run';
3+
4+
const main = left(new Error('"npm publish" can not be run from root, run "npm run release" instead'));
5+
6+
run(main);

scripts/release.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { run } from './run';
2+
import * as child_process from 'child_process';
3+
import { left, right } from 'fp-ts/lib/Either';
4+
import * as TE from 'fp-ts/lib/TaskEither';
5+
6+
const DIST = 'dist';
7+
8+
const exec = (cmd: string, args?: child_process.ExecOptions): TE.TaskEither<Error, void> => () =>
9+
new Promise(resolve => {
10+
child_process.exec(cmd, args, err => {
11+
if (err !== null) {
12+
return resolve(left(err));
13+
}
14+
15+
return resolve(right(undefined));
16+
});
17+
});
18+
19+
export const main = exec('npm publish', {
20+
cwd: DIST,
21+
});
22+
23+
run(main);

scripts/run.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { fold } from 'fp-ts/lib/Either';
2+
import { TaskEither } from 'fp-ts/lib/TaskEither';
3+
4+
export function run<A>(eff: TaskEither<Error, A>): void {
5+
eff()
6+
.then(
7+
fold(
8+
e => {
9+
throw e;
10+
},
11+
_ => {
12+
process.exitCode = 0;
13+
},
14+
),
15+
)
16+
.catch(e => {
17+
console.error(e); // tslint:disable-line no-console
18+
19+
process.exitCode = 1;
20+
});
21+
}

tsconfig.es6.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "./tsconfig.build.json",
33
"compilerOptions": {
4-
"outDir": "./es6",
4+
"outDir": "dist/es6",
55
"target": "es6",
66
"module": "es6"
77
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
"moduleResolution": "node",
55
"target": "es5",
66
"lib": ["es6", "dom"],
7-
"outDir": "dist",
7+
"outDir": "dist/lib",
88
"declaration": true,
99
"strict": true,
1010
"importHelpers": true,
1111
"pretty": true,
1212
"noUnusedLocals": true,
13-
"noEmitOnError": true
13+
"noEmitOnError": true,
14+
"esModuleInterop": true
1415
},
1516
"include": [
1617
"./src/**/*"

0 commit comments

Comments
 (0)