Skip to content

Commit c2fc5af

Browse files
author
Martynas Žilinskas
committed
'Now it prints logs with colors. Added example folder.
1 parent 58c0e3d commit c2fc5af

File tree

11 files changed

+93
-5
lines changed

11 files changed

+93
-5
lines changed

example/dts-bundle.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "example-lib",
3+
"main": "./dist/index.d.ts",
4+
"out": "./dist/example-lib.d.ts",
5+
"removeSource": true,
6+
"proxyjs": {
7+
"default": "Hello",
8+
"generateDir": "./dist",
9+
"requireFile": "./dist/build.js"
10+
}
11+
}

example/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "example-lib",
3+
"version": "1.0.0",
4+
"main": "./dist/index.js",
5+
"devDependencies": {
6+
"rollup": "^0.34.7",
7+
"rollup-plugin-typescript": "^0.7.7"
8+
}
9+
}

example/rollup.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rollup.config.js
2+
import typescript from 'rollup-plugin-typescript';
3+
4+
export default {
5+
entry: './src/index.ts',
6+
dest: './dist/build.js',
7+
format: 'cjs',
8+
exports: 'named',
9+
plugins: [
10+
typescript()
11+
]
12+
}

example/src/hello.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default class Hello {
2+
public SayHello(name: string) {
3+
console.log(`Hello ${name}!`);
4+
}
5+
}

example/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Hello from './hello';
2+
import World from './world';
3+
4+
export default Hello;
5+
export {
6+
World
7+
}

example/src/world.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default class World {
2+
public Spin() {
3+
console.log('World is now spinning! Weeeee');
4+
}
5+
6+
public Stop() {
7+
console.log('World has stopped spinning!');
8+
}
9+
}

example/tsconfig.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"target": "es6",
5+
"noImplicitAny": false,
6+
"removeComments": false,
7+
"preserveConstEnums": true,
8+
"noLib": false,
9+
"sourceMap": false,
10+
"outDir": "dist",
11+
"rootDir": "src",
12+
"declaration": true,
13+
"noUnusedLocals": true,
14+
"noImplicitThis": true,
15+
"noImplicitUseStrict": true,
16+
"strictNullChecks": true
17+
},
18+
"exclude": [
19+
"node_modules"
20+
]
21+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010
"author": "Giedrius Grabauskas <[email protected]>, Martynas Zilinskas <[email protected]>",
1111
"license": "GPL-3.0",
1212
"devDependencies": {
13+
"@types/cli-color": "^0.3.28",
1314
"@types/mkdirp": "^0.3.28",
1415
"@types/node": "^6.0.31",
1516
"@types/yargs": "0.0.28",
1617
"typescript": "^2.0.0"
1718
},
1819
"dependencies": {
20+
"cli-color": "^1.1.0",
1921
"mkdirp": "^0.5.1",
2022
"yargs": "^4.8.1"
2123
},

src/cli.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'fs';
44
import * as path from 'path';
55
import Generator from './generator';
66
import Arguments from './arguments';
7+
import * as clc from 'cli-color';
78

89
const DEFAULT_CONFIG_NAME = 'dts-bundle.json';
910

@@ -22,9 +23,11 @@ class Cli {
2223
this.throwError(`[Error] Config file ${DEFAULT_CONFIG_NAME} is not valid.`);
2324
}) as Contracts.Config;
2425

26+
console.info(clc.cyanBright('Using config:'), fullPath);
27+
2528
try {
2629
let generator = new Generator(this.getConfig(config, argv));
27-
generator.Generate();
30+
await generator.Generate();
2831
} catch (e) {
2932
this.throwError(`[Failed] ${e}`);
3033
}
@@ -38,11 +41,12 @@ class Cli {
3841
if (args.default != null) config.proxyjs.default = args.default;
3942
if (args.generatedir != null) config.proxyjs.generateDir = args.generatedir;
4043
if (args.requirefile != null) config.proxyjs.requireFile = args.requirefile;
44+
if (config.baseDir == null) config.baseDir = '';
4145
return config;
4246
}
4347

4448
private throwError(text: string) {
45-
console.error(text);
49+
console.error(clc.redBright(text));
4650
process.exit(1);
4751
}
4852

src/generator.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from 'path';
33
import * as Contracts from './contracts';
44
import * as Helpers from './helpers';
55
import * as mkdirp from 'mkdirp';
6+
import * as clc from 'cli-color';
67

78
const HEADER_COMMENT = '/* Generated by main-file-generator */';
89

@@ -11,7 +12,10 @@ export default class Generator {
1112

1213
public async Generate() {
1314
let fullPath = path.join(process.cwd(), this.config.baseDir, this.config.out);
14-
this.readDtsFile(fullPath);
15+
console.log(clc.cyanBright('Bundled dts file:'), fullPath);
16+
this.readDtsFile(fullPath).catch((err) => {
17+
console.error(err);
18+
});
1519
}
1620

1721
private async readDtsFile(fullPath: string) {
@@ -30,16 +34,18 @@ export default class Generator {
3034
}
3135

3236
private writeFiles(matches: Array<string>) {
37+
console.info(clc.yellow('Generating files:'));
3338
matches.forEach((match: string) => {
3439
let cleanedMatch = this.cleanModule(match);
35-
let fullFilePath = path.join(process.cwd(), this.config.proxyjs.generateDir, `${match}.js`);
40+
let fullFilePath = path.join(process.cwd(), this.config.proxyjs.generateDir, `${match}.js`);
3641
// Make sure that all folders are created.
3742
mkdirp.sync(path.dirname(fullFilePath));
3843

3944
// Get `require` relative path.
4045
let requireFileFullPath = path.join(process.cwd(), this.config.proxyjs.requireFile);
4146
let requireFileRelativePath = path.relative(fullFilePath, requireFileFullPath);
4247

48+
console.log(fullFilePath);
4349
//Write to file
4450
let stream = fs.createWriteStream(fullFilePath, { 'flags': 'w' });
4551
let moduleName = Helpers.kebabCaseToPascalCase(cleanedMatch);
@@ -50,6 +56,7 @@ export default class Generator {
5056
].join('\r\n'));
5157
stream.end();
5258
});
59+
console.info(clc.green('Done generating files.'));
5360
}
5461

5562
private generateLine(requirePath: string, moduleName: string) {

0 commit comments

Comments
 (0)