Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

* Adds ability to insert current date/time into templates at the time of file creation.

## [3.0.0 - 2019-01-02]

* Adds ability to provide dynamic template replacements at the time of file creation
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,27 @@ Examples:
| Helper Name | Example Use In Templates | Example Use in File/Folder Names | Sample Result |
|----------------|--------------------------|----------------------------------|-------------------------|
| {none} | {{name}} | \_\_name\_\_ | {No transform applied} |
| upperCase | {{upperCase name}} | \_\_upperCase_name\_\_ | THIS IS UPPERCASE |
| lowerCase | {{lowerCase name}} | \_\_lowerCase_name\_\_ | this is lowercase |
| upperCase | {{upperCase name}} | \_\_upperCase_name\_\_ | THIS IS UPPERCASE |
| lowerCase | {{lowerCase name}} | \_\_lowerCase_name\_\_ | this is lowercase |
| camelCase | {{camelCase name}} | \_\_camelCase_name\_\_ | thisIsCamelCase |
| pascalCase | {{pascalCase name}} | \_\_pascalCase_name\_\_ | ThisIsPascalCase |
| snakeCase | {{snakeCase name}} | \_\_snakeCase_name\_\_ | this_is_snake_case |
| upperSnakeCase | {{upperSnakeCase name}} | \_\_upperSnakeCase_name\_\_ | THIS_IS_UPPER_SNAKE_CASE|
| kebabCase | {{kebabCase name}} | \_\_kebabCase_name\_\_ | this-is-kebab-case |
| lowerDotCase | {{lowerDotCase name}} | \_\_lowerDotCase_name\_\_ | this.is.lower.dot.case |

## Date/Time Template Variables

| Variable Name | Example Use In Templates | Example Use in File/Folder Names | Sample Result |
|----------------|--------------------------|----------------------------------|-------------------------|
| currentYear | {{currentYear}} | \_\_currentYear\_\_ | YYYY |
| currentMonth | {{currentMonth}} | \_\_currentMonth\_\_ | MM |
| currentDate | {{currentDate}} | \_\_currentDate\_\_ | DD |
| currentDay | {{currentDay}} | \_\_currentDay\_\_ | d |
| currentHour | {{currentHour}} | \_\_currentHour\_\_ | HH |
| currentMin | {{currentMin}} | \_\_currentMin\_\_ | mm |
| currentSec | {{currentSec}} | \_\_currentSec\_\_ | ss |

## Dynamic Template Variables

