Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Commit

Permalink
Merge pull request #123 from Polymer/init
Browse files Browse the repository at this point in the history
Add prompt to choose init template
  • Loading branch information
justinfagnani committed May 13, 2016
2 parents 5adb262 + 4f63dea commit ae7ce61
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 8 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ A command-line tool for Polymer projects

## Installation

Check your Node.js version.
Check your Node.js version.

$ node -v

Polymer CLI targets the LTS version (4.x) of Node.js. It should
work with the current version (6.x) but this is not officially
supported. Anything below the LTS version is not supported.
supported. Anything below the LTS version is not supported.

$ npm install -g polymer-cli

Expand Down Expand Up @@ -39,3 +39,13 @@ supported. Anything below the LTS version is not supported.
### Start the development server

$ polymer serve


## Templates and Generators

Polymer-CLI helps initialize new projects with the `init` command, and includes
a couple of built-in templates.

New templates can be distributed and installed via npm. Yeoman generators
prefixed with `generator-polymer-init` will show up in the `polymer init`
menu.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"command-line-commands": "^0.1.2",
"css-slam": "^1.1.0",
"dom5": "^1.3.1",
"findup": "^0.1.5",
"fs-extra": "^0.30.0",
"generator-polymer-init": "^0.0.3",
"github": "^0.2.4",
Expand All @@ -34,6 +35,7 @@
"gulp-if": "^2.0.0",
"gunzip-maybe": "^1.3.1",
"hydrolysis": "^1.23.3",
"inquirer": "^1.0.2",
"liftoff": "^2.2.1",
"merge-stream": "^1.0.0",
"minimatch-all": "^1.0.2",
Expand Down
77 changes: 71 additions & 6 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import {Command} from './command';
import {ArgDescriptor} from 'command-line-args';
import * as fs from 'fs';
import * as logging from 'plylog';

export class InitCommand implements Command {
Expand All @@ -28,20 +29,84 @@ export class InitCommand implements Command {

run(options, config): Promise<any> {
// Defer dependency loading until this specific command is run
const PolykartGenerator = require('../templates/polykart').PolykartGenerator;
const PolykartGenerator =
require('../templates/polykart').PolykartGenerator;
const findup = require('findup');
const inquirer = require('inquirer');
const YeomanEnvironment = require('yeoman-environment');

return new Promise((resolve, reject) => {
let logger = logging.getLogger('cli.init');
let templateName = options['name'] || 'polymer-init';

logger.debug('creating yeoman environment...');
let env = new YeomanEnvironment();
env.register(require.resolve('generator-polymer-init'), 'polymer-init:app');
env.registerStub(PolykartGenerator, 'polykart:app');
env.register(
require.resolve('generator-polymer-init'), 'polymer-init-basic:app');
env.registerStub(PolykartGenerator, 'polymer-init-shop:app');

env.lookup(() => {
let generators = env.getGeneratorsMeta();

let runGenerator = (generatorName: string, templateName?: string) => {
let generator = generators[generatorName];
if (generator) {
logger.info(`Running template ${templateName || generatorName}`);
logger.debug(`Running generator ${generatorName}`);
env.run(generatorName, {}, () => resolve());
} else {
logger.warn('Template ${options.name} not found');
logger.debug('Yeoman generator ${generatorName} not found');
}
};

let getDisplayName = (generatorName) => {
let nameEnd = generatorName.indexOf(':');
if (nameEnd == -1) nameEnd = generatorName.length;
return generatorName.substring('polymer-init-'.length, nameEnd);
};

if (options.name) {
let generatorName = `polymer-init-${options.name}:app`;
runGenerator(generatorName, options.name);
} else {
let polymerInitGenerators = Object.keys(generators)
.filter((k) => k.startsWith('polymer-init')
&& k !== 'polymer-init:app');
let choices = polymerInitGenerators.map((generatorName: string) => {
let generator = generators[generatorName];
let description = 'no description';
let name = getDisplayName(generatorName);

if (name === 'basic') {
description = "built-in tempalte for basic apps and elements";
} else if (generator.resolved && generator.resolved !== 'unknown') {
let metapath = findup('package.json', {cwd: generator.resolved});
if (metapath) {
var meta = JSON.parse(fs.readFileSync(metapath, 'utf8'));
description = meta.description;
}
}

return {
name: name,
value: generatorName,
// inquirer is broken and doesn't print descriptions :(
// keeping this so things work when it does
short: description,
};
});
inquirer.prompt([{
type: 'list',
name: 'generatorName',
message: 'Choose a template to initialize this folder with',
choices: choices,
}]).then((answers) => {
let generatorName = answers.generatorName;
runGenerator(generatorName, getDisplayName(generatorName));
});
}
});

logger.debug('running template...', templateName);
env.run(templateName, {}, () => resolve());
});
}
}

0 comments on commit ae7ce61

Please sign in to comment.