Skip to content

Commit

Permalink
feat: add serve and deploy args from wrangler (#27)
Browse files Browse the repository at this point in the history
* feat: add all wrangler dev options to serve executor schema

* refactor: rename publish executor to deploy to match wrangler deprecation

* feat: add all wrangler deploy arguments to deploy executor

* docs: add new arguments documentation

* feat: add publish as an alias to deploy

* chore: upgrade nx ci to latest version

* Revert "chore: upgrade nx ci to latest version"

This reverts commit 037572e.

* feat: rename generator to set executor deploy instead of publish

* fix: adjust tests
  • Loading branch information
nacho-vazquez authored Oct 11, 2023
1 parent 35e5c10 commit beb2483
Show file tree
Hide file tree
Showing 16 changed files with 438 additions and 75 deletions.
70 changes: 61 additions & 9 deletions packages/plugins/nx-cloudflare/README.md

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions packages/plugins/nx-cloudflare/executors.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"serve": {
"implementation": "./src/executors/serve/serve.impl",
"schema": "./src/executors/serve/schema.json",
"description": "Serve cloudflare worker locally"
"description": "Serve cloudflare worker"
},
"publish": {
"implementation": "./src/executors/publish/publish.impl",
"schema": "./src/executors/publish/schema.json",
"description": "Publish worker to Cloudflare"
"deploy": {
"implementation": "./src/executors/deploy/deploy.impl",
"schema": "./src/executors/deploy/schema.json",
"description": "Deploy worker to Cloudflare",
"aliases": ["publish"]
},
"next-build": {
"implementation": "./src/executors/next-build/build.impl",
Expand Down
5 changes: 5 additions & 0 deletions packages/plugins/nx-cloudflare/src/executors/deploy/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { convertNxExecutor } from '@nx/devkit';

import deployExecutor from './deploy.impl';

export default convertNxExecutor(deployExecutor);
47 changes: 47 additions & 0 deletions packages/plugins/nx-cloudflare/src/executors/deploy/deploy.impl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ExecutorContext } from '@nx/devkit';
import { DeploySchema } from './schema';
import { fork } from 'child_process';
import { createCliOptions } from '../../utils/create-cli-options';

export default async function deployExecutor(
options: DeploySchema,
context: ExecutorContext
): Promise<{ success: boolean }> {
const projectRoot =
context.projectGraph?.nodes?.[context?.projectName]?.data?.root;

if (!projectRoot) {
throw new Error(
`Unable to find the Project Root for ${context.projectName}. Is it set in the project.json?`
);
}

const args = createCliOptions({ ...options });
const p = runWrangler(args, projectRoot);
p.stdout.on('data', (message) => {
process.stdout.write(message);
});
p.stderr.on('data', (message) => {
process.stderr.write(message);
});

return new Promise<{ success: boolean }>((resolve) => {
p.on('close', (code) => {
resolve({ success: code === 0 });
});
});
}

function runWrangler(args: string[], cwd: string) {
try {
const wranglerBin = require.resolve('wrangler/bin/wrangler');

return fork(wranglerBin, ['deploy', ...args], {
stdio: ['pipe', 'pipe', 'pipe', 'ipc'],
cwd,
});
} catch (e) {
console.error(e);
throw new Error('Unable to run Wrangler. Is Wrangler installed?');
}
}
22 changes: 22 additions & 0 deletions packages/plugins/nx-cloudflare/src/executors/deploy/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface DeploySchema {
name: string;
noBundle: boolean;
env: string;
outdir: string;
compatibilityDate: string;
compatibilityFlags: string[];
latest: boolean;
assets: string;
site: string;
siteInclude: string[];
siteExclude: string[];
var: string[];
define: string[];
triggers: string[];
routes: string[];
tsconfig: string;
minify: boolean;
nodeCompat: boolean;
dryRun: boolean;
keepVars: boolean;
}
110 changes: 110 additions & 0 deletions packages/plugins/nx-cloudflare/src/executors/deploy/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
{
"$schema": "http://json-schema.org/schema",
"version": 2,
"title": "Deploy executor",
"description": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Name of the Worker."
},
"noBundle": {
"type": "boolean",
"default": false,
"description": "Skip Wrangler’s build steps and directly deploy script without modification. Particularly useful when using custom builds."
},
"env": {
"type": "string",
"description": "Perform on a specific environment."
},
"outdir": {
"type": "string",
"description": "Path to directory where Wrangler will write the bundled Worker files."
},
"compatibilityDate": {
"type": "string",
"description": "A date in the form yyyy-mm-dd, which will be used to determine which version of the Workers runtime is used."
},
"compatibilityFlags": {
"type": "array",
"items": {
"type": "string"
},
"description": "Flags to use for compatibility checks."
},
"latest": {
"type": "boolean",
"default": true,
"description": "Use the latest version of the Workers runtime."
},
"assets": {
"type": "string",
"description": "Root folder of static assets to be served. Unlike --site, --assets does not require a Worker script to serve your assets."
},
"site": {
"type": "string",
"description": "Root folder of static assets for Workers Sites."
},
"siteInclude": {
"type": "array",
"items": {
"type": "string"
},
"description": "Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded."
},
"siteExclude": {
"type": "array",
"items": {
"type": "string"
},
"description": "Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded."
},
"var": {
"type": "array",
"items": {
"type": "string"
},
"description": "Array of key:value pairs to inject as variables into your code. The value will always be passed as a string to your Worker."
},
"define": {
"type": "array",
"items": {
"type": "string"
},
"description": "Array of key:value pairs to replace global identifiers in your code."
},
"triggers": {
"type": "array",
"items": {
"type": "string"
},
"description": "Cron schedules to attach to the deployed Worker. Refer to Cron Trigger Examples."
},
"routes": {
"type": "array",
"items": {
"type": "string"
},
"description": "Routes where this Worker will be deployed."
},
"tsconfig": {
"type": "string",
"description": "Path to a custom tsconfig.json file."
},
"minify": {
"type": "boolean",
"description": "Minify the bundled script before deploying."
},
"nodeCompat": {
"type": "boolean",
"description": "Enable node.js compatibility."
},
"keepVars": {
"type": "boolean",
"default": false,
"description": "It is recommended best practice to treat your Wrangler developer environment as a source of truth for your Worker configuration, and avoid making changes via the Cloudflare dashboard. If you change your environment variables or bindings in the Cloudflare dashboard, Wrangler will override them the next time you deploy. If you want to disable this behavior set keepVars to true."
}
},
"required": []
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

28 changes: 27 additions & 1 deletion packages/plugins/nx-cloudflare/src/executors/serve/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
export interface Schema {
export interface ServeSchema {
name?: string;
noBundle?: boolean;
env?: string;
compatibilityDate?: string;
compatibilityFlags?: string[];
latest?: boolean;
ip?: string;
port?: number;
inspectorPort?: number;
routes?: string[];
host?: string;
localProtocol?: 'http' | 'https';
localUpstream?: string;
assets?: string;
site?: string;
siteInclude?: string[];
siteExclude?: string[];
upstreamProtocol?: 'http' | 'https';
var?: string[];
define?: string[];
tsconfig?: string;
minify?: boolean;
nodeCompat?: boolean;
persistTo?: string;
remote?: boolean;
testScheduled?: boolean;
logLevel?: 'debug' | 'info' | 'log' | 'warn' | 'error' | 'none';
}
Loading

0 comments on commit beb2483

Please sign in to comment.