/
example.jsx
279 lines (224 loc) · 9.82 KB
/
example.jsx
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import url from 'node:url'
import { spawnSync } from 'node:child_process'
import { Page } from '@nakedjsx/core/page'
import { Code } from './common.jsx'
import { fixIndent } from './util.mjs'
function relativePathToUriPath(relativePath)
{
//
// Convert some/path and some\path to some/path
//
// This is a bit convoluted due to windows paths.
//
const rootUriPath = url.pathToFileURL(path.sep).pathname;
const uriPath = url.pathToFileURL(path.sep + relativePath).pathname.slice(rootUriPath.length);
return uriPath;
}
const exampleBuildCache = Page.CacheMapGet(import.meta.url.href);
export const Example =
({ captureOutput, wordwrapOutput, buildFlags, context, children }) =>
{
const hiddenBuildFlags = ['--sourcemaps-disable', '--quiet'];
buildFlags = hiddenBuildFlags.concat(buildFlags || ['--pretty']);
if (captureOutput)
captureOutput = path.join(...captureOutput);
//
// Provide a way for the Src tags to
// pass their file contents back here.
//
// The Src tags could write the files to the tmp src
// dir themselves, but collating them here allows us
// to implement a memory cache of the outputs.
//
const sourceFiles = {};
context.sourceFiles = sourceFiles;
//
// Make the build command used to any <Example.BuildCmd> child
//
const npxArgs = ['nakedjsx', 'src', '--out', 'out', ...buildFlags];
context.buildCommand = 'npx';
for (const segment of npxArgs)
if (hiddenBuildFlags.includes(segment)) // these aren't relevant to the reader
continue;
else
context.buildCommand += ' ' + segment;
//
// We need to force child Src tags to render immediately,
// so that data has been fed back via the context.
//
const sources = Page.EvaluateNow(children);
//
// Create a cache key from the filenames and their
// expected contents.
//
let cacheKey = `CAPTURE[${captureOutput ? Page.GetOutputPath(captureOutput) : ''}],BUILD[${context.buildCommand}]`;
for (const [filename, { lang, content }] of Object.entries(sourceFiles))
cacheKey += `,SOURCE[${filename},${lang},${content}]`;
let result = exampleBuildCache.get(cacheKey);
if (!result)
{
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'example-'));
const src = path.join(tmp, 'src');
const out = captureOutput ? Page.GetOutputPath(captureOutput) : path.join(tmp, 'out');
// Update the build command input and output folders
npxArgs[1] = src;
npxArgs[3] = out;
for (const [filename, { content }] of Object.entries(sourceFiles))
{
const sourceFilePath = path.join(tmp, filename);
if (!sourceFilePath.startsWith(tmp + path.sep))
throw Error(`Example.Src attempting to create file ${sourceFilePath} outside of ${tmp}`);
fs.mkdirSync(path.dirname(sourceFilePath), { recursive: true });
fs.writeFileSync(sourceFilePath, content);
}
const spawnOptions =
{
cwd: tmp,
stdio: 'inherit'
};
// Use the same 'npx nakedjsx' that is currently running
npxArgs[0] = process.argv[1];
Page.Log(`Building example: ${Page.GetOutputUri(captureOutput)}`);
const spawnResult = spawnSync(process.argv[0], npxArgs, spawnOptions);
if (spawnResult.status)
throw Error(`\n\nnpx nakedjsx exit code ${spawnResult.status} when building ${captureOutput}\n`);
if (spawnResult.error)
{
// if (spawnResult.error.code === 'ENOENT' && spawnResult.error.path === 'npx')
// {
// //
// // Node on windows requires npx.cmd -- but i'm not sure if this is true
// // for all Node.js Windows runtimes, perhaps WSL is different.
// //
// // So for now the approach is to try 'npx', look for specific error
// // details and try 'npx.cmd' if they match
// //
// spawnResult = spawnSync('npx.cmd', npxArgs, spawnOptions);
// if (spawnResult.error)
// throw spawnResult.error;
// }
// else
throw spawnResult.error;
}
//
// We should now have output files
//
function *findFiles(root, subdir = '')
{
for (const entry of fs.readdirSync(path.join(root, subdir), { withFileTypes: true }))
{
if (entry.isFile())
yield path.join(subdir, entry.name);
else if (entry.isDirectory())
yield * findFiles(root, path.join(subdir, entry.name));
}
}
result =
[...findFiles(out)].map(
filename =>
{
let lang = '';
if (filename.endsWith('.html'))
lang = 'html';
else if (filename.endsWith('.css'))
lang = 'css';
else if (filename.endsWith('.js'))
lang = 'javascript';
else if (filename.endsWith('.svg'))
lang = 'svg';
const filePath = path.join(out, filename);
return {
lang,
filename,
content: fs.readFileSync(filePath).toString(),
size: fs.statSync(filePath).size
};
});
exampleBuildCache.set(cacheKey, result);
}
function truncateIfNeeded(content)
{
const charLimit = 2000;
const lineLimit = 60;
const truncatedSuffix = '...\n\n(content too large to display in full)';
if (content.length > charLimit)
content = content.substring(0, charLimit - truncatedSuffix.length) + truncatedSuffix;
let lineCount = 1;
for(var i = 0; i < content.length; ++i)
if(content[i] === '\n')
if (++lineCount >= lineLimit)
return content.substring(0, i + 1) + truncatedSuffix;
return content;
}
function stripAssetHash(filename)
{
if (!filename.startsWith('asset/'))
return filename;
return filename.replace(/\.[^.]+\.([^\.]+)$/, ".$1");
}
function outputComparitor(a, b)
{
return stripAssetHash(a.filename).localeCompare(stripAssetHash(b.filename));
}
return <>
{sources}
{result
.sort(outputComparitor)
.map(
({ lang, filename, content, size }) =>
{
//
// Convert output folder to URI path.
//
// This is a bit convoluted due to windows paths.
//
const title = `out/${relativePathToUriPath(filename)} (${size} bytes)`;
if (captureOutput)
{
const uriPath = relativePathToUriPath(path.join(captureOutput, filename));
if (lang === 'css' || lang === 'js' || lang === 'svg')
content = truncateIfNeeded(content);
else if (lang !== 'html')
content = '(no preview available)';
return <Code wordwrap={wordwrapOutput} lang={lang} title={title} uri={uriPath} uriTarget="_blank" uriText="(open in new tab)">{content}</Code>
}
})}
</>;
}
Example.BuildCmd =
({ context }) =>
{
return <Code lang="shell" title="# build command" copyLink="(copy cmd)">$ {context.buildCommand}</Code>
}
Example.Src =
({ hidden, lang, filename, context, content, children }) =>
{
//
// A source can be supplied as a single string child,
// or as a string or Buffer content prop.
//
if (!content)
{
if (children.length != 1 || typeof children[0] != 'string')
throw Error(`Example.Src tag requires either a content prop or single string child to set as file contents of ${filename}`);
content = children[0];
}
//
// Pass the code back to the <Example> tag via context
//
const { sourceFiles } = context;
sourceFiles[filename] =
{
lang,
content
};
if (hidden)
return;
if (typeof content === 'string')
return <Code title={filename} lang={lang} fixIndent copyLink="(copy code)">{content}</Code>
else
return <Code title={filename} fixIndent>(no preview available)</Code>
}