Skip to content

Commit 12179f6

Browse files
committed
Merge pull request #1 from Polymer/initial
Initial commit
2 parents 68a6a34 + a7975e2 commit 12179f6

10 files changed

+308
-0
lines changed

.npmignore

Whitespace-only changes.

bin/polytool.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
/**
6+
* @license
7+
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
8+
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
9+
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
10+
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
11+
* Code distributed by Google as part of the polymer project is also
12+
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
13+
*/
14+
var resolve = require('resolve');
15+
16+
process.title = 'polytool';
17+
18+
resolve('polytool', {basedir: process.cwd()}, function(error, path) {
19+
let lib = path ? require(path) : require('..');
20+
let polytool = new lib.Polytool();
21+
polytool.run(process.argv.slice(2));
22+
});

custom_typings/command-line-args.d.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
declare module 'command-line-args' {
2+
function commandLineArgs(args: commandLineArgs.ArgDescriptor[])
3+
: commandLineArgs.CLI;
4+
5+
module commandLineArgs {
6+
interface ArgDescriptor {
7+
name: string;
8+
alias?: string;
9+
description?: string;
10+
defaultValue?: any;
11+
type?: Object;
12+
multiple?: boolean;
13+
}
14+
interface UsageOpts {
15+
title?: string;
16+
header?: string;
17+
description?: string;
18+
}
19+
interface CLI {
20+
parse(): any;
21+
getUsage(opts: UsageOpts): string;
22+
}
23+
}
24+
25+
export = commandLineArgs;
26+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
declare module 'command-line-commands' {
2+
module commandLineCommands {
3+
interface CommandDescriptor {
4+
name: string;
5+
definitions?: any[];
6+
description?: string;
7+
defaultOption?: boolean;
8+
}
9+
10+
interface Command {
11+
name: string;
12+
options: {[name: string]: string};
13+
}
14+
15+
interface CLI {
16+
commands: CommandDescriptor[];
17+
parse(args?: string[]): Command;
18+
getUsage(): string;
19+
}
20+
}
21+
22+
function commandLineCommands(args: commandLineCommands.CommandDescriptor[])
23+
: commandLineCommands.CLI;
24+
25+
export = commandLineCommands;
26+
}

package.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "polytool",
3+
"version": "0.0.1",
4+
"description": "A commandline tool for Polymer projects",
5+
"main": "lib/polytool.js",
6+
"bin": "bin/polytool.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/Polymer/polytool.git"
13+
},
14+
"author": "The Polymer Project Authors",
15+
"license": "BSD-3-Clause",
16+
"dependencies": {
17+
"command-line-commands": "^0.1.2",
18+
"polyserve": "^0.7.1"
19+
},
20+
"devDependencies": {
21+
"typescript": "^1.8.0"
22+
}
23+
}

src/commands/command.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {ArgDescriptor} from 'command-line-args';
2+
3+
export interface Command {
4+
name: string;
5+
description: string;
6+
args: ArgDescriptor[];
7+
run(options: {[name: string]: string}): Promise<any>;
8+
}
9+
10+
// TODO(justinfagnani): how do I re-export an interface?
11+
export interface ArgDescriptor extends ArgDescriptor {
12+
// // name: string;
13+
// // alias?: string;
14+
// // description: string;
15+
// // defaultValue?: any;
16+
// // type?: Object;
17+
// // multiple?: boolean;
18+
// // defaultOption?: boolean;
19+
}

src/commands/help.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {Command} from './command';
2+
import {CLI} from 'command-line-commands';
3+
import * as commandLineArgs from 'command-line-args';
4+
5+
export class HelpCommand implements Command {
6+
name = 'help';
7+
8+
description = 'Shows this help message, or help for a specific command';
9+
10+
args = [{
11+
name: 'command',
12+
description: 'The command to display help for',
13+
defaultOption: true,
14+
}];
15+
16+
commands : Map<String, Command> = new Map();
17+
18+
constructor(commands : Map<String, Command>) {
19+
this.commands = commands;
20+
}
21+
22+
run(options): Promise<any> {
23+
return new Promise<any>((resolve, _) => {
24+
if (options.command) {
25+
let command = this.commands.get(options.command);
26+
if (!command) {
27+
console.log(`unknown command ${command}`);
28+
} else {
29+
let argsCli = commandLineArgs(command.args);
30+
console.log(argsCli.getUsage({
31+
title: `polytool ${command.name}`,
32+
description: command.description,
33+
}));
34+
}
35+
} else {
36+
console.log(`polytool - The Polymer command-line tool\n`);
37+
console.log(`usage: polytool <command> [<args>]\n`);
38+
console.log(`polytool supports the following commands:`);
39+
for (let command of this.commands.values()) {
40+
console.log(` ${command.name}\t\t${command.description}`);
41+
}
42+
console.log(`\nRun \`polytool help <command>\` for help on a specific command.\n`);
43+
}
44+
resolve(null);
45+
});
46+
}
47+
}

