Skip to content

Commit

Permalink
feat(nx-ghpages): add syncGitOptions to provide extra flags to the co…
Browse files Browse the repository at this point in the history
…mmand when syncing changes
  • Loading branch information
AgentEnder committed Jul 27, 2024
1 parent 0fe2d02 commit e015a98
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 14 deletions.
10 changes: 9 additions & 1 deletion docs/nx-ghpages/Executors/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ Deploy a page to a specified repository's gh-pages branch.

### syncWithBaseBranch

- (string): Indicate if the gh-pages branch should be synced with the base branch
- (boolean): Indicate if the gh-pages branch should be synced with the base branch

### syncStrategy

- (string): Git command to use to sync the gh-pages branch with the base branch

### syncGitOptions

- (array): Additional git options to use when syncing the gh-pages branch with the base branch

### CNAME

- (string): Custom domain to use for the gh-pages branch. Applied by creating a CNAME file in the root of the gh-pages branch
1 change: 1 addition & 0 deletions packages/nx-ghpages/src/executors/deploy/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const options: BuildExecutorSchema = {
baseBranch: '',
syncWithBaseBranch: false,
syncStrategy: 'rebase',
syncGitOptions: [],
};

describe('Build Executor', () => {
Expand Down
54 changes: 42 additions & 12 deletions packages/nx-ghpages/src/executors/deploy/executor.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
import { logger, workspaceRoot } from '@nx/devkit';

import { exec as execCallback } from 'child_process';
import { stat } from 'fs';
import { ExecOptions, exec as execCallback } from 'child_process';
import { stat, writeFileSync } from 'fs';
import { join } from 'path';
import { promisify } from 'util';

import { BuildExecutorSchema } from './schema';
import { readNxJson } from 'nx/src/config/nx-json';

const exec = promisify(execCallback);
function exec(command: string, options: ExecOptions) {
return new Promise((resolve, reject) => {
const childProcess = execCallback(
command,
options,
(error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(stdout);
}
},
);

childProcess.stdout?.pipe(process.stdout);
childProcess.stderr?.pipe(process.stderr);
});
}

async function exists(path: string) {
return new Promise((resolve) => {
Expand All @@ -30,6 +46,7 @@ export default async function deployExecutor(options: BuildExecutorSchema) {
logger.info(`Creating CNAME file for ${options.CNAME} in ${directory}`);
writeFileSync(join(directory, 'CNAME'), options.CNAME);
}

const envToken = process.env.GH_TOKEN ?? process.env.GITHUB_TOKEN;
if (envToken) {
options.remote = options.remote.replace(
Expand Down Expand Up @@ -66,7 +83,6 @@ export default async function deployExecutor(options: BuildExecutorSchema) {
});

logger.info('Authoring Commit -- COMPLETE');
logger.info('Pushing to GH Pages');
try {
await exec(`git checkout -b gh-pages`, { cwd: directory });
} catch {
Expand All @@ -75,17 +91,31 @@ export default async function deployExecutor(options: BuildExecutorSchema) {
}
if (options.syncWithBaseBranch) {
const baseBranch =
options.baseBranch || readNxJson()?.affected?.defaultBase || 'master';
options.baseBranch || readNxJson()?.affected?.defaultBase || 'main';
const syncStrategy = options.syncStrategy;
await exec(`git ${syncStrategy} ${options.remoteName}/${baseBranch}`, {
const command = `git ${syncStrategy} ${
options.remoteName
}/${baseBranch} ${options.syncGitOptions.join(' ')}`;
logger.info('Syncing with base branch: ' + command);
await exec(command, {
cwd: directory,
});
}

await exec(`git push -f --set-upstream ${options.remoteName} gh-pages`, {
cwd: directory,
});
logger.info('Pushing to GH Pages -- COMPLETE');
logger.info('Pushing to GH Pages');
try {
await exec(`git push -f --set-upstream ${options.remoteName} gh-pages`, {
cwd: directory,
});
logger.info('Pushing to GH Pages -- COMPLETE');
} catch (error: unknown) {
logger.info(
'[hint]: You may need to set GH_TOKEN or GITHUB_TOKEN to have write access to the repository',
);
logger.error('Pushing to GH Pages -- FAILED');
return {
success: false,
};
}

return {
success: true,
Expand Down
3 changes: 2 additions & 1 deletion packages/nx-ghpages/src/executors/deploy/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ export interface BuildExecutorSchema {
baseBranch: string;
syncWithBaseBranch: boolean;
syncStrategy: 'rebase' | 'merge';
CNAME: string;
syncGitOptions: string[];
CNAME?: string;
}
9 changes: 9 additions & 0 deletions packages/nx-ghpages/src/executors/deploy/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@
"description": "Git command to use to sync the gh-pages branch with the base branch",
"enum": ["rebase", "merge"],
"default": "rebase"
},
"syncGitOptions": {
"type": "array",
"description": "Additional git options to use when syncing the gh-pages branch with the base branch",
"items": {
"type": "string"
},
"default": []
},
"CNAME": {
"type": "string",
"description": "Custom domain to use for the gh-pages branch. Applied by creating a CNAME file in the root of the gh-pages branch"
Expand Down

0 comments on commit e015a98

Please sign in to comment.