create-o2s-app 4.1.10 → 4.1.11
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/CHANGELOG.md +12 -0
- package/dist/scaffold/get-aliases-to-remove.d.ts +9 -0
- package/dist/scaffold/get-aliases-to-remove.d.ts.map +1 -0
- package/dist/scaffold/get-aliases-to-remove.js +21 -0
- package/dist/scaffold/get-aliases-to-remove.spec.d.ts +2 -0
- package/dist/scaffold/get-aliases-to-remove.spec.d.ts.map +1 -0
- package/dist/scaffold/get-aliases-to-remove.spec.js +26 -0
- package/dist/scaffold/index.d.ts.map +1 -1
- package/dist/scaffold/index.js +2 -0
- package/dist/scaffold/transform-app-module.d.ts.map +1 -1
- package/dist/scaffold/transform-app-module.js +4 -15
- package/dist/scaffold/transform-blocks-config.d.ts +7 -0
- package/dist/scaffold/transform-blocks-config.d.ts.map +1 -0
- package/dist/scaffold/transform-blocks-config.js +134 -0
- package/dist/scaffold/transform-blocks-config.spec.d.ts +2 -0
- package/dist/scaffold/transform-blocks-config.spec.d.ts.map +1 -0
- package/dist/scaffold/transform-blocks-config.spec.js +190 -0
- package/dist/scaffold/transform-legacy-block-files.spec.d.ts +2 -0
- package/dist/scaffold/transform-legacy-block-files.spec.d.ts.map +1 -0
- package/dist/scaffold/transform-legacy-block-files.spec.js +89 -0
- package/dist/scaffold/transform-page-model.d.ts.map +1 -1
- package/dist/scaffold/transform-page-model.js +4 -15
- package/dist/scaffold/transform-real-files.spec.d.ts +2 -0
- package/dist/scaffold/transform-real-files.spec.d.ts.map +1 -0
- package/dist/scaffold/transform-real-files.spec.js +154 -0
- package/dist/scaffold/transform-render-blocks.d.ts.map +1 -1
- package/dist/scaffold/transform-render-blocks.js +63 -17
- package/dist/scaffold/transform-render-blocks.spec.d.ts +2 -0
- package/dist/scaffold/transform-render-blocks.spec.d.ts.map +1 -0
- package/dist/scaffold/transform-render-blocks.spec.js +126 -0
- package/package.json +3 -1
- package/src/scaffold/get-aliases-to-remove.spec.ts +30 -0
- package/src/scaffold/get-aliases-to-remove.ts +19 -0
- package/src/scaffold/index.ts +2 -0
- package/src/scaffold/transform-app-module.ts +4 -16
- package/src/scaffold/transform-blocks-config.spec.ts +215 -0
- package/src/scaffold/transform-blocks-config.ts +113 -0
- package/src/scaffold/transform-legacy-block-files.spec.ts +68 -0
- package/src/scaffold/transform-page-model.ts +4 -16
- package/src/scaffold/transform-real-files.spec.ts +153 -0
- package/src/scaffold/transform-render-blocks.spec.ts +129 -0
- package/src/scaffold/transform-render-blocks.ts +73 -18
- package/vitest.config.mjs +3 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { transformRenderBlocks } from './transform-render-blocks';
|
|
2
|
+
import * as fs from 'fs-extra';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|
6
|
+
|
|
7
|
+
const FILE_PATH = 'apps/frontend/src/blocks/renderBlocks.tsx';
|
|
8
|
+
|
|
9
|
+
const RENDER_BLOCKS = `import * as Faq from '@o2s/blocks.faq/frontend';
|
|
10
|
+
import * as TicketList from '@o2s/blocks.ticket-list/frontend';
|
|
11
|
+
import * as UserAccount from '@o2s/blocks.user-account/frontend';
|
|
12
|
+
// BLOCK IMPORT
|
|
13
|
+
import { CMS, Models } from '@o2s/framework/modules';
|
|
14
|
+
|
|
15
|
+
import { auth } from '@/auth';
|
|
16
|
+
|
|
17
|
+
import { routing } from '@/i18n';
|
|
18
|
+
|
|
19
|
+
import { onSignOut } from '../actions/signOut';
|
|
20
|
+
|
|
21
|
+
type BlockProps = Models.BlockProps.FullBlockProps<typeof routing>;
|
|
22
|
+
|
|
23
|
+
export const renderBlocks = async (blocks: CMS.Model.Page.SlotBlock[]) => {
|
|
24
|
+
const session = await auth();
|
|
25
|
+
return blocks.map((block) => BLOCK_REGISTRY[block.__typename]({ ...block, session }));
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const BLOCK_REGISTRY = {
|
|
29
|
+
FaqBlock: (blockProps) => <Faq.Renderer {...blockProps} />,
|
|
30
|
+
TicketListBlock: (blockProps) => <TicketList.Renderer {...blockProps} />,
|
|
31
|
+
UserAccountBlock: (blockProps) => <UserAccount.Renderer {...blockProps} onSignOut={onSignOut} />,
|
|
32
|
+
// BLOCK REGISTER
|
|
33
|
+
};
|
|
34
|
+
`;
|
|
35
|
+
|
|
36
|
+
describe('transformRenderBlocks', () => {
|
|
37
|
+
let projectDir: string;
|
|
38
|
+
|
|
39
|
+
const writeFile = async (content: string) => {
|
|
40
|
+
await fs.outputFile(path.join(projectDir, FILE_PATH), content, 'utf-8');
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const readFile = () => fs.readFile(path.join(projectDir, FILE_PATH), 'utf-8');
|
|
44
|
+
|
|
45
|
+
beforeEach(async () => {
|
|
46
|
+
projectDir = await fs.mkdtemp(path.join(os.tmpdir(), 'create-o2s-app-render-blocks-'));
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
await fs.remove(projectDir);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('removes the import and registry entry of unselected blocks', async () => {
|
|
54
|
+
await writeFile(RENDER_BLOCKS);
|
|
55
|
+
|
|
56
|
+
await transformRenderBlocks(projectDir, ['faq', 'user-account']);
|
|
57
|
+
|
|
58
|
+
const result = await readFile();
|
|
59
|
+
expect(result).not.toContain('@o2s/blocks.ticket-list/frontend');
|
|
60
|
+
expect(result).not.toContain('TicketListBlock');
|
|
61
|
+
expect(result).toContain("import * as Faq from '@o2s/blocks.faq/frontend';");
|
|
62
|
+
expect(result).toContain('UserAccountBlock');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('keeps onSignOut while the user-account block is selected', async () => {
|
|
66
|
+
await writeFile(RENDER_BLOCKS);
|
|
67
|
+
|
|
68
|
+
await transformRenderBlocks(projectDir, ['faq', 'user-account']);
|
|
69
|
+
|
|
70
|
+
expect(await readFile()).toContain("import { onSignOut } from '../actions/signOut';");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('removes onSignOut together with the user-account block without leaving a double blank line', async () => {
|
|
74
|
+
await writeFile(RENDER_BLOCKS);
|
|
75
|
+
|
|
76
|
+
await transformRenderBlocks(projectDir, ['faq', 'ticket-list']);
|
|
77
|
+
|
|
78
|
+
const result = await readFile();
|
|
79
|
+
expect(result).not.toContain('onSignOut');
|
|
80
|
+
expect(result).not.toContain('UserAccount');
|
|
81
|
+
expect(result).toContain("import { routing } from '@/i18n';\n\ntype BlockProps");
|
|
82
|
+
expect(result).not.toMatch(/\n\n\n/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('does not remove imports that are still used elsewhere in the file', async () => {
|
|
86
|
+
await writeFile(
|
|
87
|
+
RENDER_BLOCKS.replace(
|
|
88
|
+
'const session = await auth();',
|
|
89
|
+
'const session = await auth();\n void onSignOut;',
|
|
90
|
+
),
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
await transformRenderBlocks(projectDir, ['faq']);
|
|
94
|
+
|
|
95
|
+
expect(await readFile()).toContain("import { onSignOut } from '../actions/signOut';");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('removes only the orphaned specifier from a multi-specifier import', async () => {
|
|
99
|
+
await writeFile(
|
|
100
|
+
RENDER_BLOCKS.replace(
|
|
101
|
+
"import { onSignOut } from '../actions/signOut';",
|
|
102
|
+
"import { onSignOut, signOutLabel } from '../actions/signOut';",
|
|
103
|
+
).replace('const session = await auth();', 'const session = await auth();\n void signOutLabel;'),
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
await transformRenderBlocks(projectDir, ['faq']);
|
|
107
|
+
|
|
108
|
+
expect(await readFile()).toContain("import { signOutLabel } from '../actions/signOut';");
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('does not touch unrelated imports', async () => {
|
|
112
|
+
await writeFile(RENDER_BLOCKS);
|
|
113
|
+
|
|
114
|
+
await transformRenderBlocks(projectDir, ['faq', 'ticket-list']);
|
|
115
|
+
|
|
116
|
+
const result = await readFile();
|
|
117
|
+
expect(result).toContain("import { CMS, Models } from '@o2s/framework/modules';");
|
|
118
|
+
expect(result).toContain("import { auth } from '@/auth';");
|
|
119
|
+
expect(result).toContain("import { routing } from '@/i18n';");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('leaves the file untouched when every block is selected', async () => {
|
|
123
|
+
await writeFile(RENDER_BLOCKS);
|
|
124
|
+
|
|
125
|
+
await transformRenderBlocks(projectDir, ['faq', 'ticket-list', 'user-account']);
|
|
126
|
+
|
|
127
|
+
expect(await readFile()).toBe(RENDER_BLOCKS);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { getAliasesToRemove } from './get-aliases-to-remove';
|
|
1
2
|
import * as fs from 'fs-extra';
|
|
2
3
|
import * as path from 'path';
|
|
3
4
|
|
|
@@ -9,35 +10,85 @@ const BLOCK_IMPORT_REGEX = /^import \* as (\w+) from '@o2s\/blocks\.([^/]+)\/fro
|
|
|
9
10
|
// Regex matches: SomeBlock: (blockProps) => <Alias.Renderer ...
|
|
10
11
|
const REGISTRY_ENTRY_REGEX = /^\s+\w+:\s*\(blockProps\)\s*=>\s*<(\w+)\.Renderer/;
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
// Regex matches a single-line named import: import { a, b as c } from '...';
|
|
14
|
+
const NAMED_IMPORT_REGEX = /^import \{([^}]+)\} from '([^']+)';$/;
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
const IDENTIFIER_REGEX = /\b[A-Za-z_$][\w$]*\b/g;
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Removes named imports that were only used by the removed registry entries
|
|
20
|
+
* (e.g. `onSignOut`, passed only to the user-account block), so the generated
|
|
21
|
+
* file passes `no-unused-vars`. Imports unrelated to removed entries are left untouched.
|
|
22
|
+
*/
|
|
23
|
+
const removeImportsOrphanedByRemovedEntries = (lines: string[], removedEntries: string[]): string[] => {
|
|
24
|
+
const candidates = new Set(removedEntries.flatMap((entry) => entry.match(IDENTIFIER_REGEX) ?? []));
|
|
25
|
+
if (candidates.size === 0) return lines;
|
|
26
|
+
|
|
27
|
+
// Identifiers still referenced outside the named imports (no dynamic RegExp needed)
|
|
28
|
+
const usedIdentifiers = new Set(
|
|
29
|
+
lines.filter((line) => !NAMED_IMPORT_REGEX.test(line)).flatMap((line) => line.match(IDENTIFIER_REGEX) ?? []),
|
|
30
|
+
);
|
|
31
|
+
const isUsed = (identifier: string) => usedIdentifiers.has(identifier);
|
|
32
|
+
|
|
33
|
+
const result: string[] = [];
|
|
34
|
+
// Set when an import line is dropped right after a blank line, so the blank line that
|
|
35
|
+
// separated it from the next import group is dropped too (no double blank lines).
|
|
36
|
+
let skipNextBlankLine = false;
|
|
19
37
|
|
|
20
|
-
// Discover alias → blockName map from the actual file
|
|
21
|
-
const aliasToBlock = new Map<string, string>();
|
|
22
38
|
for (const line of lines) {
|
|
23
|
-
const match = line.match(
|
|
24
|
-
if (match
|
|
25
|
-
|
|
39
|
+
const match = line.match(NAMED_IMPORT_REGEX);
|
|
40
|
+
if (!match || !match[1]) {
|
|
41
|
+
if (skipNextBlankLine && line.trim() === '') {
|
|
42
|
+
skipNextBlankLine = false;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
skipNextBlankLine = false;
|
|
46
|
+
result.push(line);
|
|
47
|
+
continue;
|
|
26
48
|
}
|
|
27
|
-
}
|
|
28
49
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
50
|
+
skipNextBlankLine = false;
|
|
51
|
+
|
|
52
|
+
const specifiers = match[1]
|
|
53
|
+
.split(',')
|
|
54
|
+
.map((specifier) => specifier.trim())
|
|
55
|
+
.filter(Boolean);
|
|
56
|
+
const kept = specifiers.filter((specifier) => {
|
|
57
|
+
const local =
|
|
58
|
+
specifier
|
|
59
|
+
.split(/\s+as\s+/)
|
|
60
|
+
.pop()
|
|
61
|
+
?.trim() ?? specifier;
|
|
62
|
+
return !candidates.has(local) || isUsed(local);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (kept.length === specifiers.length) {
|
|
66
|
+
result.push(line);
|
|
67
|
+
} else if (kept.length > 0) {
|
|
68
|
+
result.push(`import { ${kept.join(', ')} } from '${match[2]}';`);
|
|
69
|
+
} else {
|
|
70
|
+
skipNextBlankLine = result.length === 0 || result[result.length - 1]?.trim() === '';
|
|
34
71
|
}
|
|
35
72
|
}
|
|
36
73
|
|
|
74
|
+
return result;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const transformRenderBlocks = async (projectDir: string, selectedBlockNames: string[]): Promise<void> => {
|
|
78
|
+
const filePath = path.join(projectDir, FILE_PATH);
|
|
79
|
+
|
|
80
|
+
if (!(await fs.pathExists(filePath))) return;
|
|
81
|
+
|
|
82
|
+
const content = await fs.readFile(filePath, 'utf-8');
|
|
83
|
+
const lines = content.split('\n');
|
|
84
|
+
|
|
85
|
+
const aliasesToRemove = getAliasesToRemove(lines, BLOCK_IMPORT_REGEX, selectedBlockNames);
|
|
86
|
+
|
|
37
87
|
if (aliasesToRemove.size === 0) return;
|
|
38
88
|
|
|
39
89
|
// Process line by line
|
|
40
90
|
const result: string[] = [];
|
|
91
|
+
const removedEntries: string[] = [];
|
|
41
92
|
|
|
42
93
|
for (let i = 0; i < lines.length; i++) {
|
|
43
94
|
const line = lines[i];
|
|
@@ -49,14 +100,18 @@ export const transformRenderBlocks = async (projectDir: string, selectedBlockNam
|
|
|
49
100
|
continue;
|
|
50
101
|
}
|
|
51
102
|
|
|
52
|
-
// Remove BLOCK_REGISTRY entries for unselected blocks
|
|
103
|
+
// Remove BLOCK_REGISTRY entries for unselected blocks.
|
|
104
|
+
// Assumes every entry fits on a single line (true today). If prettier ever wraps an entry,
|
|
105
|
+
// identifiers on the following lines (e.g. `onSignOut={onSignOut}`) are not collected here;
|
|
106
|
+
// the real-file tests in transform-real-files.spec.ts would then fail.
|
|
53
107
|
const registryMatch = line.match(REGISTRY_ENTRY_REGEX);
|
|
54
108
|
if (registryMatch && registryMatch[1] && aliasesToRemove.has(registryMatch[1])) {
|
|
109
|
+
removedEntries.push(line);
|
|
55
110
|
continue;
|
|
56
111
|
}
|
|
57
112
|
|
|
58
113
|
result.push(line);
|
|
59
114
|
}
|
|
60
115
|
|
|
61
|
-
await fs.writeFile(filePath, result.join('\n'), 'utf-8');
|
|
116
|
+
await fs.writeFile(filePath, removeImportsOrphanedByRemovedEntries(result, removedEntries).join('\n'), 'utf-8');
|
|
62
117
|
};
|