Skip to content

Commit

Permalink
feat(nx-ghpages): initial work
Browse files Browse the repository at this point in the history
Signed-off-by: Craigory Coppola <craigory.coppola@universalplant.com>
  • Loading branch information
AgentEnder committed Jun 20, 2021
1 parent 77d8e6e commit 621627d
Show file tree
Hide file tree
Showing 24 changed files with 376 additions and 1 deletion.
1 change: 1 addition & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"utils",
"typescript",
"nxdoc",
"nx-ghpages",
"docs-site",
"ci",
"repo"
Expand Down
14 changes: 14 additions & 0 deletions apps/nx-ghpages-e2e/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
displayName: 'nx-ghpages-e2e',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]s$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/apps\nx-ghpages-e2e',
};
48 changes: 48 additions & 0 deletions apps/nx-ghpages-e2e/tests/nx-ghpages.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
checkFilesExist,
ensureNxProject,
readJson,
runNxCommandAsync,
uniq,
} from '@nrwl/nx-plugin/testing';
describe('nx-ghpages e2e', () => {
it('should create nx-ghpages', async (done) => {
const plugin = uniq('nx-ghpages');
ensureNxProject('@nx-dotnet/nx-ghpages', 'dist/packages/nx-ghpages');
await runNxCommandAsync(
`generate @nx-dotnet/nx-ghpages:nx-ghpages ${plugin}`,
);

const result = await runNxCommandAsync(`build ${plugin}`);
expect(result.stdout).toContain('Executor ran');

done();
});

describe('--directory', () => {
it('should create src in the specified directory', async (done) => {
const plugin = uniq('nx-ghpages');
ensureNxProject('@nx-dotnet/nx-ghpages', 'dist/packages/nx-ghpages');
await runNxCommandAsync(
`generate @nx-dotnet/nx-ghpages:nx-ghpages ${plugin} --directory subdir`,
);
expect(() =>
checkFilesExist(`libs/subdir/${plugin}/src/index.ts`),
).not.toThrow();
done();
});
});

describe('--tags', () => {
it('should add tags to nx.json', async (done) => {
const plugin = uniq('nx-ghpages');
ensureNxProject('@nx-dotnet/nx-ghpages', 'dist/packages/nx-ghpages');
await runNxCommandAsync(
`generate @nx-dotnet/nx-ghpages:nx-ghpages ${plugin} --tags e2etag,e2ePackage`,
);
const nxJson = readJson('nx.json');
expect(nxJson.projects[plugin].tags).toEqual(['e2etag', 'e2ePackage']);
done();
});
});
});
13 changes: 13 additions & 0 deletions apps/nx-ghpages-e2e/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.e2e.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
9 changes: 9 additions & 0 deletions apps/nx-ghpages-e2e/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": ["**/*.spec.ts", "**/*.d.ts"]
}
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ module.exports = {
'<rootDir>/packages/dotnet',
'<rootDir>/packages/nxdoc',
'<rootDir>/e2e\\nxdoc-e2e',
'<rootDir>/packages/nx-ghpages',
'<rootDir>/apps\\nx-ghpages-e2e',
],
};
7 changes: 7 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
},
"docs-site": {
"tags": []
},
"nx-ghpages": {
"tags": []
},
"nx-ghpages-e2e": {
"tags": [],
"implicitDependencies": ["nx-ghpages"]
}
},
"workspaceLayout": {
Expand Down
3 changes: 3 additions & 0 deletions packages/nx-ghpages/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
}
21 changes: 21 additions & 0 deletions packages/nx-ghpages/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"parserOptions": {
"project": ["packages/nx-ghpages/tsconfig.*?.json"]
},
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions packages/nx-ghpages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nx-ghpages

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test nx-ghpages` to execute the unit tests via [Jest](https://jestjs.io).
10 changes: 10 additions & 0 deletions packages/nx-ghpages/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": "Deploy a site to the gh-pages branch of a specified repository."
}
}
}
6 changes: 6 additions & 0 deletions packages/nx-ghpages/generators.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/schema",
"name": "nx-ghpages",
"version": "0.0.1",
"generators": {}
}
14 changes: 14 additions & 0 deletions packages/nx-ghpages/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
displayName: 'nx-ghpages',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/packages/nx-ghpages',
};
7 changes: 7 additions & 0 deletions packages/nx-ghpages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@nx-dotnet/nx-ghpages",
"version": "0.0.1",
"main": "src/index.js",
"generators": "./generators.json",
"executors": "./executors.json"
}
11 changes: 11 additions & 0 deletions packages/nx-ghpages/src/executors/deploy/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { BuildExecutorSchema } from './schema';
import executor from './executor';

const options: BuildExecutorSchema = {};

describe('Build Executor', () => {
it('can run', async () => {
const output = await executor(options);
expect(output.success).toBe(true);
});
});
64 changes: 64 additions & 0 deletions packages/nx-ghpages/src/executors/deploy/executor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { BuildExecutorSchema } from './schema';
import { exec as execCallback } from 'child_process';
import { stat } from 'fs';
import { dirname, join } from 'path';
import { promisify } from 'util';
import { logger } from '@nrwl/devkit';

const exec = promisify(execCallback);

async function exists(path: string) {
return new Promise((resolve) => {
stat(path, (err) => resolve(err ? false : true));
});
}

export default async function deployExecutor(options: BuildExecutorSchema) {
const directory = join(await findWorkspaceRoot(), options.directory);

if (!(await exists(directory))) {
logger.error(`Output directory does not exist! ${directory}`);
return {
success: false,
};
}

if (!(await exists(join(directory, '.git')))) {
logger.info(
`Git repository not found, initializing a blank repository ${directory}`,
);
await exec('git init', { cwd: directory });
}

try {
await exec(`git remote add ${options.remoteName} ${options.remote}`, {
cwd: directory,
});
} catch {
await exec(`git remote set-url ${options.remoteName} ${options.remote}`, {
cwd: directory,
});
}

await exec(
`git add . && git commit -m "deploy to gh-pages (nx-ghpages)" && git push --set-upstream ${options.remoteName} gh-pages`,
{ cwd: directory },
);

return {
success: true,
};
}

async function findWorkspaceRoot(dir: string = process.cwd()): Promise<string> {
if (dirname(dir) === dir) {
throw new Error(`The cwd isn't part of an Nx workspace`);
}
if (
(await exists(join(dir, 'angular.json'))) ||
(await exists(join(dir, 'workspace.json')))
) {
return dir;
}
return findWorkspaceRoot(dirname(dir));
}
5 changes: 5 additions & 0 deletions packages/nx-ghpages/src/executors/deploy/schema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface BuildExecutorSchema {
remote: string;
directory: string;
remoteName: string;
}
23 changes: 23 additions & 0 deletions packages/nx-ghpages/src/executors/deploy/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"$schema": "http://json-schema.org/schema",
"cli": "nx",
"title": "Deploy",
"description": "Deploy a page to a specified repository's gh-pages branch.",
"type": "object",
"properties": {
"remote": {
"type": "string",
"description": "URL for the git remote to deploy to."
},
"directory": {
"type": "string",
"description": "Directory to push to gh-pages"
},
"remoteName": {
"type": "string",
"description": "Name of the remote to push to",
"default": "origin"
}
},
"required": ["remote", "directory"]
}
Empty file.
13 changes: 13 additions & 0 deletions packages/nx-ghpages/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-ghpages/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"],
"include": ["**/*.ts"]
}
15 changes: 15 additions & 0 deletions packages/nx-ghpages/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.spec.js",
"**/*.spec.jsx",
"**/*.d.ts"
]
}
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"@nx-dotnet/core": ["packages/core/src/index.ts"],
"@nx-dotnet/typescript": ["packages/typescript/src/index.ts"],
"@nx-dotnet/dotnet": ["packages/dotnet/src/index.ts"],
"@nx-dotnet/nxdoc": ["packages/nxdoc/src/index.ts"]
"@nx-dotnet/nxdoc": ["packages/nxdoc/src/index.ts"],
"@nx-dotnet/nx-ghpages": ["packages/nx-ghpages/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down
Loading

0 comments on commit 621627d

Please sign in to comment.