Skip to content

Commit 1791426

Browse files
committed
feat(scripts): implement build script
- this handles dts header creation
1 parent c6bc7a2 commit 1791426

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"cleanup": "shx rm -rf dist",
3030
"prebuild": "npm run cleanup && npm run verify",
3131
"build": "tsc && tsc --target es2018 --outDir dist/esm2015 && rollup -c config/rollup.config.js && rollup -c config/rollup.config.js --environment NODE_ENV:production",
32-
"postbuild": "node scripts/copy && npm run size",
32+
"postbuild": "node scripts/copy && node scripts/build && npm run size",
3333
"docs": "typedoc -p . --theme minimal --target 'es6' --excludeNotExported --excludePrivate --ignoreCompilerErrors --exclude \"**/src/**/__tests__/*.*\" --out docs src/",
3434
"test": "jest -c ./config/jest.config.js",
3535
"test:watch": "npm t -- --watch",
@@ -46,7 +46,7 @@
4646
"lint:fix": "npm run lint -- --fix",
4747
"prerelease": "npm run build",
4848
"release": "standard-version",
49-
"postrelease": "node scripts/copy && npm run release:github && npm run release:npm",
49+
"postrelease": "node scripts/copy && node scripts/build && npm run release:github && npm run release:npm",
5050
"release:github": "git push --no-verify --follow-tags origin master",
5151
"release:npm": "npm publish dist",
5252
"release:preflight": "npm pack ./dist --dry-run",

scripts/build.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 DIST = resolve(ROOT, 'dist')
11+
const TYPES_ROOT_FILE = resolve(DIST, normalize(pkg.typings))
12+
13+
main()
14+
15+
function main() {
16+
writeDtsHeader()
17+
}
18+
19+
function writeDtsHeader() {
20+
const dtsHeader = getDtsHeader(
21+
pkg.name,
22+
pkg.version,
23+
pkg.author,
24+
pkg.repository.url,
25+
pkg.devDependencies.typescript
26+
)
27+
28+
prependFileSync(TYPES_ROOT_FILE, dtsHeader)
29+
}
30+
31+
/**
32+
*
33+
* @param {string} pkgName
34+
* @param {string} version
35+
* @param {string} author
36+
* @param {string} repoUrl
37+
* @param {string} tsVersion
38+
*/
39+
function getDtsHeader(pkgName, version, author, repoUrl, tsVersion) {
40+
const extractUserName = repoUrl.match(/\.com\/([\w-]+)\/\w+/i)
41+
const githubUserUrl = extractUserName ? extractUserName[1] : 'Unknown'
42+
43+
return `
44+
// Type definitions for ${pkgName} ${version}
45+
// Project: ${repoUrl}
46+
// Definitions by: ${author} <https://github.com/${githubUserUrl}>
47+
// Definitions: ${repoUrl}
48+
// TypeScript Version: ${tsVersion}
49+
`.replace(/^\s+/gm, '')
50+
}
51+
52+
/**
53+
*
54+
* @param {string} path
55+
* @param {string | Blob} data
56+
*/
57+
function prependFileSync(path, data) {
58+
const existingFileContent = readFileSync(path, {
59+
encoding: 'utf8',
60+
})
61+
const newFileContent = [data, existingFileContent].join('\n')
62+
writeFileSync(path, newFileContent, {
63+
flag: 'w+',
64+
encoding: 'utf8',
65+
})
66+
}

scripts/migrate/migrate-4-5.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ function updateScriptsDir() {
252252
const starterPathDir = resolve(ROOT, 'scripts')
253253
const libPackagePathDir = resolve(PACKAGE_ROOT, 'scripts')
254254

255-
const cpFiles = ['copy.js', 'file-size.js', 'tsconfig.json']
255+
const cpFiles = ['copy.js', 'file-size.js', 'build.js', 'tsconfig.json']
256256
/** @type {string[]} */
257257
const rmFiles = []
258258
const rmDirs = ['migrate']

src/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
/**
2-
* Type definitions for {package-name} 0.0.0
3-
* Project: https://github.com/{github-name}/{library-name}
4-
* Definitions by: {username} <https://github.com/{github-name}>
5-
* Definitions: https://github.com/{github-name}/{library-name}
6-
* TypeScript Version: 3.2
7-
*/
8-
91
export { Greeter } from './greeter'

0 commit comments

Comments
 (0)