Skip to content

Commit

Permalink
feat: add nx deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
klaascuvelier committed Feb 3, 2022
1 parent 12a04e5 commit 8f47241
Show file tree
Hide file tree
Showing 20 changed files with 288 additions and 28 deletions.
29 changes: 1 addition & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,5 @@

This project was generated using [Nx](https://nx.dev).

<p style="text-align: center;"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-logo.png" width="450"></p>

🔎 **Powerful, Extensible Dev Tools**

## Generate a library

## Generate library
Run `nx g @nrwl/node:lib my-lib --publishable --importPath=@k11r/my-lib` to generate a library.

> You can also use any of the plugins above to generate libraries as well.
Libraries are shareable across libraries and applications. They can be imported from `@nx-additions/mylib`.

## Build

Run `nx build my-app` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build.

## Running unit tests

Run `nx test my-app` to execute the unit tests via [Jest](https://jestjs.io).

Run `nx affected:test` to execute the unit tests affected by a change.

## Understand your workspace

Run `nx dep-graph` to see a diagram of the dependencies of your projects.

## Further help

Visit the [Nx Documentation](https://nx.dev) to learn more.
3 changes: 3 additions & 0 deletions packages/nx-netlify-deploy/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
18 changes: 18 additions & 0 deletions packages/nx-netlify-deploy/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions packages/nx-netlify-deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nx-netlify-deploy

An executor and generator for netlify deploys using the cli

## Add `deploy` target to an existing project

`npx nx g @k11r/nx-netlify-deploy:add-deploy-target <app-name> --site-id=<site-id>`
10 changes: 10 additions & 0 deletions packages/nx-netlify-deploy/executors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/schema",
"executors": {
"deploy": {
"implementation": "./src/executors/deploy/executor",
"schema": "./src/executors/deploy/schema.json",
"description": "Netlify deploy executor"
}
}
}
12 changes: 12 additions & 0 deletions packages/nx-netlify-deploy/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "http://json-schema.org/schema",
"name": "nx-netlify-deploy",
"version": "0.0.1",
"generators": {
"add-deploy-target": {
"factory": "./src/generators/add-deploy-target/generator",
"schema": "./src/generators/add-deploy-target/schema.json",
"description": "Adds the netlify deploy target"
}
}
}
15 changes: 15 additions & 0 deletions packages/nx-netlify-deploy/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
displayName: 'nx-netlify-deploy',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
testEnvironment: 'node',
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/packages/nx-netlify-deploy',
};
9 changes: 9 additions & 0 deletions packages/nx-netlify-deploy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@k11r/nx-netlify-deploy",
"version": "0.0.3",
"generators": "./generators.json",
"executors": "./executors.json",
"peerDependencies": {
"netlify-cli": ">8"
}
}
21 changes: 21 additions & 0 deletions packages/nx-netlify-deploy/src/executors/deploy/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {ExecutorContext} from "@nrwl/tao/src/shared/workspace";
import {DeployExecutorSchema} from "./schema";
import {execSync} from "child_process";

