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