src/commands/serve.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {Command} from './command';
2+
import {startServer} from 'polyserve';
3+
4+
export class ServeCommand implements Command {
5+
name = 'serve';
6+
7+
description = 'Runs the polyserve development server';
8+
9+
args = [
10+
{
11+
name: 'port',
12+
alias: 'p',
13+
description: 'The port to serve files from. Defaults to 8080',
14+
type: Number,
15+
},
16+
{
17+
name: 'hostname',
18+
alias: 'h',
19+
description: 'The hostname to serve. Defaults to localhost',
20+
type: String,
21+
},
22+
{
23+
name: 'component-dir',
24+
alias: 'c',
25+
description: 'The component directory to use. Defaults to reading from' +
26+
' the Bower config (usually bower_components/)',
27+
type: String,
28+
},
29+
{
30+
name: 'package-name',
31+
alias: 'n',
32+
description: 'Package name. Defaults to reading from bower.json',
33+
type: String,
34+
},
35+
{
36+
name: 'open',
37+
alias: 'o',
38+
description: 'Open page in default browser on startup.' +
39+
' Defaults to index.html',
40+
type: String,
41+
},
42+
{
43+
name: 'browser',
44+
alias: 'b',
45+
description: 'The browser to open when using the --open option.' +
46+
' Defaults to chrome',
47+
type: String,
48+
},
49+
];
50+
51+
run(options): Promise<any> {
52+
return startServer({
53+
port: options['port'],
54+
componentDir: options['component-dir'],
55+
packageName: options['package-name'],
56+
page: options['open'],
57+
host: options['hostname'],
58+
browser: options['browser'],
59+
});
60+
}
61+
}

src/polytool.ts

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
import * as commandLineCommands from 'command-line-commands';
4+
import {HelpCommand} from './commands/help';
5+
import {ServeCommand} from './commands/serve';
6+
import {Command} from './commands/command';
7+
8+
export class Polytool {
9+
10+
commandDescriptors = [];
11+
commands : Map<String, Command> = new Map();
12+
cli : commandLineCommands.CLI;
13+
14+
constructor() {
15+
this.addCommand(new HelpCommand(this.commands));
16+
this.addCommand(new ServeCommand());
17+
}
18+
19+
addCommand(command) {
20+
this.commands.set(command.name, command);
21+
this.commandDescriptors.push({
22+
name: command.name,
23+
definitions: command.args,
24+
description: command.description,
25+
});
26+
}
27+
28+
run(args) {
29+
this.cli = commandLineCommands(this.commandDescriptors);
30+
let cliCommand = this.cli.parse(args);
31+
32+
if (!cliCommand.name) {
33+
if (args[2]) {
34+
console.error('unknown command', args[2]);
35+
} else {
36+
console.error('must specify a command');
37+
}
38+
process.exit(1);
39+
}
40+
41+
const command = this.commands.get(cliCommand.name);
42+
command.run(cliCommand.options).then((result) => {
43+
// success
44+
}, (err) => {
45+
console.error('error', err);
46+
});
47+
}
48+
}

tsconfig.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"isolatedModules": true,
7+
"experimentalDecorators": true,
8+
"emitDecoratorMetadata": true,
9+
"declaration": false,
10+
"noImplicitAny": false,
11+
"removeComments": false,
12+
"noLib": false,
13+
"preserveConstEnums": true,
14+
"suppressImplicitAnyIndexErrors": true,
15+
"outDir": "lib"
16+
},
17+
"filesGlob": [
18+
"**/*.ts",
19+
"**/*.tsx",
20+
"!node_modules/**",
21+
"custom_typings/*.d.ts",
22+
"typings/**/*.d.ts"
23+
],
24+
"files": [
25+
"custom_typings/command-line-args.d.ts",
26+
"custom_typings/command-line-commands.d.ts",
27+
"src/commands/command.ts",
28+
"src/commands/help.ts",
29+
"src/commands/serve.ts",
30+
"src/polytool.ts",
31+
"typings/node/node.d.ts"
32+
],
33+
"atom": {
34+
"rewriteTsconfig": true
35+
}
36+
}

0 commit comments

Comments
 (0)