Skip to content

Commit

Permalink
feat: add cli tool (#82)
Browse files Browse the repository at this point in the history
* feat: add cli tool
  • Loading branch information
xudafeng authored Aug 12, 2022
1 parent 1d4eeb1 commit c039712
Show file tree
Hide file tree
Showing 28 changed files with 1,084 additions and 2 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ This project follows the git-contributor [spec](https://github.com/xudafeng/git-

## Docs

- [CLI Usage](//github.com/macacajs/reliable-cli)
- [Development](./docker/reliable-web#development)

## License
Expand Down
1 change: 0 additions & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@
## 文档

- [命令行客户端](//github.com/macacajs/reliable-cli)
- [开发 Reliable](./docker/reliable-web#development)
5 changes: 5 additions & 0 deletions cli/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
**/.*
**/node_modules
**/test
coverage/
**/docs/
8 changes: 8 additions & 0 deletions cli/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
extends: 'eslint-config-antife',
rules: {
semi: [2, 'always'],
},
};
40 changes: 40 additions & 0 deletions cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
._*
node_modules
coverage/
.nyc_output
*.gz
*.sw*
*.un~
package-lock.json
.vscode
26 changes: 26 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# reliable-cli

[![NPM version][npm-image]][npm-url]
[![CI][CI-image]][CI-url]
[![Test coverage][codecov-image]][codecov-url]
[![node version][node-image]][node-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/reliable-cli.svg
[npm-url]: https://npmjs.org/package/reliable-cli
[CI-image]: https://github.com/macacajs/reliable-cli/actions/workflows/ci.yml/badge.svg
[CI-url]: https://github.com/macacajs/reliable-cli/actions/workflows/ci.yml
[codecov-image]: https://img.shields.io/codecov/c/github/macacajs/reliable-cli.svg
[codecov-url]: https://codecov.io/gh/macacajs/reliable-cli
[node-image]: https://img.shields.io/badge/node.js-%3E=_8-green.svg
[node-url]: http://nodejs.org/download/
[download-image]: https://img.shields.io/npm/dm/reliable-cli.svg
[download-url]: https://npmjs.org/package/reliable-cli

> command-line interface for Reliable
[Reliable Offlice Site](//macacajs.github.io/reliable)

## License

The MIT License (MIT)
67 changes: 67 additions & 0 deletions cli/bin/reliable-cli-make.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env node

'use strict';

const _ = require('../lib/helper');
const childProcess = require('child_process');

const platformType = _.getPlatformType();

let cmd = '';
const args = [];

if (platformType === 'ios') {
console.log('executing make process for ios');
console.log(process.argv);

// args should be; [0]node, [1]js-file, [2]make-action....[3]configuration args
if (process.argv.length < 3) {
console.error('invalid arguments');
process.exit(0);
}

// check make action: 'build', 'release', 'test', 'pack' etc
if (process.argv[2].indexOf('=') > 0) {
console.error('invalid action');
console.log(process.argv[2]);
process.exit(0);
}

cmd = process.argv[2];
args.push(cmd);

// formalize extra args
if (process.argv.length > 3) {
args.push(`ARGS="--${process.argv.slice(3).join(' --')}"`);
}

console.log(`make ${args}`);
} else if (platformType === 'android') {

} else {

}

// execute cmd
const buildProcess = childProcess.spawn('make', args, {
stdio: [
process.stdin,
process.stdout,
2,
'ipc',
],
});

buildProcess.on('close', code => {
process.exit('process exited with code ' + code);
});

buildProcess.on('exit', code => {
process.exit(code);
});

buildProcess.on('message', e => {
if (e.signal === 'kill') {
buildProcess.kill();
}
});
5 changes: 5 additions & 0 deletions cli/bin/reliable-cli-report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

const ReportCommand = require('../lib/report-command');

new ReportCommand().run();
100 changes: 100 additions & 0 deletions cli/bin/reliable-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env node

'use strict';

const {
EOL,
} = require('os');
const _ = require('xutil');
const path = require('path');
const {
spawn,
} = require('child_process');
const program = require('commander');

const pkg = require('../package');

const buildCmds = ['build', 'release', 'pack', 'lint', 'doc', 'coverage', 'setup', 'test'];
const chalk = _.chalk;

program
.option('-v, --versions', 'show version and exit')
.option('--verbose', 'show more debugging information')
.usage('<command> [options] [arguments]')
.helpInformation = function () {
return [
'',
' ' + chalk.white(pkg.description),
'',
' Usage:',
'',
' ' + this._name + ' ' + this.usage(),
'',
' Commands:',
'',
' report report to Reliable server',
'',
' Options:',
'',
'' + this.optionHelp().replace(/^/gm, ' '),
'',
'',
].join(EOL);
};

program.parse(process.argv);

if (program.versions) {
console.info('%s %s%s', EOL, pkg.version, EOL);
process.exit(0);
}

let cmd = program.args[0];

if (!cmd) {
return program.help();
}

// for cmd which belongs to development process, all navigate to reliable-cli-make.js
if (buildCmds.includes(cmd)) {
program.rawArgs.splice(3, 0, cmd);
cmd = 'make';
}

const file = path.join(__dirname, `${pkg.name}-${cmd}.js`);

if (!_.isExistedFile(file)) {
console.log('%s command `%s` not found', EOL, chalk.yellow(cmd));
program.help();
return;
}

const args = program.rawArgs.slice(3);
args.unshift(file);

const bootstrap = spawn('node', args, {
stdio: [
process.stdin,
process.stdout,
2,
'ipc',
],
});

bootstrap.on('close', code => {
process.exit('process exited with code ' + code);
});

bootstrap.on('exit', code => {
process.exit(code);
});

bootstrap.on('message', e => {
switch (e.signal) {
case 'kill':
bootstrap.kill();
break;
default :
break;
}
});
4 changes: 4 additions & 0 deletions cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict';

exports.helper = require('./lib/helper');
exports.ReportCommand = require('./lib/report-command');
17 changes: 17 additions & 0 deletions cli/jsdoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"source": {
"include": [
"lib",
"README.md"
],
"includePattern": ".js$"
},
"opts": {
"destination": "./docs/",
"template": "node_modules/minami"
},
"templates": {
"cleverLinks": false,
"monospaceLinks": false
}
}
21 changes: 21 additions & 0 deletions cli/lib/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const os = require('os');

const _ = require('./helper');

module.exports = () => {
const env = process.env;
return {
platform: _.getPlatformType(),
ci: {
JOB_NAME: _.normalizeJobName(env.JOB_NAME),
BUILD_NUMBER: env.BUILD_NUMBER,
RUNNER_TYPE: 'JENKINS', // GITLAB_CI
},
os: {
nodeVersion: process.version,
platform: os.platform(),
},
};
};
8 changes: 8 additions & 0 deletions cli/lib/git-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

const LCL = require('last-commit-log');

module.exports = directory => {
const lcl = new LCL(directory);
return lcl.getLastCommitSync();
};
Loading

0 comments on commit c039712

Please sign in to comment.