@adonisjs/core 7.5.2 → 7.6.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.
|
@@ -9,8 +9,9 @@ import { type GeneratedStub } from '../../types/app.ts';
|
|
|
9
9
|
* This class provides APIs to modify configuration files, register middleware,
|
|
10
10
|
* generate stubs, and install packages.
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
* Some codemods, including installPackages, rely on the optional
|
|
13
|
+
* "@adonisjs/assembler" package. defineEnvVariables, makeUsingStub,
|
|
14
|
+
* addImportAlias, and createDirectory work without it.
|
|
14
15
|
*
|
|
15
16
|
* @example
|
|
16
17
|
* ```ts
|
|
@@ -67,6 +68,26 @@ export declare class Codemods extends EventEmitter {
|
|
|
67
68
|
defineEnvVariables<T extends Record<string, number | string | boolean>>(environmentVariables: T, options?: {
|
|
68
69
|
omitFromExample?: Array<keyof T>;
|
|
69
70
|
}): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Add a subpath import to the application's package.json. Existing aliases
|
|
73
|
+
* are left unchanged, even when their target differs.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* await codemods.addImportAlias('#channels/*', './app/channels/*.js')
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
addImportAlias(alias: string, target: string): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* Create a directory relative to the application root, including missing parents.
|
|
83
|
+
* Leave an existing directory unchanged.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* await codemods.createDirectory('app/channels')
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
createDirectory(directory: string): Promise<void>;
|
|
70
91
|
/**
|
|
71
92
|
* Returns the TsMorph project instance for advanced AST manipulations.
|
|
72
93
|
* See https://ts-morph.com/ for documentation.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { t as debug_default } from "../../debug-DrrWgeSu.js";
|
|
2
2
|
import { t as stringHelpers } from "../../string-DUpLZV_t.js";
|
|
3
3
|
import { isAbsolute } from "node:path";
|
|
4
|
-
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
5
5
|
import { EnvEditor } from "@adonisjs/env/editor";
|
|
6
6
|
import { EventEmitter } from "node:events";
|
|
7
7
|
//#region modules/ace/codemods.ts
|
|
@@ -10,8 +10,9 @@ import { EventEmitter } from "node:events";
|
|
|
10
10
|
* This class provides APIs to modify configuration files, register middleware,
|
|
11
11
|
* generate stubs, and install packages.
|
|
12
12
|
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* Some codemods, including installPackages, rely on the optional
|
|
14
|
+
* "@adonisjs/assembler" package. defineEnvVariables, makeUsingStub,
|
|
15
|
+
* addImportAlias, and createDirectory work without it.
|
|
15
16
|
*
|
|
16
17
|
* @example
|
|
17
18
|
* ```ts
|
|
@@ -124,6 +125,57 @@ var Codemods = class extends EventEmitter {
|
|
|
124
125
|
this.#cliLogger.action("update .env file").succeeded();
|
|
125
126
|
}
|
|
126
127
|
/**
|
|
128
|
+
* Add a subpath import to the application's package.json. Existing aliases
|
|
129
|
+
* are left unchanged, even when their target differs.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* await codemods.addImportAlias('#channels/*', './app/channels/*.js')
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
async addImportAlias(alias, target) {
|
|
137
|
+
const action = this.#cliLogger.action("update package.json file");
|
|
138
|
+
try {
|
|
139
|
+
const path = this.#app.makePath("package.json");
|
|
140
|
+
const contents = await readFile(path, "utf-8");
|
|
141
|
+
const bom = contents.startsWith("") ? "" : "";
|
|
142
|
+
const packageJson = JSON.parse(contents.slice(bom.length));
|
|
143
|
+
packageJson.imports ??= {};
|
|
144
|
+
if (Object.hasOwn(packageJson.imports, alias)) {
|
|
145
|
+
action.skipped(`import alias "${alias}" already exists`);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
packageJson.imports[alias] = target;
|
|
149
|
+
const indent = contents.match(/^([ \t]+)"[^"\n]+"\s*:/m)?.[1] ?? (contents.includes("\n") ? " " : "");
|
|
150
|
+
const newline = contents.includes("\r\n") ? "\r\n" : "\n";
|
|
151
|
+
const updated = JSON.stringify(packageJson, null, indent).replaceAll("\n", newline);
|
|
152
|
+
await writeFile(path, bom + updated + (contents.endsWith("\n") ? newline : ""));
|
|
153
|
+
action.succeeded();
|
|
154
|
+
} catch (error) {
|
|
155
|
+
this.emit("error", error);
|
|
156
|
+
action.failed(error.message);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Create a directory relative to the application root, including missing parents.
|
|
161
|
+
* Leave an existing directory unchanged.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* await codemods.createDirectory('app/channels')
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
async createDirectory(directory) {
|
|
169
|
+
const action = this.#cliLogger.action(`create ${directory} directory`);
|
|
170
|
+
try {
|
|
171
|
+
if (await mkdir(this.#app.makePath(directory), { recursive: true })) action.succeeded();
|
|
172
|
+
else action.skipped("directory already exists");
|
|
173
|
+
} catch (error) {
|
|
174
|
+
this.emit("error", error);
|
|
175
|
+
action.failed(error.message);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
127
179
|
* Returns the TsMorph project instance for advanced AST manipulations.
|
|
128
180
|
* See https://ts-morph.com/ for documentation.
|
|
129
181
|
*
|