@ttsc/banner 0.5.0-dev.20260429
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/LICENSE +21 -0
- package/README.md +62 -0
- package/go-plugin/banner/banner.go +137 -0
- package/go-plugin/go.mod +3 -0
- package/go-plugin/main.go +33 -0
- package/index.cjs +18 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jeongho Nam
|
|
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,62 @@
|
|
|
1
|
+
# `@ttsc/banner`
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
[](https://github.com/samchon/ttsc/blob/master/LICENSE)
|
|
6
|
+
[](https://www.npmjs.com/package/@ttsc/banner)
|
|
7
|
+
[](https://www.npmjs.com/package/@ttsc/banner)
|
|
8
|
+
[](https://github.com/samchon/ttsc/actions?query=workflow%3Atest)
|
|
9
|
+
[](https://discord.gg/E94XhzrUCZ)
|
|
10
|
+
|
|
11
|
+
`@ttsc/banner` prepends a fixed comment to emitted JavaScript and declaration files.
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
Install `ttsc`, TypeScript-Go, and the banner plugin:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install -D ttsc @typescript/native-preview @ttsc/banner
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Open your project's `tsconfig.json`, then add this entry under `compilerOptions.plugins`. If the file already has `compilerOptions`, merge this into the existing object:
|
|
22
|
+
|
|
23
|
+
```jsonc
|
|
24
|
+
{
|
|
25
|
+
"compilerOptions": {
|
|
26
|
+
"plugins": [
|
|
27
|
+
{
|
|
28
|
+
"transform": "@ttsc/banner",
|
|
29
|
+
"banner": "/*! @license MIT (c) 2026 Acme */"
|
|
30
|
+
}
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Run your normal `ttsc` command:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npx ttsc
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The banner is written to emitted `.js`, `.mjs`, `.cjs`, `.d.ts`, `.d.mts`, and `.d.cts` files. Source maps and build-info files are left unchanged.
|
|
43
|
+
|
|
44
|
+
## Notes
|
|
45
|
+
|
|
46
|
+
`@ttsc/banner` can be used with other `ttsc` plugins:
|
|
47
|
+
|
|
48
|
+
```jsonc
|
|
49
|
+
{
|
|
50
|
+
"compilerOptions": {
|
|
51
|
+
"plugins": [
|
|
52
|
+
// Keep lint first.
|
|
53
|
+
{ "transform": "@ttsc/lint", "rules": { "no-var": "error" } },
|
|
54
|
+
|
|
55
|
+
// Output plugins run after emit, in order.
|
|
56
|
+
{ "transform": "@ttsc/banner", "banner": "/*! @license MIT */" },
|
|
57
|
+
{ "transform": "@ttsc/paths" },
|
|
58
|
+
{ "transform": "@ttsc/strip", "calls": ["console.log"] }
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
package banner
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/json"
|
|
5
|
+
"flag"
|
|
6
|
+
"fmt"
|
|
7
|
+
"os"
|
|
8
|
+
"path/filepath"
|
|
9
|
+
"strings"
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
const modeBanner = "ttsc-banner"
|
|
13
|
+
|
|
14
|
+
type pluginEntry struct {
|
|
15
|
+
Config map[string]any `json:"config"`
|
|
16
|
+
Mode string `json:"mode"`
|
|
17
|
+
Name string `json:"name"`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
func RunOutput(args []string) int {
|
|
21
|
+
fs := flag.NewFlagSet("output", flag.ContinueOnError)
|
|
22
|
+
fs.SetOutput(os.Stderr)
|
|
23
|
+
file := fs.String("file", "", "emitted file to transform")
|
|
24
|
+
out := fs.String("out", "", "write transformed text to this file instead of updating --file")
|
|
25
|
+
_ = fs.String("cwd", "", "project directory")
|
|
26
|
+
_ = fs.String("outDir", "", "emit directory override")
|
|
27
|
+
pluginsJSON := fs.String("plugins-json", "", "ttsc plugin manifest JSON")
|
|
28
|
+
_ = fs.String("rewrite-mode", modeBanner, "native mode")
|
|
29
|
+
_ = fs.String("tsconfig", "tsconfig.json", "project tsconfig")
|
|
30
|
+
if err := fs.Parse(args); err != nil {
|
|
31
|
+
return 2
|
|
32
|
+
}
|
|
33
|
+
if *file == "" {
|
|
34
|
+
fmt.Fprintln(os.Stderr, "@ttsc/banner: output requires --file")
|
|
35
|
+
return 2
|
|
36
|
+
}
|
|
37
|
+
config, err := findConfig(*pluginsJSON)
|
|
38
|
+
if err != nil {
|
|
39
|
+
fmt.Fprintln(os.Stderr, err)
|
|
40
|
+
return 2
|
|
41
|
+
}
|
|
42
|
+
text, err := os.ReadFile(*file)
|
|
43
|
+
if err != nil {
|
|
44
|
+
fmt.Fprintf(os.Stderr, "@ttsc/banner: read %s: %v\n", *file, err)
|
|
45
|
+
return 2
|
|
46
|
+
}
|
|
47
|
+
patched, err := Apply(*file, string(text), config)
|
|
48
|
+
if err != nil {
|
|
49
|
+
fmt.Fprintln(os.Stderr, err)
|
|
50
|
+
return 2
|
|
51
|
+
}
|
|
52
|
+
target := *file
|
|
53
|
+
if *out != "" {
|
|
54
|
+
target = *out
|
|
55
|
+
}
|
|
56
|
+
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
|
|
57
|
+
fmt.Fprintf(os.Stderr, "@ttsc/banner: mkdir: %v\n", err)
|
|
58
|
+
return 2
|
|
59
|
+
}
|
|
60
|
+
if err := os.WriteFile(target, []byte(patched), 0o644); err != nil {
|
|
61
|
+
fmt.Fprintf(os.Stderr, "@ttsc/banner: write %s: %v\n", target, err)
|
|
62
|
+
return 2
|
|
63
|
+
}
|
|
64
|
+
return 0
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
func Apply(fileName string, text string, config map[string]any) (string, error) {
|
|
68
|
+
if !isBannerableOutput(fileName) {
|
|
69
|
+
return text, nil
|
|
70
|
+
}
|
|
71
|
+
banner, err := parseBanner(config)
|
|
72
|
+
if err != nil {
|
|
73
|
+
return "", err
|
|
74
|
+
}
|
|
75
|
+
if strings.HasPrefix(text, banner) {
|
|
76
|
+
return text, nil
|
|
77
|
+
}
|
|
78
|
+
return banner + text, nil
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func parseBanner(config map[string]any) (string, error) {
|
|
82
|
+
raw, ok := config["banner"]
|
|
83
|
+
if !ok {
|
|
84
|
+
return "", fmt.Errorf("@ttsc/banner: \"banner\" must be a non-empty string")
|
|
85
|
+
}
|
|
86
|
+
text, ok := raw.(string)
|
|
87
|
+
if !ok || strings.TrimSpace(text) == "" {
|
|
88
|
+
return "", fmt.Errorf("@ttsc/banner: \"banner\" must be a non-empty string")
|
|
89
|
+
}
|
|
90
|
+
if !strings.HasSuffix(text, "\n") {
|
|
91
|
+
text += "\n"
|
|
92
|
+
}
|
|
93
|
+
return text, nil
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
func findConfig(pluginsJSON string) (map[string]any, error) {
|
|
97
|
+
if strings.TrimSpace(pluginsJSON) == "" {
|
|
98
|
+
return nil, fmt.Errorf("@ttsc/banner: missing --plugins-json")
|
|
99
|
+
}
|
|
100
|
+
var entries []pluginEntry
|
|
101
|
+
if err := json.Unmarshal([]byte(pluginsJSON), &entries); err != nil {
|
|
102
|
+
return nil, fmt.Errorf("@ttsc/banner: invalid --plugins-json: %w", err)
|
|
103
|
+
}
|
|
104
|
+
for _, entry := range entries {
|
|
105
|
+
if entry.Mode == modeBanner || entry.Name == "@ttsc/banner" {
|
|
106
|
+
if entry.Config == nil {
|
|
107
|
+
return map[string]any{}, nil
|
|
108
|
+
}
|
|
109
|
+
return entry.Config, nil
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return nil, fmt.Errorf("@ttsc/banner: plugin entry not found")
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
func isBannerableOutput(fileName string) bool {
|
|
116
|
+
lower := strings.ToLower(fileName)
|
|
117
|
+
if strings.HasSuffix(lower, ".map") || strings.HasSuffix(lower, ".tsbuildinfo") {
|
|
118
|
+
return false
|
|
119
|
+
}
|
|
120
|
+
return isJavaScriptOutput(fileName) || isDeclarationOutput(fileName)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
func isJavaScriptOutput(fileName string) bool {
|
|
124
|
+
switch strings.ToLower(filepath.Ext(fileName)) {
|
|
125
|
+
case ".js", ".mjs", ".cjs":
|
|
126
|
+
return true
|
|
127
|
+
default:
|
|
128
|
+
return false
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
func isDeclarationOutput(fileName string) bool {
|
|
133
|
+
lower := strings.ToLower(fileName)
|
|
134
|
+
return strings.HasSuffix(lower, ".d.ts") ||
|
|
135
|
+
strings.HasSuffix(lower, ".d.mts") ||
|
|
136
|
+
strings.HasSuffix(lower, ".d.cts")
|
|
137
|
+
}
|
package/go-plugin/go.mod
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
package main
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"fmt"
|
|
5
|
+
"os"
|
|
6
|
+
|
|
7
|
+
"github.com/samchon/ttsc/packages/banner/go-plugin/banner"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
const version = "0.0.1"
|
|
11
|
+
|
|
12
|
+
func main() {
|
|
13
|
+
os.Exit(run(os.Args[1:]))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
func run(args []string) int {
|
|
17
|
+
if len(args) == 0 {
|
|
18
|
+
fmt.Fprintln(os.Stderr, "@ttsc/banner: command required (expected output|version)")
|
|
19
|
+
return 2
|
|
20
|
+
}
|
|
21
|
+
switch args[0] {
|
|
22
|
+
case "-v", "--version", "version":
|
|
23
|
+
fmt.Fprintf(os.Stdout, "@ttsc/banner %s\n", version)
|
|
24
|
+
return 0
|
|
25
|
+
case "check":
|
|
26
|
+
return 0
|
|
27
|
+
case "output":
|
|
28
|
+
return banner.RunOutput(args[1:])
|
|
29
|
+
default:
|
|
30
|
+
fmt.Fprintf(os.Stderr, "@ttsc/banner: unknown command %q\n", args[0])
|
|
31
|
+
return 2
|
|
32
|
+
}
|
|
33
|
+
}
|
package/index.cjs
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
|
|
6
|
+
module.exports = function createTtscBanner() {
|
|
7
|
+
return {
|
|
8
|
+
name: "@ttsc/banner",
|
|
9
|
+
native: {
|
|
10
|
+
mode: "ttsc-banner",
|
|
11
|
+
source: {
|
|
12
|
+
dir: path.resolve(__dirname, "go-plugin"),
|
|
13
|
+
},
|
|
14
|
+
contractVersion: 1,
|
|
15
|
+
capabilities: ["output"],
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ttsc/banner",
|
|
3
|
+
"version": "0.5.0-dev.20260429",
|
|
4
|
+
"description": "First-party ttsc plugin that prepends a banner comment to emitted files.",
|
|
5
|
+
"main": "index.cjs",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./index.cjs",
|
|
8
|
+
"./package.json": "./package.json"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"ttsc",
|
|
12
|
+
"banner",
|
|
13
|
+
"typescript",
|
|
14
|
+
"tsgo"
|
|
15
|
+
],
|
|
16
|
+
"files": [
|
|
17
|
+
"README.md",
|
|
18
|
+
"index.cjs",
|
|
19
|
+
"go-plugin/go.mod",
|
|
20
|
+
"go-plugin/main.go",
|
|
21
|
+
"go-plugin/banner"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"ttsc": "^0.4.4"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "https://github.com/samchon/ttsc"
|
|
29
|
+
},
|
|
30
|
+
"author": "Jeongho Nam",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/samchon/ttsc/issues"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/samchon/ttsc/tree/master/packages/banner#readme"
|
|
36
|
+
}
|