Skip to content

Commit

Permalink
Extract subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
0x2b3bfa0 authored Jun 21, 2022
1 parent 56df4a8 commit d0b5e62
Show file tree
Hide file tree
Showing 34 changed files with 839 additions and 745 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
node_modules/
runner/
.terraform/
.cml/
.DS_Store
Expand Down
13 changes: 7 additions & 6 deletions bin/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const handleError = (message, error) => {
process.exit(1);
};

const options = {
exports.options = {
log: {
type: 'string',
description: 'Maximum log level',
Expand All @@ -63,17 +63,17 @@ const options = {
type: 'string',
choices: ['github', 'gitlab', 'bitbucket'],
description:
'Platform where the repository is hosted. If not specified, it will be inferred from the environment'
'Forge where the repository is hosted. If not specified, it will be inferred from the environment'
},
repo: {
type: 'string',
description:
'Repository to be used for registering the runner. If not specified, it will be inferred from the environment'
'Repository. If not specified, it will be inferred from the environment'
},
token: {
type: 'string',
description:
'Personal access token to register a self-hosted runner on the repository. If not specified, it will be inferred from the environment'
'Personal access token. If not specified, it will be inferred from the environment'
}
};

Expand All @@ -97,8 +97,9 @@ for (const [oldName, newName] of Object.entries(legacyEnvironmentVariables)) {
yargs
.fail(handleError)
.env('CML')
.options(options)
.commandDir('./cml', { exclude: /\.test\.js$/ })
.options(exports.options)
.commandDir('./cml')
.commandDir('./legacy')
.command('$0 <command>', false, (builder) => builder.strict(false), runPlugin)
.recommendCommands()
.demandCommand()
Expand Down
30 changes: 13 additions & 17 deletions bin/cml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,26 @@ describe('command-line interface tests', () => {
"cml.js <command>
Commands:
cml.js ci Fixes specific CI setups
cml.js pr <glob path...> Create a pull request with the
specified files
cml.js rerun-workflow Reruns a workflow given the jobId or
workflow Id
cml.js runner Launch and register a self-hosted
runner
cml.js send-comment <markdown file> Comment on a commit
cml.js send-github-check <markdown file> Create a check report
cml.js tensorboard-dev Get a tensorboard link
cml.js check Manage continuous integration checks
cml.js pr Manage pull requests
cml.js report Manage reports
cml.js repository Manage repository settings
cml.js runner Manage self-hosted continuous integration runners
cml.js tensorboard Manage tensorboard.dev agents
cml.js workflow Manage continuous integration workflows
Options:
--help Show help [boolean]
--version Show version number [boolean]
--log Maximum log level
[string] [choices: \\"error\\", \\"warn\\", \\"info\\", \\"debug\\"] [default: \\"info\\"]
--driver Platform where the repository is hosted. If not specified, it will
be inferred from the environment
--driver Forge where the repository is hosted. If not specified, it will be
inferred from the environment
[string] [choices: \\"github\\", \\"gitlab\\", \\"bitbucket\\"]
--repo Repository to be used for registering the runner. If not specified,
it will be inferred from the environment [string]
--token Personal access token to register a self-hosted runner on the
repository. If not specified, it will be inferred from the
environment [string]"
--repo Repository. If not specified, it will be inferred from the
environment [string]
--token Personal access token. If not specified, it will be inferred from
the environment [string]"
`);
});
});
8 changes: 8 additions & 0 deletions bin/cml/attachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.command = 'attachment';
exports.description = false;
exports.builder = (yargs) =>
yargs
.commandDir('./attachment', { exclude: /\.test\.js$/ })
.recommendCommands()
.demandCommand()
.strict();
8 changes: 4 additions & 4 deletions bin/cml/publish.js → bin/cml/attachment/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const fs = require('fs').promises;
const kebabcaseKeys = require('kebabcase-keys');
const winston = require('winston');

const CML = require('../../src/cml').default;
const CML = require('../../../src/cml').default;

exports.command = 'publish <asset>';
exports.description = false;
exports.description = 'publish an asset';

exports.handler = async (opts) => {
if (opts.gitlabUploads) {
Expand All @@ -29,9 +29,9 @@ exports.handler = async (opts) => {
else await fs.writeFile(file, output);
};

exports.builder = (yargs) => yargs.env('CML_PUBLISH').options(options);
exports.builder = (yargs) => yargs.env('CML_PUBLISH').options(exports.options);

const options = kebabcaseKeys({
exports.options = kebabcaseKeys({
md: {
type: 'boolean',
description: 'Output in markdown format [title || name](url).'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require('fs');
const { exec } = require('../../src/utils');
const { exec } = require('../../../../src/utils');

describe('CML e2e', () => {
test('cml publish --help', async () => {
Expand Down
8 changes: 8 additions & 0 deletions bin/cml/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.command = 'check';
exports.description = 'Manage continuous integration checks';
exports.builder = (yargs) =>
yargs
.commandDir('./check', { exclude: /\.test\.js$/ })
.recommendCommands()
.demandCommand()
.strict();
8 changes: 4 additions & 4 deletions bin/cml/send-github-check.js → bin/cml/check/create.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const fs = require('fs').promises;
const kebabcaseKeys = require('kebabcase-keys');
const CML = require('../../src/cml').default;
const CML = require('../../../src/cml').default;

exports.command = 'send-github-check <markdown file>';
exports.command = 'create <markdown file>';
exports.description = 'Create a check report';

exports.handler = async (opts) => {
Expand All @@ -13,9 +13,9 @@ exports.handler = async (opts) => {
};

exports.builder = (yargs) =>
yargs.env('CML_SEND_GITHUB_CHECK').options(options);
yargs.env('CML_SEND_GITHUB_CHECK').options(exports.options);

const options = kebabcaseKeys({
exports.options = kebabcaseKeys({
commitSha: {
type: 'string',
alias: 'head-sha',
Expand Down
18 changes: 7 additions & 11 deletions bin/cml/send-github-check.test.js → bin/cml/check/create.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { exec } = require('../../src/utils');
const { exec } = require('../../../src/utils');
const fs = require('fs').promises;

describe('CML e2e', () => {
Expand Down Expand Up @@ -34,24 +34,20 @@ describe('CML e2e', () => {
const output = await exec(`node ./bin/cml.js send-github-check --help`);

expect(output).toMatchInlineSnapshot(`
"cml.js send-github-check <markdown file>
Create a check report
"cml.js send-github-check
Options:
--help Show help [boolean]
--version Show version number [boolean]
--log Maximum log level
[string] [choices: \\"error\\", \\"warn\\", \\"info\\", \\"debug\\"] [default: \\"info\\"]
--driver Platform where the repository is hosted. If not
--driver Forge where the repository is hosted. If not
specified, it will be inferred from the environment
[string] [choices: \\"github\\", \\"gitlab\\", \\"bitbucket\\"]
--repo Repository to be used for registering the runner. If
not specified, it will be inferred from the
environment [string]
--token Personal access token to register a self-hosted
runner on the repository. If not specified, it will
be inferred from the environment [string]
--repo Repository. If not specified, it will be inferred
from the environment [string]
--token Personal access token. If not specified, it will be
inferred from the environment [string]
--commit-sha, --head-sha Commit SHA linked to this comment. Defaults to HEAD.
[string]
--conclusion Sets the conclusion status of the check.
Expand Down
69 changes: 12 additions & 57 deletions bin/cml/pr.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,57 +1,12 @@
const kebabcaseKeys = require('kebabcase-keys');

const { GIT_REMOTE, GIT_USER_NAME, GIT_USER_EMAIL } = require('../../src/cml');
const CML = require('../../src/cml').default;

exports.command = 'pr <glob path...>';
exports.description = 'Create a pull request with the specified files';

exports.handler = async (opts) => {
const cml = new CML(opts);
const link = await cml.prCreate({ ...opts, globs: opts.globpath });
if (link) console.log(link);
};

exports.builder = (yargs) => yargs.env('CML_PR').options(options);

const options = kebabcaseKeys({
md: {
type: 'boolean',
description: 'Output in markdown format [](url).'
},
skipCI: {
type: 'boolean',
description: 'Force skip CI for the created commit (if any)'
},
merge: {
type: 'boolean',
alias: 'auto-merge',
conflicts: ['rebase', 'squash'],
description: 'Try to merge the pull request upon creation.'
},
rebase: {
type: 'boolean',
conflicts: ['merge', 'squash'],
description: 'Try to rebase-merge the pull request upon creation.'
},
squash: {
type: 'boolean',
conflicts: ['merge', 'rebase'],
description: 'Try to squash-merge the pull request upon creation.'
},
remote: {
type: 'string',
default: GIT_REMOTE,
description: 'Sets git remote.'
},
userEmail: {
type: 'string',
default: GIT_USER_EMAIL,
description: 'Sets git user email.'
},
userName: {
type: 'string',
default: GIT_USER_NAME,
description: 'Sets git user name.'
}
});
const { options, handler } = require('./pr/create');

exports.command = 'pr';
exports.description = 'Manage pull requests';
exports.handler = handler;
exports.builder = (yargs) =>
yargs
.commandDir('./pr', { exclude: /\.test\.js$/ })
.recommendCommands()
.env('CML_PR')
.options(options)
.strict();
61 changes: 61 additions & 0 deletions bin/cml/pr/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const kebabcaseKeys = require('kebabcase-keys');

const {
GIT_REMOTE,
GIT_USER_NAME,
GIT_USER_EMAIL
} = require('../../../src/cml');
const CML = require('../../../src/cml').default;

exports.command = 'create <glob path...>';
exports.description = 'Create a pull request with the specified files';

exports.handler = async (opts) => {
const cml = new CML(opts);
const link = await cml.prCreate({ ...opts, globs: opts.globpath });
if (link) console.log(link);
};

exports.builder = (yargs) => yargs.env('CML_PR').options(exports.options);

exports.options = kebabcaseKeys({
md: {
type: 'boolean',
description: 'Output in markdown format [](url).'
},
skipCI: {
type: 'boolean',
description: 'Force skip CI for the created commit (if any)'
},
merge: {
type: 'boolean',
alias: 'auto-merge',
conflicts: ['rebase', 'squash'],
description: 'Try to merge the pull request upon creation.'
},
rebase: {
type: 'boolean',
conflicts: ['merge', 'squash'],
description: 'Try to rebase-merge the pull request upon creation.'
},
squash: {
type: 'boolean',
conflicts: ['merge', 'rebase'],
description: 'Try to squash-merge the pull request upon creation.'
},
remote: {
type: 'string',
default: GIT_REMOTE,
description: 'Sets git remote.'
},
userEmail: {
type: 'string',
default: GIT_USER_EMAIL,
description: 'Sets git user email.'
},
userName: {
type: 'string',
default: GIT_USER_NAME,
description: 'Sets git user name.'
}
});
22 changes: 12 additions & 10 deletions bin/cml/pr.test.js → bin/cml/pr/create.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
const { exec } = require('../../src/utils');
const { exec } = require('../../../src/utils');

describe('CML e2e', () => {
test('cml-pr --help', async () => {
const output = await exec(`echo none | node ./bin/cml.js pr --help`);

expect(output).toMatchInlineSnapshot(`
"cml.js pr <glob path...>
"cml.js pr
Create a pull request with the specified files
Manage pull requests
Commands:
cml.js pr create <glob path...> Create a pull request with the specified
files
Options:
--help Show help [boolean]
--version Show version number [boolean]
--log Maximum log level
[string] [choices: \\"error\\", \\"warn\\", \\"info\\", \\"debug\\"] [default: \\"info\\"]
--driver Platform where the repository is hosted. If not
specified, it will be inferred from the environment
--driver Forge where the repository is hosted. If not specified,
it will be inferred from the environment
[string] [choices: \\"github\\", \\"gitlab\\", \\"bitbucket\\"]
--repo Repository to be used for registering the runner. If
not specified, it will be inferred from the environment
[string]
--token Personal access token to register a self-hosted runner
on the repository. If not specified, it will be
--repo Repository. If not specified, it will be inferred from
the environment [string]
--token Personal access token. If not specified, it will be
inferred from the environment [string]
--md Output in markdown format [](url). [boolean]
--skip-ci Force skip CI for the created commit (if any) [boolean]
Expand Down
8 changes: 8 additions & 0 deletions bin/cml/report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.command = 'report';
exports.description = 'Manage reports';
exports.builder = (yargs) =>
yargs
.commandDir('./report', { exclude: /\.test\.js$/ })
.recommendCommands()
.demandCommand()
.strict();
Loading

0 comments on commit d0b5e62

Please sign in to comment.