Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SamKirkland committed Dec 11, 2022
1 parent 3535c8d commit 04cc450
Show file tree
Hide file tree
Showing 8 changed files with 2,839 additions and 4,915 deletions.
6 changes: 1 addition & 5 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ node_modules
src/**
package-lock.json
tsconfig.json
webpack.config.js

# Include plop and handlebars modules
!node_modules/node-plop
!node_modules/handlebars
webpack.config.js
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

#### 1.2.0
- Plop 3.1.1 or higher required
- Removed shipped plop dependency
- Removed shipped handlebars dependency
- Reduced size from 7,200KB --> 54KB
- Reduced load time

#### 1.1.0
- Fixing bug caused in 1.0.5 causing `MODULE_NOT_FOUND` error
- Updated plop to latest version (2.7.4) and node-plop (0.26.2)
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ Install the extension on the [VSCode Marketplace](https://marketplace.visualstud

## Features
This extension adds single and multi-file templates to Visual Studio Code.
Add a template by right clicking the desired location and selecting `New File from Template`
Add a template by right clicking the desired location and selecting `📄 New File from Template`


## Creating new project item from template
To invoke template selection, simply right click on a folder or file in vscode file explorer and click the `New File from Template` menu item.
To invoke template selection, simply right click on a folder or file in vscode file explorer and click the `📄 New File from Template` menu item.

<img src="https://raw.githubusercontent.com/samkirkland/plop-templates/master/resources/menu.png">

## Extension Setup
1) Install the [VSCode extension](https://marketplace.visualstudio.com/items?itemName=SamKirkland.plop-templates)
2) Install [plop.js](https://github.com/plopjs/plop) globally using `npm install plop -g` *(optionally you can install plop for [just your current project](#Using-a-local-version-of-plop))*
2) Install [plop.js](https://github.com/plopjs/plop) globally using `npm install plop -g` *(optionally you can install plop for [just your current project](#Using-a-local-version-of-plop))*. Note: Plop 3.1.1 or higher required
3) Now for the fun part! Create a new file called `plopfile.js` at the root of your project (same folder as `package.json`, the name/location is [configurable](#Settings)).
```js
module.exports = function (plop) {
Expand Down Expand Up @@ -98,6 +98,13 @@ This extension expects plop.js to be install globally using `npm install plop -g

## Release Notes

#### 1.2.0
- Plop 3.1.1 or higher required
- Removed shipped plop dependency
- Removed shipped handlebars dependency
- Reduced size from 7,200KB --> 54KB
- Reduced load time

#### 1.1.0
- Fixing bug caused in 1.0.5 causing `MODULE_NOT_FOUND` error
- Updated plop to latest version (2.7.4) and node-plop (0.26.2)
Expand Down
7,384 changes: 2,673 additions & 4,711 deletions package-lock.json

Large diffs are not rendered by default.

206 changes: 105 additions & 101 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,104 +1,108 @@
{
"name": "plop-templates",
"displayName": "File Templates",
"description": "Create new files and folders from templates",
"version": "1.1.0",
"publisher": "samkirkland",
"engines": {
"vscode": "^1.32.0"
},
"categories": [
"Other"
"name": "plop-templates",
"displayName": "File Templates",
"description": "Create new files and folders from templates",
"version": "1.2.0",
"publisher": "samkirkland",
"engines": {
"vscode": "^1.64.0"
},
"categories": [
"Other"
],
"author": {
"name": "Sam Kirkland",
"url": "https://github.com/samkirkland"
},
"repository": {
"url": "https://github.com/samkirkland/plop-templates"
},
"bugs": {
"url": "https://github.com/samkirkland/plop-templates/issues",
"email": "[email protected]"
},
"galleryBanner": {
"color": "#333333",
"theme": "dark"
},
"qna": "https://github.com/samkirkland/plop-templates/issues",
"license": "MIT",
"keywords": [
"templates",
"create-item-by-template",
"create-folder",
"create-file",
"plop"
],
"activationEvents": [
"workspaceContains:**/plopfile.js",
"onCommand:ploptemplates.newFile"
],
"icon": "resources/logo.png",
"main": "./dist/extension.js",
"contributes": {
"commands": [
{
"command": "ploptemplates.newFile",
"title": "📄 New File from Template",
"category": "Plop File Templates"
}
],
"author": {
"name": "Sam Kirkland",
"url": "https://github.com/samkirkland"
},
"repository": {
"url": "https://github.com/samkirkland/plop-templates"
},
"bugs": {
"url": "https://github.com/samkirkland/plop-templates/issues",
"email": "[email protected]"
},
"galleryBanner": {
"color": "#333333",
"theme": "dark"
},
"qna": "https://github.com/samkirkland/plop-templates/issues",
"license": "MIT",
"keywords": [
"templates",
"create-item-by-template",
"create-folder",
"create-file",
"plop"
],
"activationEvents": [
"workspaceContains:**/plopfile.js",
"onCommand:ploptemplates.newFile"
],
"icon": "resources/logo.png",
"main": "./dist/extension.js",
"contributes": {
"commands": [{
"command": "ploptemplates.newFile",
"title": "New File from Template",
"category": "Plop File Templates"
}],
"menus": {
"explorer/context": [{
"command": "ploptemplates.newFile",
"group": "navigation@500"
}]
},
"configuration": [{
"title": "Plop Templates",
"required": [
"plopTemplates.configFileName",
"plopTemplates.terminalName",
"plopTemplates.destinationPath",
"plopTemplates.plopCommand"
],
"properties": {
"plopTemplates.configFileName": {
"type": "string",
"default": "plopfile.js",
"description": "List of paths to plop file templates"
},
"plopTemplates.terminalName": {
"type": "string",
"default": "New File from Template",
"description": "Name of the Terminal window created for plop"
},
"plopTemplates.destinationPath": {
"type": "string",
"default": "destinationpath",
"description": "Name of the prompt the destination path will be passed in as"
},
"plopTemplates.plopCommand": {
"type": "string",
"default": "plop",
"description": "By default this extension assumes plop is installed globally (npm install -g plop). If this is not the case, add a 'scripts' record in your package.json file -- example: 'scripts': { 'add-from-template': 'plop' } -- next update this setting with your command case, in this example 'add-from-template'"
}
}
}]
},
"scripts": {
"vscode:prepublish": "webpack --mode production",
"webpack": "webpack --mode development",
"webpack-dev": "webpack --mode development --watch"
},
"dependencies": {
"node-plop": "^0.26.2",
"plop": "^2.7.4"
"menus": {
"explorer/context": [
{
"command": "ploptemplates.newFile",
"group": "navigation@500"
}
]
},
"devDependencies": {
"@types/node": "^12.12.0",
"ts-loader": "^7.0.5",
"typescript": "^3.9.4",
"@types/vscode": "^1.32.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
}
}
"configuration": [
{
"title": "Plop Templates",
"required": [
"plopTemplates.configFileName",
"plopTemplates.terminalName",
"plopTemplates.destinationPath",
"plopTemplates.plopCommand"
],
"properties": {
"plopTemplates.configFileName": {
"type": "string",
"default": "plopfile.js",
"description": "List of paths to plop file templates"
},
"plopTemplates.terminalName": {
"type": "string",
"default": "New File from Template",
"description": "Name of the Terminal window created for plop"
},
"plopTemplates.destinationPath": {
"type": "string",
"default": "destinationpath",
"description": "Name of the prompt the destination path will be passed in as"
},
"plopTemplates.plopCommand": {
"type": "string",
"default": "plop",
"description": "By default this extension assumes plop is installed globally (npm install -g plop). If this is not the case, add a 'scripts' record in your package.json file -- example: 'scripts': { 'add-from-template': 'plop' } -- next update this setting with your command case, in this example 'add-from-template'"
}
}
}
]
},
"scripts": {
"vscode:prepublish": "webpack --mode production",
"webpack": "webpack --mode development",
"webpack-dev": "webpack --mode development --watch",
"pack": "vsce package --no-dependencies",
"publish": "vsce publish --no-dependencies"
},
"devDependencies": {
"@types/node": "^12.12.0",
"@types/vscode": "^1.64.0",
"ts-loader": "^9.2.6",
"typescript": "^4.5.5",
"webpack": "^5.68.0",
"webpack-cli": "^4.9.2"
}
}
Binary file modified resources/menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 9 additions & 63 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"use strict";

