@eightshift/frontend-libs-tailwind 3.4.0 → 4.0.0-beta.1
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/.github/CODEOWNERS +1 -1
- package/CHANGELOG.md +19 -1
- package/README.md +187 -0
- package/bin/tw-sweep.mjs +281 -0
- package/bun/build.mjs +341 -0
- package/bun/cli.mjs +77 -0
- package/bun/css.mjs +145 -0
- package/bun/discovery.mjs +121 -0
- package/bun/editor.mjs +105 -0
- package/bun/eightshift-css.mjs +66 -0
- package/bun/eightshift-discovery.mjs +135 -0
- package/bun/flat-publication.mjs +343 -0
- package/bun/index.mjs +118 -0
- package/bun/media.mjs +49 -0
- package/bun/preset.mjs +118 -0
- package/bun/publish.mjs +236 -0
- package/bun/release.mjs +182 -0
- package/{webpack/responsive-compiler.mjs → bun/responsive.mjs} +18 -39
- package/bun/ssr.mjs +89 -0
- package/bun/theme-build.mjs +464 -0
- package/bun/theme-discovery.mjs +171 -0
- package/bun/watch.mjs +211 -0
- package/bun/wordpress.mjs +173 -0
- package/linters/oxlint.config.mjs +65 -0
- package/linters/tw-ignore.mjs +45 -0
- package/linters/tw-sweep-extract.mjs +545 -0
- package/package.json +16 -16
- package/test/fixtures/theme/src/Blocks/custom/card/card.php +23 -0
- package/test/fixtures/theme/src/Blocks/custom/card/manifest.json +13 -0
- package/test/fixtures/theme/src/Plugins/Forms.php +15 -0
- package/test/fixtures/theme/src/card.js +2 -0
- package/test/fixtures/theme/tailwind.css +6 -0
- package/test/fixtures/theme/tw-ignore.json +1 -0
- package/test/tw-sweep.test.mjs +119 -0
- package/bun.lock +0 -1433
- package/webpack/base.mjs +0 -126
- package/webpack/helpers.mjs +0 -55
- package/webpack/index.mjs +0 -46
- package/webpack/production.mjs +0 -60
- package/webpack/project.mjs +0 -50
package/bun/index.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { basename, dirname, isAbsolute, relative, resolve, sep } from 'node:path';
|
|
2
|
+
import { buildPreset, createPreset } from './preset.mjs';
|
|
3
|
+
import { watchBuild } from './watch.mjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Define a reusable build. `preset` may be an object or a function of the
|
|
7
|
+
* invocation context. `ssr` and `publish` run after the browser build, in that
|
|
8
|
+
* order; neither is included in the browser build's atomic publication.
|
|
9
|
+
* Watch options are passed to watchBuild, with root and build controlled here.
|
|
10
|
+
*/
|
|
11
|
+
export function defineBuild({ root = process.cwd(), preset, ssr, publish, watch = {} } = {}) {
|
|
12
|
+
if (typeof root !== 'string' || !root) {
|
|
13
|
+
throw new TypeError('root must be a non-empty string');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (!preset || (typeof preset !== 'object' && typeof preset !== 'function') || Array.isArray(preset)) {
|
|
17
|
+
throw new TypeError('preset must be an object or function');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (ssr !== undefined && typeof ssr !== 'function') {
|
|
21
|
+
throw new TypeError('ssr must be a function');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (publish !== undefined && typeof publish !== 'function') {
|
|
25
|
+
throw new TypeError('publish must be a function');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!watch || typeof watch !== 'object' || Array.isArray(watch)) {
|
|
29
|
+
throw new TypeError('watch must be an object');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (watch.ignore !== undefined && typeof watch.ignore !== 'function') {
|
|
33
|
+
throw new TypeError('watch.ignore must be a function');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
root = resolve(root);
|
|
37
|
+
let active;
|
|
38
|
+
const outputs = new Set();
|
|
39
|
+
const run = async (options = {}) => {
|
|
40
|
+
if (active) {
|
|
41
|
+
throw new Error('A build is already running');
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
active = (async () => {
|
|
45
|
+
const context = { ...options, root };
|
|
46
|
+
const configured = typeof preset === 'function' ? await preset(context) : preset;
|
|
47
|
+
outputs.clear();
|
|
48
|
+
const output = typeof configured?.outdir === 'string' && configured.outdir ? resolve(root, configured.outdir) : undefined;
|
|
49
|
+
if (output) {
|
|
50
|
+
const rootFromOutput = relative(output, root);
|
|
51
|
+
if (!rootFromOutput || (!isAbsolute(rootFromOutput) && rootFromOutput !== '..' && !rootFromOutput.startsWith(`..${sep}`))) {
|
|
52
|
+
throw new Error('outdir must not contain the project root');
|
|
53
|
+
}
|
|
54
|
+
outputs.add(output);
|
|
55
|
+
}
|
|
56
|
+
const result = await buildPreset({ ...configured, root });
|
|
57
|
+
const state = { ...context, result };
|
|
58
|
+
|
|
59
|
+
if (ssr) {
|
|
60
|
+
state.ssr = await ssr(state);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (publish) {
|
|
64
|
+
state.publication = await publish(state);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return state;
|
|
68
|
+
})();
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
return await active;
|
|
72
|
+
} finally {
|
|
73
|
+
active = undefined;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
build: run,
|
|
79
|
+
watch: (options = {}) => {
|
|
80
|
+
if ((ssr || publish) && watch.sources === undefined && !watch.ignored?.length) {
|
|
81
|
+
throw new Error('Watch builds with ssr or publish callbacks require explicit watch.sources or watch.ignored');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return watchBuild({
|
|
85
|
+
sources: ['.'],
|
|
86
|
+
...watch,
|
|
87
|
+
projectDir: root,
|
|
88
|
+
ignore: (path) => {
|
|
89
|
+
if (watch.ignore?.(path)) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
for (const output of outputs) {
|
|
93
|
+
const location = relative(output, path);
|
|
94
|
+
if (!location || (!isAbsolute(location) && location !== '..' && !location.startsWith(`..${sep}`))) {
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
const sibling = relative(dirname(output), path).split(sep)[0];
|
|
98
|
+
if (sibling.startsWith(`.${basename(output)}-stage-`) || sibling.startsWith(`.${basename(output)}-backup-`)) {
|
|
99
|
+
return true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
},
|
|
104
|
+
build: async () => {
|
|
105
|
+
while (active) {
|
|
106
|
+
await active.catch(() => {});
|
|
107
|
+
}
|
|
108
|
+
return run(options);
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export { buildPreset, createPreset } from './preset.mjs';
|
|
116
|
+
export { build } from './build.mjs';
|
|
117
|
+
export { watchBuild } from './watch.mjs';
|
|
118
|
+
export { packageRelease } from './release.mjs';
|
package/bun/media.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { basename, dirname, resolve } from 'node:path';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Adapt the published UI Components smart-image worker for flat theme releases.
|
|
6
|
+
* Callbacks belong to one build and provide the worker to the publication step.
|
|
7
|
+
* Unsupported upstream worker URL forms fail rather than producing a broken URL.
|
|
8
|
+
*/
|
|
9
|
+
export function editorWorkerPlugin({ setEditorWorker, getEditorWorker }) {
|
|
10
|
+
if (typeof setEditorWorker !== 'function' || typeof getEditorWorker !== 'function') {
|
|
11
|
+
throw new TypeError('Worker callbacks are required');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
name: 'eightshift-editor-worker-url',
|
|
16
|
+
setup(builder) {
|
|
17
|
+
builder.onLoad({ filter: /[/\\]smart-image[/\\]smart-image\.js$/ }, async ({ path }) => {
|
|
18
|
+
if (!/[/\\]@eightshift[/\\]ui-components[/\\]/.test(path)) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const source = await readFile(path, 'utf8');
|
|
23
|
+
|
|
24
|
+
if (!source.includes('image-analysis-worker')) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const pattern = /new URL\("" \+ new URL\("(\.\.\/\.\.\/assets\/image-analysis-worker-[A-Za-z0-9_-]+\.js)", import\.meta\.url\)\.href \+ "", "" \+ import\.meta\.url\)/g;
|
|
29
|
+
const matches = [...source.matchAll(pattern)];
|
|
30
|
+
|
|
31
|
+
if (matches.length !== 1) {
|
|
32
|
+
throw new Error(`Unsupported smart-image worker URL in ${path}; configure media.editorWorker to override`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const worker = resolve(dirname(path), matches[0][1]);
|
|
36
|
+
const previous = getEditorWorker();
|
|
37
|
+
|
|
38
|
+
if (previous && (basename(previous) !== basename(worker) || !(await readFile(previous)).equals(await readFile(worker)))) {
|
|
39
|
+
throw new Error('Conflicting smart-image workers');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setEditorWorker(worker);
|
|
43
|
+
const base = '(typeof __eightshiftEditorScriptUrl !== "undefined" ? __eightshiftEditorScriptUrl : (() => { throw new Error("Smart-image worker requires the Eightshift editor script URL; override media.editorWorker for other bundles"); })())';
|
|
44
|
+
|
|
45
|
+
return { contents: source.replace(pattern, `new URL(${JSON.stringify(`editor-assets/${basename(worker)}`)}, ${base})`), loader: 'js', resolveDir: dirname(path) };
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
}
|
package/bun/preset.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { isAbsolute, relative, resolve, sep } from 'node:path';
|
|
2
|
+
import { build } from './build.mjs';
|
|
3
|
+
import { processEightshiftCss } from './eightshift-css.mjs';
|
|
4
|
+
|
|
5
|
+
const checkOptions = (value, keys, label) => {
|
|
6
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
7
|
+
throw new TypeError(`${label} must be an object`);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
for (const key of Object.keys(value)) {
|
|
11
|
+
if (!keys.includes(key)) {
|
|
12
|
+
throw new TypeError(`Unknown ${label} option: ${key}`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const feature = (value, keys, label) => {
|
|
18
|
+
if (value === undefined || value === false) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
checkOptions(value, keys, label);
|
|
23
|
+
|
|
24
|
+
return { ...value };
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Create build options for a project. No layout or entrypoints are assumed.
|
|
29
|
+
* discovery and responsive are disabled unless configured. CSS is disabled
|
|
30
|
+
* unless configured as a callback or an array of Eightshift surface options.
|
|
31
|
+
* Set wordpress: false to bundle dependencies instead of externalizing them.
|
|
32
|
+
* Configure Bun target, format, naming and splitting via buildOptions.
|
|
33
|
+
* Entry points, staging output and write mode are controlled by build.
|
|
34
|
+
* editor: { entry, filename, chunkDir, scriptUrlIdentifier, discovery, lazy }
|
|
35
|
+
* makes the named entry a synchronous classic script with lazy ESM chunks.
|
|
36
|
+
* discovery configures eightshiftDiscovery; lazy configures editorLazyImports.
|
|
37
|
+
* Both are opt-in, and all filenames and discovery behavior are overridable.
|
|
38
|
+
*/
|
|
39
|
+
export function createPreset(options = {}) {
|
|
40
|
+
checkOptions(options, ['root', 'outdir', 'entries', 'wpRoot', 'wordpress', 'discovery', 'responsive', 'css', 'editor', 'buildOptions', 'direct'], 'preset');
|
|
41
|
+
const { root = process.cwd(), outdir, entries, wpRoot, wordpress = true, buildOptions = {}, direct = false } = options;
|
|
42
|
+
|
|
43
|
+
if (typeof root !== 'string' || !root || typeof outdir !== 'string' || !outdir) {
|
|
44
|
+
throw new TypeError('root and outdir must be non-empty strings');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!entries || typeof entries !== 'object' || Array.isArray(entries) || !Object.keys(entries).length) {
|
|
48
|
+
throw new TypeError('entries must contain named entrypoints');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (typeof wordpress !== 'boolean') {
|
|
52
|
+
throw new TypeError('wordpress must be a boolean');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (wpRoot !== undefined && (typeof wpRoot !== 'string' || !wpRoot)) {
|
|
56
|
+
throw new TypeError('wpRoot must be a non-empty string');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (!buildOptions || typeof buildOptions !== 'object' || Array.isArray(buildOptions)) {
|
|
60
|
+
throw new TypeError('buildOptions must be an object');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
for (const key of ['entrypoints', 'outdir', 'write']) {
|
|
64
|
+
if (Object.hasOwn(buildOptions, key)) {
|
|
65
|
+
throw new TypeError(`buildOptions.${key} is controlled by build`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const editor = feature(options.editor, ['entry', 'chunkDir', 'filename', 'scriptUrlIdentifier', 'discovery', 'lazy'], 'editor');
|
|
70
|
+
const discovery = feature(options.discovery, ['output', 'definitions'], 'discovery');
|
|
71
|
+
const responsive = feature(options.responsive, ['breakpoints', 'manifestGlob', 'output'], 'responsive');
|
|
72
|
+
|
|
73
|
+
if (responsive?.output !== undefined) {
|
|
74
|
+
checkOptions(responsive.output, ['frontend', 'editor'], 'responsive.output');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let css;
|
|
78
|
+
|
|
79
|
+
if (options.css !== undefined && options.css !== false) {
|
|
80
|
+
if (typeof options.css === 'function') {
|
|
81
|
+
css = options.css;
|
|
82
|
+
} else {
|
|
83
|
+
if (!Array.isArray(options.css)) {
|
|
84
|
+
throw new TypeError('css must be false, a function, or an array of surfaces');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const surfaces = options.css.map((surface) => {
|
|
88
|
+
checkOptions(surface, ['surface', 'outputFile', 'production', 'browsers', 'blocksDir', 'manifestGlob', 'tailwindFile', 'colorThemesFile', 'applicationFile', 'groups', 'scanExclusions', 'responsiveSource', 'responsiveFile'], 'css surface');
|
|
89
|
+
|
|
90
|
+
if (!['frontend', 'editor', 'admin'].includes(surface.surface) || typeof surface.outputFile !== 'string' || !surface.outputFile || isAbsolute(surface.outputFile)) {
|
|
91
|
+
throw new TypeError('Each CSS surface requires a valid surface and relative outputFile');
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return { ...surface };
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
css = async ({ root: projectDir, outdir: stage, responsive: generated }) => {
|
|
98
|
+
for (const { outputFile, ...surface } of surfaces) {
|
|
99
|
+
const output = resolve(stage, outputFile);
|
|
100
|
+
const location = relative(stage, output);
|
|
101
|
+
|
|
102
|
+
if (!location || location === '..' || location.startsWith(`..${sep}`) || isAbsolute(location)) {
|
|
103
|
+
throw new Error(`CSS output escapes staging: ${outputFile}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
await processEightshiftCss({ ...surface, projectDir, outputFile: output, responsiveFile: surface.responsiveFile ?? generated?.[surface.surface] });
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return { root, outdir, entries: { ...entries }, wpRoot, wordpress, discovery, responsive, css, editor, direct, buildOptions: { ...buildOptions } };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Build and atomically publish a configured preset. */
|
|
116
|
+
export async function buildPreset(options) {
|
|
117
|
+
return build(createPreset(options));
|
|
118
|
+
}
|
package/bun/publish.mjs
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { randomUUID } from 'node:crypto';
|
|
2
|
+
import { cp, lstat, mkdir, readFile, readdir, rename, rm, unlink, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
4
|
+
|
|
5
|
+
const exists = async (path) => {
|
|
6
|
+
try {
|
|
7
|
+
return await lstat(path);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
if (error.code === 'ENOENT') {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const pathName = (value) => {
|
|
17
|
+
if (typeof value !== 'string' || !value || isAbsolute(value) || value.includes('\\') || value.includes('\0') || value.split('/').some((part) => !part || part === '.' || part === '..')) {
|
|
18
|
+
throw new Error(`Invalid manifest path: ${String(value)}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
const inside = (parent, child) => {
|
|
24
|
+
const path = relative(parent, child);
|
|
25
|
+
|
|
26
|
+
return !path || (!path.startsWith(`..${sep}`) && path !== '..' && !isAbsolute(path));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Publish a packageRelease directory without deleting unrelated public files.
|
|
31
|
+
* root is the theme root; publicDir defaults to root/public. publicPrefix is
|
|
32
|
+
* the URL prefix including its trailing slash. manifest maps keys to paths in
|
|
33
|
+
* release. Existing PHP manifest cache assets are updated;
|
|
34
|
+
* missing caches are left alone. Returns { publicDir, manifest }.
|
|
35
|
+
*/
|
|
36
|
+
export async function publishRelease({ root, release, manifest, publicDir, publicPrefix }) {
|
|
37
|
+
if (
|
|
38
|
+
typeof root !== 'string' ||
|
|
39
|
+
!root ||
|
|
40
|
+
typeof release !== 'string' ||
|
|
41
|
+
!release ||
|
|
42
|
+
!manifest ||
|
|
43
|
+
typeof manifest !== 'object' ||
|
|
44
|
+
Array.isArray(manifest) ||
|
|
45
|
+
typeof publicPrefix !== 'string' ||
|
|
46
|
+
!publicPrefix.startsWith('/') ||
|
|
47
|
+
publicPrefix.startsWith('//') ||
|
|
48
|
+
!publicPrefix.endsWith('/') ||
|
|
49
|
+
publicPrefix.split('/').includes('..')
|
|
50
|
+
) {
|
|
51
|
+
throw new TypeError('Expected root, release, manifest and an absolute publicPrefix ending in /');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
root = resolve(root);
|
|
55
|
+
release = resolve(release);
|
|
56
|
+
publicDir = resolve(root, publicDir ?? 'public');
|
|
57
|
+
|
|
58
|
+
if (inside(publicDir, release) || inside(release, publicDir) || publicDir === root || inside(publicDir, root)) {
|
|
59
|
+
throw new Error('Release, root and public directory must be distinct');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const cachePath = join(root, 'eightshift/manifests.json');
|
|
63
|
+
|
|
64
|
+
if (inside(publicDir, cachePath) || inside(release, cachePath)) {
|
|
65
|
+
throw new Error('Cache must be outside publication and release directories');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const parent = dirname(publicDir);
|
|
69
|
+
const stage = join(parent, `.public-stage-${randomUUID()}`);
|
|
70
|
+
const backup = join(parent, `.public-backup-${randomUUID()}`);
|
|
71
|
+
let staged = false;
|
|
72
|
+
let backedUp = false;
|
|
73
|
+
let published = false;
|
|
74
|
+
let cacheTemporary;
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
await mkdir(parent, { recursive: true });
|
|
78
|
+
const releaseInfo = await lstat(release);
|
|
79
|
+
|
|
80
|
+
if (!releaseInfo.isDirectory() || releaseInfo.isSymbolicLink()) {
|
|
81
|
+
throw new Error('Release must be a directory');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const publicInfo = await exists(publicDir);
|
|
85
|
+
|
|
86
|
+
if (publicInfo && (!publicInfo.isDirectory() || publicInfo.isSymbolicLink())) {
|
|
87
|
+
throw new Error('Refusing to replace non-directory public path');
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const publicManifest = Object.create(null);
|
|
91
|
+
|
|
92
|
+
for (const [key, value] of Object.entries(manifest)) {
|
|
93
|
+
pathName(key);
|
|
94
|
+
pathName(value);
|
|
95
|
+
publicManifest[key] = `${publicPrefix}${value}`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Include unaliased flat files, as the theme's legacy publisher did.
|
|
99
|
+
for (const name of await readdir(release)) {
|
|
100
|
+
const info = await lstat(join(release, name));
|
|
101
|
+
|
|
102
|
+
if (info.isSymbolicLink() || (!info.isDirectory() && !info.isFile())) {
|
|
103
|
+
throw new Error(`Unsupported release output: ${name}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (info.isFile() && name !== 'manifest.json' && !Object.hasOwn(publicManifest, name)) {
|
|
107
|
+
publicManifest[name] = `${publicPrefix}${name}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
for (const value of Object.values(manifest)) {
|
|
112
|
+
let current = release;
|
|
113
|
+
|
|
114
|
+
for (const part of value.split('/')) {
|
|
115
|
+
current = join(current, part);
|
|
116
|
+
const info = await lstat(current);
|
|
117
|
+
|
|
118
|
+
if (info.isSymbolicLink()) {
|
|
119
|
+
throw new Error(`Symlink in release path: ${value}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!(await lstat(current)).isFile()) {
|
|
124
|
+
throw new Error(`Manifest target is not a file: ${value}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await mkdir(stage);
|
|
129
|
+
staged = true;
|
|
130
|
+
|
|
131
|
+
if (publicInfo) {
|
|
132
|
+
await cp(publicDir, stage, { recursive: true, verbatimSymlinks: true });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Never follow pre-existing symlinks when overlaying release files.
|
|
136
|
+
const overlay = async (source, destination) => {
|
|
137
|
+
for (const name of await readdir(source)) {
|
|
138
|
+
const from = join(source, name);
|
|
139
|
+
const to = join(destination, name);
|
|
140
|
+
const info = await lstat(from);
|
|
141
|
+
const old = await exists(to);
|
|
142
|
+
|
|
143
|
+
if (info.isSymbolicLink() || (!info.isDirectory() && !info.isFile())) {
|
|
144
|
+
throw new Error(`Unsupported release output: ${from}`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (old && (old.isSymbolicLink() || (info.isDirectory() ? !old.isDirectory() : !old.isFile()))) {
|
|
148
|
+
throw new Error(`Publication path conflict: ${to}`);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (info.isDirectory()) {
|
|
152
|
+
await mkdir(to, { recursive: true });
|
|
153
|
+
await overlay(from, to);
|
|
154
|
+
} else {
|
|
155
|
+
await cp(from, to);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
await overlay(release, stage);
|
|
161
|
+
const manifestPath = join(stage, 'manifest.json');
|
|
162
|
+
const manifestInfo = await exists(manifestPath);
|
|
163
|
+
|
|
164
|
+
if (manifestInfo && (!manifestInfo.isFile() || manifestInfo.isSymbolicLink())) {
|
|
165
|
+
throw new Error('Publication manifest path conflict');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
await writeFile(manifestPath, `${JSON.stringify(publicManifest, null, 2)}\n`);
|
|
169
|
+
const cacheInfo = await exists(cachePath);
|
|
170
|
+
let cacheData;
|
|
171
|
+
|
|
172
|
+
if (cacheInfo) {
|
|
173
|
+
if (!cacheInfo.isFile() || cacheInfo.isSymbolicLink()) {
|
|
174
|
+
throw new Error('Manifest cache must be a regular file');
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
cacheData = JSON.parse(await readFile(cachePath, 'utf8'));
|
|
178
|
+
|
|
179
|
+
if (!cacheData?.assets || Array.isArray(cacheData.assets) || typeof cacheData.assets !== 'object' || !cacheData.assets.assets || Array.isArray(cacheData.assets.assets) || typeof cacheData.assets.assets !== 'object') {
|
|
180
|
+
throw new Error('Invalid manifest cache structure');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
cacheData.assets.assets = publicManifest;
|
|
184
|
+
cacheTemporary = `${cachePath}.${randomUUID()}`;
|
|
185
|
+
await writeFile(cacheTemporary, JSON.stringify(cacheData), { flag: 'wx', mode: cacheInfo.mode });
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (publicInfo) {
|
|
189
|
+
await rename(publicDir, backup);
|
|
190
|
+
backedUp = true;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
await rename(stage, publicDir);
|
|
194
|
+
staged = false;
|
|
195
|
+
published = true;
|
|
196
|
+
|
|
197
|
+
if (cacheTemporary) {
|
|
198
|
+
await rename(cacheTemporary, cachePath);
|
|
199
|
+
cacheTemporary = undefined;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (backedUp) {
|
|
203
|
+
backedUp = false;
|
|
204
|
+
await rm(backup, { recursive: true }).catch((error) => {
|
|
205
|
+
console.error(`Could not remove publication backup ${backup}:`, error);
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return { publicDir, manifest: { ...publicManifest } };
|
|
210
|
+
} catch (error) {
|
|
211
|
+
if (published && (await exists(publicDir))) {
|
|
212
|
+
await rename(publicDir, stage).then(() => {
|
|
213
|
+
staged = true;
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (backedUp) {
|
|
218
|
+
await rename(backup, publicDir);
|
|
219
|
+
backedUp = false;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
throw error;
|
|
223
|
+
} finally {
|
|
224
|
+
if (cacheTemporary) {
|
|
225
|
+
await unlink(cacheTemporary).catch((error) => {
|
|
226
|
+
if (error.code !== 'ENOENT') {
|
|
227
|
+
throw error;
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
if (staged) {
|
|
233
|
+
await rm(stage, { recursive: true, force: true });
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|