-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.js
67 lines (57 loc) · 1.53 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* This file only purpose is to execute any build related tasks
*/
const { resolve, normalize } = require('path');
const { readFileSync, writeFileSync } = require('fs');
const ROOT = resolve('./');
const DIST = resolve(ROOT, 'dist');
const pkg = require(resolve(ROOT, 'package.json'));
const TYPES_ROOT_FILE = resolve(DIST, normalize(pkg.typings));
main();
function main() {
writeDtsHeader();
}
function writeDtsHeader() {
const dtsHeader = getDtsHeader(
pkg.name,
pkg.version,
pkg.author,
pkg.repository.url,
pkg.devDependencies.typescript
);
prependFileSync(TYPES_ROOT_FILE, dtsHeader);
}
/**
*
* @param {string} pkgName
* @param {string} version
* @param {string} author
* @param {string} repoUrl
* @param {string} tsVersion
*/
function getDtsHeader(pkgName, version, author, repoUrl, tsVersion) {
const extractUserName = repoUrl.match(/\.com\/([\w-]+)\/\w+/i);
const githubUserUrl = extractUserName ? extractUserName[1] : 'Unknown';
return `
// Type definitions for ${pkgName} ${version}
// Project: ${repoUrl}
// Definitions by: ${author} <https://github.com/${githubUserUrl}>
// Definitions: ${repoUrl}
// TypeScript Version: ${tsVersion}
`.replace(/^\s+/gm, '');
}
/**
*
* @param {string} path
* @param {string | Blob} data
*/
function prependFileSync(path, data) {
const existingFileContent = readFileSync(path, {
encoding: 'utf8',
});
const newFileContent = [data, existingFileContent].join('\n');
writeFileSync(path, newFileContent, {
flag: 'w+',
encoding: 'utf8',
});
}