Skip to content

Commit

Permalink
feat(cf-workers): add cloudflare-workers package
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed May 20, 2022
1 parent e9bab52 commit fff47c8
Show file tree
Hide file tree
Showing 30 changed files with 4,315 additions and 161 deletions.
2,247 changes: 2,157 additions & 90 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@nrwl/js": "14.1.5",
"@nrwl/linter": "14.1.5",
"@nrwl/nx-cloud": "latest",
"@nrwl/nx-plugin": "^14.1.7",
"@nrwl/workspace": "14.1.5",
"@swc/cli": "~0.1.55",
"@swc/core": "~1.2.143",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ describe('cloudflare-workers e2e', () => {
it('should create cloudflare-workers', async () => {
const project = uniq('cloudflare-workers');
await runNxCommandAsync(
`generate @mono-ukulele/cf-workers:cloudflare-workers ${project}`
`generate @mono-ukulele/cf-workers:worker ${project}`
);
const result = await runNxCommandAsync(`build ${project}`);
expect(result.stdout).toContain('Executor ran');
expect(result.stdout).toContain('Successfully ran target build');
}, 120000);

describe('--directory', () => {
it('should create src in the specified directory', async () => {
const project = uniq('cloudflare-workers');
await runNxCommandAsync(
`generate @mono-ukulele/cf-workers:cloudflare-workers ${project} --directory subdir`
`generate @mono-ukulele/cf-workers:worker ${project} --directory subdir`
);
expect(() =>
checkFilesExist(`libs/subdir/${project}/src/index.ts`)
Expand All @@ -55,7 +55,7 @@ describe('cloudflare-workers e2e', () => {
'dist/packages/cloudflare/workers'
);
await runNxCommandAsync(
`generate @mono-ukulele/cf-workers:cloudflare-workers ${projectName} --tags e2etag,e2ePackage`
`generate @mono-ukulele/cf-workers:worker ${projectName} --tags e2etag,e2ePackage`
);
const project = readJson(`libs/${projectName}/project.json`);
expect(project.tags).toEqual(['e2etag', 'e2ePackage']);
Expand Down
8 changes: 4 additions & 4 deletions packages/cloudflare/workers/executors.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"$schema": "http://json-schema.org/schema",
"executors": {
"build": {
"implementation": "./src/executors/build/executor",
"schema": "./src/executors/build/schema.json",
"description": "build executor"
"serve": {
"implementation": "./src/executors/serve/serve",
"schema": "./src/executors/serve/schema.json",
"description": "serve executor"
}
}
}
18 changes: 14 additions & 4 deletions packages/cloudflare/workers/generators.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@
"name": "cloudflare-workers",
"version": "0.0.1",
"generators": {
"cloudflare-workers": {
"factory": "./src/generators/cloudflare-workers/generator",
"schema": "./src/generators/cloudflare-workers/schema.json",
"description": "cloudflare-workers generator"
"worker": {
"factory": "./src/generators/worker/worker",
"schema": "./src/generators/worker/schema.json",
"description": "Generate a cloudflare worker"
},
"kv": {
"factory": "./src/generators/kv/kv",
"schema": "./src/generators/kv/schema.json",
"description": "Add KV to a cloudflare worker"
},
"init": {
"factory": "./src/generators/init/init",
"schema": "./src/generators/init/schema.json",
"description": "init generator"
}
}
}
6 changes: 5 additions & 1 deletion packages/cloudflare/workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
"version": "0.0.1",
"main": "src/index.js",
"generators": "./generators.json",
"executors": "./executors.json"
"executors": "./executors.json",
"devDependencies": {
"wrangler": "^2.0.2",
"@cloudflare/workers-types": "^3.10.0"
}
}
11 changes: 0 additions & 11 deletions packages/cloudflare/workers/src/executors/build/executor.spec.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/cloudflare/workers/src/executors/build/executor.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export interface ServeExecutorSchema {} // eslint-disable-line
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"title": "Build executor",
"title": "Serve executor",
"description": "",
"type": "object",
"properties": {},
Expand Down
12 changes: 12 additions & 0 deletions packages/cloudflare/workers/src/executors/serve/serve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ServeExecutorSchema } from './schema';

export default async function runExecutor(
options: ServeExecutorSchema,
) {
console.log('Executor ran for Serve', options)

return {
success: true
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { Tree, readProjectConfiguration } from '@nrwl/devkit';

import generator from './generator';
import { CloudflareWorkersGeneratorSchema } from './schema';
import { InitGeneratorSchema } from './schema';

describe('cloudflare-workers generator', () => {
describe('init generator', () => {
let appTree: Tree;
const options: CloudflareWorkersGeneratorSchema = { name: 'test' };
const options: InitGeneratorSchema = { name: 'test' };

beforeEach(() => {
appTree = createTreeWithEmptyWorkspace();
Expand All @@ -16,5 +16,5 @@ describe('cloudflare-workers generator', () => {
await generator(appTree, options);
const config = readProjectConfiguration(appTree, 'test');
expect(config).toBeDefined();
});
})
});
18 changes: 18 additions & 0 deletions packages/cloudflare/workers/src/generators/init/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
addDependenciesToPackageJson,
Tree,
} from '@nrwl/devkit';

export function workerInit(tree: Tree) {
const installTask = addDependenciesToPackageJson(tree, {tslib: '^2.4.0'}, {
"@nrwl/js": "14.1.7",
"wrangler": "^2.0.2",
"@cloudflare/workers-types": "^3.10.0"
})

return () => {
installTask();
}
}

export default workerInit;
5 changes: 5 additions & 0 deletions packages/cloudflare/workers/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface InitGeneratorSchema {
name: string;
tags?: string;
directory?: string;
}
29 changes: 29 additions & 0 deletions packages/cloudflare/workers/src/generators/init/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "Init",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
},
"tags": {
"type": "string",
"description": "Add tags to the project (used for linting)",
"alias": "t"
},
"directory": {
"type": "string",
"description": "A directory where the project is placed",
"alias": "d"
}
},
"required": ["name"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ import {
Tree,
} from '@nrwl/devkit';
import * as path from 'path';
import { CloudflareWorkersGeneratorSchema } from './schema';
import { KvGeneratorSchema } from './schema';

interface NormalizedSchema extends CloudflareWorkersGeneratorSchema {
interface NormalizedSchema extends KvGeneratorSchema {
projectName: string;
projectRoot: string;
projectDirectory: string;
parsedTags: string[];
parsedTags: string[]
}

function normalizeOptions(
tree: Tree,
options: CloudflareWorkersGeneratorSchema
): NormalizedSchema {
function normalizeOptions(tree: Tree, options: KvGeneratorSchema): NormalizedSchema {
const name = names(options.name).fileName;
const projectDirectory = options.directory
? `${names(options.directory).fileName}/${name}`
Expand All @@ -41,36 +38,32 @@ function normalizeOptions(
}

function addFiles(tree: Tree, options: NormalizedSchema) {
const templateOptions = {
...options,
...names(options.name),
offsetFromRoot: offsetFromRoot(options.projectRoot),
template: '',
};
generateFiles(
tree,
path.join(__dirname, 'files'),
options.projectRoot,
templateOptions
);
const templateOptions = {
...options,
...names(options.name),
offsetFromRoot: offsetFromRoot(options.projectRoot),
template: ''
};
generateFiles(tree, path.join(__dirname, 'files'), options.projectRoot, templateOptions);
}

export default async function (
tree: Tree,
options: CloudflareWorkersGeneratorSchema
) {
export default async function (tree: Tree, options: KvGeneratorSchema) {
const normalizedOptions = normalizeOptions(tree, options);
addProjectConfiguration(tree, normalizedOptions.projectName, {
root: normalizedOptions.projectRoot,
projectType: 'library',
sourceRoot: `${normalizedOptions.projectRoot}/src`,
targets: {
build: {
executor: '@mono-ukulele/cf-workers:build',
addProjectConfiguration(
tree,
normalizedOptions.projectName,
{
root: normalizedOptions.projectRoot,
projectType: 'library',
sourceRoot: `${normalizedOptions.projectRoot}/src`,
targets: {
build: {
executor: "@mono-ukulele/cf-workers:build",
},
},
},
tags: normalizedOptions.parsedTags,
});
tags: normalizedOptions.parsedTags,
}
);
addFiles(tree, normalizedOptions);
await formatFiles(tree);
}
5 changes: 5 additions & 0 deletions packages/cloudflare/workers/src/generators/kv/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface KvGeneratorSchema {
name: string;
tags?: string;
directory?: string;
}
29 changes: 29 additions & 0 deletions packages/cloudflare/workers/src/generators/kv/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "Kv",
"title": "",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What name would you like to use?"
},
"tags": {
"type": "string",
"description": "Add tags to the project (used for linting)",
"alias": "t"
},
"directory": {
"type": "string",
"description": "A directory where the project is placed",
"alias": "d"
}
},
"required": ["name"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`cloudflare-workers generator should generate files 1`] = `
"name = \\"my-worker-app\\"
compatibility_date = \\"2022-05-20\\"
[env.dev]
kv_namespaces = []
[env.production]
kv_namespaces = []
"
`;

exports[`cloudflare-workers generator should generate files 2`] = `
Object {
"configurations": Object {
"production": Object {
"commands": Array [
"npx wrangler dev src/index.ts --tsconfig.json=tsconfig.lib.json --env=production",
],
},
},
"executor": "@nrwl/workspace:run-commands",
"options": Object {
"commands": Array [
"npx wrangler dev src/index.ts --tsconfig.json=tsconfig.lib.json --env=dev",
],
"cwd": "libs/my-worker-app",
},
}
`;
Loading

0 comments on commit fff47c8

Please sign in to comment.