Skip to content

Commit

Permalink
feat(cli): add plugins flag to newapp command
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagoalvesdulce authored Apr 29, 2022
1 parent d9dfb5d commit 0d0b411
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 71 deletions.
59 changes: 51 additions & 8 deletions plugins-structure/packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ line structure and arguments.

## Overview

1. [Setup](#setup)
2. [New Plugin](#creating-a-new-plugin)
3. [New App](#creating-a-new-application)
- [Politeiagui CLI](#politeiagui-cli)
- [Overview](#overview)
- [Setup](#setup)
- [Creating a new plugin](#creating-a-new-plugin)
- [Creating a new application](#creating-a-new-application)

## Setup

Expand Down Expand Up @@ -72,7 +74,7 @@ Creating a new `ticketvote` plugin:
$ pgui newplugin ticketvote

Creating a new plugin: ticketvote
Directory: /Users/victorguedes/Documents/decred/politeiagui/plugins-structure/packages/ticketvote
Directory: /Directory/plugins-structure/packages/ticketvote



Expand Down Expand Up @@ -133,16 +135,21 @@ Usage: pgui newapp [options] [app-name]
Creates a new app-shell

Options:
-p, --port <port> port number (default: 3000)
-h, --help display help for command
-p, --port <port> port number (default: 3000)

--plugins [plugins] list of plugins comma separated. Will be ignored if a config file is provided. Example: ticketvote,comments

--config [config] custom config file relative to this folder. Example: ./config.json

-h, --help display help for command
```
Creating a new Proposals app
```bash
$ pgui newapp proposals
Creating a new app: proposals
Directory: /Users/victorguedes/Documents/decred/politeiagui/plugins-structure/apps/proposals
Directory: /Directory/apps/proposals



Expand All @@ -162,6 +169,7 @@ apps/proposals
│ └── public
│ └── index.html
├── webpack.common.js
├── config.json
├── webpack.dev.js
└── webpack.prod.js
```
Expand Down Expand Up @@ -189,10 +197,45 @@ Let's take a look under the `package.json`:
"start": "webpack serve --config webpack.dev.js --open"
},
"dependencies": {
"@politeiagui/core": "1.0.0"
"@politeiagui/core": "1.0.0",
"@politeiagui/common-ui": "1.0.0"
},
"devDependencies": {
"webpack-bundle-analyzer": "^4.4.2"
}
}
```
And your `config.json` will look like this:
```json
{
"plugins": {}
}
```
You can provide a config file to tell which plugins you want. To add the ticketvote plugin, for example, you can use this config file:
```json
{
"plugins": {
"ticketvote": {
"version": "1.0.0"
}
}
}
```
With the following command:
```bash
$ pgui newapp proposals --config="./config-example.json"
```
You can also pass a list of comma-separated plugins via the plugins option. For example:
```bash
$ pgui newapp proposals --plugins="comments,ticketvote"
```
Notice that if you send both a plugins list and a config file, the plugins list will be ignored.
92 changes: 40 additions & 52 deletions plugins-structure/packages/cli/lib/cmdnewapp.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,50 @@
const fs = require("fs");
const path = require("path");

const { replaceFileValuesFromMap, copyFile } = require("./utils");
const {
createSrcFiles,
validatePlugins,
validateAppName,
validateConfig,
loadConfigFile,
createNewApp,
createPackageJsonFile,
createConfigFile,
createWebpackConfigFiles,
createTestConfigFiles,
getPluginsDepsAndConfig,
} = require("./utils");

const baseAppPackageJSON = require("../app/package.json");
const baseAppPath = path.resolve(__dirname, "../app");

module.exports = function newApp(appName, { port }) {
const appShellPath = path.resolve(__dirname, "../../../apps/");
const appPath = path.resolve(appShellPath, appName);
const appExists = fs.existsSync(appPath);
if (appExists) {
console.error(`Error: App ${appName} already exists on ${appShellPath}`);
module.exports = function newApp(appName, { port, plugins, config }) {
try {
let configFile;
const isDefaultApp = !plugins && !config;

validateAppName(appName);
validatePlugins(plugins);
validateConfig(config);
if (config) configFile = loadConfigFile(config);
const { pluginsDeps, pluginsConfig } = getPluginsDepsAndConfig({
isDefaultApp,
plugins,
configFile,
});
const appPath = createNewApp(appName);
createPackageJsonFile({
appName,
baseAppPackageJSON,
pluginsDeps,
appPath,
});
createConfigFile({ appPath, config, pluginsConfig });
createWebpackConfigFiles({ appPath, port, baseAppPath });
createTestConfigFiles({ appPath, baseAppPath });
createSrcFiles({ appPath, appName, baseAppPath });
} catch (e) {
console.error(e);
return;
}

console.log(`Creating a new app: ${appName}`);
console.log(`Directory: ${appPath}`);
console.log(`\n\n`);
console.log("Creating app files...");

// create app dir
fs.mkdirSync(appPath);
fs.mkdirSync(`${appPath}/src`);
fs.mkdirSync(`${appPath}/src/public`);

// create app package.json
fs.writeFileSync(
`${appPath}/package.json`,
JSON.stringify(
{
name: appName,
...baseAppPackageJSON,
},
null,
2
)
);
// create webpack config files
const wpDev = replaceFileValuesFromMap(`${baseAppPath}/webpack.dev.js`, {
__PORT__: +port,
});
fs.writeFileSync(`${appPath}/webpack.dev.js`, wpDev);
copyFile("webpack.common.js", appPath, baseAppPath);
copyFile("webpack.prod.js", appPath, baseAppPath);
// create test config files
copyFile("jest.config.js", appPath, baseAppPath);

// create src files
const indexHtml = replaceFileValuesFromMap(
`${baseAppPath}/src/public/index.html`,
{ __APP_NAME__: appName }
);
const indexJs = replaceFileValuesFromMap(`${baseAppPath}/src/index.js`, {
__APP_NAME__: appName,
});
fs.writeFileSync(`${appPath}/src/public/index.html`, indexHtml);
fs.writeFileSync(`${appPath}/src/index.js`, indexJs);

console.log("Done!");
};
4 changes: 4 additions & 0 deletions plugins-structure/packages/cli/lib/cmdnewplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const basePluginPackageJSON = require("../plugin/package.json");
const basePluginPath = path.resolve(__dirname, "../plugin");

module.exports = function newPlugin(pluginName, { port }) {
if (!pluginName) {
console.error("Please specify the app name");
return;
}
const packagePath = path.resolve(__dirname, "../../../packages/");
const pluginPath = path.resolve(packagePath, pluginName);
const pluginExists = fs.existsSync(pluginPath);
Expand Down
7 changes: 7 additions & 0 deletions plugins-structure/packages/cli/lib/config-example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": {
"ticketvote": {
"version": "1.0.0"
}
}
}
11 changes: 10 additions & 1 deletion plugins-structure/packages/cli/lib/pgui.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ function execute() {
.action(cmdnewplugin);

program
.command("newapp [app-name]")
.command("newapp")
.argument("[app-name]", "The name of your new app")
.option(
"--plugins [plugins]",
"list of plugins comma separated. Will be ignored if a config file is provided. Example: ticketvote,comments"
)
.option(
"--config [config]",
"custom config file relative to this folder. Example: ./config.json"
)
.option("-p, --port <port>", "port number", 3000)
.description("Creates a new app-shell")
.action(cmdnewapp);
Expand Down
Loading

0 comments on commit 0d0b411

Please sign in to comment.