Skip to content

Commit

Permalink
Add URL test
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Jul 14, 2023
1 parent fad5d8d commit 466d7eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/cli/bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ async function main() {
try {
// if running `co check [tokens]`, don’t load config from file
if (cmd === 'check' && args[0]) {
console.log(args[0]);
config = await initConfig({tokens: args[0]}, cwd);
} else {
config = await loadConfig(resolveConfig(configPath));
Expand Down Expand Up @@ -137,8 +136,9 @@ async function main() {
break;
}
case 'check': {
console.log(`${UNDERLINE}${fileURLToPath(config.tokens[0])}${RESET}`);
const rawSchema = await loadTokens(config.tokens);
const filepath = config.tokens[0];
console.log(`${UNDERLINE}${filepath.protocol === 'file:' ? fileURLToPath(filepath) : filepath}${RESET}`);
const {errors, warnings} = parse(rawSchema, config); // will throw if errors
if (errors || warnings) {
printErrors(errors);
Expand Down Expand Up @@ -210,9 +210,10 @@ async function loadTokens(tokenPaths) {
for (const filepath of tokenPaths) {
const pathname = filepath.pathname.toLowerCase();
const isYAMLExt = pathname.endsWith('.yaml') || pathname.endsWith('.yml');
if (filepath.protocol === 'url:') {
if (filepath.protocol === 'http:' || filepath.protocol === 'https:') {
try {
const raw = await globalThis.fetch(filepath, {method: 'GET', headers: {Accepted: '*/*', 'User-Agent': 'cobalt'}}).then((res) => res.text());
const res = await globalThis.fetch(filepath, {method: 'GET', headers: {Accept: '*/*', 'User-Agent': 'Mozilla/5.0 Gecko/20100101 Firefox/116.0'}});
const raw = await res.text();
if (isYAMLExt || res.headers.get('content-type').includes('yaml')) {
rawTokens.push(yaml.load(raw));
} else {
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/test/check.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ describe('co check', () => {
expect(result.exitCode).toBe(0);
});

test('URL', async () => {
const cwd = import.meta.url;
const result = await execa('node', ['../bin/cli.js', 'check', 'https://raw.githubusercontent.com/drwpow/cobalt-ui/main/packages/cli/test/fixtures/check-default/tokens.json'], {cwd});
expect(result.exitCode).toBe(0);
});

test('invalid tokens', async () => {
const cwd = new URL('./fixtures/check-invalid/', import.meta.url);
await expect(async () => await execa('node', [cmd, 'check'], {cwd, throwOnStderr: false})).rejects.toThrow();
Expand Down

0 comments on commit 466d7eb

Please sign in to comment.