export default async function deployExecutor(options: DeployExecutorSchema, context: ExecutorContext) {
const {siteId, skipBuild, prod, outputPath} = options;

if (!skipBuild) {
console.log('Building app currently not supported');
}

const command = `npx netlify deploy --dir=${outputPath} --site=${siteId} ${prod ? '--prod' : ''}`;

try {
execSync(command);
} catch (e) {
return {success: false, error: e.toString()}
}

return { success: true };
}
6 changes: 6 additions & 0 deletions packages/nx-netlify-deploy/src/executors/deploy/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface DeployExecutorSchema {
siteId: string;
outputPath: string;
skipBuild?: boolean
prod?: boolean
}
28 changes: 28 additions & 0 deletions packages/nx-netlify-deploy/src/executors/deploy/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"title": "Deploy executor",
"description": "Deploys an app to Netlify",
"type": "object",
"properties": {
"siteId": {
"type": "string",
"description": "Netlify site id"
},
"outputPath": {
"type": "string",
"description": "Path to artifacts"
},
"skipBuild": {
"type": "boolean",
"description": "Should build step gets skipped",
"default": false
},
"prod": {
"type": "boolean",
"description": "Is production deploy",
"default": false
}
},
"required": ["siteId", "outputPath"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { readProjectConfiguration, Tree, updateProjectConfiguration } from '@nrwl/devkit';
import { AddDeployTargetGeneratorSchema } from './schema';

export default async function (tree: Tree, options: AddDeployTargetGeneratorSchema) {
try {
console.log("Adding 'deploy' target to " + options.appName);
const projectConfiguration = readProjectConfiguration(tree, options.appName);
const outputPath = options.outputPath ?? projectConfiguration.targets['build']['options']['outputPath'];

projectConfiguration.targets = {
...projectConfiguration.targets,
deploy: {
executor: '@k11r/nx-netlify-deploy:deploy',
options: {
siteId: options.siteId,
outputPath
},
},
};
updateProjectConfiguration(tree, options.appName, projectConfiguration);
} catch (e) {
console.error(e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface AddDeployTargetGeneratorSchema {
appName: string;
siteId: string;
outputPath?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "AddDeployTarget",
"title": "",
"type": "object",
"properties": {
"appName": {
"type": "string",
"description": "Name of the app to add the deploy target to",
"$default": {
"$source": "argv",
"index": 0
}
},
"siteId": {
"type": "string",
"description": "Netlify site id"
},
"outputPath": {
"type": "string",
"description": "Path to the artifacts"
}
},
"required": [
"appName",
"siteId"
]
}
5 changes: 5 additions & 0 deletions packages/nx-netlify-deploy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './executors/deploy/executor';
export * from './executors/deploy/schema';

export * from './generators/add-deploy-target/generator';
export * from './generators/add-deploy-target/schema';
13 changes: 13 additions & 0 deletions packages/nx-netlify-deploy/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions packages/nx-netlify-deploy/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["**/*.spec.ts", "**/*.test.ts"],
"include": ["**/*.ts"]
}
19 changes: 19 additions & 0 deletions packages/nx-netlify-deploy/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"**/*.d.ts"
]
}
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@k11r/nx-netlify-deploy": ["packages/nx-netlify-deploy/src/index.ts"],
"@k11r/nx-rsync-deployer": ["packages/nx-rsync-deployer/src/index.ts"]
}
},
Expand Down
51 changes: 51 additions & 0 deletions workspace.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,57 @@
{
"version": 2,
"projects": {
"nx-netlify-deploy": {
"root": "packages/nx-netlify-deploy",
"sourceRoot": "packages/nx-netlify-deploy/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/nx-netlify-deploy/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/packages/nx-netlify-deploy"],
"options": {
"jestConfig": "packages/nx-netlify-deploy/jest.config.js",
"passWithNoTests": true
}
},
"build": {
"executor": "@nrwl/node:package",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/nx-netlify-deploy",
"tsConfig": "packages/nx-netlify-deploy/tsconfig.lib.json",
"packageJson": "packages/nx-netlify-deploy/package.json",
"main": "packages/nx-netlify-deploy/src/index.ts",
"assets": [
"packages/nx-netlify-deploy/*.md",
{
"input": "./packages/nx-netlify-deploy/src",
"glob": "**/!(*.ts)",
"output": "./src"
},
{
"input": "./packages/nx-netlify-deploy/src",
"glob": "**/*.d.ts",
"output": "./src"
},
{
"input": "./packages/nx-netlify-deploy",
"glob": "{generators,executors}.json",
"output": "."
}
]
}
}
},
"tags": []
},
"nx-rsync-deployer": {
"root": "packages/nx-rsync-deployer",
"sourceRoot": "packages/nx-rsync-deployer/src",
Expand Down

0 comments on commit 8f47241

Please sign in to comment.