-
Notifications
You must be signed in to change notification settings - Fork 0
feat: setup docker schema #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { SchematicTestRunner, type UnitTestTree } from "@angular-devkit/schematics/testing"; | ||
| import { resolve } from "node:path"; | ||
| import { beforeAll, describe, expect, it } from "vitest"; | ||
|
|
||
| const collectionPath = resolve(__dirname, "../dist/collection.json"); | ||
|
|
||
| describe("docker schematic", () => { | ||
| const runner = new SchematicTestRunner("schematics", collectionPath); | ||
|
|
||
| describe("with npm package manager", () => { | ||
| let tree: UnitTestTree; | ||
|
|
||
| describe.each(["npm", "yarn", "pnpm", "bun"] as const)( | ||
| "with ${packageManager} package manager", | ||
| (packageManager) => { | ||
| beforeAll(async () => { | ||
| tree = await runner.runSchematic("docker", { | ||
| name: `${packageManager}-test-app`, | ||
| packageManager, | ||
| }); | ||
| }); | ||
|
|
||
| it("should create Dockerfile", () => { | ||
| expect(tree.files).toContain(`/${packageManager}-test-app/Dockerfile`); | ||
| expect(tree.files).toContain(`/${packageManager}-test-app/.dockerignore`); | ||
| }); | ||
|
|
||
| it("should create docker file with install command", () => { | ||
| const content = tree.readContent(`/${packageManager}-test-app/Dockerfile`); | ||
| expect(content).toContain(`RUN ${packageManager} install`); | ||
| }); | ||
|
|
||
| it("should create docker file with build command", () => { | ||
| const content = tree.readContent(`/${packageManager}-test-app/Dockerfile`); | ||
| expect(content).toContain(`RUN ${packageManager} run build`); | ||
| }); | ||
|
|
||
| it("should create docker file with start command", () => { | ||
| const content = tree.readContent(`/${packageManager}-test-app/Dockerfile`); | ||
| expect(content).toContain(`CMD ["${packageManager}", "run", "start"]`); | ||
| }); | ||
| }, | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| import { type Path, strings } from "@angular-devkit/core"; | ||
| import { | ||
| type Rule, | ||
| type Source, | ||
| apply, | ||
| mergeWith, | ||
| move, | ||
| template, | ||
| url, | ||
| } from "@angular-devkit/schematics"; | ||
|
|
||
| import { toKebabCase } from "@utils/formatting"; | ||
| import { resolvePackageName } from "@utils/name"; | ||
|
|
||
| import { DEFAULT_APP_NAME, DEFAULT_PACKAGE_MANAGER } from "~/defaults"; | ||
|
|
||
| import { type DockerOptions } from "./docker.options"; | ||
| import { type DockerSchema } from "./docker.schema"; | ||
|
|
||
| const transform = (schema: DockerSchema): DockerOptions => { | ||
| const name = resolvePackageName(toKebabCase(schema.name?.toString() ?? DEFAULT_APP_NAME)); | ||
|
|
||
| return { | ||
| name, | ||
| packageManager: schema.packageManager ?? DEFAULT_PACKAGE_MANAGER, | ||
| directory: schema.directory, | ||
| }; | ||
| }; | ||
|
|
||
| const generate = (options: DockerOptions, path: string): Source => { | ||
| return apply(url("./files" as Path), [ | ||
| template({ | ||
| ...strings, | ||
| ...options, | ||
| }), | ||
| move(path), | ||
| ]); | ||
| }; | ||
|
|
||
| export const main = (schema: DockerSchema): Rule => { | ||
| const options = transform(schema); | ||
| return mergeWith(generate(options, schema.directory ?? options.name)); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export interface DockerOptions { | ||
| /** | ||
| * NanoForge application name | ||
| */ | ||
| name: string; | ||
|
|
||
| /** | ||
| * NanoForge application package manager | ||
| */ | ||
| packageManager: "npm" | "yarn" | "pnpm" | "bun"; | ||
|
|
||
| /** | ||
| * Target directory for generated Docker files | ||
| */ | ||
| directory?: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| export interface DockerSchema { | ||
| /** | ||
| * NanoForge application name | ||
| */ | ||
| name: string; | ||
|
|
||
| /** | ||
| * NanoForge application package manager | ||
| */ | ||
| packageManager: "npm" | "yarn" | "pnpm" | "bun"; | ||
|
|
||
| /** | ||
| * Target directory for generated Docker files | ||
| */ | ||
| directory?: string; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| node_modules |
|
MartinFillon marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <% if (packageManager === 'bun') { %> | ||
| FROM oven/bun:1.3 | ||
| <% } else { %> | ||
| FROM node:25-alpine | ||
| <% } %> | ||
|
|
||
| <% if (packageManager === 'pnpm') { %> | ||
| ENV PNPM_HOME="/pnpm" | ||
| ENV PATH="$PNPM_HOME:$PATH" | ||
| RUN npm install -g pnpm | ||
| <% } %> | ||
|
MartinFillon marked this conversation as resolved.
|
||
|
|
||
| <% if (packageManager === 'yarn') { %> | ||
| RUN npm install -g yarn | ||
| <% } %> | ||
|
MartinFillon marked this conversation as resolved.
|
||
|
|
||
| WORKDIR /app | ||
| COPY package.json ./ | ||
| <% if (packageManager === 'npm') { %> | ||
| COPY package-lock.json ./ | ||
| <% } else if (packageManager === 'yarn') { %> | ||
| COPY yarn.lock ./ | ||
| <% } else if (packageManager === 'pnpm') { %> | ||
| COPY pnpm-lock.yaml ./ | ||
| <% } else if (packageManager === 'bun') { %> | ||
| COPY bun.lock ./ | ||
| <% } %> | ||
|
MartinFillon marked this conversation as resolved.
|
||
| <% if (packageManager === 'npm') { %> | ||
| RUN npm install --no-lockfile | ||
| <% } if (packageManager === 'pnpm') { %> | ||
| RUN pnpm install --frozen-lockfile --allow-build=bun | ||
| <% } else { %> | ||
| RUN <%= packageManager %> install --frozen-lockfile | ||
|
MartinFillon marked this conversation as resolved.
|
||
| <% } %> | ||
| COPY . . | ||
|
|
||
| RUN <%= packageManager %> run build | ||
|
|
||
| CMD ["<%= packageManager %>", "run", "start"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "$id": "SchematicsNanoForgeDocker", | ||
| "title": "NanoForge Base Docker Options Schema", | ||
| "type": "object", | ||
| "properties": { | ||
| "name": { | ||
| "type": "string", | ||
| "description": "The name of the application", | ||
| "$default": { | ||
| "$source": "argv", | ||
| "index": 0 | ||
| }, | ||
| "x-prompt": "What name would you like to use for the new project ?" | ||
| }, | ||
| "packageManager": { | ||
| "type": "string", | ||
| "enum": ["npm", "yarn", "pnpm", "bun"], | ||
| "description": "NanoForge application package manager", | ||
| "default": "npm" | ||
| }, | ||
| "directory": { | ||
| "type": "string", | ||
| "description": "NanoForge application destination directory" | ||
| } | ||
| }, | ||
| "required": ["name"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.