Skip to content

Commit

Permalink
feat: CLI - add support for --config-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
vio committed Aug 25, 2022
1 parent 2d9dfbf commit 395f64f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 19 deletions.
33 changes: 21 additions & 12 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

const path = require('path');
const { get } = require('lodash');
const { readJSONSync, pathExistsSync } = require('fs-extra');
const { cosmiconfigSync } = require('cosmiconfig');
Expand All @@ -11,7 +12,21 @@ const { agent } = require('../lib/agent');
const { debug } = require('../lib/utils');
const LOCALES = require('../locales/en');

const searchConfig = cosmiconfigSync('relativeci').search();
const args = yargs(hideBin(process.argv))
.usage('Usage: $0 OPTIONS')

.option('config-dir', { describe: 'Config directory', default: '', alias: 'c' })

.option('commit', { describe: 'Commit SHA', default: '' })
.option('commit-message', { describe: 'Commit message', default: '', alias: 'commitMessage' })
.option('branch', { describe: 'Branch name', default: '' })
.option('pr', { describe: 'Pull Request number', default: '' })
.option('slug', { describe: 'Project slug', default: '' })

.help()
.argv;

const searchConfig = cosmiconfigSync('relativeci').search(args['config-dir']);
debug('Config', searchConfig);

if (!searchConfig) {
Expand All @@ -26,7 +41,11 @@ if (!get(config, 'webpack.stats')) {
process.exit(0);
}

const webpackArtifactFilepath = get(config, 'webpack.stats');
// Load webpack stats file relative to the config file
const webpackArtifactFilepath = path.join(
path.dirname(searchConfig.filepath),
get(config, 'webpack.stats'),
);

if (!pathExistsSync(webpackArtifactFilepath)) {
console.error(LOCALES.CLI_MISSING_STATS_FILE_ERROR);
Expand All @@ -49,16 +68,6 @@ const artifactsData = [
},
];

const args = yargs(hideBin(process.argv))
.usage('Usage: $0 OPTIONS')
.option('commit', { describe: 'Commit SHA', default: '' })
.option('commit-message', { describe: 'Commit message', default: '', alias: 'commitMessage' })
.option('branch', { describe: 'Branch name', default: '' })
.option('pr', { describe: 'Pull Request number', default: '' })
.option('slug', { describe: 'Project slug', default: '' })
.help()
.argv;

debug('CLI arguments', args);

agent(artifactsData, config, args);
38 changes: 31 additions & 7 deletions test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ const MOCK_RESULT = {

const MOCK_SERVER_PORT = 5998;

const createServer = () => http.createServer((req, res) => {
res.write(JSON.stringify(MOCK_RESULT));
res.end();
});

describe('CLI', () => {
let server;

beforeAll(() => {
server = createServer().listen(MOCK_SERVER_PORT);
});

afterAll(() => {
server.close();
});

test('should return error if config is missing', (done) => {
exec('./bin/index.js', (error, stdout, sterr) => {
expect(sterr).toContain('relativeci.config.js file is missing!');
Expand All @@ -39,21 +54,30 @@ describe('CLI', () => {
});

test('should run agent successfully', (done) => {
const server = http.createServer((req, res) => {
res.write(JSON.stringify(MOCK_RESULT));
res.end();
}).listen(MOCK_SERVER_PORT);

exec(
`cd test/cli/valid-data &&
RELATIVE_CI_ENDPOINT=http://localhost:${MOCK_SERVER_PORT}/save \
RELATIVE_CI_SLUG=org/project \
RELATIVE_CI_KEY=abc123 \
../../../bin/index.js
`,
(_, stdout, sterr) => {
(_, stdout) => {
expect(stdout).toContain('Job #1 done.');
done();
},
);
});

test('should run agent successfully from parent directory', (done) => {
exec(
`cd test/cli/custom-config-dir &&
RELATIVE_CI_ENDPOINT=http://localhost:${MOCK_SERVER_PORT}/save \
RELATIVE_CI_SLUG=org/project \
RELATIVE_CI_KEY=abc123 \
../../../bin/index.js --config-dir app
`,
(_, stdout) => {
expect(stdout).toContain('Job #1 done.');
server.close();
done();
},
);
Expand Down
5 changes: 5 additions & 0 deletions test/cli/custom-config-dir/app/relativeci.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
webpack: {
stats: './webpack-stats.json',
},
};
30 changes: 30 additions & 0 deletions test/cli/custom-config-dir/app/webpack-stats.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"assets": [
{
"name": "main.js",
"size": 28
}
],
"chunks": [
{
"id": 179,
"entry": true,
"initial": true,
"files": [
"main.js"
],
"names": [
"main"
]
}
],
"modules": [
{
"name": "./src/index.js",
"size": 29,
"chunks": [
179
]
}
]
}

0 comments on commit 395f64f

Please sign in to comment.