@arcgis/codemod 5.0.0-next.162
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.md +17 -0
- package/README.md +78 -0
- package/dist/cli-DB4-Ai2J.js +256 -0
- package/dist/cli.js +6 -0
- package/dist/codemod-ZSjsrLkq.js +6077 -0
- package/dist/codemod-mjvq6uf5.js +156 -0
- package/dist/useProjectInstance-CK5Fhtu-.js +398 -0
- package/package.json +24 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Licensing
|
|
2
|
+
|
|
3
|
+
COPYRIGHT © Esri
|
|
4
|
+
|
|
5
|
+
All rights reserved under the copyright laws of the United States and applicable international laws, treaties, and conventions.
|
|
6
|
+
|
|
7
|
+
This material is licensed for use under the [Esri Master License Agreement (MLA)](https://www.esri.com/content/dam/esrisites/en-us/media/legal/ma-full/ma-full.pdf), and is bound by the terms of that agreement.
|
|
8
|
+
You may redistribute and use this code without modification, provided you adhere to the terms of the MLA and include this copyright notice.
|
|
9
|
+
|
|
10
|
+
For additional information, contact:
|
|
11
|
+
Environmental Systems Research Institute, Inc.
|
|
12
|
+
Attn: Contracts and Legal Services Department
|
|
13
|
+
380 New York Street
|
|
14
|
+
Redlands, California, USA 92373
|
|
15
|
+
USA
|
|
16
|
+
|
|
17
|
+
email: legal@esri.com
|
package/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# @arcgis/codemod
|
|
2
|
+
|
|
3
|
+
A codemod toolkit for updating and modernizing ArcGIS Maps SDK for JavaScript codebases.
|
|
4
|
+
|
|
5
|
+
This package provides automated refactors to help migrate away from deprecated or legacy patterns. Starting with removing the `__esri` namespace in TypeScript projects and replacing it with explicit imports.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Automated refactor to remove `__esri` namespace usage
|
|
10
|
+
- Converts global namespace types to explicit `@arcgis/core` imports
|
|
11
|
+
- Detects edge cases and warns when changes can’t be safely automated
|
|
12
|
+
- Respects `.gitignore` by default
|
|
13
|
+
|
|
14
|
+
## Requirements
|
|
15
|
+
|
|
16
|
+
- Node.js 24
|
|
17
|
+
- A project using the ArcGIS Maps SDK for JavaScript
|
|
18
|
+
- This codemod operates exclusively on TypeScript files. Do not need to run this tool, if the project does not use TypeScript.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
The codemod replaces `__esri` types with explicit imports from @arcgis/core.
|
|
23
|
+
|
|
24
|
+
> Note: Before running the codemod, update all `@arcgis/*` packages in your `package.json` to the most recent version.
|
|
25
|
+
|
|
26
|
+
Run it in the root of your project:
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
npx @arcgis/codemod run refactor-out-esri-namespace
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
By default, the codemod runs on all TypeScript files in the current directory and subdirectories, excluding files ignored by your `.gitignore`.
|
|
33
|
+
You can manually run it on a different directory by providing an argument:
|
|
34
|
+
|
|
35
|
+
```sh
|
|
36
|
+
npx @arcgis/codemod run refactor-out-esri-namespace target-directory
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### What codemod does
|
|
40
|
+
|
|
41
|
+
**Before:**
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
let geometry: __esri.Geometry;
|
|
45
|
+
let featureSet: __esri.FeatureSet;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**After:**
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import type Geometry from "@arcgis/core/geometry/Geometry";
|
|
52
|
+
import type FeatureSet from "@arcgis/core/rest/support/FeatureSet";
|
|
53
|
+
|
|
54
|
+
let geometry: Geometry;
|
|
55
|
+
|
|
56
|
+
let featureSet: FeatureSet;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Warnings (Manual review required)
|
|
60
|
+
|
|
61
|
+
The codemod will emit warnings for cases it cannot safely rewrite, including:
|
|
62
|
+
|
|
63
|
+
- `__esri` references inside JSDoc comments
|
|
64
|
+
- Complex computed or dynamic type usage
|
|
65
|
+
- Unresolvable symbols
|
|
66
|
+
|
|
67
|
+
These will be reported in the console, manually review all changes before committing.
|
|
68
|
+
If your project uses **ESLint, Prettier, or other formatters/linters**, run them after the codemod to ensure consistent formatting.
|
|
69
|
+
|
|
70
|
+
> Note: This codemod modifies files in place and does not provide an interactive dry-run mode. We recommend running it in a project that is under version control (Git) so you can review changes using `git diff` or your IDE’s diff tools. If your project is not under version control, create a backup before running the codemod.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
COPYRIGHT Esri
|
|
75
|
+
|
|
76
|
+
This package is licensed under the terms described in the `LICENSE.md` file, located in the root of the package, and at https://js.arcgis.com/5.0/LICENSE.txt.
|
|
77
|
+
|
|
78
|
+
For third party notices, see https://js.arcgis.com/5.0/third-party-notices.txt.
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { log as h } from "@arcgis/toolkit/log";
|
|
2
|
+
import { Command as $ } from "@commander-js/extra-typings";
|
|
3
|
+
import { statSync as K } from "node:fs";
|
|
4
|
+
import { getCwd as I, path as e, existsAsync as U, gitIgnoreFileToGlobs as L, asyncFindPath as E, toPosixPathSeparators as N, asyncRetrievePackageJson as J } from "@arcgis/components-build-utils";
|
|
5
|
+
import "ts-morph";
|
|
6
|
+
import i from "typescript";
|
|
7
|
+
import { glob as _ } from "tinyglobby";
|
|
8
|
+
import { identity as W } from "@arcgis/toolkit/function";
|
|
9
|
+
import { styleText as B } from "node:util";
|
|
10
|
+
const q = (t, o, n) => {
|
|
11
|
+
const r = t[o];
|
|
12
|
+
return r ? typeof r == "function" ? r() : Promise.resolve(r) : new Promise((a, c) => {
|
|
13
|
+
(typeof queueMicrotask == "function" ? queueMicrotask : setTimeout)(c.bind(null, /* @__PURE__ */ new Error("Unknown variable dynamic import: " + o + (o.split("/").length !== n ? ". Note that variables only represent file names one level deep." : ""))));
|
|
14
|
+
});
|
|
15
|
+
}, ge = {
|
|
16
|
+
allowImportingTsExtensions: !0,
|
|
17
|
+
allowJs: !0,
|
|
18
|
+
allowArbitraryExtensions: !0,
|
|
19
|
+
allowSyntheticDefaultImports: !0,
|
|
20
|
+
checkJs: !0,
|
|
21
|
+
experimentalDecorators: !0,
|
|
22
|
+
forceConsistentCasingInFileNames: !1,
|
|
23
|
+
isolatedModules: !1,
|
|
24
|
+
isolatedDeclarations: !1,
|
|
25
|
+
jsx: i.JsxEmit.Preserve,
|
|
26
|
+
lib: ["lib.esnext.d.ts", "lib.dom.d.ts"],
|
|
27
|
+
module: i.ModuleKind.ESNext,
|
|
28
|
+
moduleResolution: i.ModuleResolutionKind.Bundler,
|
|
29
|
+
moduleDetection: i.ModuleDetectionKind.Auto,
|
|
30
|
+
strict: !1,
|
|
31
|
+
target: i.ScriptTarget.ESNext,
|
|
32
|
+
resolveJsonModule: !0,
|
|
33
|
+
esModuleInterop: !0
|
|
34
|
+
}, j = {
|
|
35
|
+
codemod: void 0,
|
|
36
|
+
codemodName: void 0,
|
|
37
|
+
projectsData: void 0,
|
|
38
|
+
project: void 0
|
|
39
|
+
};
|
|
40
|
+
async function z() {
|
|
41
|
+
const { ignorePatterns: t, globRoot: o } = await H(), n = await Q(o);
|
|
42
|
+
return t.push(...n), { ignorePatterns: t, globRoot: o };
|
|
43
|
+
}
|
|
44
|
+
async function H() {
|
|
45
|
+
const t = [];
|
|
46
|
+
let o, n = I(), r = e.dirname(n);
|
|
47
|
+
do {
|
|
48
|
+
o = r;
|
|
49
|
+
const a = e.join(o, ".gitignore");
|
|
50
|
+
if (await U(a)) {
|
|
51
|
+
n = e.dirname(a);
|
|
52
|
+
const w = e.relative(n, I()).split("/"), y = L(a);
|
|
53
|
+
for (const f of y) {
|
|
54
|
+
const m = (f.startsWith("!") ? f.slice(1) : f).split("*")[0].split("/");
|
|
55
|
+
let p = !0;
|
|
56
|
+
for (let g = 0; g < m.length; g++)
|
|
57
|
+
if (w[g] !== m[g] && m[g] !== "") {
|
|
58
|
+
p = !1;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
p && (f.startsWith("!") || t.push(f));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
r = e.dirname(o);
|
|
65
|
+
} while (r !== o);
|
|
66
|
+
return { ignorePatterns: t, globRoot: n };
|
|
67
|
+
}
|
|
68
|
+
async function Q(t) {
|
|
69
|
+
return (await _("**/.gitignore", {
|
|
70
|
+
// Gitignore files are unlikely to be symbolic links
|
|
71
|
+
followSymbolicLinks: !1,
|
|
72
|
+
// Don't look for ignore files in these places for speed:
|
|
73
|
+
ignore: [
|
|
74
|
+
// Hardcoded ignores in TypeScript
|
|
75
|
+
"**/node_modules/**",
|
|
76
|
+
"**/bower_components/**",
|
|
77
|
+
"**/jspm_packages/**",
|
|
78
|
+
// Default build output folder in bundlers
|
|
79
|
+
"**/dist/**"
|
|
80
|
+
],
|
|
81
|
+
// Not applicable for our glob pattern
|
|
82
|
+
expandDirectories: !1
|
|
83
|
+
})).flatMap((n) => {
|
|
84
|
+
const r = e.relative(t, e.dirname(n)), a = L(n), c = [];
|
|
85
|
+
for (const w of a)
|
|
86
|
+
w.startsWith("!") || c.push(e.join(r, w));
|
|
87
|
+
return c;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
const V = ["js", "jsx", "mjs", "cjs"], X = ["ts", "tsx", "mts", "cts"], Y = ["json"], Z = `**/*.{${[...V, ...X, ...Y].join(",")}}`;
|
|
91
|
+
async function ee() {
|
|
92
|
+
const t = await z(), o = e.join(process.cwd(), Z), n = await _(o, {
|
|
93
|
+
cwd: t.globRoot,
|
|
94
|
+
ignore: t.ignorePatterns,
|
|
95
|
+
// The files we are looking for are unlikely to be symbolic links
|
|
96
|
+
followSymbolicLinks: !1,
|
|
97
|
+
// Not applicable for our glob pattern
|
|
98
|
+
expandDirectories: !1
|
|
99
|
+
// Files and folders starting with . are excluded (matches TypeScript's
|
|
100
|
+
// default behavior). If any tsconfig.json explicitly includes such files,
|
|
101
|
+
// they will still be codemodded.
|
|
102
|
+
});
|
|
103
|
+
let r = e.relative(t.globRoot, process.cwd());
|
|
104
|
+
r !== "" && (r += "/");
|
|
105
|
+
const a = e.join(e.resolve(t.globRoot), "/"), c = a.length + r.length, w = [], y = [], f = /* @__PURE__ */ new Set();
|
|
106
|
+
for (const s of n) {
|
|
107
|
+
const l = a + s;
|
|
108
|
+
if (s.endsWith(".json")) {
|
|
109
|
+
const d = e.basename(s);
|
|
110
|
+
d === "package.json" ? w.push(l) : (d.startsWith("tsconfig.") || d.startsWith("jsconfig.")) && y.push(l);
|
|
111
|
+
} else {
|
|
112
|
+
const d = s.slice(r.length);
|
|
113
|
+
f.add(d);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let v = await E("tsconfig.json", e.dirname(process.cwd()));
|
|
117
|
+
v !== void 0 && (v = N(v), y.push(v)), y.sort((s, l) => {
|
|
118
|
+
const d = l.split("/").length - s.split("/").length;
|
|
119
|
+
if (d !== 0)
|
|
120
|
+
return d;
|
|
121
|
+
const b = e.basename(s), u = e.basename(l);
|
|
122
|
+
return b === u ? 0 : b.endsWith("tsconfig.json") ? -1 : u.endsWith("tsconfig.json") ? 1 : b.endsWith("tsconfig.build.json") ? -1 : u.endsWith("tsconfig.build.json") ? 1 : b.localeCompare(u);
|
|
123
|
+
});
|
|
124
|
+
let m = await E("package.json");
|
|
125
|
+
m !== void 0 && (m = N(m));
|
|
126
|
+
const p = [], g = /* @__PURE__ */ new Set();
|
|
127
|
+
return await Promise.all(
|
|
128
|
+
y.map(async (s) => {
|
|
129
|
+
const l = e.relative(process.cwd(), s), d = i.readConfigFile(s, i.sys.readFile);
|
|
130
|
+
if (d.error !== void 0) {
|
|
131
|
+
h(
|
|
132
|
+
"error",
|
|
133
|
+
l,
|
|
134
|
+
i.formatDiagnosticsWithColorAndContext([d.error], {
|
|
135
|
+
getCanonicalFileName: W,
|
|
136
|
+
getCurrentDirectory: i.sys.getCurrentDirectory,
|
|
137
|
+
getNewLine: () => i.sys.newLine
|
|
138
|
+
})
|
|
139
|
+
);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const b = e.basename(l), u = i.parseJsonConfigFileContent(
|
|
143
|
+
d.config,
|
|
144
|
+
i.sys,
|
|
145
|
+
e.dirname(s),
|
|
146
|
+
void 0,
|
|
147
|
+
b
|
|
148
|
+
), T = 18003;
|
|
149
|
+
u.errors.filter((P) => P.code !== T).length > 0 && h(
|
|
150
|
+
"warn",
|
|
151
|
+
l,
|
|
152
|
+
i.formatDiagnosticsWithColorAndContext(u.errors, {
|
|
153
|
+
getCanonicalFileName: W,
|
|
154
|
+
getCurrentDirectory: i.sys.getCurrentDirectory,
|
|
155
|
+
getNewLine: () => i.sys.newLine
|
|
156
|
+
})
|
|
157
|
+
);
|
|
158
|
+
const F = s === v, A = l === b, k = u.fileNames.filter((P) => {
|
|
159
|
+
const D = P.slice(c);
|
|
160
|
+
return f.delete(D) ? (g.add(D), !0) : g.has(D) || D.endsWith(".json") || P.includes("node_modules") ? !1 : !F;
|
|
161
|
+
});
|
|
162
|
+
if (k.length === 0)
|
|
163
|
+
return;
|
|
164
|
+
y.length === 1 && (F || A) && f.clear();
|
|
165
|
+
const R = e.dirname(s);
|
|
166
|
+
let C = R, x = m;
|
|
167
|
+
do {
|
|
168
|
+
const P = e.join(C, "package.json");
|
|
169
|
+
if (w.includes(P)) {
|
|
170
|
+
x = P;
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
} while (C !== t.globRoot && (C = e.dirname(C)));
|
|
174
|
+
const G = x === void 0 ? void 0 : { filePath: x, contents: await J(e.dirname(x)) }, O = {
|
|
175
|
+
typeScriptConfig: {
|
|
176
|
+
filePath: s,
|
|
177
|
+
compilerOptions: u.options
|
|
178
|
+
},
|
|
179
|
+
packageJson: G,
|
|
180
|
+
cwd: R,
|
|
181
|
+
filePaths: k
|
|
182
|
+
};
|
|
183
|
+
p.push(O);
|
|
184
|
+
})
|
|
185
|
+
), f.size > 0 && p.push({
|
|
186
|
+
typeScriptConfig: void 0,
|
|
187
|
+
packageJson: m === void 0 ? void 0 : {
|
|
188
|
+
filePath: m,
|
|
189
|
+
contents: await J(e.dirname(m))
|
|
190
|
+
},
|
|
191
|
+
cwd: process.cwd(),
|
|
192
|
+
filePaths: Array.from(f).map((s) => e.join(process.cwd(), s))
|
|
193
|
+
}), p.sort((s, l) => s.cwd.localeCompare(l.cwd)), j.projectsData = p, p;
|
|
194
|
+
}
|
|
195
|
+
let M = !1;
|
|
196
|
+
function te() {
|
|
197
|
+
M || (M = !0, process.exitCode !== void 0 && process.exitCode !== 0 ? h(
|
|
198
|
+
"warn",
|
|
199
|
+
j.codemodName,
|
|
200
|
+
"One or more exceptions occurred during the codemod run. Please review the output above."
|
|
201
|
+
) : h(
|
|
202
|
+
"info",
|
|
203
|
+
j.codemodName,
|
|
204
|
+
`${B("green", "Codemod completed successfully.")} Recommended next steps:
|
|
205
|
+
- Review the codemod changes.
|
|
206
|
+
- If your project uses ESLint, run ESLint autofix on the changed files.
|
|
207
|
+
- If your project uses Prettier, run Prettier on the changed files.
|
|
208
|
+
- If your project uses TypeScript, run type checking to ensure there are no type errors.`
|
|
209
|
+
));
|
|
210
|
+
}
|
|
211
|
+
async function oe(t, o) {
|
|
212
|
+
j.codemod = t, j.codemodName = o;
|
|
213
|
+
const n = await ee();
|
|
214
|
+
if (n.length === 0) {
|
|
215
|
+
h("error", o, `Failed to find any .js or .ts files in ${process.cwd()} to run the codemod on.`), process.exitCode = 1;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
await t.run(n), te();
|
|
219
|
+
}
|
|
220
|
+
const S = new $();
|
|
221
|
+
S.name("@arcgis/codemod").description("Codemod for migrating or refactoring usages of ArcGIS Maps SDK for JavaScript");
|
|
222
|
+
S.command("run").description("Run a single codemod").argument("<codemodName>", "Name of the transform").argument("[path]", "Directory to transform", process.cwd()).action(ne);
|
|
223
|
+
async function ne(t, o) {
|
|
224
|
+
const n = K(o, { throwIfNoEntry: !1 });
|
|
225
|
+
if (n === void 0) {
|
|
226
|
+
h("error", "@arcgis/codemod", `Failed to find the target path: ${o}`), process.exitCode = 1;
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const r = n.isFile();
|
|
230
|
+
process.chdir(r ? e.dirname(o) : o);
|
|
231
|
+
let a;
|
|
232
|
+
try {
|
|
233
|
+
a = await q(/* @__PURE__ */ Object.assign({ "./codemods/arcgis-toolkit/codemod.ts": () => import("./codemod-mjvq6uf5.js"), "./codemods/refactor-out-esri-namespace/codemod.ts": () => import("./codemod-ZSjsrLkq.js") }), `./codemods/${t}/codemod.ts`, 4);
|
|
234
|
+
} catch (c) {
|
|
235
|
+
if (
|
|
236
|
+
// direct Node execution
|
|
237
|
+
typeof c == "object" && c !== null && "code" in c && c.code === "ERR_MODULE_NOT_FOUND" || // Vite build execution
|
|
238
|
+
String(c).includes("Unknown variable dynamic import:")
|
|
239
|
+
) {
|
|
240
|
+
h("error", "@arcgis/codemod", `Can't find codemod by name "${t}".`), process.exitCode = 1;
|
|
241
|
+
return;
|
|
242
|
+
} else
|
|
243
|
+
throw c;
|
|
244
|
+
}
|
|
245
|
+
if (typeof a.codemod != "object" || a.codemod === null) {
|
|
246
|
+
h("error", "@arcgis/codemod", `Can't find codemod by name "${t}".`), process.exitCode = 1;
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
await oe(a.codemod, t);
|
|
250
|
+
}
|
|
251
|
+
S.parse();
|
|
252
|
+
export {
|
|
253
|
+
ge as d,
|
|
254
|
+
j as g,
|
|
255
|
+
te as p
|
|
256
|
+
};
|