Skip to content

Commit ca99f1b

Browse files
author
Giedrius Grabauskas
committed
Init commit.
1 parent 1d40ef5 commit ca99f1b

File tree

9 files changed

+637
-0
lines changed

9 files changed

+637
-0
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# =========================
2+
# Windows detritus
3+
# =========================
4+
5+
# Windows image file caches
6+
Thumbs.db
7+
ehthumbs.db
8+
9+
# Folder config file
10+
Desktop.ini
11+
12+
# Recycle Bin used on file shares
13+
$RECYCLE.BIN/
14+
15+
# Mac desktop service store files
16+
.DS_Store
17+
18+
19+
# NuGet Packages Directory
20+
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
21+
node_modules
22+
23+
*.tgz

dist/appends.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 Appends {
15+
constructor(appends, out) {
16+
this.main(appends, out);
17+
}
18+
main(appends, out) {
19+
return __awaiter(this, void 0, void 0, function* () {
20+
let globPattern = this.generateAppendGlobPattern(appends);
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(appends) {
109+
if (typeof appends === "string") {
110+
return appends;
111+
}
112+
else if (appends.length === 1) {
113+
return appends[0];
114+
}
115+
else {
116+
return `+(${appends.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 = Appends;

dist/arguments.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"use strict";
2+
const yargs = require('yargs');
3+
Object.defineProperty(exports, "__esModule", { value: true });
4+
exports.default = yargs
5+
.help("help", "Show help")
6+
.version(() => {
7+
return `Current version: ${require('../package.json').version}`;
8+
})
9+
.option('appends', {
10+
describe: 'Appends files to global.',
11+
type: "string"
12+
})
13+
.option("out", {
14+
describe: "dts-bundle bundled out file.",
15+
type: "string"
16+
})
17+
.option("configJson", {
18+
describe: "dts-bundle configuration file.",
19+
default: "dts-bundle.json",
20+
type: "string"
21+
})
22+
.option("baseDir", {
23+
describe: "dts-bundle base directory.",
24+
type: "string"
25+
})
26+
.usage('Usage: dts-bundle-appends [options]')
27+
.argv;

dist/cli.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env node
2+
"use strict";
3+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments)).next());
9+
});
10+
};
11+
const arguments_1 = require('./arguments');
12+
const appends_1 = require('./appends');
13+
const fs = require('fs');
14+
const path = require('path');
15+
class Cli {
16+
constructor(argv) {
17+
this.startCli(argv);
18+
}
19+
startCli(argv) {
20+
return __awaiter(this, void 0, void 0, function* () {
21+
let argvCheck = yield this.checkArguments(argv);
22+
if (argvCheck.valid) {
23+
let appends, out, baseDir;
24+
if (argv.appends != null && argv.out != null && argv.out.length > 0) {
25+
out = argv.out;
26+
appends = argv.appends;
27+
baseDir = argv.baseDir || undefined;
28+
}
29+
else {
30+
let dtsConfig = yield this.readDtsBundleConfig(argv.configJson);
31+
out = argv.out || dtsConfig.out || dtsConfig.name;
32+
appends = argv.appends || dtsConfig.appends;
33+
baseDir = argv.baseDir || dtsConfig.baseDir || undefined;
34+
}
35+
if (baseDir !== undefined) {
36+
out = path.join(baseDir, out);
37+
}
38+
if (this.checkGeneratedArguments(appends, out)) {
39+
new appends_1.default(appends, out);
40+
}
41+
}
42+
else {
43+
this.throwError("[ERROR] " + argvCheck.errorMessage);
44+
}
45+
});
46+
}
47+
checkGeneratedArguments(appends, out) {
48+
if (appends == null) {
49+
this.throwError("[ERROR] Appends files list not specified");
50+
}
51+
if (out == null) {
52+
this.throwError("[ERROR] Out file not specified");
53+
}
54+
return true;
55+
}
56+
checkArguments(argv) {
57+
return __awaiter(this, void 0, void 0, function* () {
58+
return new Promise(resolve => {
59+
if (argv.appends != null && argv.appends.length <= 0) {
60+
resolve({ valid: false, errorMessage: `Invalid argument 'append'` });
61+
return;
62+
}
63+
if (argv.out != null && argv.out.length <= 0) {
64+
resolve({ valid: false, errorMessage: `Invalid argument 'out'` });
65+
return;
66+
}
67+
if (argv.configJson != null && argv.configJson.length <= 0) {
68+
resolve({ valid: false, errorMessage: `Invalid argument 'configJson'` });
69+
return;
70+
}
71+
resolve({ valid: true });
72+
});
73+
});
74+
}
75+
readDtsBundleConfig(fileName) {
76+
return __awaiter(this, void 0, void 0, function* () {
77+
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () {
78+
var file;
79+
let r = yield fs.readFile(fileName, 'utf8', (fsErr, data) => {
80+
if (fsErr) {
81+
this.throwError(`[ERROR] Config file '${fileName}' was not found.`);
82+
}
83+
else {
84+
try {
85+
let jsonData = JSON.parse(data);
86+
resolve(jsonData);
87+
}
88+
catch (tryError) {
89+
this.throwError(`[ERROR] Config file '${fileName}' is not valid.`);
90+
}
91+
}
92+
});
93+
}));
94+
});
95+
}
96+
throwError(text) {
97+
console.error(text);
98+
process.exit(1);
99+
}
100+
}
101+
new Cli(arguments_1.default);

package.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "dts-bundle-appends",
3+
"version": "0.0.1",
4+
"description": "Appends cutom definitions into bundled d.ts file.",
5+
"keywrods": [
6+
"dts-bundle",
7+
"typescript",
8+
"types",
9+
"definitelyTyped",
10+
"append",
11+
"bundle",
12+
"d.ts"
13+
],
14+
"main": "dist/appends.js",
15+
"scripts": {
16+
"build": "tsc -p .",
17+
"watch": "tsc -p . -w",
18+
"prepublish": "npm run build"
19+
},
20+
"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",
24+
"license": "GPL-3.0",
25+
"files": [
26+
"dist",
27+
"*.md",
28+
"LICENSE"
29+
],
30+
"bin": {
31+
"dts-bundle-appends": "./dist/cli.js"
32+
},
33+
"devDependencies": {
34+
"@types/yargs": "0.0.28",
35+
"@types/glob": "^5.0.29",
36+
"@types/node": "^6.0.31",
37+
"typescript": "^2.0.0"
38+
},
39+
"dependencies": {
40+
"glob": "^7.0.5",
41+
"yargs": "^4.8.1"
42+
}
43+
}

0 commit comments

Comments
 (0)