Dynamic Template Variables provide template replacements at the time of file creation in addition to the standard name. The dynamic replacement token should conform to the format of `{{ $<inputName> }}` (we will search for a `$` within `{{ }}`).
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function activate(context: vscode.ExtensionContext) {

try {
const userInput = await getUserInput(templateFolderRawPaths);
await createFiles(userInput, directoryPath);
await createFiles(userInput, directoryPath, new Date());
} catch (error) {
handleError(error);
}
Expand Down
6 changes: 4 additions & 2 deletions src/fileCreator/createFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import { IDynamicTemplateValues, IUserInput } from "../inputs";
import { getFolderNamesAtDirectory } from "../utilities/getTemplateFilesAndFolders";
import { getTemplateFileNamesAtTemplateDirectory } from "../utilities/getTemplateFilesAndFolders";
import { sanitizedName } from "./inputSanitizer";
import { replaceStringUsingTransforms, replaceTemplateContent } from "./transforms";
import { replaceStringUsingTransforms, replaceTemplateContent, setCurrentDate } from "./transforms";

export async function createFiles(userInput: IUserInput, inDirectory: string): Promise<void> {
export async function createFiles(userInput: IUserInput, inDirectory: string, date: Date): Promise<void> {

const options = getTemplateManifestAtTemplateDirectory(userInput.selectedTemplatePath);

const name = sanitizedName(userInput.inputName, options);

const temporaryDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "blueprint-"));

setCurrentDate(date);

await createFilesFromTemplateInDirectory(
userInput.selectedTemplatePath,
temporaryDirectory,
Expand Down
68 changes: 68 additions & 0 deletions src/fileCreator/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,38 @@ import * as _ from "lodash";
import { IDynamicTemplateValues } from "../inputs";

let handlebarsInitialized = false;
let currentDate: Date = new Date();

export function setCurrentDate(date: Date) {
currentDate = date;
}

export function initializeHandlebars() {
handlebars.registerHelper({
camelCase: (input) => {
return _.camelCase(input);
},
currentDate: (input) => {
return getDayOfMonth(currentDate);
},
currentDay: (input) => {
return getDay(currentDate);
},
currentHour: (input) => {
return getHours(currentDate);
},
currentMin: (input) => {
return getMinutes(currentDate);
},
currentMonth: (input) => {
return getMonth(currentDate);
},
currentSec: (input) => {
return getSeconds(currentDate);
},
currentYear: (input) => {
return getYear(currentDate);
},
kebabCase: (input) => {
return _.kebabCase(input);
},
Expand Down Expand Up @@ -73,6 +99,13 @@ export function replaceStringUsingTransforms(stringToReplace: string, name: stri
result = replaceAll(result, "__upperCase_name__", _.upperCase(name));
result = replaceAll(result, "__lowerCase_name__", _.lowerCase(name));
result = replaceAll(result, "__upperSnakeCase_name__", _.snakeCase(name).toUpperCase());
result = replaceAll(result, "__currentYear__", getYear(currentDate));
result = replaceAll(result, "__currentMonth__", getMonth(currentDate));
result = replaceAll(result, "__currentDate__", getDayOfMonth(currentDate));
result = replaceAll(result, "__currentDay__", getDay(currentDate));
result = replaceAll(result, "__currentHour__", getHours(currentDate));
result = replaceAll(result, "__currentMin__", getMinutes(currentDate));
result = replaceAll(result, "__currentSec__", getSeconds(currentDate));
return result;
}

Expand All @@ -83,3 +116,38 @@ function escapeRegExp(str): string {
function replaceAll(str, find, replace): string {
return str.replace(new RegExp(escapeRegExp(find), "g"), replace);
}

function getYear(date: Date): string {
return date.getFullYear().toString();
}

function getMonth(date: Date): string {
return padDateComponentToTwoChars((date.getMonth() + 1).toString());
}

function getDayOfMonth(date: Date): string {
return padDateComponentToTwoChars(date.getDate().toString());
}

function getDay(date: Date): string {
return date.getDay().toString();
}

function getHours(date: Date): string {
return padDateComponentToTwoChars(date.getHours().toString());
}

function getMinutes(date: Date): string {
return padDateComponentToTwoChars(date.getMinutes().toString());
}

function getSeconds(date: Date): string {
return padDateComponentToTwoChars(date.getSeconds().toString());
}

function padDateComponentToTwoChars(str: string): string {
if (str.length < 2) {
str = "0" + str;
}
return str;
}
66 changes: 58 additions & 8 deletions test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ suite("Extension Tests", () => {

const runTestForTemplateNamed = async (
templateName: string,
dynamicTemplateValues: IDynamicTemplateValues
outName: string,
dynamicTemplateValues: IDynamicTemplateValues,
testDate: Date
) => {
const directoryPath = path.join(outPath, templateName);
const directoryPath = path.join(outPath, outName);

const userInput: IUserInput = {
inputName: "MY User Input",
selectedTemplatePath: path.join(templatesPath, templateName),
dynamicTemplateValues
};

return createFiles(userInput, directoryPath)
return createFiles(userInput, directoryPath, testDate)
.then(() => {
// Ensure at least one file was created
assert.equal(fs.readdirSync(expectedOutputPath).length > 0, true);
Expand All @@ -58,7 +60,7 @@ suite("Extension Tests", () => {
noDiffSet: true
};
const result = dirCompare.compareSync(
path.join(expectedOutputPath, templateName),
path.join(expectedOutputPath, outName),
directoryPath,
options
);
Expand All @@ -73,21 +75,36 @@ suite("Extension Tests", () => {
// Defines a Mocha unit test
test("includesImages", done => {
beforeEach();
runTestForTemplateNamed("includesImages", {}).then(() => {
runTestForTemplateNamed(
"includesImages",
"includesImages",
{},
new Date()
).then(() => {
done();
});
});

test("nestedFolders", done => {
beforeEach();
runTestForTemplateNamed("nestedFolders", {}).then(() => {
runTestForTemplateNamed(
"nestedFolders",
"nestedFolders",
{},
new Date()
).then(() => {
done();
});
});

test("transforms", done => {
beforeEach();
runTestForTemplateNamed("transforms", {}).then(() => {
runTestForTemplateNamed(
"transforms",
"transforms",
{},
new Date()
).then(() => {
done();
});
});
Expand All @@ -109,7 +126,40 @@ suite("Extension Tests", () => {

runTestForTemplateNamed(
"dynamicTemplateVariables",
dynamicTemplateValues
"dynamicTemplateVariables",
dynamicTemplateValues,
new Date()
).then(() => {
done();
});
});

test("datetime-simple", done => {
beforeEach();

const testDate = new Date("1965-10-31 22:43:54");

runTestForTemplateNamed(
"datetime",
"datetime-simple",
{},
testDate
).then(() => {
done();
});
});

test("datetime-zeropadding", done => {
beforeEach();

const testDate = new Date("0100-02-03 04:05:06");
testDate.setFullYear(1);

runTestForTemplateNamed(
"datetime",
"datetime-zeropadding",
{},
testDate
).then(() => {
done();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Year: 1965
Month: 10
Date: 31
Day of week: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hour: 22
Minute: 43
Second: 54
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hour: 04
Minute: 05
Second: 06
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Year: 1
Month: 02
Date: 03
Day of week: 6
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Hour: {{currentHour}}
Minute: {{currentMin}}
Second: {{currentSec}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Year: {{currentYear}}
Month: {{currentMonth}}
Date: {{currentDate}}
Day of week: {{currentDay}}