Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI - support for config dir #688

Merged
merged 2 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
61 changes: 61 additions & 0 deletions test/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
const http = require('http');
const { exec } = require('child_process');

const MOCK_RESULT = {
res: {
job: {
internalBuildNumber: 1,
},
},
info: {
message: {
txt: 'Hello world!',
},
},
};

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 @@ -21,4 +52,34 @@ describe('CLI', () => {
done();
});
});

test('should run agent successfully', (done) => {
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) => {
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.');
done();
},
);
});
});
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
]
}
]
}
5 changes: 5 additions & 0 deletions test/cli/valid-data/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/valid-data/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
]
}
]
}