Skip to content

Commit

Permalink
feat: new template system
Browse files Browse the repository at this point in the history
BREAKING CHANGES:
- Omit {{author_full}} in favor of {{contact}}
  • Loading branch information
uetchy committed Oct 8, 2019
1 parent 1d5f8a1 commit e8e6bd9
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 40 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Edit files inside `templates/default`. Text files will be passed through Mustach
- `{{description}}` package description
- `{{author}}` author name
- `{{email}}` author email
- `{{author_full}}` author name formatted with `{{name}} <{{email}}>` if email given, otherwise `{{name}}`
- `{{contact}}` author name formatted with `{{name}} <{{email}}>` if email given, otherwise `{{name}}`
- `{{license}}` package license (e.g. `MIT`)
- `{{year}}` current year (e.g. `2020`)

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@
},
"dependencies": {
"@types/inquirer": "^6.5.0",
"@types/mustache": "^0.8.32",
"@types/yargs-interactive": "^2.1.0",
"chalk": "^2.4.2",
"cross-spawn": "^7.0.0",
"execa": "^2.0.5",
"gitconfig": "^2.0.8",
"globby": "^10.0.1",
"handlebars": "^4.4.2",
"inquirer": "^7.0.0",
"is-utf8": "^0.2.1",
"license.js": "^3.1.2",
"mustache": "^3.1.0",
"yargs-interactive": "^3.0.0"
},
"devDependencies": {
Expand Down
25 changes: 14 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ import {makeLicenseSync, availableLicenses} from 'license.js';

import {copy, getAvailableTemplates} from './template';

export interface Config {
packageDir: string;
templateDir: string;
view: View;
}

export interface View {
name: string;
description: string;
author: string;
email: string;
description: string;
contact: string;
license: string;
[key: string]: string | number | boolean | any[];
}

export interface Config {
packageDir: string;
templateDir: string;
view: View;
}

export interface Options {
extra?: Option;
caveat?: string;
after?: () => void;
}

async function getGitUser() {
Expand Down Expand Up @@ -67,7 +70,7 @@ async function initGit(root: string) {
await execa('git init', {shell: true, cwd: root});
}

function authorString(author: string, email?: string) {
function getContact(author: string, email?: string) {
return `${author}${email ? ` <${email}>` : ''}`;
}

Expand Down Expand Up @@ -153,7 +156,7 @@ export async function create(

const templateDir = path.resolve(templateRoot, args.template);
const year = new Date().getFullYear();
const author_full = authorString(args.author, args.email);
const contact = getContact(args.author, args.email);

if (!fs.existsSync(templateDir)) {
throw new Error('No template found');
Expand All @@ -173,7 +176,7 @@ export async function create(
...filterdArgs,
name,
year,
author_full,
contact,
};

// copy files from template
Expand All @@ -189,7 +192,7 @@ export async function create(
year,
project: name,
description: args.description,
organization: authorString(args.author, args.email),
organization: getContact(args.author, args.email),
});
const licenseText = license.header + license.text + license.warranty;
fs.writeFileSync(path.resolve(packageDir, 'LICENSE'), licenseText);
Expand Down
61 changes: 54 additions & 7 deletions src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,59 @@ import fs from 'fs';
import path from 'path';
import globby from 'globby';
import isUtf8 from 'is-utf8';
import Mustache from 'mustache';
import Handlebars from 'handlebars';
import {Config} from '.';

function trim(text: string) {
return text.replace(/[\r\n]/g, '');
}
Handlebars.registerHelper('trim', trim);

function upper(text: string) {
return trim(text).toUpperCase();
}
Handlebars.registerHelper('upper', upper);

function lower(text: string) {
return trim(text).toLowerCase();
}
Handlebars.registerHelper('lower', lower);

function capital(text: string, options?: any) {
const space = options && options.hash && options.hash.space;
return trim(text)
.split(/[-_\s]+/)
.map((s) => s.charAt(0).toUpperCase() + s.slice(1).toLowerCase())
.join(space ? ' ' : '');
}
Handlebars.registerHelper('capital', capital);

function camel(text: string) {
return capital(text).replace(/^./, (s) => s.toLowerCase());
}
Handlebars.registerHelper('camel', camel);

function snake(text: string) {
return capital(text)
.replace(/(?<=([a-z](?=[A-Z])|[A-Za-z](?=[0-9])))(?=[A-Z0-9])/g, '_')
.toLowerCase();
}
Handlebars.registerHelper('snake', snake);

function kebab(text: string) {
return snake(text).replace(/_/g, '-');
}
Handlebars.registerHelper('kebab', kebab);

function format<T>(text: Buffer | string, view: T) {
const template = Handlebars.compile(text.toString());
return template(view);
}

function prepareDirectory(filePath: string) {
try {
fs.mkdirSync(path.dirname(filePath), {recursive: true});
const target = path.dirname(filePath);
fs.mkdirSync(target, {recursive: true});
} catch {}
}

Expand All @@ -19,16 +66,16 @@ export async function copy(args: Config) {
const templateFiles = await globby(args.templateDir, {dot: true});
for (const sourcePath of templateFiles) {
const relativePath = path.relative(args.templateDir, sourcePath);
const targetPath = path.resolve(args.packageDir, relativePath);

const targetPath = format(
path.resolve(args.packageDir, relativePath),
args.view,
);
prepareDirectory(targetPath);

let sourceData = fs.readFileSync(sourcePath);
let targetData = sourceData;
if (isUtf8(sourceData)) {
targetData = Buffer.from(
Mustache.render(sourceData.toString(), args.view),
);
targetData = Buffer.from(format(sourceData, args.view));
}
fs.writeFileSync(targetPath, targetData, 'utf-8');
}
Expand Down
4 changes: 2 additions & 2 deletions templates/default/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# {{name}}
# {{capital name space=true}}

> {{description}}
## Usage

```bash
npx {{name}} <name>
npx {{kebab name}} <name>
```
4 changes: 2 additions & 2 deletions templates/default/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "{{name}}",
"name": "{{kebab name}}",
"description": "{{description}}",
"version": "0.0.0",
"dependencies": {
"create-whatever": "^3.0.0"
},
"author": "{{author_full}}",
"author": "{{contact}}",
"license": "{{license}}",
"bin": "src/cli.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion templates/default/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ const caveat = `This is a caveat!
You can find this section in "src/cli.ts".
`;

create('{{name}}', templateRoot, {caveat});
create('{{kebab name}}', templateRoot, {caveat});
6 changes: 2 additions & 4 deletions templates/default/templates/default/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{{=<% %>=}}
# {{name}}
# \{{name}}

{{description}}
<%={{ }}=%>
\{{description}}
4 changes: 2 additions & 2 deletions templates/typescript/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# {{name}}
# {{capital name space=true}}

> {{description}}
## Usage

```bash
npx {{name}} <name>
npx {{kebab name}} <name>
```
4 changes: 2 additions & 2 deletions templates/typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "{{name}}",
"name": "{{kebab name}}",
"description": "{{description}}",
"version": "0.0.0",
"scripts": {
Expand All @@ -13,7 +13,7 @@
"shx": "^0.3.2",
"typescript": "^3.6.3"
},
"author": "{{author_full}}",
"author": "{{contact}}",
"license": "{{license}}",
"bin": "dist/cli.js",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion templates/typescript/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ const caveat = `This is a caveat!
You can find this section in "src/cli.ts".
`;

create('{{name}}', templateRoot, {caveat});
create('{{kebab name}}', templateRoot, {caveat});
6 changes: 2 additions & 4 deletions templates/typescript/templates/default/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{{=<% %>=}}
# {{name}}
# \{{name}}

{{description}}
<%={{ }}=%>
\{{description}}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ graceful-fs@^4.1.15, graceful-fs@^4.1.2:
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.2.tgz#6f0952605d0140c1cfdb138ed005775b92d67b02"
integrity sha512-IItsdsea19BoLC7ELy13q1iJFNmd7ofZH5+X/pJr90/nRoPEX0DJo1dHDbgtYWOhJhcCgMDTOw84RZ72q6lB+Q==

handlebars@^4.1.2:
handlebars@^4.1.2, handlebars@^4.4.2:
version "4.4.2"
resolved "https://registry.yarnpkg.com/handlebars/-/handlebars-4.4.2.tgz#8810a9821a9d6d52cb2f57d326d6ce7c3dfe741d"
integrity sha512-cIv17+GhL8pHHnRJzGu2wwcthL5sb8uDKBHvZ2Dtu5s1YNt0ljbzKbamnc+gr69y7bzwQiBdr5+hOpRd5pnOdg==
Expand Down

0 comments on commit e8e6bd9

Please sign in to comment.