import { workspace, window, commands, ExtensionContext, Terminal, Uri, QuickPickItem } from "vscode";
import { workspace, window, commands, ExtensionContext, Terminal, Uri } from "vscode";
import { dirname } from "path";
import * as fs from "fs";
import nodePlop, { NodePlopAPI } from "node-plop";

function isWorkspaceOpen() {
if ((workspace) && (workspace.workspaceFolders) && (workspace.workspaceFolders.length > 0)) {
Expand All @@ -13,45 +12,6 @@ function isWorkspaceOpen() {
return false;
}

type Generator = {
name: string;
description: string;
};

async function selectGenerator(plop: NodePlopAPI, plopFile: string): Promise<Generator | undefined> {
const generators = plop.getGeneratorList();

// no generators, output error
if (generators.length === 0) {
window.showErrorMessage(`No Plop.js generators found in the config file "${plopFile}". Add one using plop.setGenerator(...)`);
throw "No Plop.js generators found...";
}

// single generator, no need in prompting for selection
if (generators.length === 1) {
return generators[0];
}

// prompt user for which generator they want to use
if (generators.length > 1) {
const result = await window.showQuickPick<QuickPickItem>(
generators.map((generator: Generator) => ({ label: generator.name, description: generator.description })),
{
placeHolder: "Please choose a generator"
}
);

if (result === undefined) {
return undefined;
}

return {
name: result.label,
description: result.description!
};
}
}

async function runPlopInNewTerminal(dirUri: Uri) {
if (!dirUri && isWorkspaceOpen()) {
window.showErrorMessage("Project items cannot be created if workspace is not open.");
Expand All @@ -61,32 +21,13 @@ async function runPlopInNewTerminal(dirUri: Uri) {

// user based settings
const userSettings = workspace.getConfiguration();
const plopFileName: string = userSettings.get("plopTemplates.configFileName") || "plopfile.js";
const plopTerminalName: string = userSettings.get("plopTemplates.terminalName") || "New File from Template";
const destinationpathName: string = userSettings.get("plopTemplates.destinationPath") || "destinationpath";
const plopCommand: string = (userSettings.get("plopTemplates.plopCommand") as string || "plop").trim();
let plopCommandRelative: string = plopCommand;

const plopFile = workspace.rootPath + "/" + plopFileName;
let plop: NodePlopAPI;

try {
plop = nodePlop(plopFile);
}
catch (e) {
window.showErrorMessage(`Couldn't load plop config file at the path: "${plopFile}" - ${e}`);
return;
}


let destPath: string = "";
let plopTerminal: Terminal;
let selectedGenerator = await selectGenerator(plop, plopFile);

if (selectedGenerator === undefined) {
window.showInformationMessage("No Plop.js generator selected, cancelling...");
return;
}

if (dirUri) {
destPath = dirUri.fsPath;
Expand Down Expand Up @@ -121,19 +62,24 @@ async function runPlopInNewTerminal(dirUri: Uri) {
}

if (plopCommand !== "plop") {
plopCommandRelative = "npm run " + plopCommand;
plopCommandRelative = `npm run ${plopCommand} --`;
}

plopTerminal.show();
plopTerminal.sendText(`${plopCommandRelative} "${selectedGenerator.name}" -- --${destinationpathName} "${destPath}"`);
if (destPath.length > 0) {
plopTerminal.sendText(`${plopCommandRelative} -- --${destinationpathName} "${destPath}"`);
}
else {
plopTerminal.sendText(`${plopCommandRelative}`);
}
}

export function activate(context: ExtensionContext) {
let disposable = commands.registerCommand("ploptemplates.newFile", (dirUri: Uri) => {
try {
runPlopInNewTerminal(dirUri);
}
catch (e) {
catch (e: any) {
window.showErrorMessage(e);
}
});
Expand Down
Loading

0 comments on commit 04cc450

Please sign in to comment.