feat(cli): add create command (#9)

This commit is contained in:
Miralem Drek
2019-02-26 11:14:05 +01:00
committed by GitHub
parent df89d1c5ef
commit ff308b076a
14 changed files with 1477 additions and 45 deletions

View File

@@ -1,9 +1,11 @@
#!/usr/bin/env node
const yargs = require('yargs');
const create = require('@nebula.js/cli-create/command');
const serve = require('@nebula.js/cli-serve/command');
yargs.usage('nucleus <command> [options]')
.command(create)
.command(serve)
.demandCommand()
.alias('h', 'help')

View File

@@ -16,6 +16,7 @@
"lint": "eslint src"
},
"dependencies": {
"@nebula.js/cli-create": "0.1.0",
"@nebula.js/cli-serve": "0.1.0",
"chalk": "^2.4.2",
"yargs": "^12.0.5"

View File

@@ -0,0 +1,31 @@
const path = require('path');
const execa = require('execa');
module.exports = {
command: 'create <name>',
desc: 'Create a supernova',
builder(yargs) {
yargs.positional('name', {
type: 'string',
description: 'name of the project',
});
yargs.option('install', {
type: 'boolean',
default: true,
description: 'Run package installation step',
});
},
async handler(argv) {
const generator = path.resolve(__dirname, 'generator/index.js');
const args = `${argv.name} ${!argv.install ? '--no-install' : ''}`;
try {
execa.shell(`yo ${generator} ${args} --no-insight`, {
localDir: path.resolve(__dirname, 'node_modules', '.bin'),
preferLocal: true,
stdio: 'inherit',
});
} catch (err) {
console.error(err);
}
},
};

View File

@@ -0,0 +1,121 @@
const Generator = require('yeoman-generator');
const chalk = require('chalk');
const fs = require('fs');
module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts);
this.argument('name', { type: String, required: true });
this.option('install', { type: Boolean, default: true });
// console.log('OPTS', this.options);
// console.log('ARGS', this.arguments);
const destination = this.destinationPath(this.options.name);
if (fs.existsSync(destination)) {
this.cancelled = true;
this.log.error(chalk.red(`Oopsie, looks like ${this.options.name} already exists.`));
// return;
}
// silence create
this.log.create = () => {};
// this.log('\n');
// this.log(`Creating a new project in ${chalk.cyan(destination)}`);
// this.log('\n');
}
async prompting() {
if (this.cancelled) {
return;
}
this.answers = await this.prompt([{
type: 'list',
name: 'packageManager',
message: 'Pick the package manager you want to use:',
choices: ['npm', 'yarn'],
}]);
}
async writing() {
if (this.cancelled) {
return;
}
const { name } = this.options;
// copy raw files
[
'.editorconfig',
'.eslintignore',
'.gitignore',
'.eslintrc.json',
].forEach(filename => this.fs.copy(
this.templatePath(`project/${filename}`),
this.destinationPath(`${name}/${filename}`),
));
this.fs.copyTpl(
this.templatePath('project/package.json'),
this.destinationPath(`${name}/package.json`),
{
name,
description: '',
user: this.user.git.name(),
email: this.user.git.email(),
// username: await this.user.github.username(),
},
);
this.fs.copyTpl(
this.templatePath('project/README.md'),
this.destinationPath(`${name}/README.md`),
{
name,
},
);
this.fs.copyTpl(
this.templatePath('project/sn.js'),
this.destinationPath(`${name}/src/index.js`),
{},
);
this.fs.copyTpl(
this.templatePath('project/object-properties.js'),
this.destinationPath(`${name}/src/object-properties.js`),
{ name },
);
}
install() {
if (this.cancelled || !this.options.install) {
return;
}
process.chdir(this.options.name);
this.installDependencies({
npm: this.answers.packageManager === 'npm',
bower: false,
yarn: this.answers.packageManager === 'yarn',
});
}
end() {
if (this.cancelled) {
return;
}
const p = this.answers.packageManager === 'npm' ? 'npm' : 'yarn';
this.log('\n');
this.log(`Successfully created project ${chalk.yellow(this.options.name)}`);
this.log('Get started with the following commands:');
this.log('\n');
this.log(chalk.cyan(` cd ${this.options.name}`));
if (this.options.install === false) {
this.log(chalk.cyan(` ${p} install`));
}
this.log(chalk.cyan(` ${p} run serve`));
this.log('\n');
}
};

View File

@@ -0,0 +1,15 @@
# http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

View File

@@ -0,0 +1,2 @@
dist
coverage

View File

@@ -0,0 +1,41 @@
{
"root": true,
"env": {
"browser": true
},
"parserOptions": {
"sourceType": "module"
},
"extends": [
"airbnb-base"
],
"rules": {
"max-len": 0,
"no-plusplus": 0,
"no-bitwise" : 0,
"no-unused-expressions": 0
},
"overrides": [
{
"files": ["**/*.spec.js"],
"env": {
"browser": false,
"node": true,
"mocha": true
},
"globals": {
"chai": false,
"expect": false,
"sinon": false,
"aw": false,
"page": false
},
"plugins": [
"mocha"
],
"rules": {
"mocha/no-exclusive-tests": "error"
}
}
]
}

View File

@@ -0,0 +1,11 @@
*.log
*.log
.cache
.DS_Store
.idea
.vscode
.npmrc
node_modules
coverage
dist/

View File

@@ -0,0 +1,7 @@
# <%= name %>
## Usage
```js
npm install <%= name %>
```

View File

@@ -0,0 +1,17 @@
const properties = {
qHyperCubeDef: {
qDimensions: [],
qMeasures: [],
qInitialDataFetch: [
{ qWidth: 10, qHeight: 500 },
],
qSuppressZero: false,
qSuppressMissing: true,
},
showTitles: true,
title: '',
subtitle: '',
footnote: '',
};
export default properties;

View File

@@ -0,0 +1,34 @@
{
"name": "<%= name %>",
"version": "0.1.0",
"description": "<%= description %>",
"license": "MIT",
"author": {
"name": "<%= user %>",
"email": "<%= email %>"
},
"keywords": [
"qlik",
"nebula",
"supernova"
],
"files": [
"dist"
],
"engines": {
"node": ">=8"
},
"main": "dist/<%= name %>.js",
"module": "dist/<%= name %>.esm.js",
"scripts": {
"lint": "eslint src",
"serve": "nebula serve"
},
"devDependencies": {
"@nebula.js/cli": "0.1.0",
"eslint": "^5.12.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.15.0",
"eslint-plugin-mocha": "^5.2.1"
}
}

View File

@@ -0,0 +1,22 @@
import properties from './object-properties';
export default function sn(env) {
return {
qae: {
properties,
},
component: {
mounted(element) {
console.log(env);
this.element = element;
this.element.innerHTML = '<div>Hello!</div>';
},
render({
layout,
context,
}) {
console.log(layout, context);
},
},
};
}

View File

@@ -0,0 +1,21 @@
{
"name": "@nebula.js/cli-create",
"version": "0.1.0",
"description": "",
"license": "MIT",
"author": "QlikTech International AB",
"keywords": [],
"files": [
"command.js",
"generator"
],
"scripts": {
"generate": "yo ./generator/index.js"
},
"dependencies": {
"chalk": "^2.4.2",
"execa": "^1.0.0",
"yeoman-generator": "^3.2.0",
"yo": "^2.0.5"
}
}

1197
yarn.lock

File diff suppressed because it is too large Load Diff