Skip to content

Commit

Permalink
feat: 🎸 work on deno
Browse files Browse the repository at this point in the history
  • Loading branch information
klaascuvelier committed Feb 9, 2022
1 parent eb3aabc commit 06982d9
Show file tree
Hide file tree
Showing 28 changed files with 414 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"singleQuote": true
"singleQuote": true,
"tabWidth": 4
}
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ This project was generated using [Nx](https://nx.dev).

## Available packages

| Package | Description |
|-----------------------------------------------------------------------------------|------------------------------------------------------------|
| [nx-cloudflare-workers-deploy](./packages/nx-cloudflare-workers-deploy/README.md) | Deployer and generator for functions/workers to Cloudflare |
| [nx-netlify-deploy](./packages/nx-netlify-deploy/README.md) | Deployer and generator for Netlify pages |
| [nx-rsync-deployer](./packages/nx-rsync-deployer/README.md) | Deployer to upload app artifacts using _rsync_ |
| Package | Description |
|-------------------------------------------------------------|------------------------------------------------|
| [nx-deno-deploy](./packages/nx-deno-deploy/README.md) | Deploy projects to Deno deploy |
| [nx-netlify-deploy](./packages/nx-netlify-deploy/README.md) | Deployer and generator for Netlify pages |
| [nx-rsync-deployer](./packages/nx-rsync-deployer/README.md) | Deployer to upload app artifacts using _rsync_ |

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

## Publish package
`npm publish -access public`
13 changes: 7 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"eslint": "8.7.0",
"eslint-config-prettier": "8.1.0",
"git-cz": "^4.8.0",
"glob": "^7.2.0",
"jest": "27.2.3",
"prettier": "2.5.1",
"ts-jest": "27.0.5",
Expand Down
3 changes: 3 additions & 0 deletions packages/nx-deno-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-deno-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": {}
}
]
}
8 changes: 8 additions & 0 deletions packages/nx-deno-deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# nx-deno-deploy

Deployer and generator for Deno projects

Requires [deno-ctl](https://github.com/denoland/deployctl).

## Add deno deploy to existing project
`npx nx g @k11r/nx-deno-deploy:add-deploy-target <app-name> --projectName=<some-name>`
10 changes: 10 additions & 0 deletions packages/nx-deno-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": "Deno deploy executor"
}
}
}
17 changes: 17 additions & 0 deletions packages/nx-deno-deploy/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$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 Deno deploy target"
},
"project": {
"factory": "./src/generators/create-deno-project/index",
"schema": "./src/generators/create-deno-project/schema.json",
"description": "Creates a deno project"
}
}
}
15 changes: 15 additions & 0 deletions packages/nx-deno-deploy/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = {
displayName: 'nx-deno-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-deno-deploy',
};
9 changes: 9 additions & 0 deletions packages/nx-deno-deploy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@k11r/nx-deno-deploy",
"version": "0.0.1",
"generators": "./generators.json",
"executors": "./executors.json",
"peerDependencies": {
"@nrwl/node": "^13.8.0"
}
}
10 changes: 10 additions & 0 deletions packages/nx-deno-deploy/src/executors/deploy/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {ExecutorContext} from "@nrwl/tao/src/shared/workspace";
import {DeployExecutorSchema} from "./schema";
import {deployProject} from "../../lib/deploy-ctl";

