Skip to content

Commit 0f570da

Browse files
committed
[TASK] Adapt vite configuration to process rendering
1 parent 9ddea04 commit 0f570da

File tree

1 file changed

+79
-19
lines changed

1 file changed

+79
-19
lines changed

vite.config.js

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,97 @@
11
import { defineConfig } from 'vite'
2-
import { exec } from 'child_process'
2+
import { execSync } from 'child_process'
3+
import { existsSync } from 'fs';
4+
import { resolve, relative, sep } from 'path';
5+
6+
function isSubdirectory(parent, child) {
7+
const resolvedParent = resolve(parent);
8+
const resolvedChild = resolve(child);
9+
10+
const relativePath = relative(resolvedParent, resolvedChild);
11+
12+
// If the relative path starts with ".." or has a leading separator, it's not a subdirectory
13+
return !relativePath.startsWith(`..${sep}`) && !relativePath.startsWith(sep);
14+
}
15+
16+
let sourceDirectory = process.env.SOURCE || 'Documentation';
17+
let targetDirectory = process.env.TARGET || 'Documentation-GENERATED-temp';
18+
let vitePort = process.env.VITE_PORT || 5173;
19+
20+
// Example how to run this with different directories:
21+
// SOURCE=./path/to/Documentation TARGET=./another/path/to/Documentation-Output/ VITE_PORT=5173 npm run dev
22+
// Note that the paths need to be within the project directory, else vite does not watch these directories.
23+
24+
if (!existsSync(sourceDirectory)) {
25+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[31mSpecified \x1b[4m${sourceDirectory}\x1b[0m\x1b[31m does not exist, fallback to current directory.\x1b[0m`);
26+
sourceDirectory = './';
27+
}
28+
29+
if (!existsSync(targetDirectory)) {
30+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[31mSpecified \x1b[4m${targetDirectory}\x1b[0m\x1b[31m does not exist, fallback to current directory.\x1b[0m`);
31+
targetDirectory = './';
32+
}
33+
34+
if (!isSubdirectory(process.cwd(), sourceDirectory)) {
35+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[31mSpecified \x1b[4m${sourceDirectory}\x1b[0m\x1b[31m is not within project scope, vite cannot watch changes.\x1b[0m`);
36+
process.exit(1);
37+
}
38+
39+
if (!isSubdirectory(process.cwd(), targetDirectory)) {
40+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[31mSpecified \x1b[4m${targetDirectory}\x1b[0m\x1b[31m is not within project scope, vite cannot watch changes.\x1b[0m`);
41+
process.exit(1);
42+
}
43+
44+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[32mListening on \x1b[4m${sourceDirectory}\x1b[0m\x1b[32m which renders to \x1b[4m${targetDirectory}\x1b[0m`);
45+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[32mUse e.g. \x1b[4mhttp://localhost:${vitePort}/${targetDirectory}/Index.html\x1b[0m\x1b[32m to view documentation.\x1b[0m`);
346

447
export default defineConfig({
548
build: {
649
watch: {
7-
include: 'Documentation/**'
50+
include: sourceDirectory + '/**'
851
},
952
},
53+
server: {
54+
port: vitePort
55+
},
1056
plugins: [
1157
{
1258
name: 'html',
1359
handleHotUpdate({file, server}) {
14-
if (file.endsWith('.html')) {
60+
if (file.indexOf('/' + targetDirectory + '/') > 0 && file.endsWith('.html')) {
61+
// Changes on targetDirectory (HTML output)
62+
63+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[32m\x1b[33mBrowser reload\x1b[32m on \x1b[4m${file}\x1b[0m`);
1564
server.ws.send({
1665
type: 'full-reload',
1766
path: '*'
1867
});
19-
} else if(file.indexOf('/Documentation/') > 0) {
20-
// TODO: Execute docker command here.
21-
// TODO: Make "Documentation" directory configurable via env/param?!
22-
console.log('Re-rendering due to change in: ' + file);
68+
} else if(file.indexOf('/' + sourceDirectory + '/') > 0) {
69+
// Changes on sourceDirectory (ReST input)
70+
71+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[32m\x1b[33mRender\x1b[32m on \x1b[4m${file}\x1b[0m`);
72+
73+
try {
74+
// The container by defaults operates on /project/Documentation/ and writes to /project/Documentation-GENERATED-temp/.
75+
// We make our lives easy by just mapping those expected directories to our sourceDirectory/targetDirectory variables.
76+
const shellExec = execSync(
77+
'docker run --rm --pull always '
78+
+ ' -v ./' + sourceDirectory + ':/project/Documentation '
79+
+ ' -v ./' + targetDirectory + ':/project/Documentation-GENERATED-temp '
80+
+ ' ghcr.io/typo3-documentation/render-guides:latest '
81+
+ ' --no-progress Documentation').toString();
82+
83+
console.log(shellExec);
84+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[32m\x1b[33mRendering performed.\x1b[0m`);
85+
86+
server.ws.send({
87+
type: 'full-reload',
88+
path: '*'
89+
});
90+
} catch (error) {
91+
console.log(`\x1b[37m\x1b[2m${new Date().toLocaleTimeString()}\x1b[0m \x1b[1m\x1b[36m[typo3-documentation-browsersync] \x1b[0m\x1b[1m\x1b[31mRendering Error:\x1b[0m\n-------------------\n${error.message}-------------------\n`);
92+
}
2393
}
2494
}
25-
},
26-
27-
{
28-
name: 'postbuild-commands',
29-
closeBundle: async () => {
30-
console.log('reload ...');
31-
32-
exec('pwd >> ./vite.log');
33-
// run during closeBundle hook. https://rollupjs.org/guide/en/#closebundle
34-
}
35-
},
95+
}
3696
]
37-
});
97+
});

0 commit comments

Comments
 (0)