polymer-cli
Version:
A commandline tool for Polymer projects
151 lines • 7.34 kB
JavaScript
;
/*
* Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = require("chalk");
const path = require("path");
const logging = require("plylog");
const Generator = require("yeoman-generator");
const validateElementName = require("validate-element-name");
const logger = logging.getLogger('init');
/**
* Returns a Yeoman Generator constructor that can be passed to yeoman to be
* run. A "template name" argument is required to choose the correct
* `/templates` directory name to generate from.
* (Ex: "polymer-2.x" to generate the `templates/polymer-2x` template directory)
*/
function createElementGenerator(templateName) {
class ElementGenerator extends Generator {
constructor(args, options) {
super(args, options);
this.sourceRoot(path.join(__dirname, '../../../templates/element', templateName));
}
// This is necessary to prevent an exception in Yeoman when creating
// storage for generators registered as a stub and used in a folder
// with a package.json but with no name property.
// https://github.com/Polymer/polymer-cli/issues/186
rootGeneratorName() {
return 'ElementGenerator';
}
initializing() {
// Yeoman replaces dashes with spaces. We want dashes.
this.appname = this.appname.replace(/\s+/g, '-');
}
prompting() {
return __awaiter(this, void 0, void 0, function* () {
const prompts = [
{
name: 'name',
type: 'input',
message: `Element name`,
default: this.appname + (this.appname.includes('-') ? '' : '-element'),
validate: (name) => {
const nameValidation = validateElementName(name);
if (!nameValidation.isValid) {
this.log(`\n${nameValidation.message}\nPlease try again.`);
}
else if (nameValidation.message) {
this.log(''); // 'empty' log inserts a line break
logger.warn(nameValidation.message);
}
return nameValidation.isValid;
},
},
{
type: 'input',
name: 'description',
message: 'Brief description of the element',
},
];
this.props = (yield this.prompt(prompts));
this.props.elementClassName = this.props.name.replace(/(^|-)(\w)/g, (_match, _p0, p1) => p1.toUpperCase());
});
}
writing() {
const name = this.props.name;
this.fs.copyTpl(`${this.templatePath()}/**/?(.)*`, this.destinationPath(), this.props, undefined, { globOptions: { ignore: ['**/_*'] } });
this.fs.copyTpl(this.templatePath('_element.html'), `${name}.html`, this.props);
this.fs.copyTpl(this.templatePath('test/_element_test.html'), `test/${name}_test.html`, this.props);
this.fs.copyTpl(this.templatePath('test/index.html'), `test/index.html`, this.props);
this.fs.move(this.destinationPath('gitignore'), this.destinationPath('.gitignore'));
}
install() {
this.log(chalk_1.default.bold('\nProject generated!'));
this.log('Installing dependencies...');
this.installDependencies({
npm: false,
});
}
end() {
this.log(chalk_1.default.bold('\nSetup Complete!'));
this.log('Check out your new project README for information about what to do next.\n');
}
}
class Polymer3ElementGenerator extends ElementGenerator {
// TODO(yeoman/generator#1065): This function is not a no-op: Yeoman only
// checks the object's prototype's own properties for generator task
// methods. http://yeoman.io/authoring/running-context.html
initializing() {
return super.initializing();
}
// TODO(yeoman/generator#1065): This function is not a no-op: Yeoman only
// checks the object's prototype's own properties for generator task
// methods. http://yeoman.io/authoring/running-context.html
prompting() {
const _super = Object.create(null, {
prompting: { get: () => super.prompting }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.prompting.call(this);
});
}
writing() {
const name = this.props.name;
this.fs.copyTpl(`${this.templatePath()}/**/?(.)*`, this.destinationPath(), this.props, undefined, { globOptions: { ignore: ['**/_*'] } });
this.fs.copyTpl(this.templatePath('_element.js'), `${name}.js`, this.props);
this.fs.copyTpl(this.templatePath('test/_element_test.html'), `test/${name}_test.html`, this.props);
this.fs.copyTpl(this.templatePath('test/index.html'), `test/index.html`, this.props);
this.fs.move(this.destinationPath('gitignore'), this.destinationPath('.gitignore'));
}
install() {
this.log(chalk_1.default.bold('\nProject generated!'));
this.log('Installing dependencies...');
this.installDependencies({
bower: false,
npm: true,
});
}
// TODO(yeoman/generator#1065): This function is not a no-op: Yeoman only
// checks the object's prototype's own properties for generator task
// methods. http://yeoman.io/authoring/running-context.html
end() {
return super.end();
}
}
switch (templateName) {
case 'polymer-3.x':
return Polymer3ElementGenerator;
default:
return ElementGenerator;
}
}
exports.createElementGenerator = createElementGenerator;
//# sourceMappingURL=element.js.map