Skip to content

Commit 7d9ee5b

Browse files
author
Giedrius Grabauskas
committed
Updated name.
1 parent d9200f8 commit 7d9ee5b

File tree

8 files changed

+202
-61
lines changed

8 files changed

+202
-61
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# dts-bundle-appends
2-
[![NPM version](http://img.shields.io/npm/v/dts-bundle-appends.svg)](https://www.npmjs.com/package/dts-bundle-appends) [![dependencies Status](https://david-dm.org/quatrocode/dts-bundle-appends/status.svg)](https://david-dm.org/quatrocode/dts-bundle-appends) [![devDependencies Status](https://david-dm.org/quatrocode/dts-bundle-appends/dev-status.svg)](https://david-dm.org/quatrocode/dts-bundle-appends?type=dev)
1+
# dts-bundle-append
2+
[![NPM version](http://img.shields.io/npm/v/dts-bundle-append.svg)](https://www.npmjs.com/package/dts-bundle-append) [![dependencies Status](https://david-dm.org/quatrocode/dts-bundle-append/status.svg)](https://david-dm.org/quatrocode/dts-bundle-append) [![devDependencies Status](https://david-dm.org/quatrocode/dts-bundle-append/dev-status.svg)](https://david-dm.org/quatrocode/dts-bundle-append?type=dev)
33

4-
Appends custom `d.ts` files into one `d.ts` file bundled by [dts-bundle](https://github.com/TypeStrong/dts-bundle).
4+
Append custom `d.ts` files into one `d.ts` file bundled by [dts-bundle](https://github.com/TypeStrong/dts-bundle).
55

66
## Usage
77
1) Install from npm:
88
```cmd
9-
npm install dts-bundle-appends
9+
npm install dts-bundle-append
1010
```
1111
2) Run `dts-bundle`.
1212

13-
3) After `d.ts` file bundled, run `dts-bundle-appends`.
13+
3) After `d.ts` file bundled, run `dts-bundle-append`.
1414

1515
## Requirements
1616
[NodeJS](https://nodejs.org/): >= 6.0.0
@@ -21,9 +21,9 @@ Default configuration are taken from a `dts-bundle.json` file.
2121
Also, you can specify a custom config file (see [#command-line](#command-line)).
2222

2323
### Configuration options
24-
| Argument | Type | Description |
25-
|--------------|--------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
26-
| appends | `string | Array<string>` | Specifies a list of glob patterns that match `d.ts` files to be appended in bundled `d.ts` file. Read more about [globs](https://github.com/isaacs/node-glob#glob-primer). |
24+
| Argument | Type | Description |
25+
|--------------|--------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
26+
| append | `string | Array<string>` | Specifies a list of glob patterns that match `d.ts` files to be appended in bundled `d.ts` file. Read more about [globs](https://github.com/isaacs/node-glob#glob-primer). |
2727

2828
*All other configuration options available from `dts-bundle` (read more [TypeStrong/dts-bundle#options](https://github.com/TypeStrong/dts-bundle#options)).
2929

@@ -35,20 +35,20 @@ Also, you can specify a custom config file (see [#command-line](#command-line)).
3535
"main": "dist/main.d.ts",
3636
"out": "module-name.d.ts",
3737
"baseDir": "dist",
38-
"appends": [
38+
"append": [
3939
"src/global.d.ts"
4040
]
4141
}
4242
```
4343

4444
## Command line
4545
```cmd
46-
Usage: dts-bundle-appends [options]
46+
Usage: dts-bundle-append [options]
4747
4848
Options:
4949
--help Show help [boolean]
5050
--version Show version number [boolean]
51-
--appends Appends files to global [string]
51+
--append Append files to global [string]
5252
--out dts-bundle bundled out file [string]
5353
--configJson dts-bundle configuration file [string] [default: "dts-bundle.json"]
5454
--baseDir dts-bundle base directory [string]

dist/append.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
return new (P || (P = Promise))(function (resolve, reject) {
4+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5+
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7+
step((generator = generator.apply(thisArg, _arguments)).next());
8+
});
9+
};
10+
const glob = require('glob');
11+
const fs = require('fs');
12+
const path = require('path');
13+
const EOL = "\r\n";
14+
class Append {
15+
constructor(append, out) {
16+
this.main(append, out);
17+
}
18+
main(append, out) {
19+
return __awaiter(this, void 0, void 0, function* () {
20+
let globPattern = this.generateAppendGlobPattern(append);
21+
let filesList = yield this.getFilesListByGlobPattern(globPattern);
22+
filesList = this.filterOnlyDTsFiles(filesList);
23+
if (filesList.length === 0) {
24+
console.warn("[ERROR] Empty files list.");
25+
process.exit(1);
26+
}
27+
let outFileStream = yield this.getDtsBundleFileStream(out);
28+
let statistics = yield this.appendFiles(filesList, outFileStream);
29+
outFileStream.close();
30+
console.log("Successfully appended", statistics.Success, this.fileWordEnding(statistics.Success) + ".");
31+
if (statistics.Failed > 0) {
32+
console.log("Failed to append", statistics.Failed, this.fileWordEnding(statistics.Success) + ".");
33+
}
34+
});
35+
}
36+
fileWordEnding(count) {
37+
if (count === 1) {
38+
return "file";
39+
}
40+
else {
41+
return "files";
42+
}
43+
}
44+
appendFiles(files, outStream) {
45+
return __awaiter(this, void 0, void 0, function* () {
46+
return new Promise(resolve => {
47+
let counter = 0, success = 0;
48+
files.forEach((file) => __awaiter(this, void 0, void 0, function* () {
49+
let data = yield this.readAppendFile(file);
50+
counter++;
51+
if (data) {
52+
let fileHeader = EOL.repeat(2);
53+
fileHeader += "// " + file;
54+
fileHeader += EOL.repeat(2);
55+
if (outStream.write(fileHeader + data)) {
56+
success++;
57+
}
58+
}
59+
if (counter === files.length) {
60+
resolve({ Total: counter, Success: success, Failed: counter - success });
61+
}
62+
}));
63+
});
64+
});
65+
}
66+
readAppendFile(file) {
67+
return __awaiter(this, void 0, void 0, function* () {
68+
return new Promise(resolve => {
69+
fs.readFile(file, "utf8", (err, data) => {
70+
if (err) {
71+
console.log(err.message);
72+
resolve(undefined);
73+
}
74+
else {
75+
resolve(data);
76+
}
77+
});
78+
});
79+
});
80+
}
81+
filterOnlyDTsFiles(files) {
82+
return files.filter(file => {
83+
let parse = path.parse(file);
84+
if (parse.ext === ".ts") {
85+
let extName = path.extname(parse.name);
86+
if (extName === ".d") {
87+
return true;
88+
}
89+
}
90+
console.warn(`[WARNING] Skipping file '${file}'`);
91+
return false;
92+
});
93+
}
94+
getDtsBundleFileStream(out) {
95+
return __awaiter(this, void 0, void 0, function* () {
96+
return new Promise(resolve => {
97+
fs.access(out, fs.W_OK, (err) => {
98+
if (err) {
99+
console.log(err.message);
100+
}
101+
else {
102+
resolve(fs.createWriteStream(out, { flags: "a", encoding: "utf8" }));
103+
}
104+
});
105+
});
106+
});
107+
}
108+
generateAppendGlobPattern(append) {
109+
if (typeof append === "string") {
110+
return append;
111+
}
112+
else if (append.length === 1) {
113+
return append[0];
114+
}
115+
else {
116+
return `+(${append.join("|")})`;
117+
}
118+
}
119+
getFilesListByGlobPattern(globPattern) {
120+
return __awaiter(this, void 0, void 0, function* () {
121+
return new Promise(resolve => {
122+
glob(globPattern, (err, matches) => {
123+
if (err) {
124+
console.error(err.message);
125+
}
126+
else {
127+
if (matches.length > 0) {
128+
resolve(matches);
129+
}
130+
else {
131+
console.warn(`[WARNING] Didn't find any file by glob pattern '${globPattern}'`);
132+
process.exit(1);
133+
}
134+
}
135+
});
136+
});
137+
});
138+
}
139+
}
140+
Object.defineProperty(exports, "__esModule", { value: true });
141+
exports.default = Append;

dist/arguments.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ exports.default = yargs
66
.version(() => {
77
return `Current version: ${require('../package.json').version}`;
88
})
9-
.option('appends', {
10-
describe: 'Appends files to global',
9+
.option('append', {
10+
describe: 'Append files to global',
1111
type: "string"
1212
})
1313
.option("out", {
@@ -23,5 +23,5 @@ exports.default = yargs
2323
describe: "dts-bundle base directory",
2424
type: "string"
2525
})
26-
.usage('Usage: dts-bundle-appends [options]')
26+
.usage('Usage: dts-bundle-append [options]')
2727
.argv;

dist/cli.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
99
});
1010
};
1111
const arguments_1 = require('./arguments');
12-
const appends_1 = require('./appends');
12+
const append_1 = require('./append');
1313
const fs = require('fs');
1414
const path = require('path');
1515
class Cli {
@@ -20,23 +20,23 @@ class Cli {
2020
return __awaiter(this, void 0, void 0, function* () {
2121
let argvCheck = yield this.checkArguments(argv);
2222
if (argvCheck.valid) {
23-
let appends, out, baseDir;
24-
if (argv.appends != null && argv.out != null && argv.out.length > 0 && argv.baseDir != null && argv.baseDir.length > 0) {
23+
let append, out, baseDir;
24+
if (argv.append != null && argv.out != null && argv.out.length > 0 && argv.baseDir != null && argv.baseDir.length > 0) {
2525
out = argv.out;
26-
appends = argv.appends;
26+
append = argv.append;
2727
baseDir = argv.baseDir || undefined;
2828
}
2929
else {
3030
let dtsConfig = yield this.readDtsBundleConfig(argv.configJson);
3131
out = argv.out || dtsConfig.out || this.addDTsExtension(dtsConfig.name);
32-
appends = argv.appends || dtsConfig.appends;
32+
append = argv.append || dtsConfig.append;
3333
baseDir = argv.baseDir || dtsConfig.baseDir || undefined;
3434
}
35-
if (this.checkGeneratedArguments(appends, out)) {
35+
if (this.checkGeneratedArguments(append, out)) {
3636
if (baseDir !== undefined) {
3737
out = path.join(baseDir, out);
3838
}
39-
new appends_1.default(appends, out);
39+
new append_1.default(append, out);
4040
}
4141
}
4242
else {
@@ -50,9 +50,9 @@ class Cli {
5050
}
5151
return undefined;
5252
}
53-
checkGeneratedArguments(appends, out) {
54-
if (appends == null) {
55-
this.throwError("[ERROR] Appends files list not specified");
53+
checkGeneratedArguments(append, out) {
54+
if (append == null) {
55+
this.throwError("[ERROR] Append files list not specified");
5656
}
5757
if (out == null) {
5858
this.throwError("[ERROR] Out file not specified");
@@ -62,7 +62,7 @@ class Cli {
6262
checkArguments(argv) {
6363
return __awaiter(this, void 0, void 0, function* () {
6464
return new Promise(resolve => {
65-
if (argv.appends != null && argv.appends.length <= 0) {
65+
if (argv.append != null && argv.append.length <= 0) {
6666
resolve({ valid: false, errorMessage: `Invalid argument 'append'` });
6767
return;
6868
}

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "dts-bundle-appends",
2+
"name": "dts-bundle-append",
33
"version": "0.0.2",
4-
"description": "Appends custom d.ts files into d.ts file bundle by dts-bundle.",
4+
"description": "Append custom d.ts files into d.ts file bundle by dts-bundle.",
55
"keywrods": [
66
"dts-bundle",
77
"typescript",
@@ -11,24 +11,24 @@
1111
"bundle",
1212
"d.ts"
1313
],
14-
"main": "dist/appends.js",
14+
"main": "dist/append.js",
1515
"scripts": {
1616
"build": "tsc -p .",
1717
"watch": "tsc -p . -w",
1818
"prepublish": "npm run build"
1919
},
2020
"author": "Giedrius Grabauskas <[email protected]> (https://github.com/GiedriusGrabauskas)",
21-
"bugs": "https://github.com/QuatroCode/dts-bundle-appends/issues",
22-
"repository": "QuatroCode/dts-bundle-appends",
23-
"homepage": "https://github.com/QuatroCode/dts-bundle-appends",
21+
"bugs": "https://github.com/QuatroCode/dts-bundle-append/issues",
22+
"repository": "QuatroCode/dts-bundle-append",
23+
"homepage": "https://github.com/QuatroCode/dts-bundle-append",
2424
"license": "GPL-3.0",
2525
"files": [
2626
"dist",
2727
"*.md",
2828
"LICENSE"
2929
],
3030
"bin": {
31-
"dts-bundle-appends": "./dist/cli.js"
31+
"dts-bundle-append": "./dist/cli.js"
3232
},
3333
"devDependencies": {
3434
"@types/yargs": "0.0.28",

src/appends.ts renamed to src/append.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import * as path from 'path';
44

55
const EOL = "\r\n";
66

7-
interface AppendsFilesResult {
7+
interface AppendFilesResult {
88
Total: number;
99
Success: number;
1010
Failed: number;
1111
}
1212

13-
export default class Appends {
13+
export default class Append {
1414

15-
constructor(appends: string | Array<string>, out: string) {
16-
this.main(appends, out);
15+
constructor(append: string | Array<string>, out: string) {
16+
this.main(append, out);
1717
}
1818

19-
private async main(appends: string | Array<string>, out: string) {
20-
let globPattern = this.generateAppendGlobPattern(appends);
19+
private async main(append: string | Array<string>, out: string) {
20+
let globPattern = this.generateAppendGlobPattern(append);
2121
let filesList = await this.getFilesListByGlobPattern(globPattern);
2222
filesList = this.filterOnlyDTsFiles(filesList);
2323
if (filesList.length === 0) {
@@ -42,7 +42,7 @@ export default class Appends {
4242
}
4343

4444
private async appendFiles(files: Array<string>, outStream: fs.WriteStream) {
45-
return new Promise<AppendsFilesResult>(resolve => {
45+
return new Promise<AppendFilesResult>(resolve => {
4646
let counter = 0,
4747
success = 0;
4848
files.forEach(async file => {
@@ -103,13 +103,13 @@ export default class Appends {
103103
});
104104
}
105105

106-
private generateAppendGlobPattern(appends: string | Array<string>) {
107-
if (typeof appends === "string") {
108-
return appends;
109-
} else if (appends.length === 1) {
110-
return appends[0];
106+
private generateAppendGlobPattern(append: string | Array<string>) {
107+
if (typeof append === "string") {
108+
return append;
109+
} else if (append.length === 1) {
110+
return append[0];
111111
} else {
112-
return `+(${appends.join("|")})`;
112+
return `+(${append.join("|")})`;
113113
}
114114
}
115115

0 commit comments

Comments
 (0)