wovn-nextjs 0.0.21 → 0.0.22
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/bin/wovn-nextjs +31 -7
- package/package.json +1 -1
package/bin/wovn-nextjs
CHANGED
|
@@ -31,7 +31,8 @@ async function main() {
|
|
|
31
31
|
if (command === 'build' && context.projectToken && context.hostname) {
|
|
32
32
|
context.wovnJsData = await httpsRequest(`https://data.wovn.io/js_data/json/1/${context.projectToken}/?u=http%3A%2F%2F${context.hostname}`).then(body => JSON.parse(body.toString()))
|
|
33
33
|
await overwriteTsxAndJsxFiles(context, undo) // Make sure to collect translatable texts
|
|
34
|
-
await overwriteTsxAndJsxFiles(context, transform)
|
|
34
|
+
await overwriteTsxAndJsxFiles(context, transform)
|
|
35
|
+
await reportToWovn(context)
|
|
35
36
|
await overwriteInitializationProcess(context)
|
|
36
37
|
await createMiddlewareFile(context)
|
|
37
38
|
if (process.env.WOVN_DEBUG) {
|
|
@@ -39,6 +40,8 @@ async function main() {
|
|
|
39
40
|
}
|
|
40
41
|
} else if (command === 'build-config' && context.projectToken && context.hostname) {
|
|
41
42
|
context.wovnJsData = await httpsRequest(`https://data.wovn.io/js_data/json/1/${context.projectToken}/?u=http%3A%2F%2F${context.hostname}`).then(body => JSON.parse(body.toString()))
|
|
43
|
+
await walkTsxAndJsx(context, transform)
|
|
44
|
+
await reportToWovn(context)
|
|
42
45
|
await overwriteInitializationProcess(context)
|
|
43
46
|
await createMiddlewareFile(context)
|
|
44
47
|
if (process.env.WOVN_DEBUG) {
|
|
@@ -212,6 +215,10 @@ function transform(context, source) {
|
|
|
212
215
|
code = code.replaceAll(/^(import { *useRouter *} from ["'`]next\/navigation.*)/mg, 'import { useAppRouter as useRouter } from "wovn-nextjs" //$1 wovn-nextjs-command-comment')
|
|
213
216
|
code = code.match(/^['"]use /) ? code.replace(/\n/, '\n' + importer) : importer + code
|
|
214
217
|
}
|
|
218
|
+
for (const text of getTextsFromTokens(tokens)) {
|
|
219
|
+
context.uvs.set(text, -1)
|
|
220
|
+
component.uvs.add(text)
|
|
221
|
+
}
|
|
215
222
|
context.components.push(component)
|
|
216
223
|
return code
|
|
217
224
|
}
|
|
@@ -225,6 +232,14 @@ function undo(context, code) {
|
|
|
225
232
|
return code
|
|
226
233
|
}
|
|
227
234
|
|
|
235
|
+
function getTextsFromTokens(tokens) {
|
|
236
|
+
function unquote(s) {
|
|
237
|
+
return s[0] === '"' ? s.slice(1, -1).replace(/\\"/g, '"') :
|
|
238
|
+
s[0] === "'" ? s.slice(1, -1).replace(/\\'/g, "'") : '';
|
|
239
|
+
}
|
|
240
|
+
return tokens.filter((t, i) => i >=2 && t.type === 'String' && (t.value[0] === '"' || t.value[0] === "'") && tokens[i-1].value === '(' && tokens[i-2].value === 't').map(t => unquote(t.value)).filter(s => s.length)
|
|
241
|
+
}
|
|
242
|
+
|
|
228
243
|
const appendMarker = '\n// wovn-nextjs-command-appended'
|
|
229
244
|
|
|
230
245
|
async function overwriteInitializationProcess(context) {
|
|
@@ -261,6 +276,17 @@ async function overwriteJavaScriptFiles(filename, convert) {
|
|
|
261
276
|
}
|
|
262
277
|
|
|
263
278
|
async function overwriteTsxAndJsxFiles(context, convert) {
|
|
279
|
+
const promises = []
|
|
280
|
+
await walkTsxAndJsx(async (context, source) => {
|
|
281
|
+
const convertedSource = convert(context, source)
|
|
282
|
+
if (source !== convertedSource) {
|
|
283
|
+
promises.push(fsp.writeFile(context.pathname, convertedSource))
|
|
284
|
+
}
|
|
285
|
+
})
|
|
286
|
+
return Promise.all(promises)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async function walkTsxAndJsx(context, callback){
|
|
264
290
|
async function* targetFiles(dir) {
|
|
265
291
|
const dirents = await fsp.readdir(dir, { withFileTypes: true });
|
|
266
292
|
for (const dirent of dirents) {
|
|
@@ -274,17 +300,12 @@ async function overwriteTsxAndJsxFiles(context, convert) {
|
|
|
274
300
|
}
|
|
275
301
|
}
|
|
276
302
|
}
|
|
277
|
-
const promises = []
|
|
278
303
|
const pwd = process.env.PWD
|
|
279
304
|
for await (const pathname of targetFiles(buildDir)) {
|
|
280
305
|
context.pathname = pathname.slice(pwd.length + buildDir.length)
|
|
281
306
|
const source = await fsp.readFile(pathname, 'utf-8')
|
|
282
|
-
|
|
283
|
-
if (source !== convertedSource) {
|
|
284
|
-
promises.push(fsp.writeFile(pathname, convertedSource))
|
|
285
|
-
}
|
|
307
|
+
callback(context, source)
|
|
286
308
|
}
|
|
287
|
-
return Promise.all(promises)
|
|
288
309
|
}
|
|
289
310
|
|
|
290
311
|
async function reportToWovn(context) {
|
|
@@ -307,6 +328,9 @@ async function reportToWovn(context) {
|
|
|
307
328
|
function httpsRequest(url, method='get', body='') {
|
|
308
329
|
return new Promise((resolve, reject) => {
|
|
309
330
|
try {
|
|
331
|
+
if (process.env.WOVN_DEBUG) {
|
|
332
|
+
console.dir({method, url, body}, {depth: null})
|
|
333
|
+
}
|
|
310
334
|
const req = https.request(url, { method }, res => {
|
|
311
335
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
|
312
336
|
const chunks = []
|