Skip to content

Commit

Permalink
Merge pull request #186 from andreban/bubblewrap-version
Browse files Browse the repository at this point in the history
Adds version, --version and --help commands
  • Loading branch information
andreban authored May 28, 2020
2 parents d8205c5 + b91854d commit 0845aae
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
"files": [
"dist",
"bin"
"bin",
"package.json"
],
"keywords": [],
"repository": {
Expand Down
21 changes: 19 additions & 2 deletions packages/cli/src/lib/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {validate} from './cmds/validate';
import {install} from './cmds/install';
import {loadOrCreateConfig} from './config';
import {major} from 'semver';
import {version} from './cmds/version';

export class Cli {
async run(args: string[]): Promise<boolean> {
Expand All @@ -33,7 +34,19 @@ export class Cli {
const config = await loadOrCreateConfig();

const parsedArgs = minimist(args);
const command = args[0] || 'help';

let command;
if (parsedArgs._.length === 0) {
// Accept --version and --help alternatives for the help and version commands.
if (parsedArgs.version) {
command = 'version';
} else if (parsedArgs.help) {
command = 'help';
}
} else {
command = parsedArgs._[0];
}

switch (command) {
case 'help':
return await help(parsedArgs);
Expand All @@ -45,10 +58,14 @@ export class Cli {
return await build(config, parsedArgs);
case 'validate':
return await validate(parsedArgs);
case 'version': {
return await version();
}
case 'install':
return await install(parsedArgs, config);
default:
throw new Error(`"${command}" is not a valid command!`);
throw new Error(
`"${command}" is not a valid command! Use 'bubblewrap help' for a list of commands`);
}
}
}
27 changes: 27 additions & 0 deletions packages/cli/src/lib/cmds/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2019 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as fs from 'fs';
import * as path from 'path';
import {Log} from '@bubblewrap/core';

export async function version(log = new Log('version')): Promise<boolean> {
const packageJsonFile = path.join(__dirname, '../../../package.json');
const packageJsonContents = await (await fs.promises.readFile(packageJsonFile)).toString();
const packageJson = JSON.parse(packageJsonContents);
log.info(packageJson.version);
return true;
}

0 comments on commit 0845aae

Please sign in to comment.