@lightnet/cli 4.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 LightNet
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
+ # `@lightnet/cli`
2
+
3
+ Command-line tools for working with LightNet projects.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install --save-dev @lightnet/cli
9
+ ```
10
+
11
+ You can also run the CLI without adding it to your project first:
12
+
13
+ ```bash
14
+ npx @lightnet/cli --help
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ The package exposes the `lightnet` binary:
20
+
21
+ ```bash
22
+ lightnet <command> [options]
23
+ ```
24
+
25
+ Get the full command list at any time with:
26
+
27
+ ```bash
28
+ lightnet --help
29
+ ```
30
+
31
+ ## Commands
32
+
33
+ ### `check-translations`
34
+
35
+ Checks the translation data produced by the latest LightNet build and reports
36
+ missing locale values.
37
+
38
+ ```bash
39
+ lightnet check-translations
40
+ ```
41
+
42
+ > [!NOTE]
43
+ > `check-translations` only validates translations that were actually used
44
+ > during the last build. For example, it would not report a missing category
45
+ > label translation if that category label was never referenced while building
46
+ > the site.
47
+
48
+ What it does:
49
+
50
+ - Reads the cached translation build output from `node_modules/.cache/lightnet/`
51
+ - Verifies every translation has values for every configured locale
52
+ - Groups missing translations by source to make follow-up work clearer
53
+
54
+ When to use it:
55
+
56
+ - After building a LightNet site
57
+ - In CI to catch incomplete translations before deploys
58
+ - While adding new locales or updating translation maps
59
+
60
+ Expected setup:
61
+
62
+ - Run your project build first so LightNet can generate its translation cache
63
+ If the cache is missing, the command will fail and prompt you to run a build
64
+ first.
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@lightnet/cli",
3
+ "description": "CLI for managing LightNet projects",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "version": "4.0.0",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/LightNetDev/lightnet",
13
+ "directory": "packages/cli"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/LightNetDev/lightnet/issues"
17
+ },
18
+ "bin": {
19
+ "lightnet": "./src/index.js"
20
+ },
21
+ "homepage": "https://lightnet.community",
22
+ "files": [
23
+ "src",
24
+ "README.md",
25
+ "CHANGELOG.md"
26
+ ],
27
+ "dependencies": {
28
+ "commander": "^14.0.3"
29
+ },
30
+ "engines": {
31
+ "node": ">=22"
32
+ }
33
+ }
@@ -0,0 +1,135 @@
1
+ // @ts-check
2
+
3
+ import { readFile } from "node:fs/promises"
4
+ import { resolve } from "node:path"
5
+ import { cwd } from "node:process"
6
+
7
+ const lightnetCachePath = resolve(cwd(), "node_modules", ".cache", "lightnet")
8
+
9
+ /** @type {{type:Translation["type"], title:string, action:string}[]} */
10
+ const translationSources = [
11
+ {
12
+ type: "lightnet",
13
+ title: "LightNet built-in translations",
14
+ action: "Add the missing entries in your /src/translations/*.yaml files.",
15
+ },
16
+ {
17
+ type: "user",
18
+ title: "User translation files",
19
+ action: "Add the missing entries in your /src/translations/*.yaml files.",
20
+ },
21
+ {
22
+ type: "map",
23
+ title: "Inline translation maps",
24
+ action:
25
+ "Update the inline translation map to include values for every configured site language.",
26
+ },
27
+ ]
28
+
29
+ export async function checkTranslations() {
30
+ const translations = await readTranslations()
31
+ const languages = await readLanguages()
32
+ if (!translations || !languages || translations.length === 0) {
33
+ return false
34
+ }
35
+ const incompleteTranslations = translations
36
+ .map((translation) => ({
37
+ ...translation,
38
+ missingLocales: getMissingLocales(translation, languages),
39
+ }))
40
+ .filter((translation) => translation.missingLocales.length > 0)
41
+
42
+ if (incompleteTranslations.length === 0) {
43
+ return true
44
+ }
45
+
46
+ const grouped = Object.groupBy(
47
+ incompleteTranslations,
48
+ (translation) => translation.type,
49
+ )
50
+
51
+ console.log("Translation check failed")
52
+ for (const source of translationSources) {
53
+ printMissingTranslations(source, grouped[source.type])
54
+ }
55
+
56
+ console.log("\n\n")
57
+ return false
58
+ }
59
+
60
+ /**
61
+ *
62
+ * @param {{title:string, action:string}} source
63
+ * @param {(Translation & {missingLocales:string[]})[]|undefined} translations
64
+ */
65
+ function printMissingTranslations(source, translations) {
66
+ if (!translations || translations.length === 0) {
67
+ return
68
+ }
69
+
70
+ console.log()
71
+ console.log(source.title)
72
+ translations
73
+ .toSorted(
74
+ (t1, t2) =>
75
+ t2.missingLocales.length - t1.missingLocales.length ||
76
+ t1.key.localeCompare(t2.key),
77
+ )
78
+ .forEach(({ key, missingLocales }) => {
79
+ console.log(`- ${key}`)
80
+ console.log(` Missing locales: ${missingLocales.join(", ")}`)
81
+ })
82
+
83
+ console.log(`Action: ${source.action}`)
84
+ }
85
+
86
+ /**
87
+ *
88
+ * @param {Translation} translation
89
+ * @param {Languages} languages
90
+ * @returns {string[]}
91
+ */
92
+ function getMissingLocales(translation, languages) {
93
+ return languages.locales.filter((locale) => !translation.values[locale])
94
+ }
95
+
96
+ /**
97
+ * @returns {Promise<Translation[]|undefined>}
98
+ */
99
+ async function readTranslations() {
100
+ try {
101
+ const translationsText = await readFile(
102
+ resolve(lightnetCachePath, "translations.jsonl"),
103
+ "utf-8",
104
+ )
105
+ return translationsText
106
+ .split("\n")
107
+ .filter((line) => line.trim())
108
+ .map((line) => JSON.parse(line))
109
+ } catch {
110
+ console.error("No translation build cache found.")
111
+ console.error(
112
+ "Action: Run build and try lightnet check-translations again.",
113
+ )
114
+ return undefined
115
+ }
116
+ }
117
+
118
+ /**
119
+ * @returns {Promise<Languages|undefined>}
120
+ */
121
+ async function readLanguages() {
122
+ try {
123
+ const languagesText = await readFile(
124
+ resolve(lightnetCachePath, "languages.json"),
125
+ "utf-8",
126
+ )
127
+ return JSON.parse(languagesText)
128
+ } catch {
129
+ console.error("No language manifest found from the last build.")
130
+ console.error(
131
+ "Action: Run build and try lightnet check-translations again.",
132
+ )
133
+ return undefined
134
+ }
135
+ }
package/src/index.js ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env node
2
+ // @ts-check
3
+
4
+ import { exit } from "node:process"
5
+
6
+ import { Command } from "commander"
7
+
8
+ import pkg from "../package.json" with { type: "json" }
9
+ import { checkTranslations } from "./check-translations.js"
10
+ const { version } = pkg
11
+
12
+ const program = new Command()
13
+
14
+ program
15
+ .name("lightnet-cli")
16
+ .description("CLI for managing LightNet projects")
17
+ .version(version)
18
+
19
+ program
20
+ .command("check-translations")
21
+ .description("check if last build has been missing any translations")
22
+ .action(async () => {
23
+ const checkSuccessful = await checkTranslations()
24
+ exit(checkSuccessful ? 0 : 1)
25
+ })
26
+
27
+ await program.parseAsync()
package/src/types.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ type Translation = {
2
+ type: "lightnet" | "user" | "map"
3
+ key: string
4
+ values: Record<string, string | undefined>
5
+ }
6
+
7
+ type Languages = {
8
+ defaultLocale: string
9
+ locales: string[]
10
+ }