Skip to content

Commit a6e725a

Browse files
committed
feat(qwik-nx): setup ssg generator
1 parent 2e1f4f6 commit a6e725a

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

packages/qwik-nx/generators.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
"factory": "./src/generators/integrations/cloudflare-pages-integration/generator",
5555
"schema": "./src/generators/integrations/cloudflare-pages-integration/schema.json",
5656
"description": "Qwik City Cloudflare Pages adaptor allows you to connect Qwik City to Cloudflare Pages"
57+
},
58+
"setup-ssg": {
59+
"factory": "./src/generators/setup-ssg/generator",
60+
"schema": "./src/generators/setup-ssg/schema.json",
61+
"description": "setup-ssg generator"
5762
}
5863
}
5964
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const variable = "<%= projectName %>";
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
2+
import { Tree, readProjectConfiguration } from '@nrwl/devkit';
3+
4+
import generator from './generator';
5+
import { SetupSsgGeneratorSchema } from './schema';
6+
7+
describe('setup-ssg generator', () => {
8+
let appTree: Tree;
9+
const options: SetupSsgGeneratorSchema = { name: 'test' };
10+
11+
beforeEach(() => {
12+
appTree = createTreeWithEmptyWorkspace();
13+
});
14+
15+
it('should run successfully', async () => {
16+
await generator(appTree, options);
17+
const config = readProjectConfiguration(appTree, 'test');
18+
expect(config).toBeDefined();
19+
});
20+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
addProjectConfiguration,
3+
formatFiles,
4+
generateFiles,
5+
getWorkspaceLayout,
6+
names,
7+
offsetFromRoot,
8+
Tree,
9+
} 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+
}
19+
20+
function normalizeOptions(
21+
tree: Tree,
22+
options: SetupSsgGeneratorSchema
23+
): 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+
: [];
33+
34+
return {
35+
...options,
36+
projectName,
37+
projectRoot,
38+
projectDirectory,
39+
parsedTags,
40+
};
41+
}
42+
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
55+
);
56+
}
57+
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`,
64+
targets: {
65+
build: {
66+
executor: 'qwik-nx:build',
67+
},
68+
},
69+
tags: normalizedOptions.parsedTags,
70+
});
71+
addFiles(tree, normalizedOptions);
72+
await formatFiles(tree);
73+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface SetupSsgGeneratorSchema {
2+
name: string;
3+
tags?: string;
4+
directory?: string;
5+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"cli": "nx",
4+
"$id": "SetupSsg",
5+
"title": "",
6+
"type": "object",
7+
"properties": {
8+
"name": {
9+
"type": "string",
10+
"description": "",
11+
"$default": {
12+
"$source": "argv",
13+
"index": 0
14+
},
15+
"x-prompt": "What name would you like to use?"
16+
},
17+
"tags": {
18+
"type": "string",
19+
"description": "Add tags to the project (used for linting)",
20+
"alias": "t"
21+
},
22+
"directory": {
23+
"type": "string",
24+
"description": "A directory where the project is placed"
25+
}
26+
},
27+
"required": ["name"]
28+
}

0 commit comments

Comments
 (0)