export default async function deployExecutor(options: DeployExecutorSchema, context: ExecutorContext) {
const {projectName, isProd, token, entryPath} = options;


return deployProject(entryPath, projectName, token, isProd);
}
6 changes: 6 additions & 0 deletions packages/nx-deno-deploy/src/executors/deploy/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface DeployExecutorSchema {
projectName: string;
entryPath: string;
token: string;
isProd?: boolean
}
27 changes: 27 additions & 0 deletions packages/nx-deno-deploy/src/executors/deploy/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"title": "Deploy executor",
"description": "Deploys a project to Deno",
"type": "object",
"properties": {
"projectName": {
"type": "string",
"description": "Deno project name"
},
"entryPath": {
"type": "string",
"description": "Entry path in the project artifacts"
},
"isProd": {
"type": "boolean",
"description": "Is production deploy",
"default": false
},
"token": {
"type": "string",
"description": "Deploy token",
}
},
"required": ["projectName", "entryPath"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 entryPath = options.entryPath ?? projectConfiguration.targets['build']['options']['outputPath'] + 'index.ts';
const projectName = options.projectName ?? options.appName;
const token = options.token ?? null;

projectConfiguration.targets = {
...projectConfiguration.targets,
deploy: {
executor: '@k11r/nx-deno-deploy:deploy',
options: {
projectName,
entryPath,
...(token ? {token} : null)
},
},
};
updateProjectConfiguration(tree, options.appName, projectConfiguration);
} catch (e) {
console.error(e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface AddDeployTargetGeneratorSchema {
appName: string;
projectName?: string;
entryPath?: string;
token?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"$id": "AddDeployTarget",
"title": "",
"type": "object",
"properties": {
"appName": {
"type": "string",
"description": "Name of the project to add deno deploy to",
"$default": {
"$source": "argv",
"index": 0
}
},
"projectName": {
"type": "string",
"description": "Name of the Deno project",
},
"token": {
"type": "string",
"description": "Optional authentication token"
},
"entryPath": {
"type": "string",
"description": "Entry path to deploy"
}
},
"required": [
"projectName"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { serve } from "https://deno.land/std@0.120.0/http/server.ts";

function handler(req: Request): Response {
return new Response("Hello from <%= name %>");
}

console.log("Listening on http://localhost:8000");
await serve(handler);
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {
Tree,
formatFiles,
installPackagesTask,
generateFiles,
joinPathFragments,
readProjectConfiguration,
} from '@nrwl/devkit';
import { applicationGenerator } from '@nrwl/node';
import denoDeployGenerator from '../add-deploy-target/generator';
import { CreateDenoProjectSchema } from './schema';

export default async function (tree: Tree, schema: CreateDenoProjectSchema) {
await applicationGenerator(tree, schema);

const appName = schema.name;
const projectConfiguration = readProjectConfiguration(tree, schema.name);
const projectRoot = projectConfiguration.root;
const buildTarget = projectConfiguration.targets['build'];
const outputPath = buildTarget.options.outputPath;
const entryPath = joinPathFragments(outputPath, 'main.ts');

// remove all files that were created
tree.listChanges()
.filter((fileChange) => fileChange.type === 'CREATE')
.forEach((fileChange) => {
tree.delete(fileChange.path);
});

generateFiles(tree, joinPathFragments(__dirname, './files'), projectRoot, {
...schema,
tmpl: '',
});

await formatFiles(tree);
await denoDeployGenerator(tree, { entryPath, appName });

return () => {
installPackagesTask(tree);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {Schema} from '@nrwl/node/src/generators/application/schema';

export type CreateDenoProjectSchema = Schema;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "../../../../../node_modules/@nrwl/node/src/generators/application/schema.json",
"cli": "nx",
"$id": "DenoProject",
"type": "object",
"properties": {
"name": {
"type": "string",
"description": "Identifier for the Deno deploy project",
"$default": {
"$source": "argv",
"index": 0
},
"x-prompt": "What is the name of the app?"
}
},
"required": ["name"]
}
4 changes: 4 additions & 0 deletions packages/nx-deno-deploy/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './lib/deploy-ctl';
export * from './executors/deploy/executor';
export * from './generators/add-deploy-target/generator';
export * from './generators/create-deno-project/index';
34 changes: 34 additions & 0 deletions packages/nx-deno-deploy/src/lib/deploy-ctl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {execSync} from "child_process";

const deployCommand = 'deployctl';

export function isDeployCtlAvailable(): boolean {
try {
execSync(`which ${deployCommand}`).toString('utf-8');
return true;
} catch (e) {
return false;
}

}

export function deployProject(entryPoint: string, projectName: string, token: string | null = null, isProd = false) {
if (!isDeployCtlAvailable()) {
throw new Error(`Deno deployctl is not installed.`);
}

const options = [
`--project=${projectName}`,
isProd ? '--prod' : null,
token?.length > 0 ? `--token=${token}` : null
].filter(part => part !== null).join(' ')

const command = `${deployCommand} deploy ${options} ${entryPoint}`

try {
execSync(command);
return {success: true};
} catch (e) {
return {success: false, error: e.toString()};
}
}
13 changes: 13 additions & 0 deletions packages/nx-deno-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"
}
]
}
Loading

0 comments on commit 06982d9

Please sign in to comment.