forked from denoland/fresh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.ts
180 lines (152 loc) · 5.94 KB
/
update.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { join, Node, parse, Project, resolve } from "./src/dev/deps.ts";
import { error } from "./src/dev/error.ts";
import { freshImports, twindImports } from "./src/dev/imports.ts";
import { collect, ensureMinDenoVersion, generate } from "./src/dev/mod.ts";
ensureMinDenoVersion();
const help = `fresh-update
Update a Fresh project. This updates dependencies and optionally performs code
mods to update a project's source code to the latest recommended patterns.
To upgrade a projecct in the current directory, run:
fresh-update .
USAGE:
fresh-update <DIRECTORY>
`;
const flags = parse(Deno.args, {});
if (flags._.length !== 1) {
error(help);
}
const unresolvedDirectory = Deno.args[0];
const resolvedDirectory = resolve(unresolvedDirectory);
// Update dependencies in the import map.
const IMPORT_MAP_PATH = join(resolvedDirectory, "import_map.json");
let importMapText = await Deno.readTextFile(IMPORT_MAP_PATH);
const importMap = JSON.parse(importMapText);
freshImports(importMap.imports);
if (importMap.imports["twind"]) {
twindImports(importMap.imports);
}
importMapText = JSON.stringify(importMap, null, 2);
await Deno.writeTextFile(IMPORT_MAP_PATH, importMapText);
// Code mod for classic JSX -> automatic JSX.
const JSX_CODEMOD =
`This project is using the classic JSX transform. Would you like to update to the
automatic JSX transform? This will remove the /** @jsx h */ pragma from your
source code and add the jsx: "react-jsx" compiler option to your deno.json file.`;
const DENO_JSON_PATH = join(resolvedDirectory, "deno.json");
let denoJsonText = await Deno.readTextFile(DENO_JSON_PATH);
const denoJson = JSON.parse(denoJsonText);
if (denoJson.compilerOptions?.jsx !== "react-jsx" && confirm(JSX_CODEMOD)) {
console.log("Updating config file...");
denoJson.compilerOptions = denoJson.compilerOptions || {};
denoJson.compilerOptions.jsx = "react-jsx";
denoJson.compilerOptions.jsxImportSource = "preact";
denoJsonText = JSON.stringify(denoJson, null, 2);
await Deno.writeTextFile(DENO_JSON_PATH, denoJsonText);
const project = new Project();
const sfs = project.addSourceFilesAtPaths(
join(resolvedDirectory, "**", "*.{js,jsx,ts,tsx}"),
);
for (const sf of sfs) {
for (const d of sf.getImportDeclarations()) {
if (d.getModuleSpecifierValue() !== "preact") continue;
for (const n of d.getNamedImports()) {
const name = n.getName();
if (name === "h" || name === "Fragment") n.remove();
}
if (
d.getNamedImports().length === 0 &&
d.getNamespaceImport() === undefined &&
d.getDefaultImport() === undefined
) {
d.remove();
}
}
let text = sf.getFullText();
text = text.replaceAll("/** @jsx h */\n", "");
text = text.replaceAll("/** @jsxFrag Fragment */\n", "");
sf.replaceWithText(text);
await sf.save();
}
}
// Code mod for class={tw`border`} to class="border".
const TWIND_CODEMOD =
`This project is using an old version of the twind integration. Would you like to
update to the new twind plugin? This will remove the 'class={tw\`border\`}'
boilerplate from your source code replace it with the simpler 'class="border"'.`;
if (importMap.imports["@twind"] && confirm(TWIND_CODEMOD)) {
await Deno.remove(join(resolvedDirectory, importMap.imports["@twind"]));
delete importMap.imports["@twind"];
importMapText = JSON.stringify(importMap, null, 2);
await Deno.writeTextFile(IMPORT_MAP_PATH, importMapText);
const MAIN_TS = `/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts";
import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";
await start(manifest, { plugins: [twindPlugin(twindConfig)] });\n`;
const MAIN_TS_PATH = join(resolvedDirectory, "main.ts");
await Deno.writeTextFile(MAIN_TS_PATH, MAIN_TS);
const TWIND_CONFIG_TS = `import { Options } from "$fresh/plugins/twind.ts";
export default {
selfURL: import.meta.url,
} as Options;
`;
await Deno.writeTextFile(
join(resolvedDirectory, "twind.config.ts"),
TWIND_CONFIG_TS,
);
const project = new Project();
const sfs = project.addSourceFilesAtPaths(
join(resolvedDirectory, "**", "*.{js,jsx,ts,tsx}"),
);
for (const sf of sfs) {
const nodes = sf.forEachDescendantAsArray();
for (const n of nodes) {
if (!n.wasForgotten() && Node.isJsxAttribute(n)) {
const init = n.getInitializer();
const name = n.getName();
if (
Node.isJsxExpression(init) &&
(name === "class" || name === "className")
) {
const expr = init.getExpression();
if (Node.isTaggedTemplateExpression(expr)) {
const tag = expr.getTag();
if (Node.isIdentifier(tag) && tag.getText() === "tw") {
const template = expr.getTemplate();
if (Node.isNoSubstitutionTemplateLiteral(template)) {
n.setInitializer(`"${template.getLiteralValue()}"`);
}
}
} else if (expr?.getFullText() === `tw(props.class ?? "")`) {
n.setInitializer(`{props.class}`);
}
}
}
}
const text = sf.getFullText();
const removeTw = [...text.matchAll(/tw[,\s`(]/g)].length === 1;
for (const d of sf.getImportDeclarations()) {
if (d.getModuleSpecifierValue() !== "@twind") continue;
for (const n of d.getNamedImports()) {
const name = n.getName();
if (name === "tw" && removeTw) n.remove();
}
d.setModuleSpecifier("twind");
if (
d.getNamedImports().length === 0 &&
d.getNamespaceImport() === undefined &&
d.getDefaultImport() === undefined
) {
d.remove();
}
}
await sf.save();
}
}
const manifest = await collect(resolvedDirectory);
await generate(resolvedDirectory, manifest);