|
1 | 1 | import {
|
2 |
| - addProjectConfiguration, |
3 | 2 | formatFiles,
|
4 |
| - generateFiles, |
5 |
| - getWorkspaceLayout, |
6 |
| - names, |
7 |
| - offsetFromRoot, |
| 3 | + joinPathFragments, |
| 4 | + logger, |
| 5 | + readProjectConfiguration, |
8 | 6 | Tree,
|
| 7 | + updateProjectConfiguration, |
9 | 8 | } from '@nrwl/devkit';
|
10 |
| -import * as path from 'path'; |
11 |
| -import { SetupSsgGeneratorSchema } from './schema'; |
12 |
| - |
13 |
| -interface NormalizedSchema extends SetupSsgGeneratorSchema { |
14 |
| - projectName: string; |
15 |
| - projectRoot: string; |
16 |
| - projectDirectory: string; |
17 |
| - parsedTags: string[]; |
18 |
| -} |
| 9 | +import { addSsgFiles } from './lib/add-ssg-files'; |
| 10 | +import { NormalizedSchema, SetupSsgGeneratorSchema } from './schema'; |
19 | 11 |
|
20 | 12 | function normalizeOptions(
|
21 | 13 | tree: Tree,
|
22 | 14 | options: SetupSsgGeneratorSchema
|
23 | 15 | ): NormalizedSchema {
|
24 |
| - const name = names(options.name).fileName; |
25 |
| - const projectDirectory = options.directory |
26 |
| - ? `${names(options.directory).fileName}/${name}` |
27 |
| - : name; |
28 |
| - const projectName = projectDirectory.replace(new RegExp('/', 'g'), '-'); |
29 |
| - const projectRoot = `${getWorkspaceLayout(tree).libsDir}/${projectDirectory}`; |
30 |
| - const parsedTags = options.tags |
31 |
| - ? options.tags.split(',').map((s) => s.trim()) |
32 |
| - : []; |
| 16 | + const project = readProjectConfiguration(tree, options.project); |
| 17 | + const projectRoot = project.root; |
33 | 18 |
|
34 | 19 | return {
|
35 | 20 | ...options,
|
36 |
| - projectName, |
37 | 21 | projectRoot,
|
38 |
| - projectDirectory, |
39 |
| - parsedTags, |
40 | 22 | };
|
41 | 23 | }
|
42 | 24 |
|
43 |
| -function addFiles(tree: Tree, options: NormalizedSchema) { |
44 |
| - const templateOptions = { |
45 |
| - ...options, |
46 |
| - ...names(options.name), |
47 |
| - offsetFromRoot: offsetFromRoot(options.projectRoot), |
48 |
| - template: '', |
49 |
| - }; |
50 |
| - generateFiles( |
51 |
| - tree, |
52 |
| - path.join(__dirname, 'files'), |
53 |
| - options.projectRoot, |
54 |
| - templateOptions |
| 25 | +export async function setupSsgGenerator( |
| 26 | + tree: Tree, |
| 27 | + options: SetupSsgGeneratorSchema |
| 28 | +) { |
| 29 | + const normalizedOptions = normalizeOptions(tree, options); |
| 30 | + |
| 31 | + const { projectRoot } = normalizedOptions; |
| 32 | + |
| 33 | + const ssgConfigPath = joinPathFragments( |
| 34 | + projectRoot, |
| 35 | + 'adapters', |
| 36 | + 'static', |
| 37 | + 'vite.config.ts' |
55 | 38 | );
|
56 |
| -} |
57 | 39 |
|
58 |
| -export default async function (tree: Tree, options: SetupSsgGeneratorSchema) { |
59 |
| - const normalizedOptions = normalizeOptions(tree, options); |
60 |
| - addProjectConfiguration(tree, normalizedOptions.projectName, { |
61 |
| - root: normalizedOptions.projectRoot, |
62 |
| - projectType: 'library', |
63 |
| - sourceRoot: `${normalizedOptions.projectRoot}/src`, |
| 40 | + if (tree.exists(ssgConfigPath)) { |
| 41 | + logger.info( |
| 42 | + `Skipping setup since there is an existing static adapter configuration. For more configuration, see https://qwik.builder.io/qwikcity/guides/static-site-generation/.` |
| 43 | + ); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const projectConfig = readProjectConfiguration(tree, options.project); |
| 48 | + |
| 49 | + addSsgFiles(tree, normalizedOptions); |
| 50 | + |
| 51 | + updateProjectConfiguration(tree, options.project, { |
| 52 | + ...projectConfig, |
64 | 53 | targets: {
|
65 |
| - build: { |
66 |
| - executor: 'qwik-nx:build', |
| 54 | + ...projectConfig.targets, |
| 55 | + 'build-server': { |
| 56 | + executor: '@nrwl/vite:build', |
| 57 | + options: { |
| 58 | + outputPath: 'dist/docs', |
| 59 | + configFile: ssgConfigPath, |
| 60 | + }, |
67 | 61 | },
|
68 | 62 | },
|
69 |
| - tags: normalizedOptions.parsedTags, |
70 | 63 | });
|
71 |
| - addFiles(tree, normalizedOptions); |
| 64 | + |
72 | 65 | await formatFiles(tree);
|
73 | 66 | }
|
| 67 | + |
| 68 | +export default setupSsgGenerator; |
0 commit comments