nibs-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.
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.createNewProject = createNewProject;
40
+ const crypto_1 = require("crypto");
41
+ const promises_1 = require("fs/promises");
42
+ const inquirer_1 = __importDefault(require("inquirer"));
43
+ const path_1 = require("path");
44
+ const yaml_1 = __importStar(require("yaml"));
45
+ const project_1 = require("./project");
46
+ // Constants
47
+ const INDEX_TS_TEMPLATE = `(async () => {
48
+ api.v1.log("Hello World!");
49
+ })();`;
50
+ const TSCONFIG = {
51
+ compilerOptions: {
52
+ target: "ES2023",
53
+ module: "ESNext",
54
+ lib: ["ES2023"],
55
+ moduleResolution: "bundler",
56
+ strict: true,
57
+ esModuleInterop: true,
58
+ skipLibCheck: true,
59
+ forceConsistentCasingInFileNames: true,
60
+ allowSyntheticDefaultImports: true,
61
+ noUnusedLocals: true,
62
+ noUnusedParameters: true,
63
+ noImplicitReturns: true,
64
+ noFallthroughCasesInSwitch: true,
65
+ noEmit: true,
66
+ typeRoots: ["./external", "./node_modules/@types"],
67
+ rewriteRelativeImportExtensions: true,
68
+ },
69
+ include: ["src/**/*", "external/**/*"],
70
+ exclude: ["node_modules", "dist", "**/*.test.ts"],
71
+ };
72
+ const COMPAT_VERSION = "naiscript-1.0";
73
+ // Helpers
74
+ async function checkExistingProject(projectPath) {
75
+ return Promise.race([
76
+ (0, promises_1.access)((0, path_1.join)(projectPath, "src"), promises_1.constants.F_OK),
77
+ (0, promises_1.access)((0, path_1.join)(projectPath, "project.yaml"), promises_1.constants.F_OK),
78
+ ])
79
+ .then(() => true)
80
+ .catch(() => false);
81
+ }
82
+ async function createNewProject(projectPath) {
83
+ const answers = await inquirer_1.default.prompt([
84
+ {
85
+ type: "input",
86
+ name: "name",
87
+ message: "What will you name your script?",
88
+ validate: (input) => input.length > 0,
89
+ },
90
+ {
91
+ type: "input",
92
+ name: "author",
93
+ message: "And who is the author?",
94
+ },
95
+ {
96
+ type: "input",
97
+ name: "description",
98
+ message: "Write a brief description:",
99
+ default: "",
100
+ },
101
+ {
102
+ type: "input",
103
+ name: "license",
104
+ message: "What license is the script published under?",
105
+ default: "MIT",
106
+ },
107
+ ]);
108
+ const projectExists = await checkExistingProject(projectPath);
109
+ if (projectExists) {
110
+ console.error("Project already exists");
111
+ }
112
+ await (0, promises_1.mkdir)((0, path_1.join)(projectPath, "src"), { recursive: true });
113
+ const projectYamlDocument = new yaml_1.Document({
114
+ compatibilityVersion: COMPAT_VERSION,
115
+ id: (0, crypto_1.randomUUID)(),
116
+ name: answers.name,
117
+ version: "0.0.0",
118
+ createdAt: (0, project_1.currentEpochS)(),
119
+ author: answers.author,
120
+ description: answers.description + ` License: ${answers.license}`,
121
+ memoryLimit: 8,
122
+ updatedAt: (0, project_1.currentEpochS)(),
123
+ config: [],
124
+ });
125
+ const project = {
126
+ name: (0, path_1.basename)(projectPath),
127
+ path: projectPath,
128
+ meta: projectYamlDocument,
129
+ };
130
+ await Promise.all([
131
+ (0, promises_1.writeFile)((0, path_1.join)(projectPath, "project.yaml"), yaml_1.default.stringify(project.meta), "utf-8"),
132
+ (0, promises_1.writeFile)((0, path_1.join)(projectPath, "src", "index.ts"), INDEX_TS_TEMPLATE, "utf-8"),
133
+ (0, promises_1.writeFile)((0, path_1.join)(projectPath, "tsconfig.json"), JSON.stringify(TSCONFIG), "utf-8"),
134
+ ]);
135
+ console.log(`Project initialized at ${projectPath}`);
136
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadProject = loadProject;
4
+ exports.currentEpochS = currentEpochS;
5
+ const promises_1 = require("fs/promises");
6
+ const path_1 = require("path");
7
+ const yaml_1 = require("yaml");
8
+ async function loadProject(projectPath) {
9
+ const project = await (0, promises_1.readFile)((0, path_1.join)(projectPath, "project.yaml"))
10
+ .then((buf) => (0, yaml_1.parseDocument)(buf.toString()))
11
+ .catch((err) => {
12
+ throw new Error(`Project file not found at ${projectPath}. Create project with \`nibs new\``);
13
+ });
14
+ return {
15
+ name: (0, path_1.basename)(projectPath),
16
+ path: projectPath,
17
+ meta: project,
18
+ };
19
+ }
20
+ function currentEpochS() {
21
+ return Math.floor(Date.now() / 1000);
22
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.rollupInputOptions = rollupInputOptions;
7
+ exports.rollupOutputOptions = rollupOutputOptions;
8
+ const plugin_typescript_1 = __importDefault(require("@rollup/plugin-typescript"));
9
+ const path_1 = require("path");
10
+ function rollupInputOptions(project) {
11
+ return {
12
+ input: (0, path_1.join)(project.path, "src", "index.ts"),
13
+ plugins: [
14
+ {
15
+ name: "watch-project-yaml",
16
+ buildStart() {
17
+ this.addWatchFile((0, path_1.join)(project.path, "project.yaml"));
18
+ },
19
+ },
20
+ (0, plugin_typescript_1.default)({
21
+ exclude: ["external/"],
22
+ tsconfig: (0, path_1.join)(project.path, "tsconfig.json"),
23
+ }),
24
+ ],
25
+ onwarn(warning) {
26
+ console.warn(warning.message);
27
+ },
28
+ };
29
+ }
30
+ function generateScriptHeader(meta) {
31
+ return `/*---
32
+ ${meta.toString()}---*/
33
+
34
+ /**
35
+ * ${meta.get("name")}
36
+ * Built with NovelAI Script Build System
37
+ */\n`;
38
+ }
39
+ function rollupOutputOptions(project) {
40
+ return {
41
+ dir: (0, path_1.join)(project.path, "dist"),
42
+ format: "esm",
43
+ entryFileNames: `${project.name}.naiscript`,
44
+ banner() {
45
+ return generateScriptHeader(project.meta);
46
+ },
47
+ };
48
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.watchProject = watchProject;
4
+ const promises_1 = require("fs/promises");
5
+ const path_1 = require("path");
6
+ const rollup_1 = require("rollup");
7
+ const yaml_1 = require("yaml");
8
+ const project_1 = require("./project");
9
+ const rollup_config_1 = require("./rollup-config");
10
+ function watchProject(project) {
11
+ console.log(` Watching project: ${project.name}`);
12
+ return (0, rollup_1.watch)({
13
+ ...(0, rollup_config_1.rollupInputOptions)(project),
14
+ ...{ output: (0, rollup_config_1.rollupOutputOptions)(project) },
15
+ }).on("event", async (event) => {
16
+ const currentProjectFile = await (0, promises_1.readFile)((0, path_1.join)(project.path, "project.yaml"));
17
+ const currentProjectMeta = (0, yaml_1.parseDocument)(currentProjectFile.toString());
18
+ currentProjectMeta.set("updatedAt", (0, project_1.currentEpochS)());
19
+ switch (event.code) {
20
+ case "START":
21
+ console.log(` Building project: ${project.name}...`);
22
+ project.meta = currentProjectMeta;
23
+ break;
24
+ case "END":
25
+ // Write new project.yaml file
26
+ await (0, promises_1.writeFile)((0, path_1.join)(project.path, "project.yaml"), currentProjectMeta.toString()).catch(console.error);
27
+ console.log(` Built project: ${project.name}...`);
28
+ }
29
+ });
30
+ }
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fetchExternalTypes = fetchExternalTypes;
4
+ const fs_1 = require("fs");
5
+ const promises_1 = require("fs/promises");
6
+ const path_1 = require("path");
7
+ const posix_1 = require("path/posix");
8
+ const promises_2 = require("stream/promises");
9
+ const NAI_TYPES_URL = "https://novelai.net/scripting/types/script-types.d.ts";
10
+ async function fetchExternalTypes(projectPath) {
11
+ const outputPath = (0, path_1.join)(projectPath, "external", "script-types.d.ts");
12
+ await (0, promises_1.mkdir)((0, posix_1.dirname)(outputPath), { recursive: true });
13
+ console.log("📥 Fetching NovelAI type definitions...");
14
+ const res = await fetch(NAI_TYPES_URL);
15
+ if (!res.ok) {
16
+ throw new Error(`Failed to fetch types: HTTP ${res.status}, ${res.statusText}`);
17
+ }
18
+ else if (!res.body) {
19
+ throw new Error("Got result, but body is empty");
20
+ }
21
+ try {
22
+ await (0, promises_2.pipeline)(res.body, (0, fs_1.createWriteStream)(outputPath));
23
+ }
24
+ catch (err) {
25
+ console.error(err);
26
+ throw err;
27
+ }
28
+ }
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "nibs-cli",
3
+ "version": "4.0.0",
4
+ "description": "Project build tool for NovelAI scripts - bundles TypeScript files into single scripts",
5
+ "bin": {
6
+ "nibs": "dist/cli.js"
7
+ },
8
+ "license": "MIT",
9
+ "type": "commonjs",
10
+ "scripts": {
11
+ "prepublishOnly": "npm run build",
12
+ "build": "tsc",
13
+ "format": "prettier . -w",
14
+ "typecheck": "tsc --noEmit",
15
+ "help": "node src/cli.ts --help"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "engines": {
21
+ "node": ">= 18"
22
+ },
23
+ "devDependencies": {
24
+ "prettier": "^3.7.4",
25
+ "@types/node": "^25.0.0",
26
+ "@types/rollup": "^0.51.4",
27
+ "typescript": "^5.3.0"
28
+ },
29
+ "dependencies": {
30
+ "@rollup/plugin-typescript": "^12.3.0",
31
+ "commander": "^14.0.2",
32
+ "inquirer": "^13.0.2",
33
+ "rollup": "^4.53.3",
34
+ "yaml": "^2.8.2"
35
+ }
36
+ }