@temir.ra/create-template 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/buildinfo.txt +1 -0
- package/dist/cli.bundle.js +4 -0
- package/dist/cli.bundle.js.map +11 -0
- package/package.json +45 -0
- package/template/CHANGELOG.md +5 -0
- package/template/README.md +98 -0
- package/template/buildinfo.txt +1 -0
- package/template/gitignore +9 -0
- package/template/package.json +43 -0
- package/template/scripts/build-bundle.ts +161 -0
- package/template/scripts/buildinfo.ts +25 -0
- package/template/scripts/cdn-rewrite-map.json +1 -0
- package/template/scripts/dev.ts +13 -0
- package/template/src/cli.ts +41 -0
- package/template/src/constants.ts +12 -0
- package/template/tests/buildinfo.test.ts +23 -0
- package/template/tsconfig.json +38 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Temir Ra
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Introduction
|
|
2
|
+
|
|
3
|
+
*<INTRO TEXT>*
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
1. [Quick Start](#quick-start)
|
|
8
|
+
2. [Documentation](#documentation)
|
|
9
|
+
3. [DevOps](#devops)
|
|
10
|
+
1. [Change Management](#change-management)
|
|
11
|
+
2. [Publish](#publish)
|
|
12
|
+
|
|
13
|
+
# Quick Start
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
# placeholder:
|
|
17
|
+
# <TEMPLATE_PACKAGE: @temir.ra/create-template
|
|
18
|
+
# <TEMPLATE_NAME: @temir.ra/template
|
|
19
|
+
# <NEW_PACKAGE: <NEW_PACKAGE>
|
|
20
|
+
# is used as:
|
|
21
|
+
# - the path where the package is created
|
|
22
|
+
# - the "name" field in the generated package.json
|
|
23
|
+
# <@_VERSION: <@_VERSION>
|
|
24
|
+
|
|
25
|
+
# pinned version
|
|
26
|
+
bun info "@temir.ra/create-template" version
|
|
27
|
+
bun create --no-install --no-git "@temir.ra/template<@_VERSION>" <NEW_PACKAGE>
|
|
28
|
+
|
|
29
|
+
# latest
|
|
30
|
+
# clear the cache to pick up the latest version
|
|
31
|
+
bun pm cache rm
|
|
32
|
+
bun create --no-install --no-git "@temir.ra/template" <NEW_PACKAGE>
|
|
33
|
+
|
|
34
|
+
# templates only copy files, run install and any setup scripts manually
|
|
35
|
+
cd <NEW_PACKAGE>
|
|
36
|
+
bun install
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
# Documentation
|
|
40
|
+
|
|
41
|
+
*<DOCUMENTATION>*
|
|
42
|
+
|
|
43
|
+
# DevOps
|
|
44
|
+
|
|
45
|
+
## Change Management
|
|
46
|
+
|
|
47
|
+
1. Create a new branch for the change.
|
|
48
|
+
2. Make the changes and commit.
|
|
49
|
+
3. Bump the version in [`package.json`](package.json).
|
|
50
|
+
4. Add an entry for the new version in [`CHANGELOG.md`](CHANGELOG.md).
|
|
51
|
+
5. Pull request the branch.
|
|
52
|
+
6. Ensure package artifacts are current.
|
|
53
|
+
7. Publish.
|
|
54
|
+
|
|
55
|
+
## Publish
|
|
56
|
+
|
|
57
|
+
Publish to the public npm registry.
|
|
58
|
+
|
|
59
|
+
```powershell
|
|
60
|
+
# authenticate
|
|
61
|
+
npm login
|
|
62
|
+
# publish
|
|
63
|
+
bun publish --registry https://registry.npmjs.org/ --access public
|
|
64
|
+
```
|
package/buildinfo.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.1.0+4727289
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{cpSync as s,readFileSync as f,renameSync as u,writeFileSync as P}from"fs";import{resolve as t}from"path";import{resolve as o}from"path";import{fileURLToPath as h}from"url";var d=new URL("../",import.meta.url),n=o(h(d)),m=o(n,"template/"),p=o(n,"CHANGELOG.md"),g=o(n,"buildinfo.txt"),l=o(n,"README.md");try{let r=process.argv[2];if(!r)throw Error('First argument must be the package name (e.g. "my-package" or "@my-scope/my-package").');let a=r.replace(/\\/g,"/"),e=t(process.cwd(),a);s(m,e,{recursive:!0}),s(p,t(e,"CHANGELOG-template.md")),s(g,t(e,"buildinfo-template.txt")),s(l,t(e,"README-template.md"));let c=t(e,"package.json"),i=JSON.parse(f(c,"utf-8"));i.name=a,P(c,JSON.stringify(i,null,2)),u(t(e,"gitignore"),t(e,".gitignore")),console.log(`Template has been successfully instantiated at '${e}' with package name '${a}'.`)}catch(r){let a=r instanceof Error?r:Error(String(r));console.error("Error:",a.message),process.exit(1)}
|
|
3
|
+
|
|
4
|
+
//# debugId=C4A0E6EEF1219B4364756E2164756E21
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["..\\src\\cli.ts", "..\\src\\constants.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"#!/usr/bin/env node\r\n\r\nimport { cpSync, readFileSync, renameSync, writeFileSync } from 'fs';\r\nimport { resolve } from 'path';\r\nimport {\r\n templatePath,\r\n changelogPath,\r\n buildinfoPath,\r\n readmePath\r\n} from './constants.js';\r\n\r\n\r\ntry {\r\n\r\n const packageNameArgument = process.argv[2];\r\n if (!packageNameArgument)\r\n throw new Error('First argument must be the package name (e.g. \"my-package\" or \"@my-scope/my-package\").');\r\n const packageName = packageNameArgument.replace(/\\\\/g, '/');\r\n\r\n const destinationPath = resolve(process.cwd(), packageName);\r\n\r\n cpSync(templatePath, destinationPath, { recursive: true });\r\n cpSync(changelogPath, resolve(destinationPath, 'CHANGELOG-template.md'));\r\n cpSync(buildinfoPath, resolve(destinationPath, 'buildinfo-template.txt'));\r\n cpSync(readmePath, resolve(destinationPath, 'README-template.md'));\r\n\r\n const packageJsonPath = resolve(destinationPath, 'package.json');\r\n const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));\r\n packageJson.name = packageName;\r\n writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));\r\n\r\n renameSync(resolve(destinationPath, 'gitignore'), resolve(destinationPath, '.gitignore'));\r\n\r\n console.log(`Template has been successfully instantiated at '${destinationPath}' with package name '${packageName}'.`);\r\n\r\n}\r\ncatch (error) {\r\n const err = error instanceof Error ? error : new Error(String(error));\r\n console.error('Error:', err.message);\r\n process.exit(1);\r\n}\r\n",
|
|
6
|
+
"import { resolve } from 'path';\r\nimport { fileURLToPath } from 'url';\r\n\r\n\r\nexport const packageUrl: URL = new URL('../', import.meta.url);\r\nexport const packagePath: string = resolve(fileURLToPath(packageUrl));\r\n\r\nexport const templatePath: string = resolve(packagePath, 'template/');\r\n\r\nexport const changelogPath: string = resolve(packagePath, 'CHANGELOG.md');\r\nexport const buildinfoPath: string = resolve(packagePath, 'buildinfo.txt');\r\nexport const readmePath: string = resolve(packagePath, 'README.md');\r\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";AAEA,iBAAS,kBAAQ,gBAAc,mBAAY,WAC3C,kBAAS,aCHT,kBAAS,aACT,wBAAS,YAGF,IAAM,EAAkB,IAAI,IAAI,MAAO,YAAY,GAAG,EAChD,EAAsB,EAAQ,EAAc,CAAU,CAAC,EAEvD,EAAuB,EAAQ,EAAa,WAAW,EAEvD,EAAwB,EAAQ,EAAa,cAAc,EAC3D,EAAwB,EAAQ,EAAa,eAAe,EAC5D,EAAqB,EAAQ,EAAa,WAAW,EDClE,GAAI,CAEA,IAAM,EAAsB,QAAQ,KAAK,GACzC,GAAI,CAAC,EACD,MAAU,MAAM,wFAAwF,EAC5G,IAAM,EAAc,EAAoB,QAAQ,MAAO,GAAG,EAEpD,EAAkB,EAAQ,QAAQ,IAAI,EAAG,CAAW,EAE1D,EAAO,EAAc,EAAiB,CAAE,UAAW,EAAK,CAAC,EACzD,EAAO,EAAe,EAAQ,EAAiB,uBAAuB,CAAC,EACvE,EAAO,EAAe,EAAQ,EAAiB,wBAAwB,CAAC,EACxE,EAAO,EAAY,EAAQ,EAAiB,oBAAoB,CAAC,EAEjE,IAAM,EAAkB,EAAQ,EAAiB,cAAc,EACzD,EAAc,KAAK,MAAM,EAAa,EAAiB,OAAO,CAAC,EACrE,EAAY,KAAO,EACnB,EAAc,EAAiB,KAAK,UAAU,EAAa,KAAM,CAAC,CAAC,EAEnE,EAAW,EAAQ,EAAiB,WAAW,EAAG,EAAQ,EAAiB,YAAY,CAAC,EAExF,QAAQ,IAAI,mDAAmD,yBAAuC,KAAe,EAGzH,MAAO,EAAO,CACV,IAAM,EAAM,aAAiB,MAAQ,EAAY,MAAM,OAAO,CAAK,CAAC,EACpE,QAAQ,MAAM,SAAU,EAAI,OAAO,EACnC,QAAQ,KAAK,CAAC",
|
|
9
|
+
"debugId": "C4A0E6EEF1219B4364756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@temir.ra/create-template",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Template package to create new template packages",
|
|
5
|
+
"author": "temir.ra",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"typescript",
|
|
9
|
+
"template",
|
|
10
|
+
"bun"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://git.chimps.quest/trs/create-template.git"
|
|
15
|
+
},
|
|
16
|
+
"private": false,
|
|
17
|
+
"type": "module",
|
|
18
|
+
"imports": {
|
|
19
|
+
"#src/*.js": "./src/*.ts"
|
|
20
|
+
},
|
|
21
|
+
"bin": "./dist/cli.bundle.js",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"CHANGELOG.md",
|
|
25
|
+
"buildinfo.txt",
|
|
26
|
+
"template"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"clean:dist": "rm -rf dist/",
|
|
30
|
+
"clean:tsbuildinfo": "rm -f *.tsbuildinfo || true",
|
|
31
|
+
"clean": "bun run clean:dist && bun run clean:tsbuildinfo",
|
|
32
|
+
"buildinfo": "bun run scripts/buildinfo.ts",
|
|
33
|
+
"tests": "bun test",
|
|
34
|
+
"typecheck": "tsc --noEmit",
|
|
35
|
+
"reinstall": "rm -rf node_modules && rm -f bun.lock && bun pm cache rm && bun install && bunx tsc --version",
|
|
36
|
+
"dev": "bun run buildinfo && bun run --watch scripts/dev.ts",
|
|
37
|
+
"prebuild": "bun run buildinfo",
|
|
38
|
+
"build": "bun run build:cli-bundle",
|
|
39
|
+
"build:cli-bundle": "bun build src/cli.ts --entry-naming \"[dir]/[name].bundle.[ext]\" --outdir dist --target node --format esm --minify --sourcemap=external"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/bun": "latest",
|
|
43
|
+
"typescript": "^6.0.2"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# Introduction
|
|
2
|
+
|
|
3
|
+
*<INTRO TEXT>*
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
1. [Quick Start](#quick-start)
|
|
8
|
+
2. [Documentation](#documentation)
|
|
9
|
+
3. [DevOps](#devops)
|
|
10
|
+
1. [Change Management](#change-management)
|
|
11
|
+
2. [Publish](#publish)
|
|
12
|
+
1. [npmjs.org](#npmjsorg)
|
|
13
|
+
2. [Custom registry](#custom-registry)
|
|
14
|
+
|
|
15
|
+
# Quick Start
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# placeholder:
|
|
19
|
+
# <TEMPLATE_PACKAGE: <TEMPLATE_PACKAGE>
|
|
20
|
+
# <TEMPLATE_NAME: <TEMPLATE_NAME>
|
|
21
|
+
# <NEW_PACKAGE: <NEW_PACKAGE>
|
|
22
|
+
# is used as:
|
|
23
|
+
# - the path where the package is created
|
|
24
|
+
# - the "name" field in the generated package.json
|
|
25
|
+
# <@_VERSION: <@_VERSION>
|
|
26
|
+
|
|
27
|
+
# pinned version
|
|
28
|
+
bun info "<TEMPLATE_PACKAGE>" version
|
|
29
|
+
bun create --no-install --no-git "<TEMPLATE_NAME><@_VERSION>" <NEW_PACKAGE>
|
|
30
|
+
|
|
31
|
+
# latest
|
|
32
|
+
# clear the cache to pick up the latest version
|
|
33
|
+
bun pm cache rm
|
|
34
|
+
bun create --no-install --no-git "<TEMPLATE_NAME>" <NEW_PACKAGE>
|
|
35
|
+
|
|
36
|
+
# templates only copy files, run install and any setup scripts manually
|
|
37
|
+
cd <NEW_PACKAGE>
|
|
38
|
+
bun install
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
# Documentation
|
|
42
|
+
|
|
43
|
+
*<DOCUMENTATION>*
|
|
44
|
+
|
|
45
|
+
# DevOps
|
|
46
|
+
|
|
47
|
+
## Change Management
|
|
48
|
+
|
|
49
|
+
1. Create a new branch for the change.
|
|
50
|
+
2. Make the changes and commit.
|
|
51
|
+
3. Bump the version in [`package.json`](package.json).
|
|
52
|
+
4. Add an entry for the new version in [`CHANGELOG.md`](CHANGELOG.md).
|
|
53
|
+
5. Pull request the branch.
|
|
54
|
+
6. Ensure package artifacts are current.
|
|
55
|
+
7. Publish.
|
|
56
|
+
|
|
57
|
+
## Publish
|
|
58
|
+
|
|
59
|
+
See the following sources to configure the target registry and authentication.
|
|
60
|
+
|
|
61
|
+
- [Configuring npm - `npmrc`](https://docs.npmjs.com/cli/v10/configuring-npm/npmrc)
|
|
62
|
+
- [Bun package manager - `install.registry`](https://bun.com/docs/runtime/bunfig#install-scopes)
|
|
63
|
+
|
|
64
|
+
⚠️ Package Scope and the authentication for the target registry must be aligned.
|
|
65
|
+
|
|
66
|
+
### `npmjs.org`
|
|
67
|
+
|
|
68
|
+
Publish to the public npm registry.
|
|
69
|
+
|
|
70
|
+
```powershell
|
|
71
|
+
# authenticate
|
|
72
|
+
npm login
|
|
73
|
+
# publish
|
|
74
|
+
bun publish --registry https://registry.npmjs.org/ --access public
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Custom registry
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
# placeholder:
|
|
81
|
+
# <SCOPE_WITHOUT_AT: <SCOPE_WITHOUT_AT>
|
|
82
|
+
# <REGISTRY_URL: <REGISTRY_URL>
|
|
83
|
+
# <BUN_PUBLISH_AUTH_TOKEN: <BUN_PUBLISH_AUTH_TOKEN>
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`~/.bunfig.toml` or `bunfig.toml`:
|
|
87
|
+
|
|
88
|
+
```toml
|
|
89
|
+
[install.scopes]
|
|
90
|
+
"<SCOPE_WITHOUT_AT>" = { url = "<REGISTRY_URL>", token = "$BUN_PUBLISH_AUTH_TOKEN" }
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```powershell
|
|
94
|
+
# authenticate
|
|
95
|
+
$env:BUN_PUBLISH_AUTH_TOKEN = "<BUN_PUBLISH_AUTH_TOKEN>"
|
|
96
|
+
# publish
|
|
97
|
+
bun publish
|
|
98
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.0.0
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"author": "",
|
|
6
|
+
"license": "",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"typescript"
|
|
9
|
+
],
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": ""
|
|
13
|
+
},
|
|
14
|
+
"private": true,
|
|
15
|
+
"type": "module",
|
|
16
|
+
"imports": {
|
|
17
|
+
"#src/*.js": "./src/*.ts"
|
|
18
|
+
},
|
|
19
|
+
"bin": "./dist/cli.bundle.js",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"CHANGELOG.md",
|
|
23
|
+
"buildinfo.txt",
|
|
24
|
+
"template"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"clean:dist": "rm -rf dist/",
|
|
28
|
+
"clean:tsbuildinfo": "rm -f *.tsbuildinfo || true",
|
|
29
|
+
"clean": "bun run clean:dist && bun run clean:tsbuildinfo",
|
|
30
|
+
"buildinfo": "bun run scripts/buildinfo.ts",
|
|
31
|
+
"tests": "bun test",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"reinstall": "rm -rf node_modules && rm -f bun.lock && bun pm cache rm && bun install && bunx tsc --version",
|
|
34
|
+
"dev": "bun run buildinfo && bun run --watch scripts/dev.ts",
|
|
35
|
+
"prebuild": "bun run buildinfo",
|
|
36
|
+
"build": "bun run build:cli-bundle",
|
|
37
|
+
"build:cli-bundle": "bun build src/cli.ts --entry-naming \"[dir]/[name].bundle.[ext]\" --outdir dist --target node --format esm --minify --sourcemap=external"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/bun": "latest",
|
|
41
|
+
"typescript": "^6.0.2"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { readFileSync } from 'fs';
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
|
|
4
|
+
import CDN_REWRITE_MAP from './cdn-rewrite-map.json';
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
interface ExportConditions {
|
|
8
|
+
[key: string]: string | undefined;
|
|
9
|
+
entrypoint?: string;
|
|
10
|
+
types?: string;
|
|
11
|
+
browser?: string;
|
|
12
|
+
import?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface DependencyMap {
|
|
16
|
+
[packageName: string]: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface PackageManifest {
|
|
20
|
+
version: string;
|
|
21
|
+
exports?: Record<string, ExportConditions>;
|
|
22
|
+
dependencies?: DependencyMap;
|
|
23
|
+
devDependencies?: DependencyMap;
|
|
24
|
+
peerDependencies?: DependencyMap;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function getManifest(packageIdentifier?: string): PackageManifest {
|
|
28
|
+
|
|
29
|
+
const manifestPath = packageIdentifier
|
|
30
|
+
? join('node_modules', packageIdentifier, 'package.json')
|
|
31
|
+
: 'package.json';
|
|
32
|
+
|
|
33
|
+
let manifest: PackageManifest;
|
|
34
|
+
try {
|
|
35
|
+
manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'));
|
|
36
|
+
} catch {
|
|
37
|
+
if (packageIdentifier)
|
|
38
|
+
throw new Error(`[scripts/build-bundle.ts] Could not read manifest for '${packageIdentifier}' at '${manifestPath}'.`);
|
|
39
|
+
else
|
|
40
|
+
throw new Error(`[scripts/build-bundle.ts] Could not read package manifest at '${manifestPath}'.`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return manifest;
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function getManifestEntrypoints(packageManifest: PackageManifest): string[] {
|
|
48
|
+
|
|
49
|
+
const exports = packageManifest.exports;
|
|
50
|
+
if (!exports)
|
|
51
|
+
throw new Error(`[scripts/build-bundle.ts] No 'exports' field found in the given package manifest.`);
|
|
52
|
+
|
|
53
|
+
const entrypoints = Object.entries(exports)
|
|
54
|
+
.map(([key, conditions]) => {
|
|
55
|
+
if (!conditions.entrypoint)
|
|
56
|
+
throw new Error(`[scripts/build-bundle.ts] Export '${key}' does not have an 'entrypoint' condition.`);
|
|
57
|
+
return conditions.entrypoint;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return entrypoints;
|
|
61
|
+
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function getPackageVersion(manifest: PackageManifest, packageIdentifier?: string): string {
|
|
65
|
+
|
|
66
|
+
let version: string | undefined;
|
|
67
|
+
if (packageIdentifier) {
|
|
68
|
+
|
|
69
|
+
const dependencies = {
|
|
70
|
+
...manifest.dependencies,
|
|
71
|
+
...manifest.devDependencies,
|
|
72
|
+
...manifest.peerDependencies,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
version = dependencies[packageIdentifier];
|
|
76
|
+
if (!version)
|
|
77
|
+
throw new Error(`[scripts/build-bundle.ts] Package '${packageIdentifier}' is not listed in dependencies.`);
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
if (!manifest.version)
|
|
82
|
+
throw new Error('[scripts/build-bundle.ts] Package manifest does not contain a version field.');
|
|
83
|
+
version = manifest.version;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return version;
|
|
87
|
+
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function resolveCdnUrl(importSpecifier: string, urlTemplate: string): string {
|
|
91
|
+
const manifest = getManifest();
|
|
92
|
+
const version = getPackageVersion(manifest, importSpecifier);
|
|
93
|
+
return urlTemplate.replace('<VERSION>', version);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const cdnRewritePlugin = {
|
|
97
|
+
name: 'cdn-rewrite',
|
|
98
|
+
setup(build: any) {
|
|
99
|
+
|
|
100
|
+
const resolved = new Map<string, string>();
|
|
101
|
+
for (const [importSpecifier, urlTemplate] of Object.entries(CDN_REWRITE_MAP) as [string, string][]) {
|
|
102
|
+
const url = resolveCdnUrl(importSpecifier, urlTemplate);
|
|
103
|
+
resolved.set(importSpecifier, url);
|
|
104
|
+
console.log(`[cdn-rewrite] '${importSpecifier}' → '${url}'`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
build.onResolve({ filter: /\*/ }, (args: any) => {
|
|
108
|
+
const url = resolved.get(args.path);
|
|
109
|
+
if (url) return { path: url, external: true };
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
const entrypoints = getManifestEntrypoints(getManifest());
|
|
117
|
+
console.log('[scripts/build-bundle.ts] Entrypoints:', entrypoints);
|
|
118
|
+
|
|
119
|
+
let buildResult;
|
|
120
|
+
|
|
121
|
+
console.log('[scripts/build-bundle.ts] Starting ESM bundle build...');
|
|
122
|
+
buildResult = await Bun.build({
|
|
123
|
+
entrypoints,
|
|
124
|
+
outdir: 'dist',
|
|
125
|
+
naming: '[dir]/[name].bundle.[ext]',
|
|
126
|
+
target: 'browser',
|
|
127
|
+
format: 'esm',
|
|
128
|
+
minify: true,
|
|
129
|
+
sourcemap: 'external',
|
|
130
|
+
plugins: [cdnRewritePlugin],
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
if (!buildResult.success) {
|
|
134
|
+
console.error('[scripts/build-bundle.ts] Build failed:');
|
|
135
|
+
for (const message of buildResult.logs) {
|
|
136
|
+
console.error(message);
|
|
137
|
+
}
|
|
138
|
+
process.exit(1);
|
|
139
|
+
}
|
|
140
|
+
console.log('[scripts/build-bundle.ts] ESM bundle build completed successfully.');
|
|
141
|
+
|
|
142
|
+
console.log('[scripts/build-bundle.ts] Starting IIFE bundle build...');
|
|
143
|
+
buildResult = await Bun.build({
|
|
144
|
+
entrypoints,
|
|
145
|
+
outdir: 'dist',
|
|
146
|
+
naming: '[dir]/[name].iife.[ext]',
|
|
147
|
+
target: 'browser',
|
|
148
|
+
format: 'iife',
|
|
149
|
+
minify: true,
|
|
150
|
+
sourcemap: 'external',
|
|
151
|
+
plugins: [cdnRewritePlugin],
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
if (!buildResult.success) {
|
|
155
|
+
console.error('[scripts/build-bundle.ts] Build failed:');
|
|
156
|
+
for (const message of buildResult.logs) {
|
|
157
|
+
console.error(message);
|
|
158
|
+
}
|
|
159
|
+
process.exit(1);
|
|
160
|
+
}
|
|
161
|
+
console.log('[scripts/build-bundle.ts] IIFE bundle build completed successfully.');
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { execSync } from 'child_process';
|
|
2
|
+
import { readFileSync, writeFileSync } from 'fs';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
const BUILD_INFO_FILE = 'buildinfo.txt';
|
|
6
|
+
const GIT_COMMAND = 'git rev-parse --short HEAD';
|
|
7
|
+
|
|
8
|
+
const version: string = JSON.parse(readFileSync('package.json', 'utf-8')).version;
|
|
9
|
+
|
|
10
|
+
let gitHash = '';
|
|
11
|
+
try {
|
|
12
|
+
gitHash = execSync(GIT_COMMAND, { stdio: ['ignore', 'pipe', 'ignore'] })
|
|
13
|
+
.toString()
|
|
14
|
+
.trim();
|
|
15
|
+
} catch { }
|
|
16
|
+
|
|
17
|
+
const buildinfo = gitHash
|
|
18
|
+
? version.includes('+')
|
|
19
|
+
? `${version}.${gitHash}`
|
|
20
|
+
: `${version}+${gitHash}`
|
|
21
|
+
: version;
|
|
22
|
+
|
|
23
|
+
writeFileSync(BUILD_INFO_FILE, buildinfo.trim(), 'utf-8');
|
|
24
|
+
|
|
25
|
+
console.log(`'${BUILD_INFO_FILE}' has been updated with build info: ${buildinfo}`);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { fileURLToPath } from 'url';
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
const start: number = performance.now();
|
|
5
|
+
|
|
6
|
+
const packageUrl = new URL('../', import.meta.url);
|
|
7
|
+
console.log(`'packagePath':`, fileURLToPath(packageUrl));
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
// dev scratchpad
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
console.log(`${(performance.now() - start).toFixed(3)}ms`);
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { cpSync, readFileSync, renameSync, writeFileSync } from 'fs';
|
|
4
|
+
import { resolve } from 'path';
|
|
5
|
+
import {
|
|
6
|
+
templatePath,
|
|
7
|
+
changelogPath,
|
|
8
|
+
buildinfoPath,
|
|
9
|
+
readmePath
|
|
10
|
+
} from './constants.js';
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
|
|
15
|
+
const packageNameArgument = process.argv[2];
|
|
16
|
+
if (!packageNameArgument)
|
|
17
|
+
throw new Error('First argument must be the package name (e.g. "my-package" or "@my-scope/my-package").');
|
|
18
|
+
const packageName = packageNameArgument.replace(/\\/g, '/');
|
|
19
|
+
|
|
20
|
+
const destinationPath = resolve(process.cwd(), packageName);
|
|
21
|
+
|
|
22
|
+
cpSync(templatePath, destinationPath, { recursive: true });
|
|
23
|
+
cpSync(changelogPath, resolve(destinationPath, 'CHANGELOG-template.md'));
|
|
24
|
+
cpSync(buildinfoPath, resolve(destinationPath, 'buildinfo-template.txt'));
|
|
25
|
+
cpSync(readmePath, resolve(destinationPath, 'README-template.md'));
|
|
26
|
+
|
|
27
|
+
const packageJsonPath = resolve(destinationPath, 'package.json');
|
|
28
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
|
|
29
|
+
packageJson.name = packageName;
|
|
30
|
+
writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
|
|
31
|
+
|
|
32
|
+
renameSync(resolve(destinationPath, 'gitignore'), resolve(destinationPath, '.gitignore'));
|
|
33
|
+
|
|
34
|
+
console.log(`Template has been successfully instantiated at '${destinationPath}' with package name '${packageName}'.`);
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
39
|
+
console.error('Error:', err.message);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { resolve } from 'path';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
export const packageUrl: URL = new URL('../', import.meta.url);
|
|
6
|
+
export const packagePath: string = resolve(fileURLToPath(packageUrl));
|
|
7
|
+
|
|
8
|
+
export const templatePath: string = resolve(packagePath, 'template/');
|
|
9
|
+
|
|
10
|
+
export const changelogPath: string = resolve(packagePath, 'CHANGELOG.md');
|
|
11
|
+
export const buildinfoPath: string = resolve(packagePath, 'buildinfo.txt');
|
|
12
|
+
export const readmePath: string = resolve(packagePath, 'README.md');
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, it, expect } from 'bun:test';
|
|
2
|
+
import { readFileSync } from 'fs';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
const buildinfoUrl = new URL('../buildinfo.txt', import.meta.url);
|
|
7
|
+
|
|
8
|
+
// taken from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
|
|
9
|
+
// Captures: [1]=major, [2]=minor, [3]=patch, [4]=pre-release, [5]=build-metadata
|
|
10
|
+
const SEMVER_REGEX =
|
|
11
|
+
/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
|
|
12
|
+
|
|
13
|
+
function readBuildinfo(): string {
|
|
14
|
+
return readFileSync(fileURLToPath(buildinfoUrl), 'utf-8').trim();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('buildInfo', () => {
|
|
18
|
+
|
|
19
|
+
it('should be a valid semver string', () => {
|
|
20
|
+
expect(readBuildinfo()).toMatch(SEMVER_REGEX);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ESNext",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": [
|
|
6
|
+
"ESNext"
|
|
7
|
+
],
|
|
8
|
+
"moduleResolution": "bundler",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"verbatimModuleSyntax": true,
|
|
11
|
+
"noUncheckedIndexedAccess": true,
|
|
12
|
+
"exactOptionalPropertyTypes": true,
|
|
13
|
+
"noImplicitOverride": true,
|
|
14
|
+
"isolatedDeclarations": true,
|
|
15
|
+
"esModuleInterop": true,
|
|
16
|
+
"composite": true,
|
|
17
|
+
"skipLibCheck": true,
|
|
18
|
+
"types": [
|
|
19
|
+
"bun"
|
|
20
|
+
],
|
|
21
|
+
"forceConsistentCasingInFileNames": true,
|
|
22
|
+
"resolveJsonModule": true,
|
|
23
|
+
"declaration": true,
|
|
24
|
+
"declarationMap": true,
|
|
25
|
+
"emitDeclarationOnly": true,
|
|
26
|
+
"outDir": "./dist"
|
|
27
|
+
},
|
|
28
|
+
"include": [
|
|
29
|
+
"tests/**/*.ts",
|
|
30
|
+
"scripts/**/*.ts",
|
|
31
|
+
"scripts/**/*.json",
|
|
32
|
+
"src/**/*.ts"
|
|
33
|
+
],
|
|
34
|
+
"exclude": [
|
|
35
|
+
"node_modules",
|
|
36
|
+
"dist"
|
|
37
|
+
]
|
|
38
|
+
}
|