@dword-design/nuxt-custom-cli 0.1.0 → 2.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/dist/cli.d.ts CHANGED
@@ -1,2 +1 @@
1
- #!/usr/bin/env node
2
1
  export {};
package/dist/cli.js CHANGED
@@ -1,28 +1,11 @@
1
- #!/usr/bin/env node
2
- import pathLib from "node:path";
3
- import { execa } from "execa";
4
- import build from "./build.js";
5
- import { getAliasFromNuxtTsConfig } from "./get-alias-from-tsconfig.js";
6
- const rootDir = process.cwd();
7
- const outDir = pathLib.join(rootDir, ".nuxt");
8
- const builtCliPath = pathLib.join(outDir, "cli.mjs");
9
- try {
10
- const alias = await getAliasFromNuxtTsConfig({
11
- rootDir
12
- });
13
- await build({
14
- alias,
15
- outDir,
16
- rootDir
17
- });
18
- } catch (error) {
19
- console.error("Build failed:", error);
20
- process.exit(1);
21
- }
22
- const cliArgs = process.argv.slice(2);
23
- const child = execa("node", [builtCliPath, ...cliArgs], {
1
+ import { execaCommand } from "execa";
2
+ process.on("SIGINT", () => process.exit(0));
3
+ process.on("SIGTERM", () => process.exit(1));
4
+ const args = process.argv.slice(2);
5
+ await execaCommand("nuxt dev", {
6
+ env: {
7
+ NUXT_CLI_ARGS: JSON.stringify(args),
8
+ NUXT_RUN_CLI: "1"
9
+ },
24
10
  stdio: "inherit"
25
- });
26
- child.on("exit", code => {
27
- process.exit(code || 0);
28
11
  });
package/dist/module.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export declare const CUSTOM_CLI_ERROR_MESSAGE = "Default export from server/cli.ts must be wrapped with defineCustomCli(...).";
1
2
  declare const _default: import("@nuxt/schema").NuxtModule<import("@nuxt/schema").ModuleOptions, import("@nuxt/schema").ModuleOptions, false>;
2
3
  export default _default;
package/dist/module.js CHANGED
@@ -1,19 +1,109 @@
1
1
  import pathLib from "node:path";
2
- import { defineNuxtModule } from "@nuxt/kit";
3
- import build from "./build.js";
2
+ import { addServerImports, addServerPlugin, addTemplate, defineNuxtModule } from "@nuxt/kit";
3
+ import endent from "endent";
4
+ export const CUSTOM_CLI_ERROR_MESSAGE = "Default export from server/cli.ts must be wrapped with defineCustomCli(...).";
5
+ const DEFINE_WRAPPER_USED_MARKER = "__defineCustomCliUsed";
6
+ const TEMPLATE_FOLDER = "nuxt-custom-cli";
4
7
  export default defineNuxtModule({
5
- setup: (_, nuxt) => {
6
- const globalState = globalThis;
7
- nuxt.hook("nitro:build:public-assets", async nitro => {
8
- if (globalState.__knowledgeOutputCliBuilt) {
9
- return;
10
- }
11
- globalState.__knowledgeOutputCliBuilt = true;
12
- const outputPath = pathLib.join(nitro.options.output.dir, "server");
13
- await build({
14
- alias: nuxt.options.alias,
15
- outDir: outputPath,
16
- rootDir: nuxt.options.rootDir
8
+ setup: (_options, nuxt) => {
9
+ const defineCustomCliTemplate = addTemplate({
10
+ filename: pathLib.join(TEMPLATE_FOLDER, "custom-cli-define.ts"),
11
+ getContents: () => endent`
12
+ export type CustomCliHandler = () => unknown;
13
+
14
+ export const defineCustomCli = (handler: CustomCliHandler) =>
15
+ Object.defineProperty(handler, '${DEFINE_WRAPPER_USED_MARKER}', {
16
+ configurable: false,
17
+ enumerable: false,
18
+ value: true,
19
+ writable: false,
20
+ });
21
+ `,
22
+ write: true
23
+ });
24
+ addServerImports({
25
+ as: "defineCustomCli",
26
+ from: defineCustomCliTemplate.dst,
27
+ name: "defineCustomCli"
28
+ });
29
+ if (nuxt.options.dev) {
30
+ const cliPath2 = pathLib.resolve(nuxt.options.rootDir, "server", "cli.ts");
31
+ const relativeCliPath2 = pathLib.relative(pathLib.join(nuxt.options.buildDir, TEMPLATE_FOLDER), cliPath2).replaceAll("\\", "/");
32
+ const cliImportPath2 = relativeCliPath2.startsWith(".") ? relativeCliPath2 : `./${relativeCliPath2}`;
33
+ const devPlugin = addTemplate({
34
+ filename: pathLib.join(TEMPLATE_FOLDER, "dev-plugin.ts"),
35
+ getContents: () => endent`
36
+ import main from '${cliImportPath2}';
37
+
38
+ const assertCustomCli = () => {
39
+ if (typeof main !== 'function' || !main['${DEFINE_WRAPPER_USED_MARKER}']) {
40
+ throw new Error('${CUSTOM_CLI_ERROR_MESSAGE}');
41
+ }
42
+ };
43
+
44
+ export default defineNitroPlugin(() => {
45
+ if (process.env.NUXT_RUN_CLI !== '1') {
46
+ return;
47
+ }
48
+
49
+ queueMicrotask(async () => {
50
+ try {
51
+ assertCustomCli();
52
+ const args = JSON.parse(process.env.NUXT_CLI_ARGS || '[]');
53
+ process.argv = [...process.argv, ...args];
54
+ await main();
55
+ process.kill(process.ppid, 'SIGINT');
56
+ } catch (error) {
57
+ console.error(error);
58
+ process.kill(process.ppid, 'SIGTERM');
59
+ }
60
+ });
61
+ });
62
+ `,
63
+ write: true
64
+ });
65
+ addServerPlugin(devPlugin.dst);
66
+ return;
67
+ }
68
+ const cliPath = pathLib.resolve(nuxt.options.rootDir, "server", "cli.ts");
69
+ const relativeCliPath = pathLib.relative(pathLib.join(nuxt.options.buildDir, TEMPLATE_FOLDER), cliPath).replaceAll("\\", "/");
70
+ const cliImportPath = relativeCliPath.startsWith(".") ? relativeCliPath : `./${relativeCliPath}`;
71
+ const entry = addTemplate({
72
+ filename: pathLib.join(TEMPLATE_FOLDER, "prod-entry.ts"),
73
+ getContents: () => endent`
74
+ import main from '${cliImportPath}';
75
+
76
+ const assertCustomCli = () => {
77
+ if (typeof main !== 'function' || !main['${DEFINE_WRAPPER_USED_MARKER}']) {
78
+ throw new Error('${CUSTOM_CLI_ERROR_MESSAGE}');
79
+ }
80
+ };
81
+
82
+ const run = async () => {
83
+ assertCustomCli();
84
+ await main();
85
+
86
+ if (process.listenerCount('SIGTERM') > 0) {
87
+ process.exit(0);
88
+ return;
89
+ }
90
+ }
91
+
92
+ run();
93
+ `,
94
+ write: true
95
+ });
96
+ nuxt.hook("nitro:init", nitro => {
97
+ nitro.hooks.hook("rollup:before", (_nitro, rollupConfig) => {
98
+ rollupConfig.input = typeof rollupConfig.input === "string" ? [rollupConfig.input, entry.dst] : Array.isArray(rollupConfig.input) ? [...rollupConfig.input, entry.dst] : {
99
+ ...rollupConfig.input,
100
+ cli: entry.dst
101
+ };
102
+ rollupConfig.output.entryFileNames = chunkInfo => {
103
+ if (chunkInfo.facadeModuleId === entry.dst) return "cli.mjs";
104
+ if (chunkInfo.name === "node-server") return "index.mjs";
105
+ return "[name].mjs";
106
+ };
17
107
  });
18
108
  });
19
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dword-design/nuxt-custom-cli",
3
- "version": "0.1.0",
3
+ "version": "2.0.0",
4
4
  "repository": "dword-design/nuxt-custom-cli",
5
5
  "funding": "https://github.com/sponsors/dword-design",
6
6
  "license": "MIT",
@@ -31,8 +31,8 @@
31
31
  },
32
32
  "dependencies": {
33
33
  "@nuxt/kit": "^4.3.1",
34
- "execa": "^9.6.1",
35
- "fs-extra": "^11.3.3"
34
+ "endent": "npm:@dword-design/endent@^1.4.7",
35
+ "execa": "^9.6.1"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@dword-design/base": "^16.2.3",
@@ -40,14 +40,10 @@
40
40
  "@playwright/test": "^1.58.2",
41
41
  "@types/fs-extra": "^11.0.4",
42
42
  "@types/node": "^25.3.2",
43
+ "fs-extra": "^11.3.3",
43
44
  "nuxt": "^4.3.1",
44
45
  "output-files": "^4.0.0",
45
- "unimport": "^6.0.0",
46
- "vite": "^7.3.1"
47
- },
48
- "peerDependencies": {
49
- "unimport": "*",
50
- "vite": "*"
46
+ "strip-ansi": "^7.2.0"
51
47
  },
52
48
  "packageManager": "pnpm@10.30.3+sha512.c961d1e0a2d8e354ecaa5166b822516668b7f44cb5bd95122d590dd81922f606f5473b6d23ec4a5be05e7fcd18e8488d47d978bbe981872f1145d06e9a740017",
53
49
  "engines": {
package/dist/build.d.ts DELETED
@@ -1,6 +0,0 @@
1
- declare const _default: ({ alias, outDir, rootDir, }: {
2
- alias: Record<string, string>;
3
- outDir: string;
4
- rootDir: string;
5
- }) => Promise<import("rollup").RollupOutput | import("rollup").RollupOutput[] | import("rollup").RollupWatcher>;
6
- export default _default;
package/dist/build.js DELETED
@@ -1,33 +0,0 @@
1
- import pathLib from "node:path";
2
- import Unimport from "unimport/unplugin";
3
- import { build } from "vite";
4
- export default ({
5
- alias,
6
- outDir,
7
- rootDir
8
- }) => build({
9
- build: {
10
- emptyOutDir: false,
11
- lib: {
12
- entry: pathLib.join(rootDir, "server", "cli.ts"),
13
- fileName: "cli",
14
- formats: ["es"]
15
- },
16
- outDir,
17
- rollupOptions: {
18
- external: [/node_modules/],
19
- output: {
20
- entryFileNames: "cli.mjs"
21
- }
22
- },
23
- ssr: true,
24
- target: "node22"
25
- },
26
- plugins: [Unimport.vite({
27
- dirs: [pathLib.join(rootDir, "server/utils/**/*.ts")],
28
- dts: false
29
- })],
30
- resolve: {
31
- alias
32
- }
33
- });
@@ -1,6 +0,0 @@
1
- export declare const getAliasFromNuxtTsConfig: ({ rootDir, tsconfigPath, }: {
2
- rootDir: string;
3
- tsconfigPath?: string;
4
- }) => Promise<{
5
- [k: string]: any;
6
- }>;
@@ -1,14 +0,0 @@
1
- import pathLib from "node:path";
2
- import fs from "fs-extra";
3
- export const getAliasFromNuxtTsConfig = async ({
4
- rootDir,
5
- tsconfigPath = ".nuxt/tsconfig.server.json"
6
- }) => {
7
- const tsconfig = JSON.parse(await fs.readFile(pathLib.join(rootDir, tsconfigPath), "utf8"));
8
- const paths = tsconfig.compilerOptions?.paths ?? {};
9
- return Object.fromEntries(Object.entries(paths).filter(([key]) => key.endsWith("/*")).map(([key, value]) => {
10
- const aliasKey = key.slice(0, -2);
11
- const aliasValue = value[0].replace(/^\.\.\//, "").slice(0, -2);
12
- return [aliasKey, pathLib.join(rootDir, aliasValue)];
13
- }));
14
- };