-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2a294be
commit a3873be
Showing
11 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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 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,35 @@ | ||
{ | ||
"name": "setup-docker", | ||
"factory": "./src/generators/setup-docker/setup-docker", | ||
"schema": { | ||
"$schema": "http://json-schema.org/schema", | ||
"cli": "nx", | ||
"$id": "SchematicsNxSetupDocker", | ||
"title": "Nx Node Docker Options Schema", | ||
"description": "Nx Node Docker Options Schema.", | ||
"type": "object", | ||
"properties": { | ||
"projectName": { | ||
"description": "The name of the project", | ||
"$default": { "$source": "argv", "index": 0 }, | ||
"type": "string" | ||
}, | ||
"targetName": { | ||
"description": "The name of the target environment", | ||
"type": "string" | ||
}, | ||
"buildTargetName": { | ||
"description": "The name of the build Target", | ||
"type": "string", | ||
"default": "build" | ||
} | ||
}, | ||
"presets": [] | ||
}, | ||
"description": "Set up Docker configuration for a project.", | ||
"hidden": false, | ||
"implementation": "/packages/node/src/generators/setup-docker/setup-docker.ts", | ||
"aliases": [], | ||
"path": "/packages/node/src/generators/setup-docker/schema.json", | ||
"type": "generator" | ||
} |
This file contains 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 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
6 changes: 6 additions & 0 deletions
6
packages/node/src/generators/setup-docker/files/Dockerfile__tmpl__
This file contains 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,6 @@ | ||
FROM node:alpine | ||
|
||
WORKDIR <%= app %> | ||
COPY <%= buildLocation %> ./ | ||
|
||
RUN npm install |
This file contains 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,8 @@ | ||
export interface SetUpDockerOptions { | ||
projectName?: string; | ||
targetName?: string; | ||
buildTarget?: string; | ||
skipPackageJson?: boolean; | ||
skipFormat?: boolean; | ||
rootProject?: boolean; | ||
} |
This file contains 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,24 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"cli": "nx", | ||
"$id": "SchematicsNxSetupDocker", | ||
"title": "Nx Node Docker Options Schema", | ||
"description": "Nx Node Docker Options Schema.", | ||
"type": "object", | ||
"properties": { | ||
"projectName": { | ||
"description": "The name of the project", | ||
"$default": { "$source": "argv", "index": 0 }, | ||
"type": "string" | ||
}, | ||
"targetName": { | ||
"description": "The name of the target environment", | ||
"type": "string" | ||
}, | ||
"buildTargetName": { | ||
"description": "The name of the build Target", | ||
"type": "string", | ||
"default": "build" | ||
} | ||
} | ||
} |
Empty file.
This file contains 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,82 @@ | ||
import { | ||
addDependenciesToPackageJson, | ||
convertNxGenerator, | ||
formatFiles, | ||
generateFiles, | ||
GeneratorCallback, | ||
joinPathFragments, | ||
logger, | ||
readNxJson, | ||
readProjectConfiguration, | ||
Tree, | ||
updateProjectConfiguration, | ||
} from '@nrwl/devkit'; | ||
import { runTasksInSerial } from '@nrwl/workspace/src/utilities/run-tasks-in-serial'; | ||
import { SetUpDockerOptions } from './schema'; | ||
|
||
function normalizeOptions( | ||
tree: Tree, | ||
setupOptions: SetUpDockerOptions | ||
): SetUpDockerOptions { | ||
return { | ||
...setupOptions, | ||
projectName: setupOptions.projectName ?? readNxJson(tree).defaultProject, | ||
buildTarget: setupOptions.buildTarget ?? 'build', | ||
}; | ||
} | ||
|
||
function addDocker(tree: Tree, options: SetUpDockerOptions) { | ||
const project = readProjectConfiguration(tree, options.projectName); | ||
if (!project || !options.targetName) { | ||
return; | ||
} | ||
|
||
if (tree.exists(joinPathFragments(project.root, 'DockerFile'))) { | ||
logger.info( | ||
`Skipping setup since a DockFile already exists inside ${project.root}` | ||
); | ||
} | ||
const outputPath = | ||
project.targets[`${options.buildTarget}`]?.options.outputPath; | ||
generateFiles(tree, joinPathFragments(__dirname, './files'), project.root, { | ||
tmpl: '', | ||
app: project.sourceRoot, | ||
buildLocation: outputPath, | ||
}); | ||
} | ||
|
||
export function updateProjectConfig(tree: Tree, options: SetUpDockerOptions) { | ||
let projectConfig = readProjectConfiguration(tree, options.projectName); | ||
|
||
const buildPort = Number(process.env.PORT) ?? 3000; | ||
|
||
projectConfig.targets[`${options.targetName}`] = { | ||
dependsOn: [`^${options.buildTarget}`], | ||
executor: 'nx:run-commands', | ||
options: { | ||
commands: ['docker build .', `docker run -p ${buildPort}:${buildPort}`], | ||
}, | ||
}; | ||
|
||
updateProjectConfiguration(tree, options.projectName, projectConfig); | ||
} | ||
|
||
export async function setupDockerGenerator( | ||
tree: Tree, | ||
setupOptions: SetUpDockerOptions | ||
) { | ||
const tasks: GeneratorCallback[] = []; | ||
const options = normalizeOptions(tree, setupOptions); | ||
// Should check if the node project exists | ||
addDocker(tree, options); | ||
updateProjectConfig(tree, options); | ||
|
||
if (!options.skipFormat) { | ||
await formatFiles(tree); | ||
} | ||
|
||
return runTasksInSerial(...tasks); | ||
} | ||
|
||
export default setupDockerGenerator; | ||
export const setupDockerSchematic = convertNxGenerator(setupDockerGenerator); |